blob: 0d29f528fbcb51df13eac225c0e2cc120198bd3f [file] [log] [blame]
Linus Torvaldsc38138c2005-06-26 20:27:56 -07001#ifndef CSUM_FILE_H
2#define CSUM_FILE_H
3
Neeraj Singh020406e2022-03-10 22:43:21 +00004#include "cache.h"
Elijah Newrenef3ca952018-08-15 10:54:05 -07005#include "hash.h"
6
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;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070023};
24
Junio C Hamano6c526142011-11-17 16:26:54 -080025/* Checkpoint */
brian m. carlson98a3bea2018-02-01 02:18:46 +000026struct hashfile_checkpoint {
Junio C Hamano6c526142011-11-17 16:26:54 -080027 off_t offset;
brian m. carlson4d273502018-02-01 02:18:47 +000028 git_hash_ctx ctx;
Junio C Hamano6c526142011-11-17 16:26:54 -080029};
30
Denton Liu55454422019-04-29 04:28:14 -040031void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *);
32int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
Junio C Hamano6c526142011-11-17 16:26:54 -080033
Derrick Stoleef2af9f52018-04-02 16:34:14 -040034/* finalize_hashfile flags */
Derrick Stoleecfe83212018-04-02 16:34:15 -040035#define CSUM_CLOSE 1
36#define CSUM_FSYNC 2
37#define CSUM_HASH_IN_STREAM 4
Linus Torvalds4c81b032008-05-30 08:42:16 -070038
Denton Liu55454422019-04-29 04:28:14 -040039struct hashfile *hashfd(int fd, const char *name);
40struct hashfile *hashfd_check(const char *name);
41struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
Neeraj Singh020406e2022-03-10 22:43:21 +000042int finalize_hashfile(struct hashfile *, unsigned char *, enum fsync_component, unsigned int);
Denton Liu55454422019-04-29 04:28:14 -040043void hashwrite(struct hashfile *, const void *, unsigned int);
44void hashflush(struct hashfile *f);
45void crc32_begin(struct hashfile *);
46uint32_t crc32_end(struct hashfile *);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070047
Taylor Blauf9221e22021-06-23 14:39:07 -040048/* Verify checksum validity while reading. Returns non-zero on success. */
49int hashfile_checksum_valid(const unsigned char *data, size_t len);
50
Jeff King2f4af772019-12-18 12:25:42 +010051/*
52 * Returns the total number of bytes fed to the hashfile so far (including ones
53 * that have not been written out to the descriptor yet).
54 */
55static inline off_t hashfile_total(struct hashfile *f)
56{
57 return f->total + f->offset;
58}
59
brian m. carlson98a3bea2018-02-01 02:18:46 +000060static inline void hashwrite_u8(struct hashfile *f, uint8_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050061{
brian m. carlson98a3bea2018-02-01 02:18:46 +000062 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050063}
64
brian m. carlson98a3bea2018-02-01 02:18:46 +000065static inline void hashwrite_be32(struct hashfile *f, uint32_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050066{
67 data = htonl(data);
brian m. carlson98a3bea2018-02-01 02:18:46 +000068 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050069}
70
René Scharfe54273d12020-11-12 13:20:19 +010071static inline size_t hashwrite_be64(struct hashfile *f, uint64_t data)
72{
73 data = htonll(data);
74 hashwrite(f, &data, sizeof(data));
75 return sizeof(data);
76}
77
Linus Torvaldsc38138c2005-06-26 20:27:56 -070078#endif