]> git.ipfire.org Git - thirdparty/git.git/blame - csum-file.c
Merge branch 'jk/commit-edit-fixup-fix'
[thirdparty/git.git] / csum-file.c
CommitLineData
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
14static 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 28static 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 43void 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
54static void free_hashfile(struct hashfile *f)
55{
56 free(f->buffer);
57 free(f->check_buffer);
58 free(f);
59}
60
f2af9f5e 61int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags)
838cd346
NP
62{
63 int fd;
64
98a3beab 65 hashflush(f);
4d273500 66 the_hash_algo->final_fn(f->buffer, &f->ctx);
ac0463ed
NP
67 if (result)
68 hashcpy(result, f->buffer);
cfe83216 69 if (flags & CSUM_HASH_IN_STREAM)
4d273500 70 flush(f, f->buffer, the_hash_algo->rawsz);
cfe83216
DS
71 if (flags & CSUM_FSYNC)
72 fsync_or_die(f->fd, f->name);
73 if (flags & CSUM_CLOSE) {
7ba502c4 74 if (close(f->fd))
d824cbba 75 die_errno("%s: sha1 file error on close", f->name);
7ba502c4
NP
76 fd = 0;
77 } else
78 fd = f->fd;
e337a04d
JH
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);
89 }
2ca245f8 90 free_hashfile(f);
7ba502c4 91 return fd;
c38138cd
LT
92}
93
98a3beab 94void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
c38138cd
LT
95{
96 while (count) {
2ca245f8 97 unsigned left = f->buffer_len - f->offset;
c38138cd 98 unsigned nr = count > left ? left : count;
a8032d12
NP
99
100 if (f->do_crc)
101 f->crc32 = crc32(f->crc32, buf, nr);
102
2ca245f8 103 if (nr == f->buffer_len) {
ddaf1f62
DS
104 /*
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.
109 */
110 the_hash_algo->update_fn(&f->ctx, buf, nr);
111 flush(f, buf, nr);
a8032d12 112 } else {
ddaf1f62
DS
113 /*
114 * Copy to the hashfile's buffer, flushing only
115 * if it became full.
116 */
117 memcpy(f->buffer + f->offset, buf, nr);
118 f->offset += nr;
119 left -= nr;
120 if (!left)
121 hashflush(f);
a8032d12 122 }
c38138cd 123
c38138cd 124 count -= nr;
1d7f171c 125 buf = (char *) buf + nr;
c38138cd 126 }
c38138cd
LT
127}
128
98a3beab 129struct hashfile *hashfd_check(const char *name)
e337a04d
JH
130{
131 int sink, check;
98a3beab 132 struct hashfile *f;
e337a04d
JH
133
134 sink = open("/dev/null", O_WRONLY);
135 if (sink < 0)
599d2231 136 die_errno("unable to open /dev/null");
e337a04d 137 check = open(name, O_RDONLY);
599d2231
JK
138 if (check < 0)
139 die_errno("unable to open '%s'", name);
98a3beab 140 f = hashfd(sink, name);
e337a04d 141 f->check_fd = check;
2ca245f8
DS
142 f->check_buffer = xmalloc(f->buffer_len);
143
e337a04d
JH
144 return f;
145}
146
2ca245f8
DS
147static struct hashfile *hashfd_internal(int fd, const char *name,
148 struct progress *tp,
149 size_t buffer_len)
4397f014 150{
98a3beab 151 struct hashfile *f = xmalloc(sizeof(*f));
4397f014 152 f->fd = fd;
e337a04d 153 f->check_fd = -1;
4397f014 154 f->offset = 0;
218558af 155 f->total = 0;
2a128d63 156 f->tp = tp;
ec640ed1 157 f->name = name;
78d1e84f 158 f->do_crc = 0;
4d273500 159 the_hash_algo->init_fn(&f->ctx);
2ca245f8
DS
160
161 f->buffer_len = buffer_len;
162 f->buffer = xmalloc(buffer_len);
163 f->check_buffer = NULL;
164
4397f014
LT
165 return f;
166}
167
2ca245f8
DS
168struct hashfile *hashfd(int fd, const char *name)
169{
170 /*
171 * Since we are not going to use a progress meter to
172 * measure the rate of data passing through this hashfile,
173 * use a larger buffer size to reduce fsync() calls.
174 */
175 return hashfd_internal(fd, name, NULL, 128 * 1024);
176}
177
178struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
179{
180 /*
181 * Since we are expecting to report progress of the
182 * write into this hashfile, use a smaller buffer
183 * size so the progress indicators arrive at a more
184 * frequent rate.
185 */
186 return hashfd_internal(fd, name, tp, 8 * 1024);
187}
188
98a3beab 189void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
6c526148 190{
98a3beab 191 hashflush(f);
6c526148 192 checkpoint->offset = f->total;
768e30ea 193 the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx);
6c526148
JH
194}
195
98a3beab 196int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
6c526148
JH
197{
198 off_t offset = checkpoint->offset;
199
200 if (ftruncate(f->fd, offset) ||
201 lseek(f->fd, offset, SEEK_SET) != offset)
202 return -1;
203 f->total = offset;
204 f->ctx = checkpoint->ctx;
98a3beab 205 f->offset = 0; /* hashflush() was called in checkpoint */
6c526148
JH
206 return 0;
207}
208
98a3beab 209void crc32_begin(struct hashfile *f)
78d1e84f 210{
1e4cd68c 211 f->crc32 = crc32(0, NULL, 0);
78d1e84f
NP
212 f->do_crc = 1;
213}
c38138cd 214
98a3beab 215uint32_t crc32_end(struct hashfile *f)
78d1e84f
NP
216{
217 f->do_crc = 0;
218 return f->crc32;
219}
f9221e2c
TB
220
221int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
222{
223 unsigned char got[GIT_MAX_RAWSZ];
224 git_hash_ctx ctx;
225 size_t data_len = total_len - the_hash_algo->rawsz;
226
227 if (total_len < the_hash_algo->rawsz)
228 return 0; /* say "too short"? */
229
230 the_hash_algo->init_fn(&ctx);
231 the_hash_algo->update_fn(&ctx, data, data_len);
232 the_hash_algo->final_fn(got, &ctx);
233
234 return hasheq(got, data + data_len);
235}