blob: bc5bec27acbab4a3303f4ea11575b0c1accda806 [file] [log] [blame]
Linus Torvaldsc38138c2005-06-26 20:27:56 -07001#ifndef CSUM_FILE_H
2#define CSUM_FILE_H
3
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00004#include "hash-ll.h"
Elijah Newrend48be352023-03-21 06:26:07 +00005#include "write-or-die.h"
Elijah Newrenef3ca952018-08-15 10:54:05 -07006
Nicolas Pitre2a128d62007-10-30 17:06:21 -04007struct progress;
8
Linus Torvaldsc38138c2005-06-26 20:27:56 -07009/* A SHA1-protected file */
brian m. carlson98a3bea2018-02-01 02:18:46 +000010struct hashfile {
Nicolas Pitreec640ed2007-11-04 22:54:50 -050011 int fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -080012 int check_fd;
Nicolas Pitreec640ed2007-11-04 22:54:50 -050013 unsigned int offset;
brian m. carlson4d273502018-02-01 02:18:47 +000014 git_hash_ctx ctx;
Nicolas Pitre218558a2007-11-04 22:15:41 -050015 off_t total;
Nicolas Pitre2a128d62007-10-30 17:06:21 -040016 struct progress *tp;
Nicolas Pitreec640ed2007-11-04 22:54:50 -050017 const char *name;
Nicolas Pitre78d1e842007-04-09 01:06:31 -040018 int do_crc;
19 uint32_t crc32;
Derrick Stolee2ca245f2021-05-18 18:32:46 +000020 size_t buffer_len;
21 unsigned char *buffer;
22 unsigned char *check_buffer;
Derrick Stolee16871502023-01-06 16:31:53 +000023
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 Torvaldsc38138c2005-06-26 20:27:56 -070030};
31
Junio C Hamano6c526142011-11-17 16:26:54 -080032/* Checkpoint */
brian m. carlson98a3bea2018-02-01 02:18:46 +000033struct hashfile_checkpoint {
Junio C Hamano6c526142011-11-17 16:26:54 -080034 off_t offset;
brian m. carlson4d273502018-02-01 02:18:47 +000035 git_hash_ctx ctx;
Junio C Hamano6c526142011-11-17 16:26:54 -080036};
37
Denton Liu55454422019-04-29 04:28:14 -040038void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *);
39int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
Junio C Hamano6c526142011-11-17 16:26:54 -080040
Derrick Stoleef2af9f52018-04-02 16:34:14 -040041/* finalize_hashfile flags */
Derrick Stoleecfe83212018-04-02 16:34:15 -040042#define CSUM_CLOSE 1
43#define CSUM_FSYNC 2
44#define CSUM_HASH_IN_STREAM 4
Linus Torvalds4c81b032008-05-30 08:42:16 -070045
Denton Liu55454422019-04-29 04:28:14 -040046struct hashfile *hashfd(int fd, const char *name);
47struct hashfile *hashfd_check(const char *name);
48struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
Neeraj Singh020406e2022-03-10 22:43:21 +000049int finalize_hashfile(struct hashfile *, unsigned char *, enum fsync_component, unsigned int);
Denton Liu55454422019-04-29 04:28:14 -040050void hashwrite(struct hashfile *, const void *, unsigned int);
51void hashflush(struct hashfile *f);
52void crc32_begin(struct hashfile *);
53uint32_t crc32_end(struct hashfile *);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070054
Taylor Blauf9221e22021-06-23 14:39:07 -040055/* Verify checksum validity while reading. Returns non-zero on success. */
56int hashfile_checksum_valid(const unsigned char *data, size_t len);
57
Jeff King2f4af772019-12-18 12:25:42 +010058/*
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 */
62static inline off_t hashfile_total(struct hashfile *f)
63{
64 return f->total + f->offset;
65}
66
brian m. carlson98a3bea2018-02-01 02:18:46 +000067static inline void hashwrite_u8(struct hashfile *f, uint8_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050068{
brian m. carlson98a3bea2018-02-01 02:18:46 +000069 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050070}
71
brian m. carlson98a3bea2018-02-01 02:18:46 +000072static inline void hashwrite_be32(struct hashfile *f, uint32_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050073{
74 data = htonl(data);
brian m. carlson98a3bea2018-02-01 02:18:46 +000075 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050076}
77
René Scharfe54273d12020-11-12 13:20:19 +010078static 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 Torvaldsc38138c2005-06-26 20:27:56 -070085#endif