statinfo: move stat_{data,validity} functions from cache/read-cache
[alt-git.git] / pack-mtimes.c
blob020a37f8fe3a837de959ceade8dca86849253593
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "pack-mtimes.h"
4 #include "object-file.h"
5 #include "object-store.h"
6 #include "packfile.h"
8 static char *pack_mtimes_filename(struct packed_git *p)
10 size_t len;
11 if (!strip_suffix(p->pack_name, ".pack", &len))
12 BUG("pack_name does not end in .pack");
13 return xstrfmt("%.*s.mtimes", (int)len, p->pack_name);
16 #define MTIMES_HEADER_SIZE (12)
18 struct mtimes_header {
19 uint32_t signature;
20 uint32_t version;
21 uint32_t hash_id;
24 static int load_pack_mtimes_file(char *mtimes_file,
25 uint32_t num_objects,
26 const uint32_t **data_p, size_t *len_p)
28 int fd, ret = 0;
29 struct stat st;
30 uint32_t *data = NULL;
31 size_t mtimes_size, expected_size;
32 struct mtimes_header header;
34 fd = git_open(mtimes_file);
36 if (fd < 0) {
37 ret = -1;
38 goto cleanup;
40 if (fstat(fd, &st)) {
41 ret = error_errno(_("failed to read %s"), mtimes_file);
42 goto cleanup;
45 mtimes_size = xsize_t(st.st_size);
47 if (mtimes_size < MTIMES_HEADER_SIZE) {
48 ret = error(_("mtimes file %s is too small"), mtimes_file);
49 goto cleanup;
52 data = xmmap(NULL, mtimes_size, PROT_READ, MAP_PRIVATE, fd, 0);
54 header.signature = ntohl(data[0]);
55 header.version = ntohl(data[1]);
56 header.hash_id = ntohl(data[2]);
58 if (header.signature != MTIMES_SIGNATURE) {
59 ret = error(_("mtimes file %s has unknown signature"), mtimes_file);
60 goto cleanup;
63 if (header.version != 1) {
64 ret = error(_("mtimes file %s has unsupported version %"PRIu32),
65 mtimes_file, header.version);
66 goto cleanup;
69 if (!(header.hash_id == 1 || header.hash_id == 2)) {
70 ret = error(_("mtimes file %s has unsupported hash id %"PRIu32),
71 mtimes_file, header.hash_id);
72 goto cleanup;
76 expected_size = MTIMES_HEADER_SIZE;
77 expected_size = st_add(expected_size, st_mult(sizeof(uint32_t), num_objects));
78 expected_size = st_add(expected_size, 2 * (header.hash_id == 1 ? GIT_SHA1_RAWSZ : GIT_SHA256_RAWSZ));
80 if (mtimes_size != expected_size) {
81 ret = error(_("mtimes file %s is corrupt"), mtimes_file);
82 goto cleanup;
85 cleanup:
86 if (ret) {
87 if (data)
88 munmap(data, mtimes_size);
89 } else {
90 *len_p = mtimes_size;
91 *data_p = data;
94 if (fd >= 0)
95 close(fd);
96 return ret;
99 int load_pack_mtimes(struct packed_git *p)
101 char *mtimes_name = NULL;
102 int ret = 0;
104 if (!p->is_cruft)
105 return ret; /* not a cruft pack */
106 if (p->mtimes_map)
107 return ret; /* already loaded */
109 ret = open_pack_index(p);
110 if (ret < 0)
111 goto cleanup;
113 mtimes_name = pack_mtimes_filename(p);
114 ret = load_pack_mtimes_file(mtimes_name,
115 p->num_objects,
116 &p->mtimes_map,
117 &p->mtimes_size);
118 cleanup:
119 free(mtimes_name);
120 return ret;
123 uint32_t nth_packed_mtime(struct packed_git *p, uint32_t pos)
125 if (!p->mtimes_map)
126 BUG("pack .mtimes file not loaded for %s", p->pack_name);
127 if (p->num_objects <= pos)
128 BUG("pack .mtimes out-of-bounds (%"PRIu32" vs %"PRIu32")",
129 pos, p->num_objects);
131 return get_be32(p->mtimes_map + pos + 3);