blob: c5a2e335e7e063528da8386cc95fba4f7bb5bfe8 [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
Derrick Stoleef2af9f52018-04-02 16:34:14 -040029/* finalize_hashfile flags */
Derrick Stoleecfe83212018-04-02 16:34:15 -040030#define CSUM_CLOSE 1
31#define CSUM_FSYNC 2
32#define CSUM_HASH_IN_STREAM 4
Linus Torvalds4c81b032008-05-30 08:42:16 -070033
brian m. carlson98a3bea2018-02-01 02:18:46 +000034extern struct hashfile *hashfd(int fd, const char *name);
35extern struct hashfile *hashfd_check(const char *name);
36extern struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
Derrick Stoleef2af9f52018-04-02 16:34:14 -040037extern int finalize_hashfile(struct hashfile *, unsigned char *, unsigned int);
brian m. carlson98a3bea2018-02-01 02:18:46 +000038extern void hashwrite(struct hashfile *, const void *, unsigned int);
39extern void hashflush(struct hashfile *f);
40extern void crc32_begin(struct hashfile *);
41extern uint32_t crc32_end(struct hashfile *);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070042
brian m. carlson98a3bea2018-02-01 02:18:46 +000043static inline void hashwrite_u8(struct hashfile *f, uint8_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050044{
brian m. carlson98a3bea2018-02-01 02:18:46 +000045 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050046}
47
brian m. carlson98a3bea2018-02-01 02:18:46 +000048static inline void hashwrite_be32(struct hashfile *f, uint32_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050049{
50 data = htonl(data);
brian m. carlson98a3bea2018-02-01 02:18:46 +000051 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050052}
53
Linus Torvaldsc38138c2005-06-26 20:27:56 -070054#endif