blob: 59ef3398ca2b01676055fff40b80ac7896f87ffc [file] [log] [blame]
Linus Torvaldsc38138c2005-06-26 20:27:56 -07001/*
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"
Nicolas Pitre2a128d62007-10-30 17:06:21 -040011#include "progress.h"
Linus Torvaldsc38138c2005-06-26 20:27:56 -070012#include "csum-file.h"
13
Derrick Stolee2ca245f2021-05-18 18:32:46 +000014static 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
brian m. carlson98a3bea2018-02-01 02:18:46 +000028static void flush(struct hashfile *f, const void *buf, unsigned int count)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070029{
Derrick Stolee2ca245f2021-05-18 18:32:46 +000030 if (0 <= f->check_fd && count)
31 verify_buffer_or_die(f, buf, count);
Junio C Hamanoe337a042011-02-02 17:29:01 -080032
Derrick Stolee68142e12021-05-17 12:24:49 +000033 if (write_in_full(f->fd, buf, count) < 0) {
34 if (errno == ENOSPC)
Linus Torvaldse1808842005-06-26 22:01:46 -070035 die("sha1 file '%s' write error. Out of diskspace", f->name);
Thomas Rastd824cbb2009-06-27 17:58:46 +020036 die_errno("sha1 file '%s' write error", f->name);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070037 }
Derrick Stolee68142e12021-05-17 12:24:49 +000038
39 f->total += count;
40 display_throughput(f->tp, f->total);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070041}
42
brian m. carlson98a3bea2018-02-01 02:18:46 +000043void hashflush(struct hashfile *f)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070044{
45 unsigned offset = f->offset;
Linus Torvalds4c81b032008-05-30 08:42:16 -070046
Linus Torvaldsc38138c2005-06-26 20:27:56 -070047 if (offset) {
brian m. carlson4d273502018-02-01 02:18:47 +000048 the_hash_algo->update_fn(&f->ctx, f->buffer, offset);
Shawn O. Pearcee782e122008-10-10 08:39:20 -070049 flush(f, f->buffer, offset);
Dana L. Howf0215362007-05-13 11:28:19 -070050 f->offset = 0;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070051 }
Nicolas Pitre838cd342008-10-09 22:08:51 -040052}
53
Derrick Stolee2ca245f2021-05-18 18:32:46 +000054static void free_hashfile(struct hashfile *f)
55{
56 free(f->buffer);
57 free(f->check_buffer);
58 free(f);
59}
60
Neeraj Singh020406e2022-03-10 22:43:21 +000061int finalize_hashfile(struct hashfile *f, unsigned char *result,
62 enum fsync_component component, unsigned int flags)
Nicolas Pitre838cd342008-10-09 22:08:51 -040063{
64 int fd;
65
brian m. carlson98a3bea2018-02-01 02:18:46 +000066 hashflush(f);
brian m. carlson4d273502018-02-01 02:18:47 +000067 the_hash_algo->final_fn(f->buffer, &f->ctx);
Nicolas Pitreac0463e2008-08-29 16:08:00 -040068 if (result)
69 hashcpy(result, f->buffer);
Derrick Stoleecfe83212018-04-02 16:34:15 -040070 if (flags & CSUM_HASH_IN_STREAM)
brian m. carlson4d273502018-02-01 02:18:47 +000071 flush(f, f->buffer, the_hash_algo->rawsz);
Derrick Stoleecfe83212018-04-02 16:34:15 -040072 if (flags & CSUM_FSYNC)
Neeraj Singh020406e2022-03-10 22:43:21 +000073 fsync_component_or_die(component, f->fd, f->name);
Derrick Stoleecfe83212018-04-02 16:34:15 -040074 if (flags & CSUM_CLOSE) {
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040075 if (close(f->fd))
Thomas Rastd824cbb2009-06-27 17:58:46 +020076 die_errno("%s: sha1 file error on close", f->name);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040077 fd = 0;
78 } else
79 fd = f->fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -080080 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 }
Derrick Stolee2ca245f2021-05-18 18:32:46 +000091 free_hashfile(f);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040092 return fd;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070093}
94
brian m. carlson98a3bea2018-02-01 02:18:46 +000095void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070096{
97 while (count) {
Derrick Stolee2ca245f2021-05-18 18:32:46 +000098 unsigned left = f->buffer_len - f->offset;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070099 unsigned nr = count > left ? left : count;
100
Nicolas Pitrea8032d12008-09-02 10:22:20 -0400101 if (f->do_crc)
102 f->crc32 = crc32(f->crc32, buf, nr);
103
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000104 if (nr == f->buffer_len) {
Derrick Stoleeddaf1f62021-03-26 12:38:11 +0000105 /*
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);
Nicolas Pitrea8032d12008-09-02 10:22:20 -0400113 } else {
Derrick Stoleeddaf1f62021-03-26 12:38:11 +0000114 /*
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);
Nicolas Pitrea8032d12008-09-02 10:22:20 -0400123 }
124
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700125 count -= nr;
Florian Forster1d7f1712006-06-18 17:18:09 +0200126 buf = (char *) buf + nr;
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700127 }
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700128}
129
brian m. carlson98a3bea2018-02-01 02:18:46 +0000130struct hashfile *hashfd_check(const char *name)
Junio C Hamanoe337a042011-02-02 17:29:01 -0800131{
132 int sink, check;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000133 struct hashfile *f;
Junio C Hamanoe337a042011-02-02 17:29:01 -0800134
René Scharfe66e905b2021-08-25 22:16:46 +0200135 sink = xopen("/dev/null", O_WRONLY);
136 check = xopen(name, O_RDONLY);
brian m. carlson98a3bea2018-02-01 02:18:46 +0000137 f = hashfd(sink, name);
Junio C Hamanoe337a042011-02-02 17:29:01 -0800138 f->check_fd = check;
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000139 f->check_buffer = xmalloc(f->buffer_len);
140
Junio C Hamanoe337a042011-02-02 17:29:01 -0800141 return f;
142}
143
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000144static struct hashfile *hashfd_internal(int fd, const char *name,
145 struct progress *tp,
146 size_t buffer_len)
Nicolas Pitre2a128d62007-10-30 17:06:21 -0400147{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000148 struct hashfile *f = xmalloc(sizeof(*f));
Linus Torvalds4397f012005-06-28 11:10:06 -0700149 f->fd = fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -0800150 f->check_fd = -1;
Linus Torvalds4397f012005-06-28 11:10:06 -0700151 f->offset = 0;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500152 f->total = 0;
Nicolas Pitre2a128d62007-10-30 17:06:21 -0400153 f->tp = tp;
Nicolas Pitreec640ed2007-11-04 22:54:50 -0500154 f->name = name;
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400155 f->do_crc = 0;
brian m. carlson4d273502018-02-01 02:18:47 +0000156 the_hash_algo->init_fn(&f->ctx);
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000157
158 f->buffer_len = buffer_len;
159 f->buffer = xmalloc(buffer_len);
160 f->check_buffer = NULL;
161
Linus Torvalds4397f012005-06-28 11:10:06 -0700162 return f;
163}
164
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000165struct 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
175struct 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
brian m. carlson98a3bea2018-02-01 02:18:46 +0000186void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
Junio C Hamano6c526142011-11-17 16:26:54 -0800187{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000188 hashflush(f);
Junio C Hamano6c526142011-11-17 16:26:54 -0800189 checkpoint->offset = f->total;
brian m. carlson768e30e2020-02-22 20:17:27 +0000190 the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx);
Junio C Hamano6c526142011-11-17 16:26:54 -0800191}
192
brian m. carlson98a3bea2018-02-01 02:18:46 +0000193int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
Junio C Hamano6c526142011-11-17 16:26:54 -0800194{
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;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000202 f->offset = 0; /* hashflush() was called in checkpoint */
Junio C Hamano6c526142011-11-17 16:26:54 -0800203 return 0;
204}
205
brian m. carlson98a3bea2018-02-01 02:18:46 +0000206void crc32_begin(struct hashfile *f)
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400207{
Stephen Boyd1e4cd682011-04-03 00:06:54 -0700208 f->crc32 = crc32(0, NULL, 0);
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400209 f->do_crc = 1;
210}
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700211
brian m. carlson98a3bea2018-02-01 02:18:46 +0000212uint32_t crc32_end(struct hashfile *f)
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400213{
214 f->do_crc = 0;
215 return f->crc32;
216}
Taylor Blauf9221e22021-06-23 14:39:07 -0400217
218int 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}