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