blob: 26e8a6df44e9415ffe02fc612ec045c6fa32b032 [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
Derrick Stoleef2af9f52018-04-02 16:34:14 -040061int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags)
Nicolas Pitre838cd342008-10-09 22:08:51 -040062{
63 int fd;
64
brian m. carlson98a3bea2018-02-01 02:18:46 +000065 hashflush(f);
brian m. carlson4d273502018-02-01 02:18:47 +000066 the_hash_algo->final_fn(f->buffer, &f->ctx);
Nicolas Pitreac0463e2008-08-29 16:08:00 -040067 if (result)
68 hashcpy(result, f->buffer);
Derrick Stoleecfe83212018-04-02 16:34:15 -040069 if (flags & CSUM_HASH_IN_STREAM)
brian m. carlson4d273502018-02-01 02:18:47 +000070 flush(f, f->buffer, the_hash_algo->rawsz);
Derrick Stoleecfe83212018-04-02 16:34:15 -040071 if (flags & CSUM_FSYNC)
72 fsync_or_die(f->fd, f->name);
73 if (flags & CSUM_CLOSE) {
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040074 if (close(f->fd))
Thomas Rastd824cbb2009-06-27 17:58:46 +020075 die_errno("%s: sha1 file error on close", f->name);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040076 fd = 0;
77 } else
78 fd = f->fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -080079 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 }
Derrick Stolee2ca245f2021-05-18 18:32:46 +000090 free_hashfile(f);
Nicolas Pitre7ba502c2007-10-16 21:55:48 -040091 return fd;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070092}
93
brian m. carlson98a3bea2018-02-01 02:18:46 +000094void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
Linus Torvaldsc38138c2005-06-26 20:27:56 -070095{
96 while (count) {
Derrick Stolee2ca245f2021-05-18 18:32:46 +000097 unsigned left = f->buffer_len - f->offset;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070098 unsigned nr = count > left ? left : count;
99
Nicolas Pitrea8032d12008-09-02 10:22:20 -0400100 if (f->do_crc)
101 f->crc32 = crc32(f->crc32, buf, nr);
102
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000103 if (nr == f->buffer_len) {
Derrick Stoleeddaf1f62021-03-26 12:38:11 +0000104 /*
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);
Nicolas Pitrea8032d12008-09-02 10:22:20 -0400112 } else {
Derrick Stoleeddaf1f62021-03-26 12:38:11 +0000113 /*
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);
Nicolas Pitrea8032d12008-09-02 10:22:20 -0400122 }
123
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700124 count -= nr;
Florian Forster1d7f1712006-06-18 17:18:09 +0200125 buf = (char *) buf + nr;
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700126 }
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700127}
128
brian m. carlson98a3bea2018-02-01 02:18:46 +0000129struct hashfile *hashfd_check(const char *name)
Junio C Hamanoe337a042011-02-02 17:29:01 -0800130{
131 int sink, check;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000132 struct hashfile *f;
Junio C Hamanoe337a042011-02-02 17:29:01 -0800133
René Scharfe66e905b2021-08-25 22:16:46 +0200134 sink = xopen("/dev/null", O_WRONLY);
135 check = xopen(name, O_RDONLY);
brian m. carlson98a3bea2018-02-01 02:18:46 +0000136 f = hashfd(sink, name);
Junio C Hamanoe337a042011-02-02 17:29:01 -0800137 f->check_fd = check;
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000138 f->check_buffer = xmalloc(f->buffer_len);
139
Junio C Hamanoe337a042011-02-02 17:29:01 -0800140 return f;
141}
142
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000143static struct hashfile *hashfd_internal(int fd, const char *name,
144 struct progress *tp,
145 size_t buffer_len)
Nicolas Pitre2a128d62007-10-30 17:06:21 -0400146{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000147 struct hashfile *f = xmalloc(sizeof(*f));
Linus Torvalds4397f012005-06-28 11:10:06 -0700148 f->fd = fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -0800149 f->check_fd = -1;
Linus Torvalds4397f012005-06-28 11:10:06 -0700150 f->offset = 0;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500151 f->total = 0;
Nicolas Pitre2a128d62007-10-30 17:06:21 -0400152 f->tp = tp;
Nicolas Pitreec640ed2007-11-04 22:54:50 -0500153 f->name = name;
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400154 f->do_crc = 0;
brian m. carlson4d273502018-02-01 02:18:47 +0000155 the_hash_algo->init_fn(&f->ctx);
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000156
157 f->buffer_len = buffer_len;
158 f->buffer = xmalloc(buffer_len);
159 f->check_buffer = NULL;
160
Linus Torvalds4397f012005-06-28 11:10:06 -0700161 return f;
162}
163
Derrick Stolee2ca245f2021-05-18 18:32:46 +0000164struct hashfile *hashfd(int fd, const char *name)
165{
166 /*
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.
170 */
171 return hashfd_internal(fd, name, NULL, 128 * 1024);
172}
173
174struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp)
175{
176 /*
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.
181 */
182 return hashfd_internal(fd, name, tp, 8 * 1024);
183}
184
brian m. carlson98a3bea2018-02-01 02:18:46 +0000185void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
Junio C Hamano6c526142011-11-17 16:26:54 -0800186{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000187 hashflush(f);
Junio C Hamano6c526142011-11-17 16:26:54 -0800188 checkpoint->offset = f->total;
brian m. carlson768e30e2020-02-22 20:17:27 +0000189 the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx);
Junio C Hamano6c526142011-11-17 16:26:54 -0800190}
191
brian m. carlson98a3bea2018-02-01 02:18:46 +0000192int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
Junio C Hamano6c526142011-11-17 16:26:54 -0800193{
194 off_t offset = checkpoint->offset;
195
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;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000201 f->offset = 0; /* hashflush() was called in checkpoint */
Junio C Hamano6c526142011-11-17 16:26:54 -0800202 return 0;
203}
204
brian m. carlson98a3bea2018-02-01 02:18:46 +0000205void crc32_begin(struct hashfile *f)
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400206{
Stephen Boyd1e4cd682011-04-03 00:06:54 -0700207 f->crc32 = crc32(0, NULL, 0);
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400208 f->do_crc = 1;
209}
Linus Torvaldsc38138c2005-06-26 20:27:56 -0700210
brian m. carlson98a3bea2018-02-01 02:18:46 +0000211uint32_t crc32_end(struct hashfile *f)
Nicolas Pitre78d1e842007-04-09 01:06:31 -0400212{
213 f->do_crc = 0;
214 return f->crc32;
215}
Taylor Blauf9221e22021-06-23 14:39:07 -0400216
217int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
218{
219 unsigned char got[GIT_MAX_RAWSZ];
220 git_hash_ctx ctx;
221 size_t data_len = total_len - the_hash_algo->rawsz;
222
223 if (total_len < the_hash_algo->rawsz)
224 return 0; /* say "too short"? */
225
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);
229
230 return hasheq(got, data + data_len);
231}