]> git.ipfire.org Git - thirdparty/git.git/blame - midx.c
pack-objects: consider packs in multi-pack-index
[thirdparty/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
DS
9#include "midx.h"
10
fc59e748
DS
11#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
12#define MIDX_VERSION 1
4d80560c
DS
13#define MIDX_BYTE_FILE_VERSION 4
14#define MIDX_BYTE_HASH_VERSION 5
15#define MIDX_BYTE_NUM_CHUNKS 6
16#define MIDX_BYTE_NUM_PACKS 8
fc59e748
DS
17#define MIDX_HASH_VERSION 1
18#define MIDX_HEADER_SIZE 12
4d80560c
DS
19#define MIDX_HASH_LEN 20
20#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + MIDX_HASH_LEN)
fc59e748 21
662148c4 22#define MIDX_MAX_CHUNKS 5
32f3c541
DS
23#define MIDX_CHUNK_ALIGNMENT 4
24#define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
d7cacf29 25#define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
0d5b3a5e 26#define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
662148c4
DS
27#define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
28#define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
32f3c541 29#define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
d7cacf29 30#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
662148c4
DS
31#define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
32#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
33#define MIDX_LARGE_OFFSET_NEEDED 0x80000000
32f3c541 34
fc59e748
DS
35static char *get_midx_filename(const char *object_dir)
36{
37 return xstrfmt("%s/pack/multi-pack-index", object_dir);
38}
39
2cf489a3 40struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
4d80560c
DS
41{
42 struct multi_pack_index *m = NULL;
43 int fd;
44 struct stat st;
45 size_t midx_size;
46 void *midx_map = NULL;
47 uint32_t hash_version;
48 char *midx_name = get_midx_filename(object_dir);
32f3c541 49 uint32_t i;
3227565c 50 const char *cur_pack_name;
4d80560c
DS
51
52 fd = git_open(midx_name);
53
54 if (fd < 0)
55 goto cleanup_fail;
56 if (fstat(fd, &st)) {
57 error_errno(_("failed to read %s"), midx_name);
58 goto cleanup_fail;
59 }
60
61 midx_size = xsize_t(st.st_size);
62
63 if (midx_size < MIDX_MIN_SIZE) {
64 error(_("multi-pack-index file %s is too small"), midx_name);
65 goto cleanup_fail;
66 }
67
68 FREE_AND_NULL(midx_name);
69
70 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
71
72 FLEX_ALLOC_MEM(m, object_dir, object_dir, strlen(object_dir));
73 m->fd = fd;
74 m->data = midx_map;
75 m->data_len = midx_size;
2cf489a3 76 m->local = local;
4d80560c
DS
77
78 m->signature = get_be32(m->data);
79 if (m->signature != MIDX_SIGNATURE) {
80 error(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
81 m->signature, MIDX_SIGNATURE);
82 goto cleanup_fail;
83 }
84
85 m->version = m->data[MIDX_BYTE_FILE_VERSION];
86 if (m->version != MIDX_VERSION) {
87 error(_("multi-pack-index version %d not recognized"),
88 m->version);
89 goto cleanup_fail;
90 }
91
92 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
93 if (hash_version != MIDX_HASH_VERSION) {
94 error(_("hash version %u does not match"), hash_version);
95 goto cleanup_fail;
96 }
97 m->hash_len = MIDX_HASH_LEN;
98
99 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
100
101 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
102
32f3c541
DS
103 for (i = 0; i < m->num_chunks; i++) {
104 uint32_t chunk_id = get_be32(m->data + MIDX_HEADER_SIZE +
105 MIDX_CHUNKLOOKUP_WIDTH * i);
106 uint64_t chunk_offset = get_be64(m->data + MIDX_HEADER_SIZE + 4 +
107 MIDX_CHUNKLOOKUP_WIDTH * i);
108
109 switch (chunk_id) {
110 case MIDX_CHUNKID_PACKNAMES:
111 m->chunk_pack_names = m->data + chunk_offset;
112 break;
113
d7cacf29
DS
114 case MIDX_CHUNKID_OIDFANOUT:
115 m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset);
116 break;
117
0d5b3a5e
DS
118 case MIDX_CHUNKID_OIDLOOKUP:
119 m->chunk_oid_lookup = m->data + chunk_offset;
120 break;
121
662148c4
DS
122 case MIDX_CHUNKID_OBJECTOFFSETS:
123 m->chunk_object_offsets = m->data + chunk_offset;
124 break;
125
126 case MIDX_CHUNKID_LARGEOFFSETS:
127 m->chunk_large_offsets = m->data + chunk_offset;
128 break;
129
32f3c541
DS
130 case 0:
131 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
132 break;
133
134 default:
135 /*
136 * Do nothing on unrecognized chunks, allowing future
137 * extensions to add optional chunks.
138 */
139 break;
140 }
141 }
142
143 if (!m->chunk_pack_names)
144 die(_("multi-pack-index missing required pack-name chunk"));
d7cacf29
DS
145 if (!m->chunk_oid_fanout)
146 die(_("multi-pack-index missing required OID fanout chunk"));
0d5b3a5e
DS
147 if (!m->chunk_oid_lookup)
148 die(_("multi-pack-index missing required OID lookup chunk"));
662148c4
DS
149 if (!m->chunk_object_offsets)
150 die(_("multi-pack-index missing required object offsets chunk"));
32f3c541 151
d7cacf29
DS
152 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
153
3227565c 154 m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
3715a633 155 m->packs = xcalloc(m->num_packs, sizeof(*m->packs));
3227565c
DS
156
157 cur_pack_name = (const char *)m->chunk_pack_names;
158 for (i = 0; i < m->num_packs; i++) {
159 m->pack_names[i] = cur_pack_name;
160
161 cur_pack_name += strlen(cur_pack_name) + 1;
162
163 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0) {
164 error(_("multi-pack-index pack names out of order: '%s' before '%s'"),
165 m->pack_names[i - 1],
166 m->pack_names[i]);
167 goto cleanup_fail;
168 }
169 }
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
a40498a1
DS
183static void close_midx(struct multi_pack_index *m)
184{
185 uint32_t i;
186 munmap((unsigned char *)m->data, m->data_len);
187 close(m->fd);
188 m->fd = -1;
189
190 for (i = 0; i < m->num_packs; i++) {
191 if (m->packs[i]) {
192 close_pack(m->packs[i]);
193 free(m->packs);
194 }
195 }
196 FREE_AND_NULL(m->packs);
197 FREE_AND_NULL(m->pack_names);
198}
199
0bff5269 200int prepare_midx_pack(struct multi_pack_index *m, uint32_t pack_int_id)
3715a633
DS
201{
202 struct strbuf pack_name = STRBUF_INIT;
203
204 if (pack_int_id >= m->num_packs)
205 BUG("bad pack-int-id");
206
207 if (m->packs[pack_int_id])
208 return 0;
209
210 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
211 m->pack_names[pack_int_id]);
212
2cf489a3 213 m->packs[pack_int_id] = add_packed_git(pack_name.buf, pack_name.len, m->local);
3715a633
DS
214 strbuf_release(&pack_name);
215 return !m->packs[pack_int_id];
216}
217
218int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
219{
220 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
221 MIDX_HASH_LEN, result);
222}
223
8aac67a1
DS
224struct object_id *nth_midxed_object_oid(struct object_id *oid,
225 struct multi_pack_index *m,
226 uint32_t n)
227{
228 if (n >= m->num_objects)
229 return NULL;
230
231 hashcpy(oid->hash, m->chunk_oid_lookup + m->hash_len * n);
232 return oid;
233}
234
3715a633
DS
235static off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
236{
237 const unsigned char *offset_data;
238 uint32_t offset32;
239
240 offset_data = m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH;
241 offset32 = get_be32(offset_data + sizeof(uint32_t));
242
243 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
244 if (sizeof(offset32) < sizeof(uint64_t))
245 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
246
247 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
248 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
249 }
250
251 return offset32;
252}
253
254static uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
255{
256 return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH);
257}
258
259static int nth_midxed_pack_entry(struct multi_pack_index *m, struct pack_entry *e, uint32_t pos)
260{
261 uint32_t pack_int_id;
262 struct packed_git *p;
263
264 if (pos >= m->num_objects)
265 return 0;
266
267 pack_int_id = nth_midxed_pack_int_id(m, pos);
268
269 if (prepare_midx_pack(m, pack_int_id))
270 die(_("error preparing packfile from multi-pack-index"));
271 p = m->packs[pack_int_id];
272
273 /*
274 * We are about to tell the caller where they can locate the
275 * requested object. We better make sure the packfile is
276 * still here and can be accessed before supplying that
277 * answer, as it may have been deleted since the MIDX was
278 * loaded!
279 */
280 if (!is_pack_valid(p))
281 return 0;
282
c39b02ae
DS
283 if (p->num_bad_objects) {
284 uint32_t i;
285 struct object_id oid;
286 nth_midxed_object_oid(&oid, m, pos);
287 for (i = 0; i < p->num_bad_objects; i++)
288 if (!hashcmp(oid.hash,
289 p->bad_object_sha1 + the_hash_algo->rawsz * i))
290 return 0;
291 }
292
3715a633
DS
293 e->offset = nth_midxed_offset(m, pos);
294 e->p = p;
295
296 return 1;
297}
298
299int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct multi_pack_index *m)
300{
301 uint32_t pos;
302
303 if (!bsearch_midx(oid, m, &pos))
304 return 0;
305
306 return nth_midxed_pack_entry(m, e, pos);
307}
308
a40498a1
DS
309int midx_contains_pack(struct multi_pack_index *m, const char *idx_name)
310{
311 uint32_t first = 0, last = m->num_packs;
312
313 while (first < last) {
314 uint32_t mid = first + (last - first) / 2;
315 const char *current;
316 int cmp;
317
318 current = m->pack_names[mid];
319 cmp = strcmp(idx_name, current);
320 if (!cmp)
321 return 1;
322 if (cmp > 0) {
323 first = mid + 1;
324 continue;
325 }
326 last = mid;
327 }
328
329 return 0;
330}
331
2cf489a3 332int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
c4d25228 333{
29e2016b 334 struct multi_pack_index *m;
c4d25228
DS
335 struct multi_pack_index *m_search;
336 int config_value;
337
338 if (repo_config_get_bool(r, "core.multipackindex", &config_value) ||
339 !config_value)
340 return 0;
341
29e2016b 342 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
c4d25228
DS
343 if (!strcmp(object_dir, m_search->object_dir))
344 return 1;
345
29e2016b 346 m = load_multi_pack_index(object_dir, local);
c4d25228 347
29e2016b
DS
348 if (m) {
349 m->next = r->objects->multi_pack_index;
350 r->objects->multi_pack_index = m;
c4d25228
DS
351 return 1;
352 }
353
354 return 0;
355}
356
fc59e748
DS
357static size_t write_midx_header(struct hashfile *f,
358 unsigned char num_chunks,
359 uint32_t num_packs)
360{
361 unsigned char byte_values[4];
362
363 hashwrite_be32(f, MIDX_SIGNATURE);
364 byte_values[0] = MIDX_VERSION;
365 byte_values[1] = MIDX_HASH_VERSION;
366 byte_values[2] = num_chunks;
367 byte_values[3] = 0; /* unused */
368 hashwrite(f, byte_values, sizeof(byte_values));
369 hashwrite_be32(f, num_packs);
370
371 return MIDX_HEADER_SIZE;
372}
373
396f2570
DS
374struct pack_list {
375 struct packed_git **list;
32f3c541 376 char **names;
396f2570
DS
377 uint32_t nr;
378 uint32_t alloc_list;
32f3c541
DS
379 uint32_t alloc_names;
380 size_t pack_name_concat_len;
a40498a1 381 struct multi_pack_index *m;
396f2570
DS
382};
383
384static void add_pack_to_midx(const char *full_path, size_t full_path_len,
385 const char *file_name, void *data)
386{
387 struct pack_list *packs = (struct pack_list *)data;
388
389 if (ends_with(file_name, ".idx")) {
a40498a1
DS
390 if (packs->m && midx_contains_pack(packs->m, file_name))
391 return;
392
396f2570 393 ALLOC_GROW(packs->list, packs->nr + 1, packs->alloc_list);
32f3c541 394 ALLOC_GROW(packs->names, packs->nr + 1, packs->alloc_names);
396f2570
DS
395
396 packs->list[packs->nr] = add_packed_git(full_path,
397 full_path_len,
398 0);
fe1ed56f 399
396f2570
DS
400 if (!packs->list[packs->nr]) {
401 warning(_("failed to add packfile '%s'"),
402 full_path);
403 return;
404 }
405
fe1ed56f
DS
406 if (open_pack_index(packs->list[packs->nr])) {
407 warning(_("failed to open pack-index '%s'"),
408 full_path);
409 close_pack(packs->list[packs->nr]);
410 FREE_AND_NULL(packs->list[packs->nr]);
411 return;
412 }
413
32f3c541
DS
414 packs->names[packs->nr] = xstrdup(file_name);
415 packs->pack_name_concat_len += strlen(file_name) + 1;
396f2570
DS
416 packs->nr++;
417 }
418}
419
32f3c541
DS
420struct pack_pair {
421 uint32_t pack_int_id;
422 char *pack_name;
423};
424
425static int pack_pair_compare(const void *_a, const void *_b)
426{
427 struct pack_pair *a = (struct pack_pair *)_a;
428 struct pack_pair *b = (struct pack_pair *)_b;
429 return strcmp(a->pack_name, b->pack_name);
430}
431
432static void sort_packs_by_name(char **pack_names, uint32_t nr_packs, uint32_t *perm)
433{
434 uint32_t i;
435 struct pack_pair *pairs;
436
437 ALLOC_ARRAY(pairs, nr_packs);
438
439 for (i = 0; i < nr_packs; i++) {
440 pairs[i].pack_int_id = i;
441 pairs[i].pack_name = pack_names[i];
442 }
443
444 QSORT(pairs, nr_packs, pack_pair_compare);
445
446 for (i = 0; i < nr_packs; i++) {
447 pack_names[i] = pairs[i].pack_name;
448 perm[pairs[i].pack_int_id] = i;
449 }
450
451 free(pairs);
452}
453
fe1ed56f
DS
454struct pack_midx_entry {
455 struct object_id oid;
456 uint32_t pack_int_id;
457 time_t pack_mtime;
458 uint64_t offset;
459};
460
461static int midx_oid_compare(const void *_a, const void *_b)
462{
463 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
464 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
465 int cmp = oidcmp(&a->oid, &b->oid);
466
467 if (cmp)
468 return cmp;
469
470 if (a->pack_mtime > b->pack_mtime)
471 return -1;
472 else if (a->pack_mtime < b->pack_mtime)
473 return 1;
474
475 return a->pack_int_id - b->pack_int_id;
476}
477
a40498a1
DS
478static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
479 uint32_t *pack_perm,
480 struct pack_midx_entry *e,
481 uint32_t pos)
482{
483 if (pos >= m->num_objects)
484 return 1;
485
486 nth_midxed_object_oid(&e->oid, m, pos);
487 e->pack_int_id = pack_perm[nth_midxed_pack_int_id(m, pos)];
488 e->offset = nth_midxed_offset(m, pos);
489
490 /* consider objects in midx to be from "old" packs */
491 e->pack_mtime = 0;
492 return 0;
493}
494
fe1ed56f
DS
495static void fill_pack_entry(uint32_t pack_int_id,
496 struct packed_git *p,
497 uint32_t cur_object,
498 struct pack_midx_entry *entry)
499{
500 if (!nth_packed_object_oid(&entry->oid, p, cur_object))
501 die(_("failed to locate object %d in packfile"), cur_object);
502
503 entry->pack_int_id = pack_int_id;
504 entry->pack_mtime = p->mtime;
505
506 entry->offset = nth_packed_object_offset(p, cur_object);
507}
508
509/*
510 * It is possible to artificially get into a state where there are many
511 * duplicate copies of objects. That can create high memory pressure if
512 * we are to create a list of all objects before de-duplication. To reduce
513 * this memory pressure without a significant performance drop, automatically
514 * group objects by the first byte of their object id. Use the IDX fanout
515 * tables to group the data, copy to a local array, then sort.
516 *
517 * Copy only the de-duplicated entries (selected by most-recent modified time
518 * of a packfile containing the object).
519 */
a40498a1
DS
520static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
521 struct packed_git **p,
fe1ed56f
DS
522 uint32_t *perm,
523 uint32_t nr_packs,
524 uint32_t *nr_objects)
525{
526 uint32_t cur_fanout, cur_pack, cur_object;
527 uint32_t alloc_fanout, alloc_objects, total_objects = 0;
528 struct pack_midx_entry *entries_by_fanout = NULL;
529 struct pack_midx_entry *deduplicated_entries = NULL;
a40498a1 530 uint32_t start_pack = m ? m->num_packs : 0;
fe1ed56f 531
a40498a1 532 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
fe1ed56f
DS
533 total_objects += p[cur_pack]->num_objects;
534
535 /*
536 * As we de-duplicate by fanout value, we expect the fanout
537 * slices to be evenly distributed, with some noise. Hence,
538 * allocate slightly more than one 256th.
539 */
540 alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16;
541
542 ALLOC_ARRAY(entries_by_fanout, alloc_fanout);
543 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
544 *nr_objects = 0;
545
546 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
547 uint32_t nr_fanout = 0;
548
a40498a1
DS
549 if (m) {
550 uint32_t start = 0, end;
551
552 if (cur_fanout)
553 start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
554 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
555
556 for (cur_object = start; cur_object < end; cur_object++) {
557 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
558 nth_midxed_pack_midx_entry(m, perm,
559 &entries_by_fanout[nr_fanout],
560 cur_object);
561 nr_fanout++;
562 }
563 }
564
565 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
fe1ed56f
DS
566 uint32_t start = 0, end;
567
568 if (cur_fanout)
569 start = get_pack_fanout(p[cur_pack], cur_fanout - 1);
570 end = get_pack_fanout(p[cur_pack], cur_fanout);
571
572 for (cur_object = start; cur_object < end; cur_object++) {
573 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
574 fill_pack_entry(perm[cur_pack], p[cur_pack], cur_object, &entries_by_fanout[nr_fanout]);
575 nr_fanout++;
576 }
577 }
578
579 QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
580
581 /*
582 * The batch is now sorted by OID and then mtime (descending).
583 * Take only the first duplicate.
584 */
585 for (cur_object = 0; cur_object < nr_fanout; cur_object++) {
586 if (cur_object && !oidcmp(&entries_by_fanout[cur_object - 1].oid,
587 &entries_by_fanout[cur_object].oid))
588 continue;
589
590 ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
591 memcpy(&deduplicated_entries[*nr_objects],
592 &entries_by_fanout[cur_object],
593 sizeof(struct pack_midx_entry));
594 (*nr_objects)++;
595 }
596 }
597
598 free(entries_by_fanout);
599 return deduplicated_entries;
600}
601
32f3c541
DS
602static size_t write_midx_pack_names(struct hashfile *f,
603 char **pack_names,
604 uint32_t num_packs)
605{
606 uint32_t i;
607 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
608 size_t written = 0;
609
610 for (i = 0; i < num_packs; i++) {
611 size_t writelen = strlen(pack_names[i]) + 1;
612
613 if (i && strcmp(pack_names[i], pack_names[i - 1]) <= 0)
614 BUG("incorrect pack-file order: %s before %s",
615 pack_names[i - 1],
616 pack_names[i]);
617
618 hashwrite(f, pack_names[i], writelen);
619 written += writelen;
620 }
621
622 /* add padding to be aligned */
623 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
624 if (i < MIDX_CHUNK_ALIGNMENT) {
625 memset(padding, 0, sizeof(padding));
626 hashwrite(f, padding, i);
627 written += i;
628 }
629
630 return written;
631}
632
d7cacf29
DS
633static size_t write_midx_oid_fanout(struct hashfile *f,
634 struct pack_midx_entry *objects,
635 uint32_t nr_objects)
636{
637 struct pack_midx_entry *list = objects;
638 struct pack_midx_entry *last = objects + nr_objects;
639 uint32_t count = 0;
640 uint32_t i;
641
642 /*
643 * Write the first-level table (the list is sorted,
644 * but we use a 256-entry lookup to be able to avoid
645 * having to do eight extra binary search iterations).
646 */
647 for (i = 0; i < 256; i++) {
648 struct pack_midx_entry *next = list;
649
650 while (next < last && next->oid.hash[0] == i) {
651 count++;
652 next++;
653 }
654
655 hashwrite_be32(f, count);
656 list = next;
657 }
658
659 return MIDX_CHUNK_FANOUT_SIZE;
660}
661
0d5b3a5e
DS
662static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len,
663 struct pack_midx_entry *objects,
664 uint32_t nr_objects)
665{
666 struct pack_midx_entry *list = objects;
667 uint32_t i;
668 size_t written = 0;
669
670 for (i = 0; i < nr_objects; i++) {
671 struct pack_midx_entry *obj = list++;
672
673 if (i < nr_objects - 1) {
674 struct pack_midx_entry *next = list;
675 if (oidcmp(&obj->oid, &next->oid) >= 0)
676 BUG("OIDs not in order: %s >= %s",
677 oid_to_hex(&obj->oid),
678 oid_to_hex(&next->oid));
679 }
680
681 hashwrite(f, obj->oid.hash, (int)hash_len);
682 written += hash_len;
683 }
684
685 return written;
686}
687
662148c4
DS
688static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed,
689 struct pack_midx_entry *objects, uint32_t nr_objects)
690{
691 struct pack_midx_entry *list = objects;
692 uint32_t i, nr_large_offset = 0;
693 size_t written = 0;
694
695 for (i = 0; i < nr_objects; i++) {
696 struct pack_midx_entry *obj = list++;
697
698 hashwrite_be32(f, obj->pack_int_id);
699
700 if (large_offset_needed && obj->offset >> 31)
701 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
702 else if (!large_offset_needed && obj->offset >> 32)
703 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
704 oid_to_hex(&obj->oid),
705 obj->offset);
706 else
707 hashwrite_be32(f, (uint32_t)obj->offset);
708
709 written += MIDX_CHUNK_OFFSET_WIDTH;
710 }
711
712 return written;
713}
714
715static size_t write_midx_large_offsets(struct hashfile *f, uint32_t nr_large_offset,
716 struct pack_midx_entry *objects, uint32_t nr_objects)
717{
718 struct pack_midx_entry *list = objects;
719 size_t written = 0;
720
721 while (nr_large_offset) {
722 struct pack_midx_entry *obj = list++;
723 uint64_t offset = obj->offset;
724
725 if (!(offset >> 31))
726 continue;
727
728 hashwrite_be32(f, offset >> 32);
729 hashwrite_be32(f, offset & 0xffffffffUL);
730 written += 2 * sizeof(uint32_t);
731
732 nr_large_offset--;
733 }
734
735 return written;
736}
737
a3407730
DS
738int write_midx_file(const char *object_dir)
739{
32f3c541 740 unsigned char cur_chunk, num_chunks = 0;
fc59e748 741 char *midx_name;
396f2570 742 uint32_t i;
fc59e748
DS
743 struct hashfile *f = NULL;
744 struct lock_file lk;
396f2570 745 struct pack_list packs;
32f3c541
DS
746 uint32_t *pack_perm = NULL;
747 uint64_t written = 0;
748 uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
749 uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
662148c4 750 uint32_t nr_entries, num_large_offsets = 0;
fe1ed56f 751 struct pack_midx_entry *entries = NULL;
662148c4 752 int large_offsets_needed = 0;
fc59e748
DS
753
754 midx_name = get_midx_filename(object_dir);
755 if (safe_create_leading_directories(midx_name)) {
756 UNLEAK(midx_name);
757 die_errno(_("unable to create leading directories of %s"),
758 midx_name);
759 }
760
2cf489a3 761 packs.m = load_multi_pack_index(object_dir, 1);
a40498a1 762
396f2570 763 packs.nr = 0;
a40498a1
DS
764 packs.alloc_list = packs.m ? packs.m->num_packs : 16;
765 packs.alloc_names = packs.alloc_list;
396f2570 766 packs.list = NULL;
a40498a1 767 packs.names = NULL;
32f3c541 768 packs.pack_name_concat_len = 0;
396f2570 769 ALLOC_ARRAY(packs.list, packs.alloc_list);
32f3c541 770 ALLOC_ARRAY(packs.names, packs.alloc_names);
396f2570 771
a40498a1
DS
772 if (packs.m) {
773 for (i = 0; i < packs.m->num_packs; i++) {
774 ALLOC_GROW(packs.list, packs.nr + 1, packs.alloc_list);
775 ALLOC_GROW(packs.names, packs.nr + 1, packs.alloc_names);
776
777 packs.list[packs.nr] = NULL;
778 packs.names[packs.nr] = xstrdup(packs.m->pack_names[i]);
779 packs.pack_name_concat_len += strlen(packs.names[packs.nr]) + 1;
780 packs.nr++;
781 }
782 }
783
396f2570
DS
784 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
785
a40498a1
DS
786 if (packs.m && packs.nr == packs.m->num_packs)
787 goto cleanup;
788
32f3c541
DS
789 if (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
790 packs.pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
791 (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
792
793 ALLOC_ARRAY(pack_perm, packs.nr);
794 sort_packs_by_name(packs.names, packs.nr, pack_perm);
795
a40498a1
DS
796 entries = get_sorted_entries(packs.m, packs.list, pack_perm, packs.nr, &nr_entries);
797
662148c4
DS
798 for (i = 0; i < nr_entries; i++) {
799 if (entries[i].offset > 0x7fffffff)
800 num_large_offsets++;
801 if (entries[i].offset > 0xffffffff)
802 large_offsets_needed = 1;
803 }
fe1ed56f 804
fc59e748
DS
805 hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
806 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
807 FREE_AND_NULL(midx_name);
808
a40498a1
DS
809 if (packs.m)
810 close_midx(packs.m);
811
32f3c541 812 cur_chunk = 0;
662148c4 813 num_chunks = large_offsets_needed ? 5 : 4;
32f3c541
DS
814
815 written = write_midx_header(f, num_chunks, packs.nr);
816
817 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
818 chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
819
820 cur_chunk++;
d7cacf29 821 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
32f3c541
DS
822 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + packs.pack_name_concat_len;
823
d7cacf29
DS
824 cur_chunk++;
825 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;
826 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE;
827
0d5b3a5e 828 cur_chunk++;
662148c4 829 chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS;
0d5b3a5e
DS
830 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_HASH_LEN;
831
662148c4
DS
832 cur_chunk++;
833 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_CHUNK_OFFSET_WIDTH;
834 if (large_offsets_needed) {
835 chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS;
836
837 cur_chunk++;
838 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] +
839 num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
840 }
841
842 chunk_ids[cur_chunk] = 0;
843
32f3c541
DS
844 for (i = 0; i <= num_chunks; i++) {
845 if (i && chunk_offsets[i] < chunk_offsets[i - 1])
846 BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64,
847 chunk_offsets[i - 1],
848 chunk_offsets[i]);
849
850 if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT)
851 BUG("chunk offset %"PRIu64" is not properly aligned",
852 chunk_offsets[i]);
853
854 hashwrite_be32(f, chunk_ids[i]);
855 hashwrite_be32(f, chunk_offsets[i] >> 32);
856 hashwrite_be32(f, chunk_offsets[i]);
857
858 written += MIDX_CHUNKLOOKUP_WIDTH;
859 }
860
861 for (i = 0; i < num_chunks; i++) {
862 if (written != chunk_offsets[i])
863 BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32,
864 chunk_offsets[i],
865 written,
866 chunk_ids[i]);
867
868 switch (chunk_ids[i]) {
869 case MIDX_CHUNKID_PACKNAMES:
870 written += write_midx_pack_names(f, packs.names, packs.nr);
871 break;
872
d7cacf29
DS
873 case MIDX_CHUNKID_OIDFANOUT:
874 written += write_midx_oid_fanout(f, entries, nr_entries);
875 break;
876
0d5b3a5e
DS
877 case MIDX_CHUNKID_OIDLOOKUP:
878 written += write_midx_oid_lookup(f, MIDX_HASH_LEN, entries, nr_entries);
879 break;
880
662148c4
DS
881 case MIDX_CHUNKID_OBJECTOFFSETS:
882 written += write_midx_object_offsets(f, large_offsets_needed, entries, nr_entries);
883 break;
884
885 case MIDX_CHUNKID_LARGEOFFSETS:
886 written += write_midx_large_offsets(f, num_large_offsets, entries, nr_entries);
887 break;
888
32f3c541
DS
889 default:
890 BUG("trying to write unknown chunk id %"PRIx32,
891 chunk_ids[i]);
892 }
893 }
894
895 if (written != chunk_offsets[num_chunks])
896 BUG("incorrect final offset %"PRIu64" != %"PRIu64,
897 written,
898 chunk_offsets[num_chunks]);
fc59e748
DS
899
900 finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
901 commit_lock_file(&lk);
902
a40498a1 903cleanup:
396f2570
DS
904 for (i = 0; i < packs.nr; i++) {
905 if (packs.list[i]) {
906 close_pack(packs.list[i]);
907 free(packs.list[i]);
908 }
32f3c541 909 free(packs.names[i]);
396f2570
DS
910 }
911
912 free(packs.list);
32f3c541 913 free(packs.names);
fe1ed56f 914 free(entries);
a40498a1
DS
915 free(pack_perm);
916 free(midx_name);
a3407730
DS
917 return 0;
918}
525e18c0
DS
919
920void clear_midx_file(const char *object_dir)
921{
922 char *midx = get_midx_filename(object_dir);
923
924 if (remove_path(midx)) {
925 UNLEAK(midx);
926 die(_("failed to clear multi-pack-index at %s"), midx);
927 }
928
929 free(midx);
930}