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