| 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 | { |