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