midx: pass a repository pointer
[git/git.git] / midx.c
CommitLineData
a3407730 1#include "cache.h"
c4d25228 2#include "config.h"
fc59e748 3#include "csum-file.h"
396f2570 4#include "dir.h"
fc59e748 5#include "lockfile.h"
396f2570 6#include "packfile.h"
4d80560c 7#include "object-store.h"
3715a633 8#include "sha1-lookup.h"
a3407730 9#include "midx.h"
144d7033 10#include "progress.h"
d829223a 11#include "trace2.h"
a3407730 12
fc59e748
DS
13#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
14#define MIDX_VERSION 1
4d80560c
DS
15#define MIDX_BYTE_FILE_VERSION 4
16#define MIDX_BYTE_HASH_VERSION 5
17#define MIDX_BYTE_NUM_CHUNKS 6
18#define MIDX_BYTE_NUM_PACKS 8
fc59e748
DS
19#define MIDX_HASH_VERSION 1
20#define MIDX_HEADER_SIZE 12
4d80560c
DS
21#define MIDX_HASH_LEN 20
22#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + MIDX_HASH_LEN)
fc59e748 23
662148c4 24#define MIDX_MAX_CHUNKS 5
32f3c541
DS
25#define MIDX_CHUNK_ALIGNMENT 4
26#define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
d7cacf29 27#define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
0d5b3a5e 28#define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
662148c4
DS
29#define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
30#define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
32f3c541 31#define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
d7cacf29 32#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
662148c4
DS
33#define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
34#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
35#define MIDX_LARGE_OFFSET_NEEDED 0x80000000
32f3c541 36
fc59e748
DS
37static char *get_midx_filename(const char *object_dir)
38{
39 return xstrfmt("%s/pack/multi-pack-index", object_dir);
40}
41
2cf489a3 42struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
4d80560c
DS
43{
44 struct multi_pack_index *m = NULL;
45 int fd;
46 struct stat st;
47 size_t midx_size;
48 void *midx_map = NULL;
49 uint32_t hash_version;
50 char *midx_name = get_midx_filename(object_dir);
32f3c541 51 uint32_t i;
3227565c 52 const char *cur_pack_name;
4d80560c
DS
53
54 fd = git_open(midx_name);
55
56 if (fd < 0)
57 goto cleanup_fail;
58 if (fstat(fd, &st)) {
59 error_errno(_("failed to read %s"), midx_name);
60 goto cleanup_fail;
61 }
62
63 midx_size = xsize_t(st.st_size);
64
65 if (midx_size < MIDX_MIN_SIZE) {
66 error(_("multi-pack-index file %s is too small"), midx_name);
67 goto cleanup_fail;
68 }
69
70 FREE_AND_NULL(midx_name);
71
72 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
73
577314ca 74 FLEX_ALLOC_STR(m, object_dir, object_dir);
4d80560c
DS
75 m->fd = fd;
76 m->data = midx_map;
77 m->data_len = midx_size;
2cf489a3 78 m->local = local;
4d80560c
DS
79
80 m->signature = get_be32(m->data);
53ad0407
DS
81 if (m->signature != MIDX_SIGNATURE)
82 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
4d80560c 83 m->signature, MIDX_SIGNATURE);
4d80560c
DS
84
85 m->version = m->data[MIDX_BYTE_FILE_VERSION];
53ad0407
DS
86 if (m->version != MIDX_VERSION)
87 die(_("multi-pack-index version %d not recognized"),
4d80560c 88 m->version);
4d80560c
DS
89
90 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
53ad0407
DS
91 if (hash_version != MIDX_HASH_VERSION)
92 die(_("hash version %u does not match"), hash_version);
4d80560c
DS
93 m->hash_len = MIDX_HASH_LEN;
94
95 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
96
97 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
98
32f3c541
DS
99 for (i = 0; i < m->num_chunks; i++) {
100 uint32_t chunk_id = get_be32(m->data + MIDX_HEADER_SIZE +
101 MIDX_CHUNKLOOKUP_WIDTH * i);
102 uint64_t chunk_offset = get_be64(m->data + MIDX_HEADER_SIZE + 4 +
103 MIDX_CHUNKLOOKUP_WIDTH * i);
104
d3f8e211
DS
105 if (chunk_offset >= m->data_len)
106 die(_("invalid chunk offset (too large)"));
107
32f3c541
DS
108 switch (chunk_id) {
109 case MIDX_CHUNKID_PACKNAMES:
110 m->chunk_pack_names = m->data + chunk_offset;
111 break;
112
d7cacf29
DS
113 case MIDX_CHUNKID_OIDFANOUT:
114 m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset);
115 break;
116
0d5b3a5e
DS
117 case MIDX_CHUNKID_OIDLOOKUP:
118 m->chunk_oid_lookup = m->data + chunk_offset;
119 break;
120
662148c4
DS
121 case MIDX_CHUNKID_OBJECTOFFSETS:
122 m->chunk_object_offsets = m->data + chunk_offset;
123 break;
124
125 case MIDX_CHUNKID_LARGEOFFSETS:
126 m->chunk_large_offsets = m->data + chunk_offset;
127 break;
128
32f3c541
DS
129 case 0:
130 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
131 break;
132
133 default:
134 /*
135 * Do nothing on unrecognized chunks, allowing future
136 * extensions to add optional chunks.
137 */
138 break;
139 }
140 }
141
142 if (!m->chunk_pack_names)
143 die(_("multi-pack-index missing required pack-name chunk"));
d7cacf29
DS
144 if (!m->chunk_oid_fanout)
145 die(_("multi-pack-index missing required OID fanout chunk"));
0d5b3a5e
DS
146 if (!m->chunk_oid_lookup)
147 die(_("multi-pack-index missing required OID lookup chunk"));
662148c4
DS
148 if (!m->chunk_object_offsets)
149 die(_("multi-pack-index missing required object offsets chunk"));
32f3c541 150
d7cacf29
DS
151 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
152
3227565c 153 m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
3715a633 154 m->packs = xcalloc(m->num_packs, sizeof(*m->packs));
3227565c
DS
155
156 cur_pack_name = (const char *)m->chunk_pack_names;
157 for (i = 0; i < m->num_packs; i++) {
158 m->pack_names[i] = cur_pack_name;
159
160 cur_pack_name += strlen(cur_pack_name) + 1;
161
8e72a3c3
DS
162 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
163 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
3227565c
DS
164 m->pack_names[i - 1],
165 m->pack_names[i]);
3227565c
DS
166 }
167
d829223a
JH
168 trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
169 trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
170
4d80560c
DS
171 return m;
172
173cleanup_fail:
174 free(m);
175 free(midx_name);
176 if (midx_map)
177 munmap(midx_map, midx_size);
178 if (0 <= fd)
179 close(fd);
180 return NULL;
181}
182
1dcd9f20 183void close_midx(struct multi_pack_index *m)
a40498a1
DS
184{
185 uint32_t i;
1dcd9f20
DS
186
187 if (!m)
188 return;
189
a40498a1
DS
190 munmap((unsigned char *)m->data, m->data_len);
191 close(m->fd);
192 m->fd = -1;
193
194 for (i = 0; i < m->num_packs; i++) {
195 if (m->packs[i]) {
196 close_pack(m->packs[i]);
0ce4ff94 197 free(m->packs[i]);
a40498a1
DS
198 }
199 }
200 FREE_AND_NULL(m->packs);
201 FREE_AND_NULL(m->pack_names);
202}
203
64404a24 204int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
3715a633
DS
205{
206 struct strbuf pack_name = STRBUF_INIT;
207
208 if (pack_int_id >= m->num_packs)
d355e46a 209 die(_("bad pack-int-id: %u (%u total packs)"),
cc6af73c 210 pack_int_id, m->num_packs);
3715a633
DS
211
212 if (m->packs[pack_int_id])
213 return 0;
214
215 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
216 m->pack_names[pack_int_id]);
217
2cf489a3 218 m->packs[pack_int_id] = add_packed_git(pack_name.buf, pack_name.len, m->local);
3715a633
DS
219 strbuf_release(&pack_name);
220 return !m->packs[pack_int_id];
221}
222
223int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
224{
225 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
226 MIDX_HASH_LEN, result);
227}
228
8aac67a1
DS
229struct object_id *nth_midxed_object_oid(struct object_id *oid,
230 struct multi_pack_index *m,
231 uint32_t n)
232{
233 if (n >= m->num_objects)
234 return NULL;
235
236 hashcpy(oid->hash, m->chunk_oid_lookup + m->hash_len * n);
237 return oid;
238}
239
3715a633
DS
240static off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
241{
242 const unsigned char *offset_data;
243 uint32_t offset32;
244
245 offset_data = m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH;
246 offset32 = get_be32(offset_data + sizeof(uint32_t));
247
248 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
d8ac9ee1 249 if (sizeof(off_t) < sizeof(uint64_t))
3715a633
DS
250 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
251
252 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
253 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
254 }
255
256 return offset32;
257}
258
259static uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
260{
261 return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH);
262}
263
64404a24
DS
264static int nth_midxed_pack_entry(struct repository *r,
265 struct multi_pack_index *m,
266 struct pack_entry *e,
267 uint32_t pos)
3715a633
DS
268{
269 uint32_t pack_int_id;
270 struct packed_git *p;
271
272 if (pos >= m->num_objects)
273 return 0;
274
275 pack_int_id = nth_midxed_pack_int_id(m, pos);
276
64404a24 277 if (prepare_midx_pack(r, m, pack_int_id))
3715a633
DS
278 die(_("error preparing packfile from multi-pack-index"));
279 p = m->packs[pack_int_id];
280
281 /*
282 * We are about to tell the caller where they can locate the
283 * requested object. We better make sure the packfile is
284 * still here and can be accessed before supplying that
285 * answer, as it may have been deleted since the MIDX was
286 * loaded!
287 */
288 if (!is_pack_valid(p))
289 return 0;
290
c39b02ae
DS
291 if (p->num_bad_objects) {
292 uint32_t i;
293 struct object_id oid;
294 nth_midxed_object_oid(&oid, m, pos);
295 for (i = 0; i < p->num_bad_objects; i++)
e43d2dcc
JK
296 if (hasheq(oid.hash,
297 p->bad_object_sha1 + the_hash_algo->rawsz * i))
c39b02ae
DS
298 return 0;
299 }
300
3715a633
DS
301 e->offset = nth_midxed_offset(m, pos);
302 e->p = p;
303
304 return 1;
305}
306
64404a24
DS
307int fill_midx_entry(struct repository * r,
308 const struct object_id *oid,
309 struct pack_entry *e,
310 struct multi_pack_index *m)
3715a633
DS
311{
312 uint32_t pos;
313
314 if (!bsearch_midx(oid, m, &pos))
315 return 0;
316
64404a24 317 return nth_midxed_pack_entry(r, m, e, pos);
3715a633
DS
318}
319
013fd7ad
JK
320/* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
321static int cmp_idx_or_pack_name(const char *idx_or_pack_name,
322 const char *idx_name)
323{
324 /* Skip past any initial matching prefix. */
325 while (*idx_name && *idx_name == *idx_or_pack_name) {
326 idx_name++;
327 idx_or_pack_name++;
328 }
329
330 /*
331 * If we didn't match completely, we may have matched "pack-1234." and
332 * be left with "idx" and "pack" respectively, which is also OK. We do
333 * not have to check for "idx" and "idx", because that would have been
334 * a complete match (and in that case these strcmps will be false, but
335 * we'll correctly return 0 from the final strcmp() below.
336 *
337 * Technically this matches "fooidx" and "foopack", but we'd never have
338 * such names in the first place.
339 */
340 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
341 return 0;
342
343 /*
344 * This not only checks for a complete match, but also orders based on
345 * the first non-identical character, which means our ordering will
346 * match a raw strcmp(). That makes it OK to use this to binary search
347 * a naively-sorted list.
348 */
349 return strcmp(idx_or_pack_name, idx_name);
350}
351
352int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
a40498a1
DS
353{
354 uint32_t first = 0, last = m->num_packs;
355
356 while (first < last) {
357 uint32_t mid = first + (last - first) / 2;
358 const char *current;
359 int cmp;
360
361 current = m->pack_names[mid];
013fd7ad 362 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
a40498a1
DS
363 if (!cmp)
364 return 1;
365 if (cmp > 0) {
366 first = mid + 1;
367 continue;
368 }
369 last = mid;
370 }
371
372 return 0;
373}
374
2cf489a3 375int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
c4d25228 376{
29e2016b 377 struct multi_pack_index *m;
c4d25228
DS
378 struct multi_pack_index *m_search;
379 int config_value;
0465a505 380 static int env_value = -1;
c4d25228 381
0465a505
DS
382 if (env_value < 0)
383 env_value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);
c4d25228 384
0465a505
DS
385 if (!env_value &&
386 (repo_config_get_bool(r, "core.multipackindex", &config_value) ||
387 !config_value))
c4d25228
DS
388 return 0;
389
29e2016b 390 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
c4d25228
DS
391 if (!strcmp(object_dir, m_search->object_dir))
392 return 1;
393
29e2016b 394 m = load_multi_pack_index(object_dir, local);
c4d25228 395
29e2016b
DS
396 if (m) {
397 m->next = r->objects->multi_pack_index;
398 r->objects->multi_pack_index = m;
c4d25228
DS
399 return 1;
400 }
401
402 return 0;
403}
404
fc59e748
DS
405static size_t write_midx_header(struct hashfile *f,
406 unsigned char num_chunks,
407 uint32_t num_packs)
408{
409 unsigned char byte_values[4];
410
411 hashwrite_be32(f, MIDX_SIGNATURE);
412 byte_values[0] = MIDX_VERSION;
413 byte_values[1] = MIDX_HASH_VERSION;
414 byte_values[2] = num_chunks;
415 byte_values[3] = 0; /* unused */
416 hashwrite(f, byte_values, sizeof(byte_values));
417 hashwrite_be32(f, num_packs);
418
419 return MIDX_HEADER_SIZE;
420}
421
396f2570
DS
422struct pack_list {
423 struct packed_git **list;
32f3c541 424 char **names;
396f2570
DS
425 uint32_t nr;
426 uint32_t alloc_list;
32f3c541
DS
427 uint32_t alloc_names;
428 size_t pack_name_concat_len;
a40498a1 429 struct multi_pack_index *m;
396f2570
DS
430};
431
432static void add_pack_to_midx(const char *full_path, size_t full_path_len,
433 const char *file_name, void *data)
434{
435 struct pack_list *packs = (struct pack_list *)data;
436
437 if (ends_with(file_name, ".idx")) {
a40498a1
DS
438 if (packs->m && midx_contains_pack(packs->m, file_name))
439 return;
440
396f2570 441 ALLOC_GROW(packs->list, packs->nr + 1, packs->alloc_list);
32f3c541 442 ALLOC_GROW(packs->names, packs->nr + 1, packs->alloc_names);
396f2570
DS
443
444 packs->list[packs->nr] = add_packed_git(full_path,
445 full_path_len,
446 0);
fe1ed56f 447
396f2570
DS
448 if (!packs->list[packs->nr]) {
449 warning(_("failed to add packfile '%s'"),
450 full_path);
451 return;
452 }
453
fe1ed56f
DS
454 if (open_pack_index(packs->list[packs->nr])) {
455 warning(_("failed to open pack-index '%s'"),
456 full_path);
457 close_pack(packs->list[packs->nr]);
458 FREE_AND_NULL(packs->list[packs->nr]);
459 return;
460 }
461
32f3c541
DS
462 packs->names[packs->nr] = xstrdup(file_name);
463 packs->pack_name_concat_len += strlen(file_name) + 1;
396f2570
DS
464 packs->nr++;
465 }
466}
467
32f3c541
DS
468struct pack_pair {
469 uint32_t pack_int_id;
470 char *pack_name;
471};
472
473static int pack_pair_compare(const void *_a, const void *_b)
474{
475 struct pack_pair *a = (struct pack_pair *)_a;
476 struct pack_pair *b = (struct pack_pair *)_b;
477 return strcmp(a->pack_name, b->pack_name);
478}
479
480static void sort_packs_by_name(char **pack_names, uint32_t nr_packs, uint32_t *perm)
481{
482 uint32_t i;
483 struct pack_pair *pairs;
484
485 ALLOC_ARRAY(pairs, nr_packs);
486
487 for (i = 0; i < nr_packs; i++) {
488 pairs[i].pack_int_id = i;
489 pairs[i].pack_name = pack_names[i];
490 }
491
492 QSORT(pairs, nr_packs, pack_pair_compare);
493
494 for (i = 0; i < nr_packs; i++) {
495 pack_names[i] = pairs[i].pack_name;
496 perm[pairs[i].pack_int_id] = i;
497 }
498
499 free(pairs);
500}
501
fe1ed56f
DS
502struct pack_midx_entry {
503 struct object_id oid;
504 uint32_t pack_int_id;
505 time_t pack_mtime;
506 uint64_t offset;
507};
508
509static int midx_oid_compare(const void *_a, const void *_b)
510{
511 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
512 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
513 int cmp = oidcmp(&a->oid, &b->oid);
514
515 if (cmp)
516 return cmp;
517
518 if (a->pack_mtime > b->pack_mtime)
519 return -1;
520 else if (a->pack_mtime < b->pack_mtime)
521 return 1;
522
523 return a->pack_int_id - b->pack_int_id;
524}
525
a40498a1
DS
526static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
527 uint32_t *pack_perm,
528 struct pack_midx_entry *e,
529 uint32_t pos)
530{
531 if (pos >= m->num_objects)
532 return 1;
533
534 nth_midxed_object_oid(&e->oid, m, pos);
535 e->pack_int_id = pack_perm[nth_midxed_pack_int_id(m, pos)];
536 e->offset = nth_midxed_offset(m, pos);
537
538 /* consider objects in midx to be from "old" packs */
539 e->pack_mtime = 0;
540 return 0;
541}
542
fe1ed56f
DS
543static void fill_pack_entry(uint32_t pack_int_id,
544 struct packed_git *p,
545 uint32_t cur_object,
546 struct pack_midx_entry *entry)
547{
548 if (!nth_packed_object_oid(&entry->oid, p, cur_object))
549 die(_("failed to locate object %d in packfile"), cur_object);
550
551 entry->pack_int_id = pack_int_id;
552 entry->pack_mtime = p->mtime;
553
554 entry->offset = nth_packed_object_offset(p, cur_object);
555}
556
557/*
558 * It is possible to artificially get into a state where there are many
559 * duplicate copies of objects. That can create high memory pressure if
560 * we are to create a list of all objects before de-duplication. To reduce
561 * this memory pressure without a significant performance drop, automatically
562 * group objects by the first byte of their object id. Use the IDX fanout
563 * tables to group the data, copy to a local array, then sort.
564 *
565 * Copy only the de-duplicated entries (selected by most-recent modified time
566 * of a packfile containing the object).
567 */
a40498a1
DS
568static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
569 struct packed_git **p,
fe1ed56f
DS
570 uint32_t *perm,
571 uint32_t nr_packs,
572 uint32_t *nr_objects)
573{
574 uint32_t cur_fanout, cur_pack, cur_object;
575 uint32_t alloc_fanout, alloc_objects, total_objects = 0;
576 struct pack_midx_entry *entries_by_fanout = NULL;
577 struct pack_midx_entry *deduplicated_entries = NULL;
a40498a1 578 uint32_t start_pack = m ? m->num_packs : 0;
fe1ed56f 579
a40498a1 580 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
fe1ed56f
DS
581 total_objects += p[cur_pack]->num_objects;
582
583 /*
584 * As we de-duplicate by fanout value, we expect the fanout
585 * slices to be evenly distributed, with some noise. Hence,
586 * allocate slightly more than one 256th.
587 */
588 alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16;
589
590 ALLOC_ARRAY(entries_by_fanout, alloc_fanout);
591 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
592 *nr_objects = 0;
593
594 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
595 uint32_t nr_fanout = 0;
596
a40498a1
DS
597 if (m) {
598 uint32_t start = 0, end;
599
600 if (cur_fanout)
601 start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
602 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
603
604 for (cur_object = start; cur_object < end; cur_object++) {
605 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
606 nth_midxed_pack_midx_entry(m, perm,
607 &entries_by_fanout[nr_fanout],
608 cur_object);
609 nr_fanout++;
610 }
611 }
612
613 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
fe1ed56f
DS
614 uint32_t start = 0, end;
615
616 if (cur_fanout)
617 start = get_pack_fanout(p[cur_pack], cur_fanout - 1);
618 end = get_pack_fanout(p[cur_pack], cur_fanout);
619
620 for (cur_object = start; cur_object < end; cur_object++) {
621 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
622 fill_pack_entry(perm[cur_pack], p[cur_pack], cur_object, &entries_by_fanout[nr_fanout]);
623 nr_fanout++;
624 }
625 }
626
627 QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
628
629 /*
630 * The batch is now sorted by OID and then mtime (descending).
631 * Take only the first duplicate.
632 */
633 for (cur_object = 0; cur_object < nr_fanout; cur_object++) {
e43d2dcc
JK
634 if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid,
635 &entries_by_fanout[cur_object].oid))
fe1ed56f
DS
636 continue;
637
638 ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
639 memcpy(&deduplicated_entries[*nr_objects],
640 &entries_by_fanout[cur_object],
641 sizeof(struct pack_midx_entry));
642 (*nr_objects)++;
643 }
644 }
645
646 free(entries_by_fanout);
647 return deduplicated_entries;
648}
649
32f3c541
DS
650static size_t write_midx_pack_names(struct hashfile *f,
651 char **pack_names,
652 uint32_t num_packs)
653{
654 uint32_t i;
655 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
656 size_t written = 0;
657
658 for (i = 0; i < num_packs; i++) {
659 size_t writelen = strlen(pack_names[i]) + 1;
660
661 if (i && strcmp(pack_names[i], pack_names[i - 1]) <= 0)
662 BUG("incorrect pack-file order: %s before %s",
663 pack_names[i - 1],
664 pack_names[i]);
665
666 hashwrite(f, pack_names[i], writelen);
667 written += writelen;
668 }
669
670 /* add padding to be aligned */
671 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
672 if (i < MIDX_CHUNK_ALIGNMENT) {
673 memset(padding, 0, sizeof(padding));
674 hashwrite(f, padding, i);
675 written += i;
676 }
677
678 return written;
679}
680
d7cacf29
DS
681static size_t write_midx_oid_fanout(struct hashfile *f,
682 struct pack_midx_entry *objects,
683 uint32_t nr_objects)
684{
685 struct pack_midx_entry *list = objects;
686 struct pack_midx_entry *last = objects + nr_objects;
687 uint32_t count = 0;
688 uint32_t i;
689
690 /*
691 * Write the first-level table (the list is sorted,
692 * but we use a 256-entry lookup to be able to avoid
693 * having to do eight extra binary search iterations).
694 */
695 for (i = 0; i < 256; i++) {
696 struct pack_midx_entry *next = list;
697
698 while (next < last && next->oid.hash[0] == i) {
699 count++;
700 next++;
701 }
702
703 hashwrite_be32(f, count);
704 list = next;
705 }
706
707 return MIDX_CHUNK_FANOUT_SIZE;
708}
709
0d5b3a5e
DS
710static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len,
711 struct pack_midx_entry *objects,
712 uint32_t nr_objects)
713{
714 struct pack_midx_entry *list = objects;
715 uint32_t i;
716 size_t written = 0;
717
718 for (i = 0; i < nr_objects; i++) {
719 struct pack_midx_entry *obj = list++;
720
721 if (i < nr_objects - 1) {
722 struct pack_midx_entry *next = list;
723 if (oidcmp(&obj->oid, &next->oid) >= 0)
724 BUG("OIDs not in order: %s >= %s",
725 oid_to_hex(&obj->oid),
726 oid_to_hex(&next->oid));
727 }
728
729 hashwrite(f, obj->oid.hash, (int)hash_len);
730 written += hash_len;
731 }
732
733 return written;
734}
735
662148c4
DS
736static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed,
737 struct pack_midx_entry *objects, uint32_t nr_objects)
738{
739 struct pack_midx_entry *list = objects;
740 uint32_t i, nr_large_offset = 0;
741 size_t written = 0;
742
743 for (i = 0; i < nr_objects; i++) {
744 struct pack_midx_entry *obj = list++;
745
746 hashwrite_be32(f, obj->pack_int_id);
747
748 if (large_offset_needed && obj->offset >> 31)
749 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
750 else if (!large_offset_needed && obj->offset >> 32)
751 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
752 oid_to_hex(&obj->oid),
753 obj->offset);
754 else
755 hashwrite_be32(f, (uint32_t)obj->offset);
756
757 written += MIDX_CHUNK_OFFSET_WIDTH;
758 }
759
760 return written;
761}
762
763static size_t write_midx_large_offsets(struct hashfile *f, uint32_t nr_large_offset,
764 struct pack_midx_entry *objects, uint32_t nr_objects)
765{
61b0fcbb 766 struct pack_midx_entry *list = objects, *end = objects + nr_objects;
662148c4
DS
767 size_t written = 0;
768
769 while (nr_large_offset) {
61b0fcbb
JK
770 struct pack_midx_entry *obj;
771 uint64_t offset;
772
773 if (list >= end)
774 BUG("too many large-offset objects");
775
776 obj = list++;
777 offset = obj->offset;
662148c4
DS
778
779 if (!(offset >> 31))
780 continue;
781
782 hashwrite_be32(f, offset >> 32);
783 hashwrite_be32(f, offset & 0xffffffffUL);
784 written += 2 * sizeof(uint32_t);
785
786 nr_large_offset--;
787 }
788
789 return written;
790}
791
a3407730
DS
792int write_midx_file(const char *object_dir)
793{
32f3c541 794 unsigned char cur_chunk, num_chunks = 0;
fc59e748 795 char *midx_name;
396f2570 796 uint32_t i;
fc59e748
DS
797 struct hashfile *f = NULL;
798 struct lock_file lk;
396f2570 799 struct pack_list packs;
32f3c541
DS
800 uint32_t *pack_perm = NULL;
801 uint64_t written = 0;
802 uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
803 uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
662148c4 804 uint32_t nr_entries, num_large_offsets = 0;
fe1ed56f 805 struct pack_midx_entry *entries = NULL;
662148c4 806 int large_offsets_needed = 0;
fc59e748
DS
807
808 midx_name = get_midx_filename(object_dir);
809 if (safe_create_leading_directories(midx_name)) {
810 UNLEAK(midx_name);
811 die_errno(_("unable to create leading directories of %s"),
812 midx_name);
813 }
814
2cf489a3 815 packs.m = load_multi_pack_index(object_dir, 1);
a40498a1 816
396f2570 817 packs.nr = 0;
a40498a1
DS
818 packs.alloc_list = packs.m ? packs.m->num_packs : 16;
819 packs.alloc_names = packs.alloc_list;
396f2570 820 packs.list = NULL;
a40498a1 821 packs.names = NULL;
32f3c541 822 packs.pack_name_concat_len = 0;
396f2570 823 ALLOC_ARRAY(packs.list, packs.alloc_list);
32f3c541 824 ALLOC_ARRAY(packs.names, packs.alloc_names);
396f2570 825
a40498a1
DS
826 if (packs.m) {
827 for (i = 0; i < packs.m->num_packs; i++) {
828 ALLOC_GROW(packs.list, packs.nr + 1, packs.alloc_list);
829 ALLOC_GROW(packs.names, packs.nr + 1, packs.alloc_names);
830
831 packs.list[packs.nr] = NULL;
832 packs.names[packs.nr] = xstrdup(packs.m->pack_names[i]);
833 packs.pack_name_concat_len += strlen(packs.names[packs.nr]) + 1;
834 packs.nr++;
835 }
836 }
837
396f2570
DS
838 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
839
a40498a1
DS
840 if (packs.m && packs.nr == packs.m->num_packs)
841 goto cleanup;
842
32f3c541
DS
843 if (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
844 packs.pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
845 (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
846
847 ALLOC_ARRAY(pack_perm, packs.nr);
848 sort_packs_by_name(packs.names, packs.nr, pack_perm);
849
a40498a1
DS
850 entries = get_sorted_entries(packs.m, packs.list, pack_perm, packs.nr, &nr_entries);
851
662148c4
DS
852 for (i = 0; i < nr_entries; i++) {
853 if (entries[i].offset > 0x7fffffff)
854 num_large_offsets++;
855 if (entries[i].offset > 0xffffffff)
856 large_offsets_needed = 1;
857 }
fe1ed56f 858
fc59e748
DS
859 hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
860 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
861 FREE_AND_NULL(midx_name);
862
a40498a1
DS
863 if (packs.m)
864 close_midx(packs.m);
865
32f3c541 866 cur_chunk = 0;
662148c4 867 num_chunks = large_offsets_needed ? 5 : 4;
32f3c541
DS
868
869 written = write_midx_header(f, num_chunks, packs.nr);
870
871 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
872 chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
873
874 cur_chunk++;
d7cacf29 875 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
32f3c541
DS
876 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + packs.pack_name_concat_len;
877
d7cacf29
DS
878 cur_chunk++;
879 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;
880 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE;
881
0d5b3a5e 882 cur_chunk++;
662148c4 883 chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS;
0d5b3a5e
DS
884 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_HASH_LEN;
885
662148c4
DS
886 cur_chunk++;
887 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_CHUNK_OFFSET_WIDTH;
888 if (large_offsets_needed) {
889 chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS;
890
891 cur_chunk++;
892 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] +
893 num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
894 }
895
896 chunk_ids[cur_chunk] = 0;
897
32f3c541
DS
898 for (i = 0; i <= num_chunks; i++) {
899 if (i && chunk_offsets[i] < chunk_offsets[i - 1])
900 BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64,
901 chunk_offsets[i - 1],
902 chunk_offsets[i]);
903
904 if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT)
905 BUG("chunk offset %"PRIu64" is not properly aligned",
906 chunk_offsets[i]);
907
908 hashwrite_be32(f, chunk_ids[i]);
909 hashwrite_be32(f, chunk_offsets[i] >> 32);
910 hashwrite_be32(f, chunk_offsets[i]);
911
912 written += MIDX_CHUNKLOOKUP_WIDTH;
913 }
914
915 for (i = 0; i < num_chunks; i++) {
916 if (written != chunk_offsets[i])
917 BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32,
918 chunk_offsets[i],
919 written,
920 chunk_ids[i]);
921
922 switch (chunk_ids[i]) {
923 case MIDX_CHUNKID_PACKNAMES:
924 written += write_midx_pack_names(f, packs.names, packs.nr);
925 break;
926
d7cacf29
DS
927 case MIDX_CHUNKID_OIDFANOUT:
928 written += write_midx_oid_fanout(f, entries, nr_entries);
929 break;
930
0d5b3a5e
DS
931 case MIDX_CHUNKID_OIDLOOKUP:
932 written += write_midx_oid_lookup(f, MIDX_HASH_LEN, entries, nr_entries);
933 break;
934
662148c4
DS
935 case MIDX_CHUNKID_OBJECTOFFSETS:
936 written += write_midx_object_offsets(f, large_offsets_needed, entries, nr_entries);
937 break;
938
939 case MIDX_CHUNKID_LARGEOFFSETS:
940 written += write_midx_large_offsets(f, num_large_offsets, entries, nr_entries);
941 break;
942
32f3c541
DS
943 default:
944 BUG("trying to write unknown chunk id %"PRIx32,
945 chunk_ids[i]);
946 }
947 }
948
949 if (written != chunk_offsets[num_chunks])
950 BUG("incorrect final offset %"PRIu64" != %"PRIu64,
951 written,
952 chunk_offsets[num_chunks]);
fc59e748
DS
953
954 finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
955 commit_lock_file(&lk);
956
a40498a1 957cleanup:
396f2570
DS
958 for (i = 0; i < packs.nr; i++) {
959 if (packs.list[i]) {
960 close_pack(packs.list[i]);
961 free(packs.list[i]);
962 }
32f3c541 963 free(packs.names[i]);
396f2570
DS
964 }
965
966 free(packs.list);
32f3c541 967 free(packs.names);
fe1ed56f 968 free(entries);
a40498a1
DS
969 free(pack_perm);
970 free(midx_name);
a3407730
DS
971 return 0;
972}
525e18c0 973
1dcd9f20 974void clear_midx_file(struct repository *r)
525e18c0 975{
3b2f8a02 976 char *midx = get_midx_filename(r->objects->odb->path);
1dcd9f20
DS
977
978 if (r->objects && r->objects->multi_pack_index) {
979 close_midx(r->objects->multi_pack_index);
980 r->objects->multi_pack_index = NULL;
981 }
525e18c0
DS
982
983 if (remove_path(midx)) {
984 UNLEAK(midx);
985 die(_("failed to clear multi-pack-index at %s"), midx);
986 }
987
988 free(midx);
989}
56ee7ff1
DS
990
991static int verify_midx_error;
992
d4bf1d88
DS
993static void midx_report(const char *fmt, ...)
994{
995 va_list ap;
996 verify_midx_error = 1;
997 va_start(ap, fmt);
998 vfprintf(stderr, fmt, ap);
999 fprintf(stderr, "\n");
1000 va_end(ap);
1001}
1002
5ae18df9
JH
1003struct pair_pos_vs_id
1004{
1005 uint32_t pos;
1006 uint32_t pack_int_id;
1007};
1008
1009static int compare_pair_pos_vs_id(const void *_a, const void *_b)
1010{
1011 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
1012 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
1013
1014 return b->pack_int_id - a->pack_int_id;
1015}
1016
430efb8a
JH
1017/*
1018 * Limit calls to display_progress() for performance reasons.
1019 * The interval here was arbitrarily chosen.
1020 */
1021#define SPARSE_PROGRESS_INTERVAL (1 << 12)
1022#define midx_display_sparse_progress(progress, n) \
1023 do { \
1024 uint64_t _n = (n); \
1025 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1026 display_progress(progress, _n); \
1027 } while (0)
1028
64404a24 1029int verify_midx_file(struct repository *r, const char *object_dir)
56ee7ff1 1030{
5ae18df9 1031 struct pair_pos_vs_id *pairs = NULL;
d4bf1d88 1032 uint32_t i;
17eeb067 1033 struct progress *progress;
56ee7ff1
DS
1034 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1035 verify_midx_error = 0;
1036
1037 if (!m)
1038 return 0;
1039
430efb8a
JH
1040 progress = start_progress(_("Looking for referenced packfiles"),
1041 m->num_packs);
d4bf1d88 1042 for (i = 0; i < m->num_packs; i++) {
64404a24 1043 if (prepare_midx_pack(r, m, i))
d4bf1d88 1044 midx_report("failed to load pack in position %d", i);
430efb8a
JH
1045
1046 display_progress(progress, i + 1);
d4bf1d88 1047 }
430efb8a 1048 stop_progress(&progress);
d4bf1d88 1049
2f23d3f3
DS
1050 for (i = 0; i < 255; i++) {
1051 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
1052 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]);
1053
1054 if (oid_fanout1 > oid_fanout2)
1055 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
1056 i, oid_fanout1, oid_fanout2, i + 1);
1057 }
1058
430efb8a
JH
1059 progress = start_sparse_progress(_("Verifying OID order in MIDX"),
1060 m->num_objects - 1);
55c5648d
DS
1061 for (i = 0; i < m->num_objects - 1; i++) {
1062 struct object_id oid1, oid2;
1063
1064 nth_midxed_object_oid(&oid1, m, i);
1065 nth_midxed_object_oid(&oid2, m, i + 1);
1066
1067 if (oidcmp(&oid1, &oid2) >= 0)
1068 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1069 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
430efb8a
JH
1070
1071 midx_display_sparse_progress(progress, i + 1);
55c5648d 1072 }
430efb8a 1073 stop_progress(&progress);
55c5648d 1074
5ae18df9
JH
1075 /*
1076 * Create an array mapping each object to its packfile id. Sort it
1077 * to group the objects by packfile. Use this permutation to visit
1078 * each of the objects and only require 1 packfile to be open at a
1079 * time.
1080 */
1081 ALLOC_ARRAY(pairs, m->num_objects);
1082 for (i = 0; i < m->num_objects; i++) {
1083 pairs[i].pos = i;
1084 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
1085 }
1086
1087 progress = start_sparse_progress(_("Sorting objects by packfile"),
1088 m->num_objects);
1089 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
1090 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
1091 stop_progress(&progress);
1092
430efb8a 1093 progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
cc6af73c
DS
1094 for (i = 0; i < m->num_objects; i++) {
1095 struct object_id oid;
1096 struct pack_entry e;
1097 off_t m_offset, p_offset;
1098
5ae18df9
JH
1099 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
1100 m->packs[pairs[i-1].pack_int_id])
1101 {
1102 close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
1103 close_pack_index(m->packs[pairs[i-1].pack_int_id]);
1104 }
1105
1106 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1107
64404a24 1108 if (!fill_midx_entry(r, &oid, &e, m)) {
cc6af73c 1109 midx_report(_("failed to load pack entry for oid[%d] = %s"),
5ae18df9 1110 pairs[i].pos, oid_to_hex(&oid));
cc6af73c
DS
1111 continue;
1112 }
1113
1114 if (open_pack_index(e.p)) {
1115 midx_report(_("failed to load pack-index for packfile %s"),
1116 e.p->pack_name);
1117 break;
1118 }
1119
1120 m_offset = e.offset;
1121 p_offset = find_pack_entry_one(oid.hash, e.p);
1122
1123 if (m_offset != p_offset)
1124 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
5ae18df9 1125 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
144d7033 1126
430efb8a 1127 midx_display_sparse_progress(progress, i + 1);
cc6af73c 1128 }
144d7033 1129 stop_progress(&progress);
cc6af73c 1130
5ae18df9
JH
1131 free(pairs);
1132
56ee7ff1
DS
1133 return verify_midx_error;
1134}