csum-file.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef CSUM_FILE_H
  2. #define CSUM_FILE_H
  3. #include "hash.h"
  4. struct progress;
  5. /* A SHA1-protected file */
  6. struct hashfile {
  7. int fd;
  8. int check_fd;
  9. unsigned int offset;
  10. git_hash_ctx ctx;
  11. off_t total;
  12. struct progress *tp;
  13. const char *name;
  14. int do_crc;
  15. uint32_t crc32;
  16. unsigned char buffer[8192];
  17. };
  18. /* Checkpoint */
  19. struct hashfile_checkpoint {
  20. off_t offset;
  21. git_hash_ctx ctx;
  22. };
  23. void hashfile_checkpoint(struct hashfile *, struct hashfile_checkpoint *);
  24. int hashfile_truncate(struct hashfile *, struct hashfile_checkpoint *);
  25. /* finalize_hashfile flags */
  26. #define CSUM_CLOSE 1
  27. #define CSUM_FSYNC 2
  28. #define CSUM_HASH_IN_STREAM 4
  29. struct hashfile *hashfd(int fd, const char *name);
  30. struct hashfile *hashfd_check(const char *name);
  31. struct hashfile *hashfd_throughput(int fd, const char *name, struct progress *tp);
  32. int finalize_hashfile(struct hashfile *, unsigned char *, unsigned int);
  33. void hashwrite(struct hashfile *, const void *, unsigned int);
  34. void hashflush(struct hashfile *f);
  35. void crc32_begin(struct hashfile *);
  36. uint32_t crc32_end(struct hashfile *);
  37. /*
  38. * Returns the total number of bytes fed to the hashfile so far (including ones
  39. * that have not been written out to the descriptor yet).
  40. */
  41. static inline off_t hashfile_total(struct hashfile *f)
  42. {
  43. return f->total + f->offset;
  44. }
  45. static inline void hashwrite_u8(struct hashfile *f, uint8_t data)
  46. {
  47. hashwrite(f, &data, sizeof(data));
  48. }
  49. static inline void hashwrite_be32(struct hashfile *f, uint32_t data)
  50. {
  51. data = htonl(data);
  52. hashwrite(f, &data, sizeof(data));
  53. }
  54. #endif