]> git.ipfire.org Git - thirdparty/git.git/blob - csum-file.h
reftable: add blocksource, an abstraction for random access reads
[thirdparty/git.git] / csum-file.h
1 #ifndef CSUM_FILE_H
2 #define CSUM_FILE_H
3
4 #include "hash.h"
5
6 struct progress;
7
8 /* A SHA1-protected file */
9 struct hashfile {
10 int fd;
11 int check_fd;
12 unsigned int offset;
13 git_hash_ctx ctx;
14 off_t total;
15 struct progress *tp;
16 const char *name;
17 int do_crc;
18 uint32_t crc32;
19 size_t buffer_len;
20 unsigned char *buffer;
21 unsigned char *check_buffer;
22 };
23
24 /* Checkpoint */
25 struct hashfile_checkpoint {
26 off_t offset;
27 git_hash_ctx ctx;
28 };
29
30 void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *);
31 int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
32
33 /* finalize_hashfile flags */
34 #define CSUM_CLOSE 1
35 #define CSUM_FSYNC 2
36 #define CSUM_HASH_IN_STREAM 4
37
38 struct hashfile *hashfd(int fd, const char *name);
39 struct hashfile *hashfd_check(const char *name);
40 struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
41 int finalize_hashfile(struct hashfile *, unsigned char *, unsigned int);
42 void hashwrite(struct hashfile *, const void *, unsigned int);
43 void hashflush(struct hashfile *f);
44 void crc32_begin(struct hashfile *);
45 uint32_t crc32_end(struct hashfile *);
46
47 /* Verify checksum validity while reading. Returns non-zero on success. */
48 int hashfile_checksum_valid(const unsigned char *data, size_t len);
49
50 /*
51 * Returns the total number of bytes fed to the hashfile so far (including ones
52 * that have not been written out to the descriptor yet).
53 */
54 static inline off_t hashfile_total(struct hashfile *f)
55 {
56 return f->total + f->offset;
57 }
58
59 static inline void hashwrite_u8(struct hashfile *f, uint8_t data)
60 {
61 hashwrite(f, &data, sizeof(data));
62 }
63
64 static inline void hashwrite_be32(struct hashfile *f, uint32_t data)
65 {
66 data = htonl(data);
67 hashwrite(f, &data, sizeof(data));
68 }
69
70 static inline size_t hashwrite_be64(struct hashfile *f, uint64_t data)
71 {
72 data = htonll(data);
73 hashwrite(f, &data, sizeof(data));
74 return sizeof(data);
75 }
76
77 #endif