| 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 | |
| 11 | #include "git-compat-util.h" |
| 12 | #include "csum-file.h" |
| 13 | #include "git-zlib.h" |
| 14 | #include "hash.h" |
| 15 | #include "progress.h" |
| 16 | |
| 17 | static void verify_buffer_or_die(struct hashfile *f, |
| 18 | const void *buf, |
| 19 | unsigned int count) |
| 20 | { |
| 21 | ssize_t ret = read_in_full(f->check_fd, f->check_buffer, count); |
| 22 | |
| 23 | if (ret < 0) |
| 24 | die_errno("%s: sha1 file read error", f->name); |
| 25 | if ((size_t)ret != count) |
| 26 | die("%s: sha1 file truncated", f->name); |
| 27 | if (memcmp(buf, f->check_buffer, count)) |
| 28 | die("sha1 file '%s' validation error", f->name); |
| 29 | } |
| 30 | |
| 31 | static void flush(struct hashfile *f, const void *buf, unsigned int count) |
| 32 | { |
| 33 | if (0 <= f->check_fd && count) |
| 34 | verify_buffer_or_die(f, buf, count); |
| 35 | |
| 36 | if (write_in_full(f->fd, buf, count) < 0) { |
| 37 | if (errno == ENOSPC) |
| 38 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
| 39 | die_errno("sha1 file '%s' write error", f->name); |
| 40 | } |
| 41 | |
| 42 | f->total += count; |
| 43 | display_throughput(f->tp, f->total); |
| 44 | } |
| 45 | |
| 46 | void hashflush(struct hashfile *f) |
| 47 | { |
| 48 | unsigned offset = f->offset; |
| 49 | |
| 50 | if (offset) { |
| 51 | if (!f->skip_hash) |
| 52 | git_hash_update(&f->ctx, f->buffer, offset); |
| 53 | flush(f, f->buffer, offset); |
| 54 | f->offset = 0; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void free_hashfile(struct hashfile *f) |
| 59 | { |
| 60 | free(f->buffer); |
| 61 | free(f->check_buffer); |
| 62 | free(f); |
| 63 | } |
| 64 | |
| 65 | int finalize_hashfile(struct hashfile *f, unsigned char *result, |
| 66 | enum fsync_component component, unsigned int flags) |
| 67 | { |
| 68 | int fd; |
| 69 | |
| 70 | hashflush(f); |
| 71 | |
| 72 | if (f->skip_hash) |
| 73 | hashclr(f->buffer, f->algop); |
| 74 | else |
| 75 | git_hash_final(f->buffer, &f->ctx); |
| 76 | |
| 77 | if (result) |
| 78 | hashcpy(result, f->buffer, f->algop); |
| 79 | if (flags & CSUM_HASH_IN_STREAM) |
| 80 | flush(f, f->buffer, f->algop->rawsz); |
| 81 | if (flags & CSUM_FSYNC) |
| 82 | fsync_component_or_die(component, f->fd, f->name); |
| 83 | if (flags & CSUM_CLOSE) { |
| 84 | if (close(f->fd)) |
| 85 | die_errno("%s: sha1 file error on close", f->name); |
| 86 | fd = 0; |
| 87 | } else |
| 88 | fd = f->fd; |
| 89 | if (0 <= f->check_fd) { |
| 90 | char discard; |
| 91 | int cnt = read_in_full(f->check_fd, &discard, 1); |
| 92 | if (cnt < 0) |
| 93 | die_errno("%s: error when reading the tail of sha1 file", |
| 94 | f->name); |
| 95 | if (cnt) |
| 96 | die("%s: sha1 file has trailing garbage", f->name); |
| 97 | if (close(f->check_fd)) |
| 98 | die_errno("%s: sha1 file error on close", f->name); |
| 99 | } |
| 100 | free_hashfile(f); |
| 101 | return fd; |
| 102 | } |
| 103 | |
| 104 | void discard_hashfile(struct hashfile *f) |
| 105 | { |
| 106 | if (0 <= f->check_fd) |
| 107 | close(f->check_fd); |
| 108 | if (0 <= f->fd) |
| 109 | close(f->fd); |
| 110 | free_hashfile(f); |
| 111 | } |
| 112 | |
| 113 | void hashwrite(struct hashfile *f, const void *buf, uint32_t count) |
| 114 | { |
| 115 | while (count) { |
| 116 | unsigned left = f->buffer_len - f->offset; |
| 117 | unsigned nr = count > left ? left : count; |
| 118 | |
| 119 | if (f->do_crc) |
| 120 | f->crc32 = crc32(f->crc32, buf, nr); |
| 121 | |
| 122 | if (nr == f->buffer_len) { |
| 123 | /* |
| 124 | * Flush a full batch worth of data directly |
| 125 | * from the input, skipping the memcpy() to |
| 126 | * the hashfile's buffer. In this block, |
| 127 | * f->offset is necessarily zero. |
| 128 | */ |
| 129 | if (!f->skip_hash) |
| 130 | git_hash_update(&f->ctx, buf, nr); |
| 131 | flush(f, buf, nr); |
| 132 | } else { |
| 133 | /* |
| 134 | * Copy to the hashfile's buffer, flushing only |
| 135 | * if it became full. |
| 136 | */ |
| 137 | memcpy(f->buffer + f->offset, buf, nr); |
| 138 | f->offset += nr; |
| 139 | left -= nr; |
| 140 | if (!left) |
| 141 | hashflush(f); |
| 142 | } |
| 143 | |
| 144 | count -= nr; |
| 145 | buf = (char *) buf + nr; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | struct hashfile *hashfd_check(const struct git_hash_algo *algop, |
| 150 | const char *name) |
| 151 | { |
| 152 | int sink, check; |
| 153 | struct hashfile *f; |
| 154 | |
| 155 | sink = xopen("/dev/null", O_WRONLY); |
| 156 | check = xopen(name, O_RDONLY); |
| 157 | f = hashfd(algop, sink, name); |
| 158 | f->check_fd = check; |
| 159 | f->check_buffer = xmalloc(f->buffer_len); |
| 160 | |
| 161 | return f; |
| 162 | } |
| 163 | |
| 164 | struct hashfile *hashfd_ext(const struct git_hash_algo *algop, |
| 165 | int fd, const char *name, |
| 166 | const struct hashfd_options *opts) |
| 167 | { |
| 168 | struct hashfile *f = xmalloc(sizeof(*f)); |
| 169 | f->fd = fd; |
| 170 | f->check_fd = -1; |
| 171 | f->offset = 0; |
| 172 | f->total = 0; |
| 173 | f->tp = opts->progress; |
| 174 | f->name = name; |
| 175 | f->do_crc = 0; |
| 176 | f->skip_hash = 0; |
| 177 | |
| 178 | f->algop = unsafe_hash_algo(algop); |
| 179 | f->algop->init_fn(&f->ctx); |
| 180 | |
| 181 | f->buffer_len = opts->buffer_len ? opts->buffer_len : DEFAULT_IO_BUFFER_SIZE; |
| 182 | f->buffer = xmalloc(f->buffer_len); |
| 183 | f->check_buffer = NULL; |
| 184 | |
| 185 | return f; |
| 186 | } |
| 187 | |
| 188 | struct hashfile *hashfd(const struct git_hash_algo *algop, |
| 189 | int fd, const char *name) |
| 190 | { |
| 191 | /* |
| 192 | * Since we are not going to use a progress meter to |
| 193 | * measure the rate of data passing through this hashfile, |
| 194 | * use a larger buffer size to reduce fsync() calls. |
| 195 | */ |
| 196 | struct hashfd_options opts = { 0 }; |
| 197 | return hashfd_ext(algop, fd, name, &opts); |
| 198 | } |
| 199 | |
| 200 | void hashfile_checkpoint_init(struct hashfile *f, |
| 201 | struct hashfile_checkpoint *checkpoint) |
| 202 | { |
| 203 | memset(checkpoint, 0, sizeof(*checkpoint)); |
| 204 | f->algop->init_fn(&checkpoint->ctx); |
| 205 | } |
| 206 | |
| 207 | void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
| 208 | { |
| 209 | hashflush(f); |
| 210 | checkpoint->offset = f->total; |
| 211 | git_hash_clone(&checkpoint->ctx, &f->ctx); |
| 212 | } |
| 213 | |
| 214 | int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) |
| 215 | { |
| 216 | off_t offset = checkpoint->offset; |
| 217 | |
| 218 | if (ftruncate(f->fd, offset) || |
| 219 | lseek(f->fd, offset, SEEK_SET) != offset) |
| 220 | return -1; |
| 221 | f->total = offset; |
| 222 | git_hash_clone(&f->ctx, &checkpoint->ctx); |
| 223 | f->offset = 0; /* hashflush() was called in checkpoint */ |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | void crc32_begin(struct hashfile *f) |
| 228 | { |
| 229 | f->crc32 = crc32(0, NULL, 0); |
| 230 | f->do_crc = 1; |
| 231 | } |
| 232 | |
| 233 | uint32_t crc32_end(struct hashfile *f) |
| 234 | { |
| 235 | f->do_crc = 0; |
| 236 | return f->crc32; |
| 237 | } |
| 238 | |
| 239 | int hashfile_checksum_valid(const struct git_hash_algo *algop, |
| 240 | const unsigned char *data, size_t total_len) |
| 241 | { |
| 242 | unsigned char got[GIT_MAX_RAWSZ]; |
| 243 | struct git_hash_ctx ctx; |
| 244 | size_t data_len = total_len - algop->rawsz; |
| 245 | |
| 246 | algop = unsafe_hash_algo(algop); |
| 247 | |
| 248 | if (total_len < algop->rawsz) |
| 249 | return 0; /* say "too short"? */ |
| 250 | |
| 251 | algop->init_fn(&ctx); |
| 252 | git_hash_update(&ctx, data, data_len); |
| 253 | git_hash_final(got, &ctx); |
| 254 | |
| 255 | return hasheq(got, data + data_len, algop); |
| 256 | } |