git
/
midx.c
990 строк · 25.4 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "config.h"
5#include "dir.h"
6#include "hex.h"
7#include "packfile.h"
8#include "object-file.h"
9#include "hash-lookup.h"
10#include "midx.h"
11#include "progress.h"
12#include "trace2.h"
13#include "chunk-format.h"
14#include "pack-bitmap.h"
15#include "pack-revindex.h"
16
17int midx_checksum_valid(struct multi_pack_index *m);
18void clear_midx_files_ext(const char *object_dir, const char *ext,
19const char *keep_hash);
20void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
21char **keep_hashes,
22uint32_t hashes_nr);
23int cmp_idx_or_pack_name(const char *idx_or_pack_name,
24const char *idx_name);
25
26const unsigned char *get_midx_checksum(struct multi_pack_index *m)
27{
28return m->data + m->data_len - the_hash_algo->rawsz;
29}
30
31void get_midx_filename(struct strbuf *out, const char *object_dir)
32{
33get_midx_filename_ext(out, object_dir, NULL, NULL);
34}
35
36void get_midx_filename_ext(struct strbuf *out, const char *object_dir,
37const unsigned char *hash, const char *ext)
38{
39strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
40if (ext)
41strbuf_addf(out, "-%s.%s", hash_to_hex(hash), ext);
42}
43
44static int midx_read_oid_fanout(const unsigned char *chunk_start,
45size_t chunk_size, void *data)
46{
47int i;
48struct multi_pack_index *m = data;
49m->chunk_oid_fanout = (uint32_t *)chunk_start;
50
51if (chunk_size != 4 * 256) {
52error(_("multi-pack-index OID fanout is of the wrong size"));
53return 1;
54}
55for (i = 0; i < 255; i++) {
56uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
57uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i+1]);
58
59if (oid_fanout1 > oid_fanout2) {
60error(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
61i, oid_fanout1, oid_fanout2, i + 1);
62return 1;
63}
64}
65m->num_objects = ntohl(m->chunk_oid_fanout[255]);
66return 0;
67}
68
69static int midx_read_oid_lookup(const unsigned char *chunk_start,
70size_t chunk_size, void *data)
71{
72struct multi_pack_index *m = data;
73m->chunk_oid_lookup = chunk_start;
74
75if (chunk_size != st_mult(m->hash_len, m->num_objects)) {
76error(_("multi-pack-index OID lookup chunk is the wrong size"));
77return 1;
78}
79return 0;
80}
81
82static int midx_read_object_offsets(const unsigned char *chunk_start,
83size_t chunk_size, void *data)
84{
85struct multi_pack_index *m = data;
86m->chunk_object_offsets = chunk_start;
87
88if (chunk_size != st_mult(m->num_objects, MIDX_CHUNK_OFFSET_WIDTH)) {
89error(_("multi-pack-index object offset chunk is the wrong size"));
90return 1;
91}
92return 0;
93}
94
95#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
96
97static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir,
98const char *midx_name,
99int local)
100{
101struct multi_pack_index *m = NULL;
102int fd;
103struct stat st;
104size_t midx_size;
105void *midx_map = NULL;
106uint32_t hash_version;
107uint32_t i;
108const char *cur_pack_name;
109struct chunkfile *cf = NULL;
110
111fd = git_open(midx_name);
112
113if (fd < 0)
114goto cleanup_fail;
115if (fstat(fd, &st)) {
116error_errno(_("failed to read %s"), midx_name);
117goto cleanup_fail;
118}
119
120midx_size = xsize_t(st.st_size);
121
122if (midx_size < MIDX_MIN_SIZE) {
123error(_("multi-pack-index file %s is too small"), midx_name);
124goto cleanup_fail;
125}
126
127midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
128close(fd);
129
130FLEX_ALLOC_STR(m, object_dir, object_dir);
131m->data = midx_map;
132m->data_len = midx_size;
133m->local = local;
134
135m->signature = get_be32(m->data);
136if (m->signature != MIDX_SIGNATURE)
137die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
138m->signature, MIDX_SIGNATURE);
139
140m->version = m->data[MIDX_BYTE_FILE_VERSION];
141if (m->version != MIDX_VERSION)
142die(_("multi-pack-index version %d not recognized"),
143m->version);
144
145hash_version = m->data[MIDX_BYTE_HASH_VERSION];
146if (hash_version != oid_version(the_hash_algo)) {
147error(_("multi-pack-index hash version %u does not match version %u"),
148hash_version, oid_version(the_hash_algo));
149goto cleanup_fail;
150}
151m->hash_len = the_hash_algo->rawsz;
152
153m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
154
155m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
156
157m->preferred_pack_idx = -1;
158
159cf = init_chunkfile(NULL);
160
161if (read_table_of_contents(cf, m->data, midx_size,
162MIDX_HEADER_SIZE, m->num_chunks,
163MIDX_CHUNK_ALIGNMENT))
164goto cleanup_fail;
165
166if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names, &m->chunk_pack_names_len))
167die(_("multi-pack-index required pack-name chunk missing or corrupted"));
168if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m))
169die(_("multi-pack-index required OID fanout chunk missing or corrupted"));
170if (read_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, midx_read_oid_lookup, m))
171die(_("multi-pack-index required OID lookup chunk missing or corrupted"));
172if (read_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, midx_read_object_offsets, m))
173die(_("multi-pack-index required object offsets chunk missing or corrupted"));
174
175pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets,
176&m->chunk_large_offsets_len);
177if (git_env_bool("GIT_TEST_MIDX_READ_BTMP", 1))
178pair_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
179(const unsigned char **)&m->chunk_bitmapped_packs,
180&m->chunk_bitmapped_packs_len);
181
182if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
183pair_chunk(cf, MIDX_CHUNKID_REVINDEX, &m->chunk_revindex,
184&m->chunk_revindex_len);
185
186CALLOC_ARRAY(m->pack_names, m->num_packs);
187CALLOC_ARRAY(m->packs, m->num_packs);
188
189cur_pack_name = (const char *)m->chunk_pack_names;
190for (i = 0; i < m->num_packs; i++) {
191const char *end;
192size_t avail = m->chunk_pack_names_len -
193(cur_pack_name - (const char *)m->chunk_pack_names);
194
195m->pack_names[i] = cur_pack_name;
196
197end = memchr(cur_pack_name, '\0', avail);
198if (!end)
199die(_("multi-pack-index pack-name chunk is too short"));
200cur_pack_name = end + 1;
201
202if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
203die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
204m->pack_names[i - 1],
205m->pack_names[i]);
206}
207
208trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
209trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
210
211free_chunkfile(cf);
212return m;
213
214cleanup_fail:
215free(m);
216free_chunkfile(cf);
217if (midx_map)
218munmap(midx_map, midx_size);
219if (0 <= fd)
220close(fd);
221return NULL;
222}
223
224void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir)
225{
226strbuf_addf(buf, "%s/pack/multi-pack-index.d", object_dir);
227}
228
229void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
230{
231get_midx_chain_dirname(buf, object_dir);
232strbuf_addstr(buf, "/multi-pack-index-chain");
233}
234
235void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
236const unsigned char *hash, const char *ext)
237{
238get_midx_chain_dirname(buf, object_dir);
239strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
240}
241
242static int open_multi_pack_index_chain(const char *chain_file,
243int *fd, struct stat *st)
244{
245*fd = git_open(chain_file);
246if (*fd < 0)
247return 0;
248if (fstat(*fd, st)) {
249close(*fd);
250return 0;
251}
252if (st->st_size < the_hash_algo->hexsz) {
253close(*fd);
254if (!st->st_size) {
255/* treat empty files the same as missing */
256errno = ENOENT;
257} else {
258warning(_("multi-pack-index chain file too small"));
259errno = EINVAL;
260}
261return 0;
262}
263return 1;
264}
265
266static int add_midx_to_chain(struct multi_pack_index *midx,
267struct multi_pack_index *midx_chain)
268{
269if (midx_chain) {
270if (unsigned_add_overflows(midx_chain->num_packs,
271midx_chain->num_packs_in_base)) {
272warning(_("pack count in base MIDX too high: %"PRIuMAX),
273(uintmax_t)midx_chain->num_packs_in_base);
274return 0;
275}
276if (unsigned_add_overflows(midx_chain->num_objects,
277midx_chain->num_objects_in_base)) {
278warning(_("object count in base MIDX too high: %"PRIuMAX),
279(uintmax_t)midx_chain->num_objects_in_base);
280return 0;
281}
282midx->num_packs_in_base = midx_chain->num_packs +
283midx_chain->num_packs_in_base;
284midx->num_objects_in_base = midx_chain->num_objects +
285midx_chain->num_objects_in_base;
286}
287
288midx->base_midx = midx_chain;
289midx->has_chain = 1;
290
291return 1;
292}
293
294static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
295int local,
296int fd, struct stat *st,
297int *incomplete_chain)
298{
299struct multi_pack_index *midx_chain = NULL;
300struct strbuf buf = STRBUF_INIT;
301int valid = 1;
302uint32_t i, count;
303FILE *fp = xfdopen(fd, "r");
304
305count = st->st_size / (the_hash_algo->hexsz + 1);
306
307for (i = 0; i < count; i++) {
308struct multi_pack_index *m;
309struct object_id layer;
310
311if (strbuf_getline_lf(&buf, fp) == EOF)
312break;
313
314if (get_oid_hex(buf.buf, &layer)) {
315warning(_("invalid multi-pack-index chain: line '%s' "
316"not a hash"),
317buf.buf);
318valid = 0;
319break;
320}
321
322valid = 0;
323
324strbuf_reset(&buf);
325get_split_midx_filename_ext(&buf, object_dir, layer.hash,
326MIDX_EXT_MIDX);
327m = load_multi_pack_index_one(object_dir, buf.buf, local);
328
329if (m) {
330if (add_midx_to_chain(m, midx_chain)) {
331midx_chain = m;
332valid = 1;
333} else {
334close_midx(m);
335}
336}
337if (!valid) {
338warning(_("unable to find all multi-pack index files"));
339break;
340}
341}
342
343fclose(fp);
344strbuf_release(&buf);
345
346*incomplete_chain = !valid;
347return midx_chain;
348}
349
350static struct multi_pack_index *load_multi_pack_index_chain(const char *object_dir,
351int local)
352{
353struct strbuf chain_file = STRBUF_INIT;
354struct stat st;
355int fd;
356struct multi_pack_index *m = NULL;
357
358get_midx_chain_filename(&chain_file, object_dir);
359if (open_multi_pack_index_chain(chain_file.buf, &fd, &st)) {
360int incomplete;
361/* ownership of fd is taken over by load function */
362m = load_midx_chain_fd_st(object_dir, local, fd, &st,
363&incomplete);
364}
365
366strbuf_release(&chain_file);
367return m;
368}
369
370struct multi_pack_index *load_multi_pack_index(const char *object_dir,
371int local)
372{
373struct strbuf midx_name = STRBUF_INIT;
374struct multi_pack_index *m;
375
376get_midx_filename(&midx_name, object_dir);
377
378m = load_multi_pack_index_one(object_dir, midx_name.buf, local);
379if (!m)
380m = load_multi_pack_index_chain(object_dir, local);
381
382strbuf_release(&midx_name);
383
384return m;
385}
386
387void close_midx(struct multi_pack_index *m)
388{
389uint32_t i;
390
391if (!m)
392return;
393
394close_midx(m->next);
395close_midx(m->base_midx);
396
397munmap((unsigned char *)m->data, m->data_len);
398
399for (i = 0; i < m->num_packs; i++) {
400if (m->packs[i])
401m->packs[i]->multi_pack_index = 0;
402}
403FREE_AND_NULL(m->packs);
404FREE_AND_NULL(m->pack_names);
405free(m);
406}
407
408static uint32_t midx_for_object(struct multi_pack_index **_m, uint32_t pos)
409{
410struct multi_pack_index *m = *_m;
411while (m && pos < m->num_objects_in_base)
412m = m->base_midx;
413
414if (!m)
415BUG("NULL multi-pack-index for object position: %"PRIu32, pos);
416
417if (pos >= m->num_objects + m->num_objects_in_base)
418die(_("invalid MIDX object position, MIDX is likely corrupt"));
419
420*_m = m;
421
422return pos - m->num_objects_in_base;
423}
424
425static uint32_t midx_for_pack(struct multi_pack_index **_m,
426uint32_t pack_int_id)
427{
428struct multi_pack_index *m = *_m;
429while (m && pack_int_id < m->num_packs_in_base)
430m = m->base_midx;
431
432if (!m)
433BUG("NULL multi-pack-index for pack ID: %"PRIu32, pack_int_id);
434
435if (pack_int_id >= m->num_packs + m->num_packs_in_base)
436die(_("bad pack-int-id: %u (%u total packs)"),
437pack_int_id, m->num_packs + m->num_packs_in_base);
438
439*_m = m;
440
441return pack_int_id - m->num_packs_in_base;
442}
443
444int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
445uint32_t pack_int_id)
446{
447struct strbuf pack_name = STRBUF_INIT;
448struct packed_git *p;
449
450pack_int_id = midx_for_pack(&m, pack_int_id);
451
452if (m->packs[pack_int_id])
453return 0;
454
455strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
456m->pack_names[pack_int_id]);
457
458p = add_packed_git(pack_name.buf, pack_name.len, m->local);
459strbuf_release(&pack_name);
460
461if (!p)
462return 1;
463
464p->multi_pack_index = 1;
465m->packs[pack_int_id] = p;
466install_packed_git(r, p);
467list_add_tail(&p->mru, &r->objects->packed_git_mru);
468
469return 0;
470}
471
472struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
473uint32_t pack_int_id)
474{
475uint32_t local_pack_int_id = midx_for_pack(&m, pack_int_id);
476return m->packs[local_pack_int_id];
477}
478
479#define MIDX_CHUNK_BITMAPPED_PACKS_WIDTH (2 * sizeof(uint32_t))
480
481int nth_bitmapped_pack(struct repository *r, struct multi_pack_index *m,
482struct bitmapped_pack *bp, uint32_t pack_int_id)
483{
484uint32_t local_pack_int_id = midx_for_pack(&m, pack_int_id);
485
486if (!m->chunk_bitmapped_packs)
487return error(_("MIDX does not contain the BTMP chunk"));
488
489if (prepare_midx_pack(r, m, pack_int_id))
490return error(_("could not load bitmapped pack %"PRIu32), pack_int_id);
491
492bp->p = m->packs[local_pack_int_id];
493bp->bitmap_pos = get_be32((char *)m->chunk_bitmapped_packs +
494MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * local_pack_int_id);
495bp->bitmap_nr = get_be32((char *)m->chunk_bitmapped_packs +
496MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * local_pack_int_id +
497sizeof(uint32_t));
498bp->pack_int_id = pack_int_id;
499
500return 0;
501}
502
503int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
504uint32_t *result)
505{
506int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
507m->chunk_oid_lookup, the_hash_algo->rawsz,
508result);
509if (result)
510*result += m->num_objects_in_base;
511return ret;
512}
513
514int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m,
515uint32_t *result)
516{
517for (; m; m = m->base_midx)
518if (bsearch_one_midx(oid, m, result))
519return 1;
520return 0;
521}
522
523int midx_has_oid(struct multi_pack_index *m, const struct object_id *oid)
524{
525return bsearch_midx(oid, m, NULL);
526}
527
528struct object_id *nth_midxed_object_oid(struct object_id *oid,
529struct multi_pack_index *m,
530uint32_t n)
531{
532if (n >= m->num_objects + m->num_objects_in_base)
533return NULL;
534
535n = midx_for_object(&m, n);
536
537oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
538the_repository->hash_algo);
539return oid;
540}
541
542off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
543{
544const unsigned char *offset_data;
545uint32_t offset32;
546
547pos = midx_for_object(&m, pos);
548
549offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH;
550offset32 = get_be32(offset_data + sizeof(uint32_t));
551
552if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
553if (sizeof(off_t) < sizeof(uint64_t))
554die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
555
556offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
557if (offset32 >= m->chunk_large_offsets_len / sizeof(uint64_t))
558die(_("multi-pack-index large offset out of bounds"));
559return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
560}
561
562return offset32;
563}
564
565uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
566{
567pos = midx_for_object(&m, pos);
568
569return m->num_packs_in_base + get_be32(m->chunk_object_offsets +
570(off_t)pos * MIDX_CHUNK_OFFSET_WIDTH);
571}
572
573int fill_midx_entry(struct repository *r,
574const struct object_id *oid,
575struct pack_entry *e,
576struct multi_pack_index *m)
577{
578uint32_t pos;
579uint32_t pack_int_id;
580struct packed_git *p;
581
582if (!bsearch_midx(oid, m, &pos))
583return 0;
584
585midx_for_object(&m, pos);
586pack_int_id = nth_midxed_pack_int_id(m, pos);
587
588if (prepare_midx_pack(r, m, pack_int_id))
589return 0;
590p = m->packs[pack_int_id - m->num_packs_in_base];
591
592/*
593* We are about to tell the caller where they can locate the
594* requested object. We better make sure the packfile is
595* still here and can be accessed before supplying that
596* answer, as it may have been deleted since the MIDX was
597* loaded!
598*/
599if (!is_pack_valid(p))
600return 0;
601
602if (oidset_size(&p->bad_objects) &&
603oidset_contains(&p->bad_objects, oid))
604return 0;
605
606e->offset = nth_midxed_offset(m, pos);
607e->p = p;
608
609return 1;
610}
611
612/* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
613int cmp_idx_or_pack_name(const char *idx_or_pack_name,
614const char *idx_name)
615{
616/* Skip past any initial matching prefix. */
617while (*idx_name && *idx_name == *idx_or_pack_name) {
618idx_name++;
619idx_or_pack_name++;
620}
621
622/*
623* If we didn't match completely, we may have matched "pack-1234." and
624* be left with "idx" and "pack" respectively, which is also OK. We do
625* not have to check for "idx" and "idx", because that would have been
626* a complete match (and in that case these strcmps will be false, but
627* we'll correctly return 0 from the final strcmp() below.
628*
629* Technically this matches "fooidx" and "foopack", but we'd never have
630* such names in the first place.
631*/
632if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
633return 0;
634
635/*
636* This not only checks for a complete match, but also orders based on
637* the first non-identical character, which means our ordering will
638* match a raw strcmp(). That makes it OK to use this to binary search
639* a naively-sorted list.
640*/
641return strcmp(idx_or_pack_name, idx_name);
642}
643
644static int midx_contains_pack_1(struct multi_pack_index *m,
645const char *idx_or_pack_name)
646{
647uint32_t first = 0, last = m->num_packs;
648
649while (first < last) {
650uint32_t mid = first + (last - first) / 2;
651const char *current;
652int cmp;
653
654current = m->pack_names[mid];
655cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
656if (!cmp)
657return 1;
658if (cmp > 0) {
659first = mid + 1;
660continue;
661}
662last = mid;
663}
664
665return 0;
666}
667
668int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
669{
670for (; m; m = m->base_midx)
671if (midx_contains_pack_1(m, idx_or_pack_name))
672return 1;
673return 0;
674}
675
676int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id)
677{
678if (m->preferred_pack_idx == -1) {
679uint32_t midx_pos;
680if (load_midx_revindex(m) < 0) {
681m->preferred_pack_idx = -2;
682return -1;
683}
684
685midx_pos = pack_pos_to_midx(m, m->num_objects_in_base);
686
687m->preferred_pack_idx = nth_midxed_pack_int_id(m, midx_pos);
688
689} else if (m->preferred_pack_idx == -2)
690return -1; /* no revindex */
691
692*pack_int_id = m->preferred_pack_idx;
693return 0;
694}
695
696int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
697{
698struct multi_pack_index *m;
699struct multi_pack_index *m_search;
700
701prepare_repo_settings(r);
702if (!r->settings.core_multi_pack_index)
703return 0;
704
705for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
706if (!strcmp(object_dir, m_search->object_dir))
707return 1;
708
709m = load_multi_pack_index(object_dir, local);
710
711if (m) {
712struct multi_pack_index *mp = r->objects->multi_pack_index;
713if (mp) {
714m->next = mp->next;
715mp->next = m;
716} else
717r->objects->multi_pack_index = m;
718return 1;
719}
720
721return 0;
722}
723
724int midx_checksum_valid(struct multi_pack_index *m)
725{
726return hashfile_checksum_valid(m->data, m->data_len);
727}
728
729struct clear_midx_data {
730char **keep;
731uint32_t keep_nr;
732const char *ext;
733};
734
735static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUSED,
736const char *file_name, void *_data)
737{
738struct clear_midx_data *data = _data;
739uint32_t i;
740
741if (!(starts_with(file_name, "multi-pack-index-") &&
742ends_with(file_name, data->ext)))
743return;
744for (i = 0; i < data->keep_nr; i++) {
745if (!strcmp(data->keep[i], file_name))
746return;
747}
748if (unlink(full_path))
749die_errno(_("failed to remove %s"), full_path);
750}
751
752void clear_midx_files_ext(const char *object_dir, const char *ext,
753const char *keep_hash)
754{
755struct clear_midx_data data;
756memset(&data, 0, sizeof(struct clear_midx_data));
757
758if (keep_hash) {
759ALLOC_ARRAY(data.keep, 1);
760
761data.keep[0] = xstrfmt("multi-pack-index-%s.%s", keep_hash, ext);
762data.keep_nr = 1;
763}
764data.ext = ext;
765
766for_each_file_in_pack_dir(object_dir,
767clear_midx_file_ext,
768&data);
769
770if (keep_hash)
771free(data.keep[0]);
772free(data.keep);
773}
774
775void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
776char **keep_hashes,
777uint32_t hashes_nr)
778{
779struct clear_midx_data data;
780uint32_t i;
781
782memset(&data, 0, sizeof(struct clear_midx_data));
783
784ALLOC_ARRAY(data.keep, hashes_nr);
785for (i = 0; i < hashes_nr; i++)
786data.keep[i] = xstrfmt("multi-pack-index-%s.%s", keep_hashes[i],
787ext);
788data.keep_nr = hashes_nr;
789data.ext = ext;
790
791for_each_file_in_pack_subdir(object_dir, "multi-pack-index.d",
792clear_midx_file_ext, &data);
793
794for (i = 0; i < hashes_nr; i++)
795free(data.keep[i]);
796free(data.keep);
797}
798
799void clear_midx_file(struct repository *r)
800{
801struct strbuf midx = STRBUF_INIT;
802
803get_midx_filename(&midx, r->objects->odb->path);
804
805if (r->objects && r->objects->multi_pack_index) {
806close_midx(r->objects->multi_pack_index);
807r->objects->multi_pack_index = NULL;
808}
809
810if (remove_path(midx.buf))
811die(_("failed to clear multi-pack-index at %s"), midx.buf);
812
813clear_midx_files_ext(r->objects->odb->path, MIDX_EXT_BITMAP, NULL);
814clear_midx_files_ext(r->objects->odb->path, MIDX_EXT_REV, NULL);
815
816strbuf_release(&midx);
817}
818
819static int verify_midx_error;
820
821__attribute__((format (printf, 1, 2)))
822static void midx_report(const char *fmt, ...)
823{
824va_list ap;
825verify_midx_error = 1;
826va_start(ap, fmt);
827vfprintf(stderr, fmt, ap);
828fprintf(stderr, "\n");
829va_end(ap);
830}
831
832struct pair_pos_vs_id
833{
834uint32_t pos;
835uint32_t pack_int_id;
836};
837
838static int compare_pair_pos_vs_id(const void *_a, const void *_b)
839{
840struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
841struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
842
843return b->pack_int_id - a->pack_int_id;
844}
845
846/*
847* Limit calls to display_progress() for performance reasons.
848* The interval here was arbitrarily chosen.
849*/
850#define SPARSE_PROGRESS_INTERVAL (1 << 12)
851#define midx_display_sparse_progress(progress, n) \
852do { \
853uint64_t _n = (n); \
854if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
855display_progress(progress, _n); \
856} while (0)
857
858int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
859{
860struct pair_pos_vs_id *pairs = NULL;
861uint32_t i;
862struct progress *progress = NULL;
863struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
864struct multi_pack_index *curr;
865verify_midx_error = 0;
866
867if (!m) {
868int result = 0;
869struct stat sb;
870struct strbuf filename = STRBUF_INIT;
871
872get_midx_filename(&filename, object_dir);
873
874if (!stat(filename.buf, &sb)) {
875error(_("multi-pack-index file exists, but failed to parse"));
876result = 1;
877}
878strbuf_release(&filename);
879return result;
880}
881
882if (!midx_checksum_valid(m))
883midx_report(_("incorrect checksum"));
884
885if (flags & MIDX_PROGRESS)
886progress = start_delayed_progress(_("Looking for referenced packfiles"),
887m->num_packs + m->num_packs_in_base);
888for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) {
889if (prepare_midx_pack(r, m, i))
890midx_report("failed to load pack in position %d", i);
891
892display_progress(progress, i + 1);
893}
894stop_progress(&progress);
895
896if (m->num_objects == 0) {
897midx_report(_("the midx contains no oid"));
898/*
899* Remaining tests assume that we have objects, so we can
900* return here.
901*/
902goto cleanup;
903}
904
905if (flags & MIDX_PROGRESS)
906progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
907m->num_objects - 1);
908
909for (curr = m; curr; curr = curr->base_midx) {
910for (i = 0; i < m->num_objects - 1; i++) {
911struct object_id oid1, oid2;
912
913nth_midxed_object_oid(&oid1, m, m->num_objects_in_base + i);
914nth_midxed_object_oid(&oid2, m, m->num_objects_in_base + i + 1);
915
916if (oidcmp(&oid1, &oid2) >= 0)
917midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
918i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
919
920midx_display_sparse_progress(progress, i + 1);
921}
922}
923stop_progress(&progress);
924
925/*
926* Create an array mapping each object to its packfile id. Sort it
927* to group the objects by packfile. Use this permutation to visit
928* each of the objects and only require 1 packfile to be open at a
929* time.
930*/
931ALLOC_ARRAY(pairs, m->num_objects + m->num_objects_in_base);
932for (i = 0; i < m->num_objects + m->num_objects_in_base; i++) {
933pairs[i].pos = i;
934pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
935}
936
937if (flags & MIDX_PROGRESS)
938progress = start_sparse_progress(_("Sorting objects by packfile"),
939m->num_objects);
940display_progress(progress, 0); /* TODO: Measure QSORT() progress */
941QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
942stop_progress(&progress);
943
944if (flags & MIDX_PROGRESS)
945progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
946for (i = 0; i < m->num_objects + m->num_objects_in_base; i++) {
947struct object_id oid;
948struct pack_entry e;
949off_t m_offset, p_offset;
950
951if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
952nth_midxed_pack(m, pairs[i-1].pack_int_id)) {
953uint32_t pack_int_id = pairs[i-1].pack_int_id;
954struct packed_git *p = nth_midxed_pack(m, pack_int_id);
955
956close_pack_fd(p);
957close_pack_index(p);
958}
959
960nth_midxed_object_oid(&oid, m, pairs[i].pos);
961
962if (!fill_midx_entry(r, &oid, &e, m)) {
963midx_report(_("failed to load pack entry for oid[%d] = %s"),
964pairs[i].pos, oid_to_hex(&oid));
965continue;
966}
967
968if (open_pack_index(e.p)) {
969midx_report(_("failed to load pack-index for packfile %s"),
970e.p->pack_name);
971break;
972}
973
974m_offset = e.offset;
975p_offset = find_pack_entry_one(oid.hash, e.p);
976
977if (m_offset != p_offset)
978midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
979pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
980
981midx_display_sparse_progress(progress, i + 1);
982}
983stop_progress(&progress);
984
985cleanup:
986free(pairs);
987close_midx(m);
988
989return verify_midx_error;
990}
991