]>
Commit | Line | Data |
---|---|---|
3449f8c4 NP |
1 | #include "cache.h" |
2 | #include "pack-revindex.h" | |
a80d72db | 3 | #include "object-store.h" |
4828ce98 | 4 | #include "packfile.h" |
ec8e7760 | 5 | #include "config.h" |
3449f8c4 | 6 | |
d5bc7c60 TB |
7 | struct revindex_entry { |
8 | off_t offset; | |
9 | unsigned int nr; | |
10 | }; | |
11 | ||
3449f8c4 NP |
12 | /* |
13 | * Pack index for existing packs give us easy access to the offsets into | |
14 | * corresponding pack file where each object's data starts, but the entries | |
15 | * do not store the size of the compressed representation (uncompressed | |
16 | * size is easily available by examining the pack entry header). It is | |
17 | * also rather expensive to find the sha1 for an object given its offset. | |
18 | * | |
f4015337 JK |
19 | * The pack index file is sorted by object name mapping to offset; |
20 | * this revindex array is a list of offset/index_nr pairs | |
3449f8c4 NP |
21 | * ordered by offset, so if you know the offset of an object, next offset |
22 | * is where its packed representation ends and the index_nr can be used to | |
23 | * get the object sha1 from the main index. | |
24 | */ | |
25 | ||
8b8dfd51 JK |
26 | /* |
27 | * This is a least-significant-digit radix sort. | |
28 | * | |
29 | * It sorts each of the "n" items in "entries" by its offset field. The "max" | |
30 | * parameter must be at least as large as the largest offset in the array, | |
31 | * and lets us quit the sort early. | |
32 | */ | |
33 | static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max) | |
3449f8c4 | 34 | { |
8b8dfd51 JK |
35 | /* |
36 | * We use a "digit" size of 16 bits. That keeps our memory | |
37 | * usage reasonable, and we can generally (for a 4G or smaller | |
38 | * packfile) quit after two rounds of radix-sorting. | |
39 | */ | |
40 | #define DIGIT_SIZE (16) | |
41 | #define BUCKETS (1 << DIGIT_SIZE) | |
42 | /* | |
43 | * We want to know the bucket that a[i] will go into when we are using | |
44 | * the digit that is N bits from the (least significant) end. | |
45 | */ | |
46 | #define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1)) | |
47 | ||
48 | /* | |
49 | * We need O(n) temporary storage. Rather than do an extra copy of the | |
50 | * partial results into "entries", we sort back and forth between the | |
51 | * real array and temporary storage. In each iteration of the loop, we | |
52 | * keep track of them with alias pointers, always sorting from "from" | |
53 | * to "to". | |
54 | */ | |
b32fa95f | 55 | struct revindex_entry *tmp, *from, *to; |
8b8dfd51 | 56 | int bits; |
b32fa95f JK |
57 | unsigned *pos; |
58 | ||
59 | ALLOC_ARRAY(pos, BUCKETS); | |
60 | ALLOC_ARRAY(tmp, n); | |
61 | from = entries; | |
62 | to = tmp; | |
8b8dfd51 JK |
63 | |
64 | /* | |
65 | * If (max >> bits) is zero, then we know that the radix digit we are | |
66 | * on (and any higher) will be zero for all entries, and our loop will | |
67 | * be a no-op, as everybody lands in the same zero-th bucket. | |
68 | */ | |
69 | for (bits = 0; max >> bits; bits += DIGIT_SIZE) { | |
8b8dfd51 JK |
70 | unsigned i; |
71 | ||
72 | memset(pos, 0, BUCKETS * sizeof(*pos)); | |
73 | ||
74 | /* | |
75 | * We want pos[i] to store the index of the last element that | |
76 | * will go in bucket "i" (actually one past the last element). | |
77 | * To do this, we first count the items that will go in each | |
78 | * bucket, which gives us a relative offset from the last | |
79 | * bucket. We can then cumulatively add the index from the | |
80 | * previous bucket to get the true index. | |
81 | */ | |
82 | for (i = 0; i < n; i++) | |
83 | pos[BUCKET_FOR(from, i, bits)]++; | |
84 | for (i = 1; i < BUCKETS; i++) | |
85 | pos[i] += pos[i-1]; | |
86 | ||
87 | /* | |
88 | * Now we can drop the elements into their correct buckets (in | |
89 | * our temporary array). We iterate the pos counter backwards | |
90 | * to avoid using an extra index to count up. And since we are | |
91 | * going backwards there, we must also go backwards through the | |
92 | * array itself, to keep the sort stable. | |
93 | * | |
94 | * Note that we use an unsigned iterator to make sure we can | |
95 | * handle 2^32-1 objects, even on a 32-bit system. But this | |
96 | * means we cannot use the more obvious "i >= 0" loop condition | |
97 | * for counting backwards, and must instead check for | |
98 | * wrap-around with UINT_MAX. | |
99 | */ | |
100 | for (i = n - 1; i != UINT_MAX; i--) | |
101 | to[--pos[BUCKET_FOR(from, i, bits)]] = from[i]; | |
102 | ||
103 | /* | |
104 | * Now "to" contains the most sorted list, so we swap "from" and | |
105 | * "to" for the next iteration. | |
106 | */ | |
35d803bc | 107 | SWAP(from, to); |
8b8dfd51 JK |
108 | } |
109 | ||
110 | /* | |
111 | * If we ended with our data in the original array, great. If not, | |
112 | * we have to move it back from the temporary storage. | |
113 | */ | |
114 | if (from != entries) | |
45ccef87 | 115 | COPY_ARRAY(entries, tmp, n); |
8b8dfd51 JK |
116 | free(tmp); |
117 | free(pos); | |
118 | ||
119 | #undef BUCKET_FOR | |
120 | #undef BUCKETS | |
121 | #undef DIGIT_SIZE | |
3449f8c4 NP |
122 | } |
123 | ||
124 | /* | |
125 | * Ordered list of offsets of objects in the pack. | |
126 | */ | |
9d98bbf5 | 127 | static void create_pack_revindex(struct packed_git *p) |
3449f8c4 | 128 | { |
33de80b1 | 129 | const unsigned num_ent = p->num_objects; |
012b32bb | 130 | unsigned i; |
3449f8c4 | 131 | const char *index = p->index_data; |
fa130802 | 132 | const unsigned hashsz = the_hash_algo->rawsz; |
3449f8c4 | 133 | |
11529ece | 134 | ALLOC_ARRAY(p->revindex, num_ent + 1); |
3449f8c4 NP |
135 | index += 4 * 256; |
136 | ||
137 | if (p->index_version > 1) { | |
138 | const uint32_t *off_32 = | |
f86f7695 | 139 | (uint32_t *)(index + 8 + (size_t)p->num_objects * (hashsz + 4)); |
3449f8c4 NP |
140 | const uint32_t *off_64 = off_32 + p->num_objects; |
141 | for (i = 0; i < num_ent; i++) { | |
33de80b1 | 142 | const uint32_t off = ntohl(*off_32++); |
3449f8c4 | 143 | if (!(off & 0x80000000)) { |
9d98bbf5 | 144 | p->revindex[i].offset = off; |
3449f8c4 | 145 | } else { |
ad622a25 DS |
146 | p->revindex[i].offset = get_be64(off_64); |
147 | off_64 += 2; | |
3449f8c4 | 148 | } |
9d98bbf5 | 149 | p->revindex[i].nr = i; |
3449f8c4 NP |
150 | } |
151 | } else { | |
152 | for (i = 0; i < num_ent; i++) { | |
33de80b1 | 153 | const uint32_t hl = *((uint32_t *)(index + (hashsz + 4) * i)); |
9d98bbf5 JK |
154 | p->revindex[i].offset = ntohl(hl); |
155 | p->revindex[i].nr = i; | |
3449f8c4 NP |
156 | } |
157 | } | |
158 | ||
fa130802 | 159 | /* |
160 | * This knows the pack format -- the hash trailer | |
3449f8c4 NP |
161 | * follows immediately after the last object data. |
162 | */ | |
fa130802 | 163 | p->revindex[num_ent].offset = p->pack_size - hashsz; |
9d98bbf5 JK |
164 | p->revindex[num_ent].nr = -1; |
165 | sort_revindex(p->revindex, num_ent, p->pack_size); | |
3449f8c4 NP |
166 | } |
167 | ||
2f4ba2a8 | 168 | static int create_pack_revindex_in_memory(struct packed_git *p) |
3449f8c4 | 169 | { |
ec8e7760 TB |
170 | if (git_env_bool(GIT_TEST_REV_INDEX_DIE_IN_MEMORY, 0)) |
171 | die("dying as requested by '%s'", | |
172 | GIT_TEST_REV_INDEX_DIE_IN_MEMORY); | |
2f4ba2a8 TB |
173 | if (open_pack_index(p)) |
174 | return -1; | |
175 | create_pack_revindex(p); | |
4828ce98 | 176 | return 0; |
92e5c77c VM |
177 | } |
178 | ||
2f4ba2a8 TB |
179 | static char *pack_revindex_filename(struct packed_git *p) |
180 | { | |
181 | size_t len; | |
182 | if (!strip_suffix(p->pack_name, ".pack", &len)) | |
183 | BUG("pack_name does not end in .pack"); | |
184 | return xstrfmt("%.*s.rev", (int)len, p->pack_name); | |
185 | } | |
186 | ||
187 | #define RIDX_HEADER_SIZE (12) | |
188 | #define RIDX_MIN_SIZE (RIDX_HEADER_SIZE + (2 * the_hash_algo->rawsz)) | |
189 | ||
190 | struct revindex_header { | |
191 | uint32_t signature; | |
192 | uint32_t version; | |
193 | uint32_t hash_id; | |
194 | }; | |
195 | ||
196 | static int load_revindex_from_disk(char *revindex_name, | |
197 | uint32_t num_objects, | |
198 | const uint32_t **data_p, size_t *len_p) | |
199 | { | |
200 | int fd, ret = 0; | |
201 | struct stat st; | |
202 | void *data = NULL; | |
203 | size_t revindex_size; | |
204 | struct revindex_header *hdr; | |
205 | ||
206 | fd = git_open(revindex_name); | |
207 | ||
208 | if (fd < 0) { | |
209 | ret = -1; | |
210 | goto cleanup; | |
211 | } | |
212 | if (fstat(fd, &st)) { | |
213 | ret = error_errno(_("failed to read %s"), revindex_name); | |
214 | goto cleanup; | |
215 | } | |
216 | ||
217 | revindex_size = xsize_t(st.st_size); | |
218 | ||
219 | if (revindex_size < RIDX_MIN_SIZE) { | |
220 | ret = error(_("reverse-index file %s is too small"), revindex_name); | |
221 | goto cleanup; | |
222 | } | |
223 | ||
224 | if (revindex_size - RIDX_MIN_SIZE != st_mult(sizeof(uint32_t), num_objects)) { | |
225 | ret = error(_("reverse-index file %s is corrupt"), revindex_name); | |
226 | goto cleanup; | |
227 | } | |
228 | ||
229 | data = xmmap(NULL, revindex_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
230 | hdr = data; | |
231 | ||
232 | if (ntohl(hdr->signature) != RIDX_SIGNATURE) { | |
233 | ret = error(_("reverse-index file %s has unknown signature"), revindex_name); | |
234 | goto cleanup; | |
235 | } | |
236 | if (ntohl(hdr->version) != 1) { | |
237 | ret = error(_("reverse-index file %s has unsupported version %"PRIu32), | |
238 | revindex_name, ntohl(hdr->version)); | |
239 | goto cleanup; | |
240 | } | |
241 | if (!(ntohl(hdr->hash_id) == 1 || ntohl(hdr->hash_id) == 2)) { | |
242 | ret = error(_("reverse-index file %s has unsupported hash id %"PRIu32), | |
243 | revindex_name, ntohl(hdr->hash_id)); | |
244 | goto cleanup; | |
245 | } | |
246 | ||
247 | cleanup: | |
248 | if (ret) { | |
249 | if (data) | |
250 | munmap(data, revindex_size); | |
251 | } else { | |
252 | *len_p = revindex_size; | |
253 | *data_p = (const uint32_t *)data; | |
254 | } | |
255 | ||
256 | close(fd); | |
257 | return ret; | |
258 | } | |
259 | ||
260 | static int load_pack_revindex_from_disk(struct packed_git *p) | |
261 | { | |
262 | char *revindex_name; | |
263 | int ret; | |
264 | if (open_pack_index(p)) | |
265 | return -1; | |
266 | ||
267 | revindex_name = pack_revindex_filename(p); | |
268 | ||
269 | ret = load_revindex_from_disk(revindex_name, | |
270 | p->num_objects, | |
271 | &p->revindex_map, | |
272 | &p->revindex_size); | |
273 | if (ret) | |
274 | goto cleanup; | |
275 | ||
276 | p->revindex_data = (const uint32_t *)((const char *)p->revindex_map + RIDX_HEADER_SIZE); | |
277 | ||
278 | cleanup: | |
279 | free(revindex_name); | |
280 | return ret; | |
281 | } | |
282 | ||
283 | int load_pack_revindex(struct packed_git *p) | |
284 | { | |
285 | if (p->revindex || p->revindex_data) | |
286 | return 0; | |
287 | ||
288 | if (!load_pack_revindex_from_disk(p)) | |
289 | return 0; | |
290 | else if (!create_pack_revindex_in_memory(p)) | |
291 | return 0; | |
292 | return -1; | |
293 | } | |
294 | ||
8389855a | 295 | int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos) |
92e5c77c | 296 | { |
8389855a | 297 | unsigned lo, hi; |
8389855a TB |
298 | |
299 | if (load_pack_revindex(p) < 0) | |
300 | return -1; | |
301 | ||
302 | lo = 0; | |
303 | hi = p->num_objects + 1; | |
92e5c77c | 304 | |
3449f8c4 | 305 | do { |
33de80b1 | 306 | const unsigned mi = lo + (hi - lo) / 2; |
e5dcd784 TB |
307 | off_t got = pack_pos_to_offset(p, mi); |
308 | ||
309 | if (got == ofs) { | |
8389855a TB |
310 | *pos = mi; |
311 | return 0; | |
e5dcd784 | 312 | } else if (ofs < got) |
3449f8c4 NP |
313 | hi = mi; |
314 | else | |
315 | lo = mi + 1; | |
316 | } while (lo < hi); | |
92e5c77c | 317 | |
08698b1e | 318 | error("bad offset for revindex"); |
92e5c77c VM |
319 | return -1; |
320 | } | |
321 | ||
f33fb6e4 TB |
322 | uint32_t pack_pos_to_index(struct packed_git *p, uint32_t pos) |
323 | { | |
2f4ba2a8 | 324 | if (!(p->revindex || p->revindex_data)) |
f33fb6e4 TB |
325 | BUG("pack_pos_to_index: reverse index not yet loaded"); |
326 | if (p->num_objects <= pos) | |
327 | BUG("pack_pos_to_index: out-of-bounds object at %"PRIu32, pos); | |
2f4ba2a8 TB |
328 | |
329 | if (p->revindex) | |
330 | return p->revindex[pos].nr; | |
331 | else | |
332 | return get_be32(p->revindex_data + pos); | |
f33fb6e4 TB |
333 | } |
334 | ||
335 | off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos) | |
336 | { | |
2f4ba2a8 | 337 | if (!(p->revindex || p->revindex_data)) |
f33fb6e4 TB |
338 | BUG("pack_pos_to_index: reverse index not yet loaded"); |
339 | if (p->num_objects < pos) | |
340 | BUG("pack_pos_to_offset: out-of-bounds object at %"PRIu32, pos); | |
2f4ba2a8 TB |
341 | |
342 | if (p->revindex) | |
343 | return p->revindex[pos].offset; | |
344 | else if (pos == p->num_objects) | |
345 | return p->pack_size - the_hash_algo->rawsz; | |
346 | else | |
347 | return nth_packed_object_offset(p, pack_pos_to_index(p, pos)); | |
f33fb6e4 | 348 | } |