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