blob: 992e5c014122d8fed3ee782d400e61de78e55271 [file] [log] [blame]
Linus Torvaldsc38138c2005-06-26 20:27:56 -07001#ifndef CSUM_FILE_H
2#define CSUM_FILE_H
3
Nicolas Pitre2a128d62007-10-30 17:06:21 -04004struct progress;
5
Linus Torvaldsc38138c2005-06-26 20:27:56 -07006/* A SHA1-protected file */
brian m. carlson98a3bea2018-02-01 02:18:46 +00007struct hashfile {
Nicolas Pitreec640ed2007-11-04 22:54:50 -05008 int fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -08009 int check_fd;
Nicolas Pitreec640ed2007-11-04 22:54:50 -050010 unsigned int offset;
brian m. carlson4d273502018-02-01 02:18:47 +000011 git_hash_ctx ctx;
Nicolas Pitre218558a2007-11-04 22:15:41 -050012 off_t total;
Nicolas Pitre2a128d62007-10-30 17:06:21 -040013 struct progress *tp;
Nicolas Pitreec640ed2007-11-04 22:54:50 -050014 const char *name;
Nicolas Pitre78d1e842007-04-09 01:06:31 -040015 int do_crc;
16 uint32_t crc32;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070017 unsigned char buffer[8192];
18};
19
Junio C Hamano6c526142011-11-17 16:26:54 -080020/* Checkpoint */
brian m. carlson98a3bea2018-02-01 02:18:46 +000021struct hashfile_checkpoint {
Junio C Hamano6c526142011-11-17 16:26:54 -080022 off_t offset;
brian m. carlson4d273502018-02-01 02:18:47 +000023 git_hash_ctx ctx;
Junio C Hamano6c526142011-11-17 16:26:54 -080024};
25
brian m. carlson98a3bea2018-02-01 02:18:46 +000026extern void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *);
27extern int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
Junio C Hamano6c526142011-11-17 16:26:54 -080028
brian m. carlson98a3bea2018-02-01 02:18:46 +000029/* hashclose flags */
Linus Torvalds4c81b032008-05-30 08:42:16 -070030#define CSUM_CLOSE 1
31#define CSUM_FSYNC 2
32
brian m. carlson98a3bea2018-02-01 02:18:46 +000033extern struct hashfile *hashfd(int fd, const char *name);
34extern struct hashfile *hashfd_check(const char *name);
35extern struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
36extern int hashclose(struct hashfile *, unsigned char *, unsigned int);
37extern void hashwrite(struct hashfile *, const void *, unsigned int);
38extern void hashflush(struct hashfile *f);
39extern void crc32_begin(struct hashfile *);
40extern uint32_t crc32_end(struct hashfile *);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070041
brian m. carlson98a3bea2018-02-01 02:18:46 +000042static inline void hashwrite_u8(struct hashfile *f, uint8_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050043{
brian m. carlson98a3bea2018-02-01 02:18:46 +000044 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050045}
46
brian m. carlson98a3bea2018-02-01 02:18:46 +000047static inline void hashwrite_be32(struct hashfile *f, uint32_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050048{
49 data = htonl(data);
brian m. carlson98a3bea2018-02-01 02:18:46 +000050 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050051}
52
Linus Torvaldsc38138c2005-06-26 20:27:56 -070053#endif