blob: 4d9bb41b4dbcda342ebfced2e15e05f27d1e12a7 [file] [log] [blame]
Elijah Newrenb6fdc442023-04-11 00:41:54 -07001#include "git-compat-util.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00002#include "gettext.h"
Nicolas Pitre3449f8c2008-02-28 00:25:17 -05003#include "pack-revindex.h"
Elijah Newren87bed172023-04-11 00:41:53 -07004#include "object-file.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +01005#include "object-store.h"
Jeff King4828ce92019-04-05 14:04:24 -04006#include "packfile.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +00007#include "trace2.h"
Taylor Blauec8e7762021-01-25 18:37:46 -05008#include "config.h"
Taylor Blauf8940812021-03-30 11:04:26 -04009#include "midx.h"
Nicolas Pitre3449f8c2008-02-28 00:25:17 -050010
Taylor Blaud5bc7c62021-01-13 17:25:06 -050011struct revindex_entry {
12 off_t offset;
13 unsigned int nr;
14};
15
Nicolas Pitre3449f8c2008-02-28 00:25:17 -050016/*
17 * Pack index for existing packs give us easy access to the offsets into
18 * corresponding pack file where each object's data starts, but the entries
19 * do not store the size of the compressed representation (uncompressed
20 * size is easily available by examining the pack entry header). It is
21 * also rather expensive to find the sha1 for an object given its offset.
22 *
Jeff Kingf4015332015-12-21 01:19:49 -050023 * The pack index file is sorted by object name mapping to offset;
24 * this revindex array is a list of offset/index_nr pairs
Nicolas Pitre3449f8c2008-02-28 00:25:17 -050025 * ordered by offset, so if you know the offset of an object, next offset
26 * is where its packed representation ends and the index_nr can be used to
27 * get the object sha1 from the main index.
28 */
29
Jeff King8b8dfd52013-07-11 08:16:00 -040030/*
31 * This is a least-significant-digit radix sort.
32 *
33 * It sorts each of the "n" items in "entries" by its offset field. The "max"
34 * parameter must be at least as large as the largest offset in the array,
35 * and lets us quit the sort early.
36 */
37static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
Nicolas Pitre3449f8c2008-02-28 00:25:17 -050038{
Jeff King8b8dfd52013-07-11 08:16:00 -040039 /*
40 * We use a "digit" size of 16 bits. That keeps our memory
41 * usage reasonable, and we can generally (for a 4G or smaller
42 * packfile) quit after two rounds of radix-sorting.
43 */
44#define DIGIT_SIZE (16)
45#define BUCKETS (1 << DIGIT_SIZE)
46 /*
47 * We want to know the bucket that a[i] will go into when we are using
48 * the digit that is N bits from the (least significant) end.
49 */
50#define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1))
51
52 /*
53 * We need O(n) temporary storage. Rather than do an extra copy of the
54 * partial results into "entries", we sort back and forth between the
55 * real array and temporary storage. In each iteration of the loop, we
56 * keep track of them with alias pointers, always sorting from "from"
57 * to "to".
58 */
Jeff Kingb32fa952016-02-22 17:44:25 -050059 struct revindex_entry *tmp, *from, *to;
Jeff King8b8dfd52013-07-11 08:16:00 -040060 int bits;
Jeff Kingb32fa952016-02-22 17:44:25 -050061 unsigned *pos;
62
63 ALLOC_ARRAY(pos, BUCKETS);
64 ALLOC_ARRAY(tmp, n);
65 from = entries;
66 to = tmp;
Jeff King8b8dfd52013-07-11 08:16:00 -040067
68 /*
69 * If (max >> bits) is zero, then we know that the radix digit we are
70 * on (and any higher) will be zero for all entries, and our loop will
71 * be a no-op, as everybody lands in the same zero-th bucket.
72 */
73 for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
Jeff King8b8dfd52013-07-11 08:16:00 -040074 unsigned i;
75
76 memset(pos, 0, BUCKETS * sizeof(*pos));
77
78 /*
79 * We want pos[i] to store the index of the last element that
80 * will go in bucket "i" (actually one past the last element).
81 * To do this, we first count the items that will go in each
82 * bucket, which gives us a relative offset from the last
83 * bucket. We can then cumulatively add the index from the
84 * previous bucket to get the true index.
85 */
86 for (i = 0; i < n; i++)
87 pos[BUCKET_FOR(from, i, bits)]++;
88 for (i = 1; i < BUCKETS; i++)
89 pos[i] += pos[i-1];
90
91 /*
92 * Now we can drop the elements into their correct buckets (in
93 * our temporary array). We iterate the pos counter backwards
94 * to avoid using an extra index to count up. And since we are
95 * going backwards there, we must also go backwards through the
96 * array itself, to keep the sort stable.
97 *
98 * Note that we use an unsigned iterator to make sure we can
99 * handle 2^32-1 objects, even on a 32-bit system. But this
100 * means we cannot use the more obvious "i >= 0" loop condition
101 * for counting backwards, and must instead check for
102 * wrap-around with UINT_MAX.
103 */
104 for (i = n - 1; i != UINT_MAX; i--)
105 to[--pos[BUCKET_FOR(from, i, bits)]] = from[i];
106
107 /*
108 * Now "to" contains the most sorted list, so we swap "from" and
109 * "to" for the next iteration.
110 */
René Scharfe35d803b2017-01-28 22:40:58 +0100111 SWAP(from, to);
Jeff King8b8dfd52013-07-11 08:16:00 -0400112 }
113
114 /*
115 * If we ended with our data in the original array, great. If not,
116 * we have to move it back from the temporary storage.
117 */
118 if (from != entries)
René Scharfe45ccef82016-09-25 09:24:03 +0200119 COPY_ARRAY(entries, tmp, n);
Jeff King8b8dfd52013-07-11 08:16:00 -0400120 free(tmp);
121 free(pos);
122
123#undef BUCKET_FOR
124#undef BUCKETS
125#undef DIGIT_SIZE
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500126}
127
128/*
129 * Ordered list of offsets of objects in the pack.
130 */
Jeff King9d98bbf2015-12-21 01:20:33 -0500131static void create_pack_revindex(struct packed_git *p)
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500132{
Shahzad Lone33de80b2019-02-02 23:16:45 +0000133 const unsigned num_ent = p->num_objects;
Jeff King012b32b2013-07-10 07:50:26 -0400134 unsigned i;
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500135 const char *index = p->index_data;
brian m. carlsonfa130802018-10-15 00:01:53 +0000136 const unsigned hashsz = the_hash_algo->rawsz;
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500137
Junio C Hamano11529ec2016-02-26 13:37:16 -0800138 ALLOC_ARRAY(p->revindex, num_ent + 1);
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500139 index += 4 * 256;
140
141 if (p->index_version > 1) {
142 const uint32_t *off_32 =
Jeff Kingf86f7692020-11-13 00:06:48 -0500143 (uint32_t *)(index + 8 + (size_t)p->num_objects * (hashsz + 4));
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500144 const uint32_t *off_64 = off_32 + p->num_objects;
145 for (i = 0; i < num_ent; i++) {
Shahzad Lone33de80b2019-02-02 23:16:45 +0000146 const uint32_t off = ntohl(*off_32++);
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500147 if (!(off & 0x80000000)) {
Jeff King9d98bbf2015-12-21 01:20:33 -0500148 p->revindex[i].offset = off;
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500149 } else {
Derrick Stoleead622a22018-01-17 14:08:23 -0500150 p->revindex[i].offset = get_be64(off_64);
151 off_64 += 2;
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500152 }
Jeff King9d98bbf2015-12-21 01:20:33 -0500153 p->revindex[i].nr = i;
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500154 }
155 } else {
156 for (i = 0; i < num_ent; i++) {
Shahzad Lone33de80b2019-02-02 23:16:45 +0000157 const uint32_t hl = *((uint32_t *)(index + (hashsz + 4) * i));
Jeff King9d98bbf2015-12-21 01:20:33 -0500158 p->revindex[i].offset = ntohl(hl);
159 p->revindex[i].nr = i;
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500160 }
161 }
162
brian m. carlsonfa130802018-10-15 00:01:53 +0000163 /*
164 * This knows the pack format -- the hash trailer
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500165 * follows immediately after the last object data.
166 */
brian m. carlsonfa130802018-10-15 00:01:53 +0000167 p->revindex[num_ent].offset = p->pack_size - hashsz;
Jeff King9d98bbf2015-12-21 01:20:33 -0500168 p->revindex[num_ent].nr = -1;
169 sort_revindex(p->revindex, num_ent, p->pack_size);
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500170}
171
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500172static int create_pack_revindex_in_memory(struct packed_git *p)
173{
Taylor Blauec8e7762021-01-25 18:37:46 -0500174 if (git_env_bool(GIT_TEST_REV_INDEX_DIE_IN_MEMORY, 0))
175 die("dying as requested by '%s'",
176 GIT_TEST_REV_INDEX_DIE_IN_MEMORY);
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500177 if (open_pack_index(p))
178 return -1;
179 create_pack_revindex(p);
180 return 0;
181}
182
183static char *pack_revindex_filename(struct packed_git *p)
184{
185 size_t len;
186 if (!strip_suffix(p->pack_name, ".pack", &len))
187 BUG("pack_name does not end in .pack");
188 return xstrfmt("%.*s.rev", (int)len, p->pack_name);
189}
190
191#define RIDX_HEADER_SIZE (12)
192#define RIDX_MIN_SIZE (RIDX_HEADER_SIZE + (2 * the_hash_algo->rawsz))
193
194struct revindex_header {
195 uint32_t signature;
196 uint32_t version;
197 uint32_t hash_id;
198};
199
200static int load_revindex_from_disk(char *revindex_name,
201 uint32_t num_objects,
202 const uint32_t **data_p, size_t *len_p)
203{
204 int fd, ret = 0;
205 struct stat st;
206 void *data = NULL;
207 size_t revindex_size;
208 struct revindex_header *hdr;
209
210 fd = git_open(revindex_name);
211
212 if (fd < 0) {
213 ret = -1;
214 goto cleanup;
215 }
216 if (fstat(fd, &st)) {
217 ret = error_errno(_("failed to read %s"), revindex_name);
218 goto cleanup;
219 }
220
221 revindex_size = xsize_t(st.st_size);
222
223 if (revindex_size < RIDX_MIN_SIZE) {
224 ret = error(_("reverse-index file %s is too small"), revindex_name);
225 goto cleanup;
226 }
227
228 if (revindex_size - RIDX_MIN_SIZE != st_mult(sizeof(uint32_t), num_objects)) {
229 ret = error(_("reverse-index file %s is corrupt"), revindex_name);
230 goto cleanup;
231 }
232
233 data = xmmap(NULL, revindex_size, PROT_READ, MAP_PRIVATE, fd, 0);
234 hdr = data;
235
236 if (ntohl(hdr->signature) != RIDX_SIGNATURE) {
237 ret = error(_("reverse-index file %s has unknown signature"), revindex_name);
238 goto cleanup;
239 }
240 if (ntohl(hdr->version) != 1) {
241 ret = error(_("reverse-index file %s has unsupported version %"PRIu32),
242 revindex_name, ntohl(hdr->version));
243 goto cleanup;
244 }
245 if (!(ntohl(hdr->hash_id) == 1 || ntohl(hdr->hash_id) == 2)) {
246 ret = error(_("reverse-index file %s has unsupported hash id %"PRIu32),
247 revindex_name, ntohl(hdr->hash_id));
248 goto cleanup;
249 }
250
251cleanup:
252 if (ret) {
253 if (data)
254 munmap(data, revindex_size);
255 } else {
256 *len_p = revindex_size;
257 *data_p = (const uint32_t *)data;
258 }
259
Taylor Blau66f52fa2021-02-26 11:31:02 -0500260 if (fd >= 0)
261 close(fd);
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500262 return ret;
263}
264
265static int load_pack_revindex_from_disk(struct packed_git *p)
266{
267 char *revindex_name;
268 int ret;
269 if (open_pack_index(p))
270 return -1;
271
272 revindex_name = pack_revindex_filename(p);
273
274 ret = load_revindex_from_disk(revindex_name,
275 p->num_objects,
276 &p->revindex_map,
277 &p->revindex_size);
278 if (ret)
279 goto cleanup;
280
281 p->revindex_data = (const uint32_t *)((const char *)p->revindex_map + RIDX_HEADER_SIZE);
282
283cleanup:
284 free(revindex_name);
285 return ret;
286}
287
Jeff King4828ce92019-04-05 14:04:24 -0400288int load_pack_revindex(struct packed_git *p)
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500289{
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500290 if (p->revindex || p->revindex_data)
291 return 0;
292
293 if (!load_pack_revindex_from_disk(p))
294 return 0;
295 else if (!create_pack_revindex_in_memory(p))
296 return 0;
297 return -1;
Vicent Marti92e5c772013-10-24 14:00:36 -0400298}
299
Taylor Blauf8940812021-03-30 11:04:26 -0400300int load_midx_revindex(struct multi_pack_index *m)
301{
Taylor Blau60980ae2021-10-26 17:01:21 -0400302 struct strbuf revindex_name = STRBUF_INIT;
Taylor Blauf8940812021-03-30 11:04:26 -0400303 int ret;
Taylor Blau7f514b72022-01-25 17:41:17 -0500304
Taylor Blauf8940812021-03-30 11:04:26 -0400305 if (m->revindex_data)
306 return 0;
307
Taylor Blau7f514b72022-01-25 17:41:17 -0500308 if (m->chunk_revindex) {
309 /*
310 * If the MIDX `m` has a `RIDX` chunk, then use its contents for
311 * the reverse index instead of trying to load a separate `.rev`
312 * file.
313 *
314 * Note that we do *not* set `m->revindex_map` here, since we do
315 * not want to accidentally call munmap() in the middle of the
316 * MIDX.
317 */
318 trace2_data_string("load_midx_revindex", the_repository,
319 "source", "midx");
320 m->revindex_data = (const uint32_t *)m->chunk_revindex;
321 return 0;
322 }
323
Taylor Blau09a77992022-01-25 17:41:05 -0500324 trace2_data_string("load_midx_revindex", the_repository,
325 "source", "rev");
326
Taylor Blau60980ae2021-10-26 17:01:21 -0400327 get_midx_rev_filename(&revindex_name, m);
Taylor Blauf8940812021-03-30 11:04:26 -0400328
Taylor Blau60980ae2021-10-26 17:01:21 -0400329 ret = load_revindex_from_disk(revindex_name.buf,
Taylor Blauf8940812021-03-30 11:04:26 -0400330 m->num_objects,
331 &m->revindex_map,
332 &m->revindex_len);
333 if (ret)
334 goto cleanup;
335
336 m->revindex_data = (const uint32_t *)((const char *)m->revindex_map + RIDX_HEADER_SIZE);
337
338cleanup:
Taylor Blau60980ae2021-10-26 17:01:21 -0400339 strbuf_release(&revindex_name);
Taylor Blauf8940812021-03-30 11:04:26 -0400340 return ret;
341}
342
343int close_midx_revindex(struct multi_pack_index *m)
344{
345 if (!m || !m->revindex_map)
346 return 0;
347
348 munmap((void*)m->revindex_map, m->revindex_len);
349
350 m->revindex_map = NULL;
351 m->revindex_data = NULL;
352 m->revindex_len = 0;
353
354 return 0;
355}
356
Taylor Blau83898552021-01-13 17:25:02 -0500357int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos)
Vicent Marti92e5c772013-10-24 14:00:36 -0400358{
Taylor Blau83898552021-01-13 17:25:02 -0500359 unsigned lo, hi;
Taylor Blau83898552021-01-13 17:25:02 -0500360
361 if (load_pack_revindex(p) < 0)
362 return -1;
363
364 lo = 0;
365 hi = p->num_objects + 1;
Vicent Marti92e5c772013-10-24 14:00:36 -0400366
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500367 do {
Shahzad Lone33de80b2019-02-02 23:16:45 +0000368 const unsigned mi = lo + (hi - lo) / 2;
Taylor Blaue5dcd782021-01-13 17:25:10 -0500369 off_t got = pack_pos_to_offset(p, mi);
370
371 if (got == ofs) {
Taylor Blau83898552021-01-13 17:25:02 -0500372 *pos = mi;
373 return 0;
Taylor Blaue5dcd782021-01-13 17:25:10 -0500374 } else if (ofs < got)
Nicolas Pitre3449f8c2008-02-28 00:25:17 -0500375 hi = mi;
376 else
377 lo = mi + 1;
378 } while (lo < hi);
Vicent Marti92e5c772013-10-24 14:00:36 -0400379
Nicolas Pitre08698b12008-10-29 19:02:49 -0400380 error("bad offset for revindex");
Vicent Marti92e5c772013-10-24 14:00:36 -0400381 return -1;
382}
383
Taylor Blauf33fb6e2021-01-13 17:23:31 -0500384uint32_t pack_pos_to_index(struct packed_git *p, uint32_t pos)
385{
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500386 if (!(p->revindex || p->revindex_data))
Taylor Blauf33fb6e2021-01-13 17:23:31 -0500387 BUG("pack_pos_to_index: reverse index not yet loaded");
388 if (p->num_objects <= pos)
389 BUG("pack_pos_to_index: out-of-bounds object at %"PRIu32, pos);
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500390
391 if (p->revindex)
392 return p->revindex[pos].nr;
393 else
394 return get_be32(p->revindex_data + pos);
Taylor Blauf33fb6e2021-01-13 17:23:31 -0500395}
396
397off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos)
398{
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500399 if (!(p->revindex || p->revindex_data))
Taylor Blauf33fb6e2021-01-13 17:23:31 -0500400 BUG("pack_pos_to_index: reverse index not yet loaded");
401 if (p->num_objects < pos)
402 BUG("pack_pos_to_offset: out-of-bounds object at %"PRIu32, pos);
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500403
404 if (p->revindex)
405 return p->revindex[pos].offset;
406 else if (pos == p->num_objects)
407 return p->pack_size - the_hash_algo->rawsz;
408 else
409 return nth_packed_object_offset(p, pack_pos_to_index(p, pos));
Taylor Blauf33fb6e2021-01-13 17:23:31 -0500410}
Taylor Blauf8940812021-03-30 11:04:26 -0400411
412uint32_t pack_pos_to_midx(struct multi_pack_index *m, uint32_t pos)
413{
414 if (!m->revindex_data)
415 BUG("pack_pos_to_midx: reverse index not yet loaded");
416 if (m->num_objects <= pos)
417 BUG("pack_pos_to_midx: out-of-bounds object at %"PRIu32, pos);
418 return get_be32(m->revindex_data + pos);
419}
420
421struct midx_pack_key {
422 uint32_t pack;
423 off_t offset;
424
425 uint32_t preferred_pack;
426 struct multi_pack_index *midx;
427};
428
429static int midx_pack_order_cmp(const void *va, const void *vb)
430{
431 const struct midx_pack_key *key = va;
432 struct multi_pack_index *midx = key->midx;
433
434 uint32_t versus = pack_pos_to_midx(midx, (uint32_t*)vb - (const uint32_t *)midx->revindex_data);
435 uint32_t versus_pack = nth_midxed_pack_int_id(midx, versus);
436 off_t versus_offset;
437
438 uint32_t key_preferred = key->pack == key->preferred_pack;
439 uint32_t versus_preferred = versus_pack == key->preferred_pack;
440
441 /*
442 * First, compare the preferred-ness, noting that the preferred pack
443 * comes first.
444 */
445 if (key_preferred && !versus_preferred)
446 return -1;
447 else if (!key_preferred && versus_preferred)
448 return 1;
449
450 /* Then, break ties first by comparing the pack IDs. */
451 if (key->pack < versus_pack)
452 return -1;
453 else if (key->pack > versus_pack)
454 return 1;
455
456 /* Finally, break ties by comparing offsets within a pack. */
457 versus_offset = nth_midxed_offset(midx, versus);
458 if (key->offset < versus_offset)
459 return -1;
460 else if (key->offset > versus_offset)
461 return 1;
462
463 return 0;
464}
465
466int midx_to_pack_pos(struct multi_pack_index *m, uint32_t at, uint32_t *pos)
467{
468 struct midx_pack_key key;
469 uint32_t *found;
470
471 if (!m->revindex_data)
472 BUG("midx_to_pack_pos: reverse index not yet loaded");
473 if (m->num_objects <= at)
474 BUG("midx_to_pack_pos: out-of-bounds object at %"PRIu32, at);
475
476 key.pack = nth_midxed_pack_int_id(m, at);
477 key.offset = nth_midxed_offset(m, at);
478 key.midx = m;
479 /*
480 * The preferred pack sorts first, so determine its identifier by
481 * looking at the first object in pseudo-pack order.
482 *
483 * Note that if no --preferred-pack is explicitly given when writing a
484 * multi-pack index, then whichever pack has the lowest identifier
485 * implicitly is preferred (and includes all its objects, since ties are
486 * broken first by pack identifier).
487 */
488 key.preferred_pack = nth_midxed_pack_int_id(m, pack_pos_to_midx(m, 0));
489
490 found = bsearch(&key, m->revindex_data, m->num_objects,
491 sizeof(*m->revindex_data), midx_pack_order_cmp);
492
493 if (!found)
494 return error("bad offset for revindex");
495
496 *pos = found - m->revindex_data;
497 return 0;
498}