Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 1 | #ifndef CSUM_FILE_H |
| 2 | #define CSUM_FILE_H |
| 3 | |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 4 | #include "hash-ll.h" |
Elijah Newren | d48be35 | 2023-03-21 06:26:07 +0000 | [diff] [blame] | 5 | #include "write-or-die.h" |
Elijah Newren | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 6 | |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 7 | struct progress; |
| 8 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 9 | /* A SHA1-protected file */ |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 10 | struct hashfile { |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 11 | int fd; |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 12 | int check_fd; |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 13 | unsigned int offset; |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 14 | git_hash_ctx ctx; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 15 | off_t total; |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 16 | struct progress *tp; |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 17 | const char *name; |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 18 | int do_crc; |
| 19 | uint32_t crc32; |
Derrick Stolee | 2ca245f | 2021-05-18 18:32:46 +0000 | [diff] [blame] | 20 | size_t buffer_len; |
| 21 | unsigned char *buffer; |
| 22 | unsigned char *check_buffer; |
Derrick Stolee | 1687150 | 2023-01-06 16:31:53 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * If non-zero, skip_hash indicates that we should |
| 26 | * not actually compute the hash for this hashfile and |
| 27 | * instead only use it as a buffered write. |
| 28 | */ |
| 29 | int skip_hash; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 32 | /* Checkpoint */ |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 33 | struct hashfile_checkpoint { |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 34 | off_t offset; |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 35 | git_hash_ctx ctx; |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 36 | }; |
| 37 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 38 | void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *); |
| 39 | int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *); |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 40 | |
Derrick Stolee | f2af9f5 | 2018-04-02 16:34:14 -0400 | [diff] [blame] | 41 | /* finalize_hashfile flags */ |
Derrick Stolee | cfe8321 | 2018-04-02 16:34:15 -0400 | [diff] [blame] | 42 | #define CSUM_CLOSE 1 |
| 43 | #define CSUM_FSYNC 2 |
| 44 | #define CSUM_HASH_IN_STREAM 4 |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 45 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 46 | struct hashfile *hashfd(int fd, const char *name); |
| 47 | struct hashfile *hashfd_check(const char *name); |
| 48 | struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp); |
Neeraj Singh | 020406e | 2022-03-10 22:43:21 +0000 | [diff] [blame] | 49 | int finalize_hashfile(struct hashfile *, unsigned char *, enum fsync_component, unsigned int); |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 50 | void hashwrite(struct hashfile *, const void *, unsigned int); |
| 51 | void hashflush(struct hashfile *f); |
| 52 | void crc32_begin(struct hashfile *); |
| 53 | uint32_t crc32_end(struct hashfile *); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 54 | |
Taylor Blau | f9221e2 | 2021-06-23 14:39:07 -0400 | [diff] [blame] | 55 | /* Verify checksum validity while reading. Returns non-zero on success. */ |
| 56 | int hashfile_checksum_valid(const unsigned char *data, size_t len); |
| 57 | |
Jeff King | 2f4af77 | 2019-12-18 12:25:42 +0100 | [diff] [blame] | 58 | /* |
| 59 | * Returns the total number of bytes fed to the hashfile so far (including ones |
| 60 | * that have not been written out to the descriptor yet). |
| 61 | */ |
| 62 | static inline off_t hashfile_total(struct hashfile *f) |
| 63 | { |
| 64 | return f->total + f->offset; |
| 65 | } |
| 66 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 67 | static inline void hashwrite_u8(struct hashfile *f, uint8_t data) |
Karsten Blees | b500721 | 2014-11-27 00:24:01 -0500 | [diff] [blame] | 68 | { |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 69 | hashwrite(f, &data, sizeof(data)); |
Karsten Blees | b500721 | 2014-11-27 00:24:01 -0500 | [diff] [blame] | 70 | } |
| 71 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 72 | static inline void hashwrite_be32(struct hashfile *f, uint32_t data) |
Karsten Blees | b500721 | 2014-11-27 00:24:01 -0500 | [diff] [blame] | 73 | { |
| 74 | data = htonl(data); |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 75 | hashwrite(f, &data, sizeof(data)); |
Karsten Blees | b500721 | 2014-11-27 00:24:01 -0500 | [diff] [blame] | 76 | } |
| 77 | |
René Scharfe | 54273d1 | 2020-11-12 13:20:19 +0100 | [diff] [blame] | 78 | static inline size_t hashwrite_be64(struct hashfile *f, uint64_t data) |
| 79 | { |
| 80 | data = htonll(data); |
| 81 | hashwrite(f, &data, sizeof(data)); |
| 82 | return sizeof(data); |
| 83 | } |
| 84 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 85 | #endif |