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 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 14 | static void flush(struct hashfile *f, const void *buf, unsigned int count) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 15 | { |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 16 | if (0 <= f->check_fd && count) { |
| 17 | unsigned char check_buffer[8192]; |
| 18 | ssize_t ret = read_in_full(f->check_fd, check_buffer, count); |
| 19 | |
| 20 | if (ret < 0) |
| 21 | die_errno("%s: sha1 file read error", f->name); |
Jeff King | 61d3633 | 2017-09-27 02:00:28 -0400 | [diff] [blame] | 22 | if (ret != count) |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 23 | die("%s: sha1 file truncated", f->name); |
| 24 | if (memcmp(buf, check_buffer, count)) |
| 25 | die("sha1 file '%s' validation error", f->name); |
| 26 | } |
| 27 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 28 | for (;;) { |
Junio C Hamano | 1c15afb | 2005-12-19 16:18:28 -0800 | [diff] [blame] | 29 | int ret = xwrite(f->fd, buf, count); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 30 | if (ret > 0) { |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 31 | f->total += ret; |
| 32 | display_throughput(f->tp, f->total); |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 33 | buf = (char *) buf + ret; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 34 | count -= ret; |
| 35 | if (count) |
| 36 | continue; |
David Rientjes | 78b713a | 2006-08-14 13:32:01 -0700 | [diff] [blame] | 37 | return; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 38 | } |
| 39 | if (!ret) |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 40 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 41 | die_errno("sha1 file '%s' write error", f->name); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 45 | void hashflush(struct hashfile *f) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 46 | { |
| 47 | unsigned offset = f->offset; |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 49 | if (offset) { |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 50 | the_hash_algo->update_fn(&f->ctx, f->buffer, offset); |
Shawn O. Pearce | e782e12 | 2008-10-10 08:39:20 -0700 | [diff] [blame] | 51 | flush(f, f->buffer, offset); |
Dana L. How | f021536 | 2007-05-13 11:28:19 -0700 | [diff] [blame] | 52 | f->offset = 0; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 53 | } |
Nicolas Pitre | 838cd34 | 2008-10-09 22:08:51 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Derrick Stolee | f2af9f5 | 2018-04-02 16:34:14 -0400 | [diff] [blame] | 56 | int finalize_hashfile(struct hashfile *f, unsigned char *result, unsigned int flags) |
Nicolas Pitre | 838cd34 | 2008-10-09 22:08:51 -0400 | [diff] [blame] | 57 | { |
| 58 | int fd; |
| 59 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 60 | hashflush(f); |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 61 | the_hash_algo->final_fn(f->buffer, &f->ctx); |
Nicolas Pitre | ac0463e | 2008-08-29 16:08:00 -0400 | [diff] [blame] | 62 | if (result) |
| 63 | hashcpy(result, f->buffer); |
Derrick Stolee | cfe8321 | 2018-04-02 16:34:15 -0400 | [diff] [blame] | 64 | if (flags & CSUM_HASH_IN_STREAM) |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 65 | flush(f, f->buffer, the_hash_algo->rawsz); |
Derrick Stolee | cfe8321 | 2018-04-02 16:34:15 -0400 | [diff] [blame] | 66 | if (flags & CSUM_FSYNC) |
| 67 | fsync_or_die(f->fd, f->name); |
| 68 | if (flags & CSUM_CLOSE) { |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 69 | if (close(f->fd)) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 70 | die_errno("%s: sha1 file error on close", f->name); |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 71 | fd = 0; |
| 72 | } else |
| 73 | fd = f->fd; |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 74 | if (0 <= f->check_fd) { |
| 75 | char discard; |
| 76 | int cnt = read_in_full(f->check_fd, &discard, 1); |
| 77 | if (cnt < 0) |
| 78 | die_errno("%s: error when reading the tail of sha1 file", |
| 79 | f->name); |
| 80 | if (cnt) |
| 81 | die("%s: sha1 file has trailing garbage", f->name); |
| 82 | if (close(f->check_fd)) |
| 83 | die_errno("%s: sha1 file error on close", f->name); |
| 84 | } |
Sergey Vlasov | 7bf058f | 2005-08-08 22:46:13 +0400 | [diff] [blame] | 85 | free(f); |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 86 | return fd; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 87 | } |
| 88 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 89 | void hashwrite(struct hashfile *f, const void *buf, unsigned int count) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 90 | { |
| 91 | while (count) { |
| 92 | unsigned offset = f->offset; |
| 93 | unsigned left = sizeof(f->buffer) - offset; |
| 94 | unsigned nr = count > left ? left : count; |
Jeff King | e74435a | 2013-10-24 13:59:49 -0400 | [diff] [blame] | 95 | const void *data; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 96 | |
Nicolas Pitre | a8032d1 | 2008-09-02 10:22:20 -0400 | [diff] [blame] | 97 | if (f->do_crc) |
| 98 | f->crc32 = crc32(f->crc32, buf, nr); |
| 99 | |
| 100 | if (nr == sizeof(f->buffer)) { |
| 101 | /* process full buffer directly without copy */ |
| 102 | data = buf; |
| 103 | } else { |
| 104 | memcpy(f->buffer + offset, buf, nr); |
| 105 | data = f->buffer; |
| 106 | } |
| 107 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 108 | count -= nr; |
| 109 | offset += nr; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 110 | buf = (char *) buf + nr; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 111 | left -= nr; |
| 112 | if (!left) { |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 113 | the_hash_algo->update_fn(&f->ctx, data, offset); |
Shawn O. Pearce | e782e12 | 2008-10-10 08:39:20 -0700 | [diff] [blame] | 114 | flush(f, data, offset); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 115 | offset = 0; |
| 116 | } |
| 117 | f->offset = offset; |
| 118 | } |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 119 | } |
| 120 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 121 | struct hashfile *hashfd(int fd, const char *name) |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 122 | { |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 123 | return hashfd_throughput(fd, name, NULL); |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 124 | } |
| 125 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 126 | struct hashfile *hashfd_check(const char *name) |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 127 | { |
| 128 | int sink, check; |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 129 | struct hashfile *f; |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 130 | |
| 131 | sink = open("/dev/null", O_WRONLY); |
| 132 | if (sink < 0) |
Jeff King | 599d223 | 2015-03-18 02:30:12 -0400 | [diff] [blame] | 133 | die_errno("unable to open /dev/null"); |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 134 | check = open(name, O_RDONLY); |
Jeff King | 599d223 | 2015-03-18 02:30:12 -0400 | [diff] [blame] | 135 | if (check < 0) |
| 136 | die_errno("unable to open '%s'", name); |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 137 | f = hashfd(sink, name); |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 138 | f->check_fd = check; |
| 139 | return f; |
| 140 | } |
| 141 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 142 | struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp) |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 143 | { |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 144 | struct hashfile *f = xmalloc(sizeof(*f)); |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 145 | f->fd = fd; |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 146 | f->check_fd = -1; |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 147 | f->offset = 0; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 148 | f->total = 0; |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 149 | f->tp = tp; |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 150 | f->name = name; |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 151 | f->do_crc = 0; |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 152 | the_hash_algo->init_fn(&f->ctx); |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 153 | return f; |
| 154 | } |
| 155 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 156 | void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 157 | { |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 158 | hashflush(f); |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 159 | checkpoint->offset = f->total; |
| 160 | checkpoint->ctx = f->ctx; |
| 161 | } |
| 162 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 163 | int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 164 | { |
| 165 | off_t offset = checkpoint->offset; |
| 166 | |
| 167 | if (ftruncate(f->fd, offset) || |
| 168 | lseek(f->fd, offset, SEEK_SET) != offset) |
| 169 | return -1; |
| 170 | f->total = offset; |
| 171 | f->ctx = checkpoint->ctx; |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 172 | f->offset = 0; /* hashflush() was called in checkpoint */ |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 176 | void crc32_begin(struct hashfile *f) |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 177 | { |
Stephen Boyd | 1e4cd68 | 2011-04-03 00:06:54 -0700 | [diff] [blame] | 178 | f->crc32 = crc32(0, NULL, 0); |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 179 | f->do_crc = 1; |
| 180 | } |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 181 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 182 | uint32_t crc32_end(struct hashfile *f) |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 183 | { |
| 184 | f->do_crc = 0; |
| 185 | return f->crc32; |
| 186 | } |