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