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