blob: f9cbd317fbbce89547a405783fb5cc69e7815efa [file] [log] [blame]
Linus Torvaldsc38138c2005-06-26 20:27:56 -07001#ifndef CSUM_FILE_H
2#define CSUM_FILE_H
3
Elijah Newrenef3ca952018-08-15 10:54:05 -07004#include "hash.h"
5
Nicolas Pitre2a128d62007-10-30 17:06:21 -04006struct progress;
7
Linus Torvaldsc38138c2005-06-26 20:27:56 -07008/* A SHA1-protected file */
brian m. carlson98a3bea2018-02-01 02:18:46 +00009struct hashfile {
Nicolas Pitreec640ed2007-11-04 22:54:50 -050010 int fd;
Junio C Hamanoe337a042011-02-02 17:29:01 -080011 int check_fd;
Nicolas Pitreec640ed2007-11-04 22:54:50 -050012 unsigned int offset;
brian m. carlson4d273502018-02-01 02:18:47 +000013 git_hash_ctx ctx;
Nicolas Pitre218558a2007-11-04 22:15:41 -050014 off_t total;
Nicolas Pitre2a128d62007-10-30 17:06:21 -040015 struct progress *tp;
Nicolas Pitreec640ed2007-11-04 22:54:50 -050016 const char *name;
Nicolas Pitre78d1e842007-04-09 01:06:31 -040017 int do_crc;
18 uint32_t crc32;
Linus Torvaldsc38138c2005-06-26 20:27:56 -070019 unsigned char buffer[8192];
20};
21
Junio C Hamano6c526142011-11-17 16:26:54 -080022/* Checkpoint */
brian m. carlson98a3bea2018-02-01 02:18:46 +000023struct hashfile_checkpoint {
Junio C Hamano6c526142011-11-17 16:26:54 -080024 off_t offset;
brian m. carlson4d273502018-02-01 02:18:47 +000025 git_hash_ctx ctx;
Junio C Hamano6c526142011-11-17 16:26:54 -080026};
27
Denton Liu55454422019-04-29 04:28:14 -040028void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *);
29int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
Junio C Hamano6c526142011-11-17 16:26:54 -080030
Derrick Stoleef2af9f52018-04-02 16:34:14 -040031/* finalize_hashfile flags */
Derrick Stoleecfe83212018-04-02 16:34:15 -040032#define CSUM_CLOSE 1
33#define CSUM_FSYNC 2
34#define CSUM_HASH_IN_STREAM 4
Linus Torvalds4c81b032008-05-30 08:42:16 -070035
Denton Liu55454422019-04-29 04:28:14 -040036struct hashfile *hashfd(int fd, const char *name);
37struct hashfile *hashfd_check(const char *name);
38struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
39int finalize_hashfile(struct hashfile *, unsigned char *, unsigned int);
40void hashwrite(struct hashfile *, const void *, unsigned int);
41void hashflush(struct hashfile *f);
42void crc32_begin(struct hashfile *);
43uint32_t crc32_end(struct hashfile *);
Linus Torvaldsc38138c2005-06-26 20:27:56 -070044
Jeff King2f4af772019-12-18 12:25:42 +010045/*
46 * Returns the total number of bytes fed to the hashfile so far (including ones
47 * that have not been written out to the descriptor yet).
48 */
49static inline off_t hashfile_total(struct hashfile *f)
50{
51 return f->total + f->offset;
52}
53
brian m. carlson98a3bea2018-02-01 02:18:46 +000054static inline void hashwrite_u8(struct hashfile *f, uint8_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050055{
brian m. carlson98a3bea2018-02-01 02:18:46 +000056 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050057}
58
brian m. carlson98a3bea2018-02-01 02:18:46 +000059static inline void hashwrite_be32(struct hashfile *f, uint32_t data)
Karsten Bleesb5007212014-11-27 00:24:01 -050060{
61 data = htonl(data);
brian m. carlson98a3bea2018-02-01 02:18:46 +000062 hashwrite(f, &data, sizeof(data));
Karsten Bleesb5007212014-11-27 00:24:01 -050063}
64
Linus Torvaldsc38138c2005-06-26 20:27:56 -070065#endif