Sync with 2.33.8
[git/debian.git] / csum-file.c
blob26e8a6df44e9415ffe02fc612ec045c6fa32b032
1 /*
2 * csum-file.c
4 * Copyright (C) 2005 Linus Torvalds
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"
14 static void verify_buffer_or_die(struct hashfile *f,
15 const void *buf,
16 unsigned int count)
18 ssize_t ret = read_in_full(f->check_fd, f->check_buffer, count);
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);
28 static void flush(struct hashfile *f, const void *buf, unsigned int count)
30 if (0 <= f->check_fd && count)
31 verify_buffer_or_die(f, buf, count);
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);
39 f->total += count;
40 display_throughput(f->tp, f->total);
43 void hashflush(struct hashfile *f)
45 unsigned offset = f->offset;
47 if (offset) {
48 the_hash_algo->update_fn(&f->ctx, f->buffer, offset);
49 flush(f, f->buffer, offset);
50 f->offset = 0;
54 static void free_hashfile(struct hashfile *f)
56 free(f->buffer);
57 free(f->check_buffer);
58 free(f);
61 int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags)
63 int fd;
65 hashflush(f);
66 the_hash_algo->final_fn(f->buffer, &f->ctx);
67 if (result)
68 hashcpy(result, f->buffer);
69 if (flags & CSUM_HASH_IN_STREAM)
70 flush(f, f->buffer, the_hash_algo->rawsz);
71 if (flags & CSUM_FSYNC)
72 fsync_or_die(f->fd, f->name);
73 if (flags & CSUM_CLOSE) {
74 if (close(f->fd))
75 die_errno("%s: sha1 file error on close", f->name);
76 fd = 0;
77 } else
78 fd = f->fd;
79 if (0 <= f->check_fd) {
80 char discard;
81 int cnt = read_in_full(f->check_fd, &discard, 1);
82 if (cnt < 0)
83 die_errno("%s: error when reading the tail of sha1 file",
84 f->name);
85 if (cnt)
86 die("%s: sha1 file has trailing garbage", f->name);
87 if (close(f->check_fd))
88 die_errno("%s: sha1 file error on close", f->name);
90 free_hashfile(f);
91 return fd;
94 void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
96 while (count) {
97 unsigned left = f->buffer_len - f->offset;
98 unsigned nr = count > left ? left : count;
100 if (f->do_crc)
101 f->crc32 = crc32(f->crc32, buf, nr);
103 if (nr == f->buffer_len) {
105 * Flush a full batch worth of data directly
106 * from the input, skipping the memcpy() to
107 * the hashfile's buffer. In this block,
108 * f->offset is necessarily zero.
110 the_hash_algo->update_fn(&f->ctx, buf, nr);
111 flush(f, buf, nr);
112 } else {
114 * Copy to the hashfile's buffer, flushing only
115 * if it became full.
117 memcpy(f->buffer + f->offset, buf, nr);
118 f->offset += nr;
119 left -= nr;
120 if (!left)
121 hashflush(f);
124 count -= nr;
125 buf = (char *) buf + nr;
129 struct hashfile *hashfd_check(const char *name)
131 int sink, check;
132 struct hashfile *f;
134 sink = xopen("/dev/null", O_WRONLY);
135 check = xopen(name, O_RDONLY);
136 f = hashfd(sink, name);
137 f->check_fd = check;
138 f->check_buffer = xmalloc(f->buffer_len);
140 return f;
143 static struct hashfile *hashfd_internal(int fd, const char *name,
144 struct progress *tp,
145 size_t buffer_len)
147 struct hashfile *f = xmalloc(sizeof(*f));
148 f->fd = fd;
149 f->check_fd = -1;
150 f->offset = 0;
151 f->total = 0;
152 f->tp = tp;
153 f->name = name;
154 f->do_crc = 0;
155 the_hash_algo->init_fn(&f->ctx);
157 f->buffer_len = buffer_len;
158 f->buffer = xmalloc(buffer_len);
159 f->check_buffer = NULL;
161 return f;
164 struct hashfile *hashfd(int fd, const char *name)
167 * Since we are not going to use a progress meter to
168 * measure the rate of data passing through this hashfile,
169 * use a larger buffer size to reduce fsync() calls.
171 return hashfd_internal(fd, name, NULL, 128 * 1024);
174 struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
177 * Since we are expecting to report progress of the
178 * write into this hashfile, use a smaller buffer
179 * size so the progress indicators arrive at a more
180 * frequent rate.
182 return hashfd_internal(fd, name, tp, 8 * 1024);
185 void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
187 hashflush(f);
188 checkpoint->offset = f->total;
189 the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx);
192 int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
194 off_t offset = checkpoint->offset;
196 if (ftruncate(f->fd, offset) ||
197 lseek(f->fd, offset, SEEK_SET) != offset)
198 return -1;
199 f->total = offset;
200 f->ctx = checkpoint->ctx;
201 f->offset = 0; /* hashflush() was called in checkpoint */
202 return 0;
205 void crc32_begin(struct hashfile *f)
207 f->crc32 = crc32(0, NULL, 0);
208 f->do_crc = 1;
211 uint32_t crc32_end(struct hashfile *f)
213 f->do_crc = 0;
214 return f->crc32;
217 int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
219 unsigned char got[GIT_MAX_RAWSZ];
220 git_hash_ctx ctx;
221 size_t data_len = total_len - the_hash_algo->rawsz;
223 if (total_len < the_hash_algo->rawsz)
224 return 0; /* say "too short"? */
226 the_hash_algo->init_fn(&ctx);
227 the_hash_algo->update_fn(&ctx, data, data_len);
228 the_hash_algo->final_fn(got, &ctx);
230 return hasheq(got, data + data_len);