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 | ef3ca95 | 2018-08-15 10:54:05 -0700 | [diff] [blame] | 4 | #include "hash.h" |
| 5 | |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 6 | struct progress; |
| 7 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 8 | /* A SHA1-protected file */ |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 9 | struct hashfile { |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 10 | int fd; |
Junio C Hamano | e337a04 | 2011-02-02 17:29:01 -0800 | [diff] [blame] | 11 | int check_fd; |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 12 | unsigned int offset; |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 13 | git_hash_ctx ctx; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 14 | off_t total; |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 15 | struct progress *tp; |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 16 | const char *name; |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 17 | int do_crc; |
| 18 | uint32_t crc32; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 19 | unsigned char buffer[8192]; |
| 20 | }; |
| 21 | |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 22 | /* Checkpoint */ |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 23 | struct hashfile_checkpoint { |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 24 | off_t offset; |
brian m. carlson | 4d27350 | 2018-02-01 02:18:47 +0000 | [diff] [blame] | 25 | git_hash_ctx ctx; |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 26 | }; |
| 27 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 28 | void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *); |
| 29 | int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *); |
Junio C Hamano | 6c52614 | 2011-11-17 16:26:54 -0800 | [diff] [blame] | 30 | |
Derrick Stolee | f2af9f5 | 2018-04-02 16:34:14 -0400 | [diff] [blame] | 31 | /* finalize_hashfile flags */ |
Derrick Stolee | cfe8321 | 2018-04-02 16:34:15 -0400 | [diff] [blame] | 32 | #define CSUM_CLOSE 1 |
| 33 | #define CSUM_FSYNC 2 |
| 34 | #define CSUM_HASH_IN_STREAM 4 |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 35 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 36 | struct hashfile *hashfd(int fd, const char *name); |
| 37 | struct hashfile *hashfd_check(const char *name); |
| 38 | struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp); |
| 39 | int finalize_hashfile(struct hashfile *, unsigned char *, unsigned int); |
| 40 | void hashwrite(struct hashfile *, const void *, unsigned int); |
| 41 | void hashflush(struct hashfile *f); |
| 42 | void crc32_begin(struct hashfile *); |
| 43 | uint32_t crc32_end(struct hashfile *); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 44 | |
Jeff King | 2f4af77 | 2019-12-18 12:25:42 +0100 | [diff] [blame] | 45 | /* |
| 46 | * Returns the total number of bytes fed to the hashfile so far (including ones |
| 47 | * that have not been written out to the descriptor yet). |
| 48 | */ |
| 49 | static inline off_t hashfile_total(struct hashfile *f) |
| 50 | { |
| 51 | return f->total + f->offset; |
| 52 | } |
| 53 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 54 | static inline void hashwrite_u8(struct hashfile *f, uint8_t data) |
Karsten Blees | b500721 | 2014-11-27 00:24:01 -0500 | [diff] [blame] | 55 | { |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 56 | hashwrite(f, &data, sizeof(data)); |
Karsten Blees | b500721 | 2014-11-27 00:24:01 -0500 | [diff] [blame] | 57 | } |
| 58 | |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 59 | static inline void hashwrite_be32(struct hashfile *f, uint32_t data) |
Karsten Blees | b500721 | 2014-11-27 00:24:01 -0500 | [diff] [blame] | 60 | { |
| 61 | data = htonl(data); |
brian m. carlson | 98a3bea | 2018-02-01 02:18:46 +0000 | [diff] [blame] | 62 | hashwrite(f, &data, sizeof(data)); |
Karsten Blees | b500721 | 2014-11-27 00:24:01 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 65 | #endif |