]>
Commit | Line | Data |
---|---|---|
c38138cd LT |
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 | */ | |
98750588 | 10 | #include "git-compat-util.h" |
2a128d63 | 11 | #include "progress.h" |
c38138cd | 12 | #include "csum-file.h" |
d1cbe1e6 | 13 | #include "hash.h" |
c38138cd | 14 | |
2ca245f8 DS |
15 | static void verify_buffer_or_die(struct hashfile *f, |
16 | const void *buf, | |
17 | unsigned int count) | |
18 | { | |
19 | ssize_t ret = read_in_full(f->check_fd, f->check_buffer, count); | |
20 | ||
21 | if (ret < 0) | |
22 | die_errno("%s: sha1 file read error", f->name); | |
23 | if (ret != count) | |
24 | die("%s: sha1 file truncated", f->name); | |
25 | if (memcmp(buf, f->check_buffer, count)) | |
26 | die("sha1 file '%s' validation error", f->name); | |
27 | } | |
28 | ||
98a3beab | 29 | static void flush(struct hashfile *f, const void *buf, unsigned int count) |
c38138cd | 30 | { |
2ca245f8 DS |
31 | if (0 <= f->check_fd && count) |
32 | verify_buffer_or_die(f, buf, count); | |
e337a04d | 33 | |
68142e11 DS |
34 | if (write_in_full(f->fd, buf, count) < 0) { |
35 | if (errno == ENOSPC) | |
e1808845 | 36 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
d824cbba | 37 | die_errno("sha1 file '%s' write error", f->name); |
c38138cd | 38 | } |
68142e11 DS |
39 | |
40 | f->total += count; | |
41 | display_throughput(f->tp, f->total); | |
c38138cd LT |
42 | } |
43 | ||
98a3beab | 44 | void hashflush(struct hashfile *f) |
c38138cd LT |
45 | { |
46 | unsigned offset = f->offset; | |
4c81b03e | 47 | |
c38138cd | 48 | if (offset) { |
1687150b DS |
49 | if (!f->skip_hash) |
50 | the_hash_algo->update_fn(&f->ctx, f->buffer, offset); | |
e782e12f | 51 | flush(f, f->buffer, offset); |
f0215369 | 52 | f->offset = 0; |
c38138cd | 53 | } |
838cd346 NP |
54 | } |
55 | ||
2ca245f8 DS |
56 | static void free_hashfile(struct hashfile *f) |
57 | { | |
58 | free(f->buffer); | |
59 | free(f->check_buffer); | |
60 | free(f); | |
61 | } | |
62 | ||
020406ea NS |
63 | int finalize_hashfile(struct hashfile *f, unsigned char *result, |
64 | enum fsync_component component, unsigned int flags) | |
838cd346 NP |
65 | { |
66 | int fd; | |
67 | ||
98a3beab | 68 | hashflush(f); |
1687150b DS |
69 | |
70 | if (f->skip_hash) | |
71 | hashclr(f->buffer); | |
72 | else | |
73 | the_hash_algo->final_fn(f->buffer, &f->ctx); | |
74 | ||
ac0463ed NP |
75 | if (result) |
76 | hashcpy(result, f->buffer); | |
cfe83216 | 77 | if (flags & CSUM_HASH_IN_STREAM) |
4d273500 | 78 | flush(f, f->buffer, the_hash_algo->rawsz); |
cfe83216 | 79 | if (flags & CSUM_FSYNC) |
020406ea | 80 | fsync_component_or_die(component, f->fd, f->name); |
cfe83216 | 81 | if (flags & CSUM_CLOSE) { |
7ba502c4 | 82 | if (close(f->fd)) |
d824cbba | 83 | die_errno("%s: sha1 file error on close", f->name); |
7ba502c4 NP |
84 | fd = 0; |
85 | } else | |
86 | fd = f->fd; | |
e337a04d JH |
87 | if (0 <= f->check_fd) { |
88 | char discard; | |
89 | int cnt = read_in_full(f->check_fd, &discard, 1); | |
90 | if (cnt < 0) | |
91 | die_errno("%s: error when reading the tail of sha1 file", | |
92 | f->name); | |
93 | if (cnt) | |
94 | die("%s: sha1 file has trailing garbage", f->name); | |
95 | if (close(f->check_fd)) | |
96 | die_errno("%s: sha1 file error on close", f->name); | |
97 | } | |
2ca245f8 | 98 | free_hashfile(f); |
7ba502c4 | 99 | return fd; |
c38138cd LT |
100 | } |
101 | ||
98a3beab | 102 | void hashwrite(struct hashfile *f, const void *buf, unsigned int count) |
c38138cd LT |
103 | { |
104 | while (count) { | |
2ca245f8 | 105 | unsigned left = f->buffer_len - f->offset; |
c38138cd | 106 | unsigned nr = count > left ? left : count; |
a8032d12 NP |
107 | |
108 | if (f->do_crc) | |
109 | f->crc32 = crc32(f->crc32, buf, nr); | |
110 | ||
2ca245f8 | 111 | if (nr == f->buffer_len) { |
ddaf1f62 DS |
112 | /* |
113 | * Flush a full batch worth of data directly | |
114 | * from the input, skipping the memcpy() to | |
115 | * the hashfile's buffer. In this block, | |
116 | * f->offset is necessarily zero. | |
117 | */ | |
1687150b DS |
118 | if (!f->skip_hash) |
119 | the_hash_algo->update_fn(&f->ctx, buf, nr); | |
ddaf1f62 | 120 | flush(f, buf, nr); |
a8032d12 | 121 | } else { |
ddaf1f62 DS |
122 | /* |
123 | * Copy to the hashfile's buffer, flushing only | |
124 | * if it became full. | |
125 | */ | |
126 | memcpy(f->buffer + f->offset, buf, nr); | |
127 | f->offset += nr; | |
128 | left -= nr; | |
129 | if (!left) | |
130 | hashflush(f); | |
a8032d12 | 131 | } |
c38138cd | 132 | |
c38138cd | 133 | count -= nr; |
1d7f171c | 134 | buf = (char *) buf + nr; |
c38138cd | 135 | } |
c38138cd LT |
136 | } |
137 | ||
98a3beab | 138 | struct hashfile *hashfd_check(const char *name) |
e337a04d JH |
139 | { |
140 | int sink, check; | |
98a3beab | 141 | struct hashfile *f; |
e337a04d | 142 | |
66e905b7 RS |
143 | sink = xopen("/dev/null", O_WRONLY); |
144 | check = xopen(name, O_RDONLY); | |
98a3beab | 145 | f = hashfd(sink, name); |
e337a04d | 146 | f->check_fd = check; |
2ca245f8 DS |
147 | f->check_buffer = xmalloc(f->buffer_len); |
148 | ||
e337a04d JH |
149 | return f; |
150 | } | |
151 | ||
2ca245f8 DS |
152 | static struct hashfile *hashfd_internal(int fd, const char *name, |
153 | struct progress *tp, | |
154 | size_t buffer_len) | |
4397f014 | 155 | { |
98a3beab | 156 | struct hashfile *f = xmalloc(sizeof(*f)); |
4397f014 | 157 | f->fd = fd; |
e337a04d | 158 | f->check_fd = -1; |
4397f014 | 159 | f->offset = 0; |
218558af | 160 | f->total = 0; |
2a128d63 | 161 | f->tp = tp; |
ec640ed1 | 162 | f->name = name; |
78d1e84f | 163 | f->do_crc = 0; |
1687150b | 164 | f->skip_hash = 0; |
4d273500 | 165 | the_hash_algo->init_fn(&f->ctx); |
2ca245f8 DS |
166 | |
167 | f->buffer_len = buffer_len; | |
168 | f->buffer = xmalloc(buffer_len); | |
169 | f->check_buffer = NULL; | |
170 | ||
4397f014 LT |
171 | return f; |
172 | } | |
173 | ||
2ca245f8 DS |
174 | struct hashfile *hashfd(int fd, const char *name) |
175 | { | |
176 | /* | |
177 | * Since we are not going to use a progress meter to | |
178 | * measure the rate of data passing through this hashfile, | |
179 | * use a larger buffer size to reduce fsync() calls. | |
180 | */ | |
181 | return hashfd_internal(fd, name, NULL, 128 * 1024); | |
182 | } | |
183 | ||
184 | struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp) | |
185 | { | |
186 | /* | |
187 | * Since we are expecting to report progress of the | |
188 | * write into this hashfile, use a smaller buffer | |
189 | * size so the progress indicators arrive at a more | |
190 | * frequent rate. | |
191 | */ | |
192 | return hashfd_internal(fd, name, tp, 8 * 1024); | |
193 | } | |
194 | ||
98a3beab | 195 | void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
6c526148 | 196 | { |
98a3beab | 197 | hashflush(f); |
6c526148 | 198 | checkpoint->offset = f->total; |
768e30ea | 199 | the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx); |
6c526148 JH |
200 | } |
201 | ||
98a3beab | 202 | int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
6c526148 JH |
203 | { |
204 | off_t offset = checkpoint->offset; | |
205 | ||
206 | if (ftruncate(f->fd, offset) || | |
207 | lseek(f->fd, offset, SEEK_SET) != offset) | |
208 | return -1; | |
209 | f->total = offset; | |
e0b8c842 | 210 | the_hash_algo->clone_fn(&f->ctx, &checkpoint->ctx); |
98a3beab | 211 | f->offset = 0; /* hashflush() was called in checkpoint */ |
6c526148 JH |
212 | return 0; |
213 | } | |
214 | ||
98a3beab | 215 | void crc32_begin(struct hashfile *f) |
78d1e84f | 216 | { |
1e4cd68c | 217 | f->crc32 = crc32(0, NULL, 0); |
78d1e84f NP |
218 | f->do_crc = 1; |
219 | } | |
c38138cd | 220 | |
98a3beab | 221 | uint32_t crc32_end(struct hashfile *f) |
78d1e84f NP |
222 | { |
223 | f->do_crc = 0; | |
224 | return f->crc32; | |
225 | } | |
f9221e2c TB |
226 | |
227 | int hashfile_checksum_valid(const unsigned char *data, size_t total_len) | |
228 | { | |
229 | unsigned char got[GIT_MAX_RAWSZ]; | |
230 | git_hash_ctx ctx; | |
231 | size_t data_len = total_len - the_hash_algo->rawsz; | |
232 | ||
233 | if (total_len < the_hash_algo->rawsz) | |
234 | return 0; /* say "too short"? */ | |
235 | ||
236 | the_hash_algo->init_fn(&ctx); | |
237 | the_hash_algo->update_fn(&ctx, data, data_len); | |
238 | the_hash_algo->final_fn(got, &ctx); | |
239 | ||
240 | return hasheq(got, data + data_len); | |
241 | } |