Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * csum-file.c |
| 3 | * |
| 4 | * Copyright (C) 2005 Linus Torvalds |
| 5 | * |
| 6 | * Simple file write infrastructure for writing SHA1-summed |
| 7 | * files. Useful when you write a file that you want to be |
| 8 | * able to verify hasn't been messed with afterwards. |
| 9 | */ |
| 10 | #include "cache.h" |
| 11 | #include "csum-file.h" |
| 12 | |
David Rientjes | 78b713a | 2006-08-14 13:32:01 -0700 | [diff] [blame] | 13 | static void sha1flush(struct sha1file *f, unsigned int count) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 14 | { |
| 15 | void *buf = f->buffer; |
| 16 | |
| 17 | for (;;) { |
Junio C Hamano | 1c15afb | 2005-12-19 16:18:28 -0800 | [diff] [blame] | 18 | int ret = xwrite(f->fd, buf, count); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 19 | if (ret > 0) { |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 20 | buf = (char *) buf + ret; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 21 | count -= ret; |
| 22 | if (count) |
| 23 | continue; |
David Rientjes | 78b713a | 2006-08-14 13:32:01 -0700 | [diff] [blame] | 24 | return; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 25 | } |
| 26 | if (!ret) |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 27 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 28 | die("sha1 file '%s' write error (%s)", f->name, strerror(errno)); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 32 | int sha1close(struct sha1file *f, unsigned char *result, int update) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 33 | { |
| 34 | unsigned offset = f->offset; |
| 35 | if (offset) { |
| 36 | SHA1_Update(&f->ctx, f->buffer, offset); |
| 37 | sha1flush(f, offset); |
| 38 | } |
| 39 | SHA1_Final(f->buffer, &f->ctx); |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 40 | if (result) |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 41 | hashcpy(result, f->buffer); |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 42 | if (update) |
| 43 | sha1flush(f, 20); |
| 44 | if (close(f->fd)) |
| 45 | die("%s: sha1 file error on close (%s)", f->name, strerror(errno)); |
Sergey Vlasov | 7bf058f | 2005-08-08 22:46:13 +0400 | [diff] [blame] | 46 | free(f); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int sha1write(struct sha1file *f, void *buf, unsigned int count) |
| 51 | { |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 52 | if (f->do_crc) |
| 53 | f->crc32 = crc32(f->crc32, buf, count); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 54 | while (count) { |
| 55 | unsigned offset = f->offset; |
| 56 | unsigned left = sizeof(f->buffer) - offset; |
| 57 | unsigned nr = count > left ? left : count; |
| 58 | |
| 59 | memcpy(f->buffer + offset, buf, nr); |
| 60 | count -= nr; |
| 61 | offset += nr; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 62 | buf = (char *) buf + nr; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 63 | left -= nr; |
| 64 | if (!left) { |
| 65 | SHA1_Update(&f->ctx, f->buffer, offset); |
| 66 | sha1flush(f, offset); |
| 67 | offset = 0; |
| 68 | } |
| 69 | f->offset = offset; |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | struct sha1file *sha1create(const char *fmt, ...) |
| 75 | { |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 76 | struct sha1file *f; |
| 77 | unsigned len; |
| 78 | va_list arg; |
| 79 | int fd; |
| 80 | |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 81 | f = xmalloc(sizeof(*f)); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 82 | |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 83 | va_start(arg, fmt); |
| 84 | len = vsnprintf(f->name, sizeof(f->name), fmt, arg); |
| 85 | va_end(arg); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 86 | if (len >= PATH_MAX) |
| 87 | die("you wascally wabbit, you"); |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 88 | f->namelen = len; |
| 89 | |
Junio C Hamano | f312de0 | 2005-07-06 01:21:46 -0700 | [diff] [blame] | 90 | fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0666); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 91 | if (fd < 0) |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 92 | die("unable to open %s (%s)", f->name, strerror(errno)); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 93 | f->fd = fd; |
| 94 | f->error = 0; |
| 95 | f->offset = 0; |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 96 | f->do_crc = 0; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 97 | SHA1_Init(&f->ctx); |
| 98 | return f; |
| 99 | } |
| 100 | |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 101 | struct sha1file *sha1fd(int fd, const char *name) |
| 102 | { |
| 103 | struct sha1file *f; |
| 104 | unsigned len; |
| 105 | |
| 106 | f = xmalloc(sizeof(*f)); |
| 107 | |
| 108 | len = strlen(name); |
| 109 | if (len >= PATH_MAX) |
| 110 | die("you wascally wabbit, you"); |
| 111 | f->namelen = len; |
| 112 | memcpy(f->name, name, len+1); |
| 113 | |
| 114 | f->fd = fd; |
| 115 | f->error = 0; |
| 116 | f->offset = 0; |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 117 | f->do_crc = 0; |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 118 | SHA1_Init(&f->ctx); |
| 119 | return f; |
| 120 | } |
| 121 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 122 | int sha1write_compressed(struct sha1file *f, void *in, unsigned int size) |
| 123 | { |
| 124 | z_stream stream; |
| 125 | unsigned long maxsize; |
| 126 | void *out; |
| 127 | |
| 128 | memset(&stream, 0, sizeof(stream)); |
Joachim B Haga | 12f6c30 | 2006-07-03 22:11:47 +0200 | [diff] [blame] | 129 | deflateInit(&stream, zlib_compression_level); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 130 | maxsize = deflateBound(&stream, size); |
| 131 | out = xmalloc(maxsize); |
| 132 | |
| 133 | /* Compress it */ |
| 134 | stream.next_in = in; |
| 135 | stream.avail_in = size; |
| 136 | |
| 137 | stream.next_out = out; |
| 138 | stream.avail_out = maxsize; |
| 139 | |
| 140 | while (deflate(&stream, Z_FINISH) == Z_OK) |
| 141 | /* nothing */; |
| 142 | deflateEnd(&stream); |
| 143 | |
| 144 | size = stream.total_out; |
| 145 | sha1write(f, out, size); |
| 146 | free(out); |
| 147 | return size; |
| 148 | } |
| 149 | |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 150 | void crc32_begin(struct sha1file *f) |
| 151 | { |
| 152 | f->crc32 = crc32(0, Z_NULL, 0); |
| 153 | f->do_crc = 1; |
| 154 | } |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 155 | |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 156 | uint32_t crc32_end(struct sha1file *f) |
| 157 | { |
| 158 | f->do_crc = 0; |
| 159 | return f->crc32; |
| 160 | } |