1#include "cache.h"
2#include "config.h"
3#include "csum-file.h"
4#include "dir.h"
5#include "lockfile.h"
6#include "packfile.h"
7#include "object-store.h"
8#include "sha1-lookup.h"
9#include "midx.h"
10
11#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
12#define MIDX_VERSION 1
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
17#define MIDX_HASH_VERSION 1
18#define MIDX_HEADER_SIZE 12
19#define MIDX_HASH_LEN 20
20#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + MIDX_HASH_LEN)
21
22#define MIDX_MAX_CHUNKS 5
23#define MIDX_CHUNK_ALIGNMENT 4
24#define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
25#define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
26#define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
27#define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
28#define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
29#define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
30#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
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
34
35static char *get_midx_filename(const char *object_dir)
36{
37 return xstrfmt("%s/pack/multi-pack-index", object_dir);
38}
39
40struct multi_pack_index *load_multi_pack_index(const char *object_dir)
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);
49 uint32_t i;
50 const char *cur_pack_name;
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;
76
77 m->signature = get_be32(m->data);
78 if (m->signature != MIDX_SIGNATURE) {
79 error(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
80 m->signature, MIDX_SIGNATURE);
81 goto cleanup_fail;
82 }
83
84 m->version = m->data[MIDX_BYTE_FILE_VERSION];
85 if (m->version != MIDX_VERSION) {
86 error(_("multi-pack-index version %d not recognized"),
87 m->version);
88 goto cleanup_fail;
89 }
90
91 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
92 if (hash_version != MIDX_HASH_VERSION) {
93 error(_("hash version %u does not match"), hash_version);
94 goto cleanup_fail;
95 }
96 m->hash_len = MIDX_HASH_LEN;
97
98 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
99
100 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
101
102 for (i = 0; i < m->num_chunks; i++) {
103 uint32_t chunk_id = get_be32(m->data + MIDX_HEADER_SIZE +
104 MIDX_CHUNKLOOKUP_WIDTH * i);
105 uint64_t chunk_offset = get_be64(m->data + MIDX_HEADER_SIZE + 4 +
106 MIDX_CHUNKLOOKUP_WIDTH * i);
107
108 switch (chunk_id) {
109 case MIDX_CHUNKID_PACKNAMES:
110 m->chunk_pack_names = m->data + chunk_offset;
111 break;
112
113 case MIDX_CHUNKID_OIDFANOUT:
114 m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset);
115 break;
116
117 case MIDX_CHUNKID_OIDLOOKUP:
118 m->chunk_oid_lookup = m->data + chunk_offset;
119 break;
120
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
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"));
144 if (!m->chunk_oid_fanout)
145 die(_("multi-pack-index missing required OID fanout chunk"));
146 if (!m->chunk_oid_lookup)
147 die(_("multi-pack-index missing required OID lookup chunk"));
148 if (!m->chunk_object_offsets)
149 die(_("multi-pack-index missing required object offsets chunk"));
150
151 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
152
153 m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
154 m->packs = xcalloc(m->num_packs, sizeof(*m->packs));
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
162 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0) {
163 error(_("multi-pack-index pack names out of order: '%s' before '%s'"),
164 m->pack_names[i - 1],
165 m->pack_names[i]);
166 goto cleanup_fail;
167 }
168 }
169
170 return m;
171
172cleanup_fail:
173 free(m);
174 free(midx_name);
175 if (midx_map)
176 munmap(midx_map, midx_size);
177 if (0 <= fd)
178 close(fd);
179 return NULL;
180}
181
182static int prepare_midx_pack(struct multi_pack_index *m, uint32_t pack_int_id)
183{
184 struct strbuf pack_name = STRBUF_INIT;
185
186 if (pack_int_id >= m->num_packs)
187 BUG("bad pack-int-id");
188
189 if (m->packs[pack_int_id])
190 return 0;
191
192 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
193 m->pack_names[pack_int_id]);
194
195 m->packs[pack_int_id] = add_packed_git(pack_name.buf, pack_name.len, 1);
196 strbuf_release(&pack_name);
197 return !m->packs[pack_int_id];
198}
199
200int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
201{
202 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
203 MIDX_HASH_LEN, result);
204}
205
206static off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
207{
208 const unsigned char *offset_data;
209 uint32_t offset32;
210
211 offset_data = m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH;
212 offset32 = get_be32(offset_data + sizeof(uint32_t));
213
214 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
215 if (sizeof(offset32) < sizeof(uint64_t))
216 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
217
218 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
219 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
220 }
221
222 return offset32;
223}
224
225static uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
226{
227 return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH);
228}
229
230static int nth_midxed_pack_entry(struct multi_pack_index *m, struct pack_entry *e, uint32_t pos)
231{
232 uint32_t pack_int_id;
233 struct packed_git *p;
234
235 if (pos >= m->num_objects)
236 return 0;
237
238 pack_int_id = nth_midxed_pack_int_id(m, pos);
239
240 if (prepare_midx_pack(m, pack_int_id))
241 die(_("error preparing packfile from multi-pack-index"));
242 p = m->packs[pack_int_id];
243
244 /*
245 * We are about to tell the caller where they can locate the
246 * requested object. We better make sure the packfile is
247 * still here and can be accessed before supplying that
248 * answer, as it may have been deleted since the MIDX was
249 * loaded!
250 */
251 if (!is_pack_valid(p))
252 return 0;
253
254 e->offset = nth_midxed_offset(m, pos);
255 e->p = p;
256
257 return 1;
258}
259
260int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct multi_pack_index *m)
261{
262 uint32_t pos;
263
264 if (!bsearch_midx(oid, m, &pos))
265 return 0;
266
267 return nth_midxed_pack_entry(m, e, pos);
268}
269
270int prepare_multi_pack_index_one(struct repository *r, const char *object_dir)
271{
272 struct multi_pack_index *m = r->objects->multi_pack_index;
273 struct multi_pack_index *m_search;
274 int config_value;
275
276 if (repo_config_get_bool(r, "core.multipackindex", &config_value) ||
277 !config_value)
278 return 0;
279
280 for (m_search = m; m_search; m_search = m_search->next)
281 if (!strcmp(object_dir, m_search->object_dir))
282 return 1;
283
284 r->objects->multi_pack_index = load_multi_pack_index(object_dir);
285
286 if (r->objects->multi_pack_index) {
287 r->objects->multi_pack_index->next = m;
288 return 1;
289 }
290
291 return 0;
292}
293
294static size_t write_midx_header(struct hashfile *f,
295 unsigned char num_chunks,
296 uint32_t num_packs)
297{
298 unsigned char byte_values[4];
299
300 hashwrite_be32(f, MIDX_SIGNATURE);
301 byte_values[0] = MIDX_VERSION;
302 byte_values[1] = MIDX_HASH_VERSION;
303 byte_values[2] = num_chunks;
304 byte_values[3] = 0; /* unused */
305 hashwrite(f, byte_values, sizeof(byte_values));
306 hashwrite_be32(f, num_packs);
307
308 return MIDX_HEADER_SIZE;
309}
310
311struct pack_list {
312 struct packed_git **list;
313 char **names;
314 uint32_t nr;
315 uint32_t alloc_list;
316 uint32_t alloc_names;
317 size_t pack_name_concat_len;
318};
319
320static void add_pack_to_midx(const char *full_path, size_t full_path_len,
321 const char *file_name, void *data)
322{
323 struct pack_list *packs = (struct pack_list *)data;
324
325 if (ends_with(file_name, ".idx")) {
326 ALLOC_GROW(packs->list, packs->nr + 1, packs->alloc_list);
327 ALLOC_GROW(packs->names, packs->nr + 1, packs->alloc_names);
328
329 packs->list[packs->nr] = add_packed_git(full_path,
330 full_path_len,
331 0);
332
333 if (!packs->list[packs->nr]) {
334 warning(_("failed to add packfile '%s'"),
335 full_path);
336 return;
337 }
338
339 if (open_pack_index(packs->list[packs->nr])) {
340 warning(_("failed to open pack-index '%s'"),
341 full_path);
342 close_pack(packs->list[packs->nr]);
343 FREE_AND_NULL(packs->list[packs->nr]);
344 return;
345 }
346
347 packs->names[packs->nr] = xstrdup(file_name);
348 packs->pack_name_concat_len += strlen(file_name) + 1;
349 packs->nr++;
350 }
351}
352
353struct pack_pair {
354 uint32_t pack_int_id;
355 char *pack_name;
356};
357
358static int pack_pair_compare(const void *_a, const void *_b)
359{
360 struct pack_pair *a = (struct pack_pair *)_a;
361 struct pack_pair *b = (struct pack_pair *)_b;
362 return strcmp(a->pack_name, b->pack_name);
363}
364
365static void sort_packs_by_name(char **pack_names, uint32_t nr_packs, uint32_t *perm)
366{
367 uint32_t i;
368 struct pack_pair *pairs;
369
370 ALLOC_ARRAY(pairs, nr_packs);
371
372 for (i = 0; i < nr_packs; i++) {
373 pairs[i].pack_int_id = i;
374 pairs[i].pack_name = pack_names[i];
375 }
376
377 QSORT(pairs, nr_packs, pack_pair_compare);
378
379 for (i = 0; i < nr_packs; i++) {
380 pack_names[i] = pairs[i].pack_name;
381 perm[pairs[i].pack_int_id] = i;
382 }
383
384 free(pairs);
385}
386
387struct pack_midx_entry {
388 struct object_id oid;
389 uint32_t pack_int_id;
390 time_t pack_mtime;
391 uint64_t offset;
392};
393
394static int midx_oid_compare(const void *_a, const void *_b)
395{
396 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
397 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
398 int cmp = oidcmp(&a->oid, &b->oid);
399
400 if (cmp)
401 return cmp;
402
403 if (a->pack_mtime > b->pack_mtime)
404 return -1;
405 else if (a->pack_mtime < b->pack_mtime)
406 return 1;
407
408 return a->pack_int_id - b->pack_int_id;
409}
410
411static void fill_pack_entry(uint32_t pack_int_id,
412 struct packed_git *p,
413 uint32_t cur_object,
414 struct pack_midx_entry *entry)
415{
416 if (!nth_packed_object_oid(&entry->oid, p, cur_object))
417 die(_("failed to locate object %d in packfile"), cur_object);
418
419 entry->pack_int_id = pack_int_id;
420 entry->pack_mtime = p->mtime;
421
422 entry->offset = nth_packed_object_offset(p, cur_object);
423}
424
425/*
426 * It is possible to artificially get into a state where there are many
427 * duplicate copies of objects. That can create high memory pressure if
428 * we are to create a list of all objects before de-duplication. To reduce
429 * this memory pressure without a significant performance drop, automatically
430 * group objects by the first byte of their object id. Use the IDX fanout
431 * tables to group the data, copy to a local array, then sort.
432 *
433 * Copy only the de-duplicated entries (selected by most-recent modified time
434 * of a packfile containing the object).
435 */
436static struct pack_midx_entry *get_sorted_entries(struct packed_git **p,
437 uint32_t *perm,
438 uint32_t nr_packs,
439 uint32_t *nr_objects)
440{
441 uint32_t cur_fanout, cur_pack, cur_object;
442 uint32_t alloc_fanout, alloc_objects, total_objects = 0;
443 struct pack_midx_entry *entries_by_fanout = NULL;
444 struct pack_midx_entry *deduplicated_entries = NULL;
445
446 for (cur_pack = 0; cur_pack < nr_packs; cur_pack++)
447 total_objects += p[cur_pack]->num_objects;
448
449 /*
450 * As we de-duplicate by fanout value, we expect the fanout
451 * slices to be evenly distributed, with some noise. Hence,
452 * allocate slightly more than one 256th.
453 */
454 alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16;
455
456 ALLOC_ARRAY(entries_by_fanout, alloc_fanout);
457 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
458 *nr_objects = 0;
459
460 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
461 uint32_t nr_fanout = 0;
462
463 for (cur_pack = 0; cur_pack < nr_packs; cur_pack++) {
464 uint32_t start = 0, end;
465
466 if (cur_fanout)
467 start = get_pack_fanout(p[cur_pack], cur_fanout - 1);
468 end = get_pack_fanout(p[cur_pack], cur_fanout);
469
470 for (cur_object = start; cur_object < end; cur_object++) {
471 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
472 fill_pack_entry(perm[cur_pack], p[cur_pack], cur_object, &entries_by_fanout[nr_fanout]);
473 nr_fanout++;
474 }
475 }
476
477 QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
478
479 /*
480 * The batch is now sorted by OID and then mtime (descending).
481 * Take only the first duplicate.
482 */
483 for (cur_object = 0; cur_object < nr_fanout; cur_object++) {
484 if (cur_object && !oidcmp(&entries_by_fanout[cur_object - 1].oid,
485 &entries_by_fanout[cur_object].oid))
486 continue;
487
488 ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
489 memcpy(&deduplicated_entries[*nr_objects],
490 &entries_by_fanout[cur_object],
491 sizeof(struct pack_midx_entry));
492 (*nr_objects)++;
493 }
494 }
495
496 free(entries_by_fanout);
497 return deduplicated_entries;
498}
499
500static size_t write_midx_pack_names(struct hashfile *f,
501 char **pack_names,
502 uint32_t num_packs)
503{
504 uint32_t i;
505 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
506 size_t written = 0;
507
508 for (i = 0; i < num_packs; i++) {
509 size_t writelen = strlen(pack_names[i]) + 1;
510
511 if (i && strcmp(pack_names[i], pack_names[i - 1]) <= 0)
512 BUG("incorrect pack-file order: %s before %s",
513 pack_names[i - 1],
514 pack_names[i]);
515
516 hashwrite(f, pack_names[i], writelen);
517 written += writelen;
518 }
519
520 /* add padding to be aligned */
521 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
522 if (i < MIDX_CHUNK_ALIGNMENT) {
523 memset(padding, 0, sizeof(padding));
524 hashwrite(f, padding, i);
525 written += i;
526 }
527
528 return written;
529}
530
531static size_t write_midx_oid_fanout(struct hashfile *f,
532 struct pack_midx_entry *objects,
533 uint32_t nr_objects)
534{
535 struct pack_midx_entry *list = objects;
536 struct pack_midx_entry *last = objects + nr_objects;
537 uint32_t count = 0;
538 uint32_t i;
539
540 /*
541 * Write the first-level table (the list is sorted,
542 * but we use a 256-entry lookup to be able to avoid
543 * having to do eight extra binary search iterations).
544 */
545 for (i = 0; i < 256; i++) {
546 struct pack_midx_entry *next = list;
547
548 while (next < last && next->oid.hash[0] == i) {
549 count++;
550 next++;
551 }
552
553 hashwrite_be32(f, count);
554 list = next;
555 }
556
557 return MIDX_CHUNK_FANOUT_SIZE;
558}
559
560static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len,
561 struct pack_midx_entry *objects,
562 uint32_t nr_objects)
563{
564 struct pack_midx_entry *list = objects;
565 uint32_t i;
566 size_t written = 0;
567
568 for (i = 0; i < nr_objects; i++) {
569 struct pack_midx_entry *obj = list++;
570
571 if (i < nr_objects - 1) {
572 struct pack_midx_entry *next = list;
573 if (oidcmp(&obj->oid, &next->oid) >= 0)
574 BUG("OIDs not in order: %s >= %s",
575 oid_to_hex(&obj->oid),
576 oid_to_hex(&next->oid));
577 }
578
579 hashwrite(f, obj->oid.hash, (int)hash_len);
580 written += hash_len;
581 }
582
583 return written;
584}
585
586static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed,
587 struct pack_midx_entry *objects, uint32_t nr_objects)
588{
589 struct pack_midx_entry *list = objects;
590 uint32_t i, nr_large_offset = 0;
591 size_t written = 0;
592
593 for (i = 0; i < nr_objects; i++) {
594 struct pack_midx_entry *obj = list++;
595
596 hashwrite_be32(f, obj->pack_int_id);
597
598 if (large_offset_needed && obj->offset >> 31)
599 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
600 else if (!large_offset_needed && obj->offset >> 32)
601 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
602 oid_to_hex(&obj->oid),
603 obj->offset);
604 else
605 hashwrite_be32(f, (uint32_t)obj->offset);
606
607 written += MIDX_CHUNK_OFFSET_WIDTH;
608 }
609
610 return written;
611}
612
613static size_t write_midx_large_offsets(struct hashfile *f, uint32_t nr_large_offset,
614 struct pack_midx_entry *objects, uint32_t nr_objects)
615{
616 struct pack_midx_entry *list = objects;
617 size_t written = 0;
618
619 while (nr_large_offset) {
620 struct pack_midx_entry *obj = list++;
621 uint64_t offset = obj->offset;
622
623 if (!(offset >> 31))
624 continue;
625
626 hashwrite_be32(f, offset >> 32);
627 hashwrite_be32(f, offset & 0xffffffffUL);
628 written += 2 * sizeof(uint32_t);
629
630 nr_large_offset--;
631 }
632
633 return written;
634}
635
636int write_midx_file(const char *object_dir)
637{
638 unsigned char cur_chunk, num_chunks = 0;
639 char *midx_name;
640 uint32_t i;
641 struct hashfile *f = NULL;
642 struct lock_file lk;
643 struct pack_list packs;
644 uint32_t *pack_perm = NULL;
645 uint64_t written = 0;
646 uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
647 uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
648 uint32_t nr_entries, num_large_offsets = 0;
649 struct pack_midx_entry *entries = NULL;
650 int large_offsets_needed = 0;
651
652 midx_name = get_midx_filename(object_dir);
653 if (safe_create_leading_directories(midx_name)) {
654 UNLEAK(midx_name);
655 die_errno(_("unable to create leading directories of %s"),
656 midx_name);
657 }
658
659 packs.nr = 0;
660 packs.alloc_list = 16;
661 packs.alloc_names = 16;
662 packs.list = NULL;
663 packs.pack_name_concat_len = 0;
664 ALLOC_ARRAY(packs.list, packs.alloc_list);
665 ALLOC_ARRAY(packs.names, packs.alloc_names);
666
667 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
668
669 if (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
670 packs.pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
671 (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
672
673 ALLOC_ARRAY(pack_perm, packs.nr);
674 sort_packs_by_name(packs.names, packs.nr, pack_perm);
675
676 entries = get_sorted_entries(packs.list, pack_perm, packs.nr, &nr_entries);
677 for (i = 0; i < nr_entries; i++) {
678 if (entries[i].offset > 0x7fffffff)
679 num_large_offsets++;
680 if (entries[i].offset > 0xffffffff)
681 large_offsets_needed = 1;
682 }
683
684 hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
685 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
686 FREE_AND_NULL(midx_name);
687
688 cur_chunk = 0;
689 num_chunks = large_offsets_needed ? 5 : 4;
690
691 written = write_midx_header(f, num_chunks, packs.nr);
692
693 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
694 chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
695
696 cur_chunk++;
697 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
698 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + packs.pack_name_concat_len;
699
700 cur_chunk++;
701 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;
702 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE;
703
704 cur_chunk++;
705 chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS;
706 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_HASH_LEN;
707
708 cur_chunk++;
709 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_CHUNK_OFFSET_WIDTH;
710 if (large_offsets_needed) {
711 chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS;
712
713 cur_chunk++;
714 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] +
715 num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
716 }
717
718 chunk_ids[cur_chunk] = 0;
719
720 for (i = 0; i <= num_chunks; i++) {
721 if (i && chunk_offsets[i] < chunk_offsets[i - 1])
722 BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64,
723 chunk_offsets[i - 1],
724 chunk_offsets[i]);
725
726 if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT)
727 BUG("chunk offset %"PRIu64" is not properly aligned",
728 chunk_offsets[i]);
729
730 hashwrite_be32(f, chunk_ids[i]);
731 hashwrite_be32(f, chunk_offsets[i] >> 32);
732 hashwrite_be32(f, chunk_offsets[i]);
733
734 written += MIDX_CHUNKLOOKUP_WIDTH;
735 }
736
737 for (i = 0; i < num_chunks; i++) {
738 if (written != chunk_offsets[i])
739 BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32,
740 chunk_offsets[i],
741 written,
742 chunk_ids[i]);
743
744 switch (chunk_ids[i]) {
745 case MIDX_CHUNKID_PACKNAMES:
746 written += write_midx_pack_names(f, packs.names, packs.nr);
747 break;
748
749 case MIDX_CHUNKID_OIDFANOUT:
750 written += write_midx_oid_fanout(f, entries, nr_entries);
751 break;
752
753 case MIDX_CHUNKID_OIDLOOKUP:
754 written += write_midx_oid_lookup(f, MIDX_HASH_LEN, entries, nr_entries);
755 break;
756
757 case MIDX_CHUNKID_OBJECTOFFSETS:
758 written += write_midx_object_offsets(f, large_offsets_needed, entries, nr_entries);
759 break;
760
761 case MIDX_CHUNKID_LARGEOFFSETS:
762 written += write_midx_large_offsets(f, num_large_offsets, entries, nr_entries);
763 break;
764
765 default:
766 BUG("trying to write unknown chunk id %"PRIx32,
767 chunk_ids[i]);
768 }
769 }
770
771 if (written != chunk_offsets[num_chunks])
772 BUG("incorrect final offset %"PRIu64" != %"PRIu64,
773 written,
774 chunk_offsets[num_chunks]);
775
776 finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
777 commit_lock_file(&lk);
778
779 for (i = 0; i < packs.nr; i++) {
780 if (packs.list[i]) {
781 close_pack(packs.list[i]);
782 free(packs.list[i]);
783 }
784 free(packs.names[i]);
785 }
786
787 free(packs.list);
788 free(packs.names);
789 free(entries);
790 return 0;
791}