Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 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" |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 11 | #include "progress.h" |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 12 | #include "csum-file.h" |
| 13 | |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 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 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 28 | static void flush(struct hashfile *f, const void *buf, unsigned int count) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 29 | { |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 30 | if (0 <= f->check_fd && count) |
| 31 | verify_buffer_or_die(f, buf, count); |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 32 | |
Derrick Stolee | 68142e1 | 2021-05-17 12:24:49 +0000 | [diff] [blame] | 33 | if (write_in_full(f->fd, buf, count) < 0) { |
| 34 | if (errno == ENOSPC) |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 35 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 36 | die_errno("sha1 file '%s' write error", f->name); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 37 | } |
Derrick Stolee | 68142e1 | 2021-05-17 12:24:49 +0000 | [diff] [blame] | 38 | |
| 39 | f->total += count; |
| 40 | display_throughput(f->tp, f->total); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 41 | } |
| 42 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 43 | void hashflush(struct hashfile *f) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 44 | { |
| 45 | unsigned offset = f->offset; |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 47 | if (offset) { |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 48 | the_hash_algo->update_fn(&f->ctx, f->buffer, offset); |
Shawn O. Pearce | e782e12 | 2008-10-10 08:39:20 -0700 | [diff] [blame] | 49 | flush(f, f->buffer, offset); |
Dana L. How | f021536 | 2007-05-13 11:28:19 -0700 | [diff] [blame] | 50 | f->offset = 0; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 51 | } |
Nicolas Pitre | 838cd34 | 2008-10-09 22:08:51 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 54 | static void free_hashfile(struct hashfile *f) |
| 55 | { |
| 56 | free(f->buffer); |
| 57 | free(f->check_buffer); |
| 58 | free(f); |
| 59 | } |
| 60 | |
Derrick Stolee | f2af9f5 | 2018-04-02 16:34:14 -0400 | [diff] [blame] | 61 | int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags) |
Nicolas Pitre | 838cd34 | 2008-10-09 22:08:51 -0400 | [diff] [blame] | 62 | { |
| 63 | int fd; |
| 64 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 65 | hashflush(f); |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 66 | the_hash_algo->final_fn(f->buffer, &f->ctx); |
Nicolas Pitre | ac0463e | 2008-08-29 16:08:00 -0400 | [diff] [blame] | 67 | if (result) |
| 68 | hashcpy(result, f->buffer); |
Derrick Stolee | cfe8321 | 2018-04-02 16:34:15 -0400 | [diff] [blame] | 69 | if (flags & CSUM_HASH_IN_STREAM) |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 70 | flush(f, f->buffer, the_hash_algo->rawsz); |
Derrick Stolee | cfe8321 | 2018-04-02 16:34:15 -0400 | [diff] [blame] | 71 | if (flags & CSUM_FSYNC) |
| 72 | fsync_or_die(f->fd, f->name); |
| 73 | if (flags & CSUM_CLOSE) { |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 74 | if (close(f->fd)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 75 | die_errno("%s: sha1 file error on close", f->name); |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 76 | fd = 0; |
| 77 | } else |
| 78 | fd = f->fd; |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 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 | } |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 90 | free_hashfile(f); |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 91 | return fd; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 92 | } |
| 93 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 94 | void hashwrite(struct hashfile *f, const void *buf, unsigned int count) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 95 | { |
| 96 | while (count) { |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 97 | unsigned left = f->buffer_len - f->offset; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 98 | unsigned nr = count > left ? left : count; |
| 99 | |
Nicolas Pitre | a8032d1 | 2008-09-02 10:22:20 -0400 | [diff] [blame] | 100 | if (f->do_crc) |
| 101 | f->crc32 = crc32(f->crc32, buf, nr); |
| 102 | |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 103 | if (nr == f->buffer_len) { |
Derrick Stolee | ddaf1f6 | 2021-03-26 12:38:11 +0000 | [diff] [blame] | 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); |
Nicolas Pitre | a8032d1 | 2008-09-02 10:22:20 -0400 | [diff] [blame] | 112 | } else { |
Derrick Stolee | ddaf1f6 | 2021-03-26 12:38:11 +0000 | [diff] [blame] | 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); |
Nicolas Pitre | a8032d1 | 2008-09-02 10:22:20 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 124 | count -= nr; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 125 | buf = (char *) buf + nr; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 126 | } |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 127 | } |
| 128 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 129 | struct hashfile *hashfd_check(const char *name) |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 130 | { |
| 131 | int sink, check; |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 132 | struct hashfile *f; |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 133 | |
René Scharfe | 66e905b | 2021-08-25 22:16:46 +0200 | [diff] [blame] | 134 | sink = xopen("/dev/null", O_WRONLY); |
| 135 | check = xopen(name, O_RDONLY); |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 136 | f = hashfd(sink, name); |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 137 | f->check_fd = check; |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 138 | f->check_buffer = xmalloc(f->buffer_len); |
| 139 | |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 140 | return f; |
| 141 | } |
| 142 | |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 143 | static struct hashfile *hashfd_internal(int fd, const char *name, |
| 144 | struct progress *tp, |
| 145 | size_t buffer_len) |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 146 | { |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 147 | struct hashfile *f = xmalloc(sizeof(*f)); |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 148 | f->fd = fd; |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 149 | f->check_fd = -1; |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 150 | f->offset = 0; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 151 | f->total = 0; |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 152 | f->tp = tp; |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 153 | f->name = name; |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 154 | f->do_crc = 0; |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 155 | the_hash_algo->init_fn(&f->ctx); |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 156 | |
| 157 | f->buffer_len = buffer_len; |
| 158 | f->buffer = xmalloc(buffer_len); |
| 159 | f->check_buffer = NULL; |
| 160 | |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 161 | return f; |
| 162 | } |
| 163 | |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 164 | struct 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 | |
| 174 | struct 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. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 185 | void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 186 | { |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 187 | hashflush(f); |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 188 | checkpoint->offset = f->total; |
brian m. carlson | 768e30e | 2020-02-22 20:17:27 +0000 | [diff] [blame] | 189 | the_hash_algo->clone_fn(&checkpoint->ctx, &f->ctx); |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 190 | } |
| 191 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 192 | int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 193 | { |
| 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. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 201 | f->offset = 0; /* hashflush() was called in checkpoint */ |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 205 | void crc32_begin(struct hashfile *f) |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 206 | { |
Stephen Boyd | 1e4cd68 | 2011-04-03 00:06:54 -0700 | [diff] [blame] | 207 | f->crc32 = crc32(0, NULL, 0); |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 208 | f->do_crc = 1; |
| 209 | } |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 210 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 211 | uint32_t crc32_end(struct hashfile *f) |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 212 | { |
| 213 | f->do_crc = 0; |
| 214 | return f->crc32; |
| 215 | } |
Taylor Blau | f9221e2 | 2021-06-23 14:39:07 -0400 | [diff] [blame] | 216 | |
| 217 | int 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 | } |