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