]> git.ipfire.org Git - thirdparty/git.git/blame - midx.c
midx: use hashwrite_u8() in write_midx_header()
[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 9#include "midx.h"
144d7033 10#include "progress.h"
d829223a 11#include "trace2.h"
ce1e4a10 12#include "run-command.h"
a3407730 13
fc59e748
DS
14#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
15#define MIDX_VERSION 1
4d80560c
DS
16#define MIDX_BYTE_FILE_VERSION 4
17#define MIDX_BYTE_HASH_VERSION 5
18#define MIDX_BYTE_NUM_CHUNKS 6
19#define MIDX_BYTE_NUM_PACKS 8
fc59e748 20#define MIDX_HEADER_SIZE 12
aaa95dfa 21#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
fc59e748 22
662148c4 23#define MIDX_MAX_CHUNKS 5
32f3c541
DS
24#define MIDX_CHUNK_ALIGNMENT 4
25#define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
d7cacf29 26#define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
0d5b3a5e 27#define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
662148c4
DS
28#define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
29#define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
32f3c541 30#define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
d7cacf29 31#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
662148c4
DS
32#define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
33#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
34#define MIDX_LARGE_OFFSET_NEEDED 0x80000000
32f3c541 35
19575c7c
DS
36#define PACK_EXPIRED UINT_MAX
37
d9607542
DS
38static uint8_t oid_version(void)
39{
40 switch (hash_algo_by_ptr(the_hash_algo)) {
41 case GIT_HASH_SHA1:
42 return 1;
43 case GIT_HASH_SHA256:
44 return 2;
45 default:
46 die(_("invalid hash version"));
47 }
48}
49
fc59e748
DS
50static char *get_midx_filename(const char *object_dir)
51{
52 return xstrfmt("%s/pack/multi-pack-index", object_dir);
53}
54
2cf489a3 55struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
4d80560c
DS
56{
57 struct multi_pack_index *m = NULL;
58 int fd;
59 struct stat st;
60 size_t midx_size;
61 void *midx_map = NULL;
62 uint32_t hash_version;
63 char *midx_name = get_midx_filename(object_dir);
32f3c541 64 uint32_t i;
3227565c 65 const char *cur_pack_name;
4d80560c
DS
66
67 fd = git_open(midx_name);
68
69 if (fd < 0)
70 goto cleanup_fail;
71 if (fstat(fd, &st)) {
72 error_errno(_("failed to read %s"), midx_name);
73 goto cleanup_fail;
74 }
75
76 midx_size = xsize_t(st.st_size);
77
78 if (midx_size < MIDX_MIN_SIZE) {
79 error(_("multi-pack-index file %s is too small"), midx_name);
80 goto cleanup_fail;
81 }
82
83 FREE_AND_NULL(midx_name);
84
85 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
6c7ff7cf 86 close(fd);
4d80560c 87
577314ca 88 FLEX_ALLOC_STR(m, object_dir, object_dir);
4d80560c
DS
89 m->data = midx_map;
90 m->data_len = midx_size;
2cf489a3 91 m->local = local;
4d80560c
DS
92
93 m->signature = get_be32(m->data);
53ad0407
DS
94 if (m->signature != MIDX_SIGNATURE)
95 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
4d80560c 96 m->signature, MIDX_SIGNATURE);
4d80560c
DS
97
98 m->version = m->data[MIDX_BYTE_FILE_VERSION];
53ad0407
DS
99 if (m->version != MIDX_VERSION)
100 die(_("multi-pack-index version %d not recognized"),
4d80560c 101 m->version);
4d80560c
DS
102
103 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
d9607542
DS
104 if (hash_version != oid_version()) {
105 error(_("multi-pack-index hash version %u does not match version %u"),
106 hash_version, oid_version());
107 goto cleanup_fail;
108 }
aaa95dfa 109 m->hash_len = the_hash_algo->rawsz;
4d80560c
DS
110
111 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
112
113 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
114
32f3c541
DS
115 for (i = 0; i < m->num_chunks; i++) {
116 uint32_t chunk_id = get_be32(m->data + MIDX_HEADER_SIZE +
117 MIDX_CHUNKLOOKUP_WIDTH * i);
118 uint64_t chunk_offset = get_be64(m->data + MIDX_HEADER_SIZE + 4 +
119 MIDX_CHUNKLOOKUP_WIDTH * i);
120
d3f8e211
DS
121 if (chunk_offset >= m->data_len)
122 die(_("invalid chunk offset (too large)"));
123
32f3c541
DS
124 switch (chunk_id) {
125 case MIDX_CHUNKID_PACKNAMES:
126 m->chunk_pack_names = m->data + chunk_offset;
127 break;
128
d7cacf29
DS
129 case MIDX_CHUNKID_OIDFANOUT:
130 m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset);
131 break;
132
0d5b3a5e
DS
133 case MIDX_CHUNKID_OIDLOOKUP:
134 m->chunk_oid_lookup = m->data + chunk_offset;
135 break;
136
662148c4
DS
137 case MIDX_CHUNKID_OBJECTOFFSETS:
138 m->chunk_object_offsets = m->data + chunk_offset;
139 break;
140
141 case MIDX_CHUNKID_LARGEOFFSETS:
142 m->chunk_large_offsets = m->data + chunk_offset;
143 break;
144
32f3c541
DS
145 case 0:
146 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
147 break;
148
149 default:
150 /*
151 * Do nothing on unrecognized chunks, allowing future
152 * extensions to add optional chunks.
153 */
154 break;
155 }
156 }
157
158 if (!m->chunk_pack_names)
159 die(_("multi-pack-index missing required pack-name chunk"));
d7cacf29
DS
160 if (!m->chunk_oid_fanout)
161 die(_("multi-pack-index missing required OID fanout chunk"));
0d5b3a5e
DS
162 if (!m->chunk_oid_lookup)
163 die(_("multi-pack-index missing required OID lookup chunk"));
662148c4
DS
164 if (!m->chunk_object_offsets)
165 die(_("multi-pack-index missing required object offsets chunk"));
32f3c541 166
d7cacf29
DS
167 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
168
3227565c 169 m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
3715a633 170 m->packs = xcalloc(m->num_packs, sizeof(*m->packs));
3227565c
DS
171
172 cur_pack_name = (const char *)m->chunk_pack_names;
173 for (i = 0; i < m->num_packs; i++) {
174 m->pack_names[i] = cur_pack_name;
175
176 cur_pack_name += strlen(cur_pack_name) + 1;
177
8e72a3c3
DS
178 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
179 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
3227565c
DS
180 m->pack_names[i - 1],
181 m->pack_names[i]);
3227565c
DS
182 }
183
d829223a
JH
184 trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
185 trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
186
4d80560c
DS
187 return m;
188
189cleanup_fail:
190 free(m);
191 free(midx_name);
192 if (midx_map)
193 munmap(midx_map, midx_size);
194 if (0 <= fd)
195 close(fd);
196 return NULL;
197}
198
1dcd9f20 199void close_midx(struct multi_pack_index *m)
a40498a1
DS
200{
201 uint32_t i;
1dcd9f20
DS
202
203 if (!m)
204 return;
205
a40498a1 206 munmap((unsigned char *)m->data, m->data_len);
a40498a1
DS
207
208 for (i = 0; i < m->num_packs; i++) {
af96fe33
DS
209 if (m->packs[i])
210 m->packs[i]->multi_pack_index = 0;
a40498a1
DS
211 }
212 FREE_AND_NULL(m->packs);
213 FREE_AND_NULL(m->pack_names);
214}
215
64404a24 216int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
3715a633
DS
217{
218 struct strbuf pack_name = STRBUF_INIT;
af96fe33 219 struct packed_git *p;
3715a633
DS
220
221 if (pack_int_id >= m->num_packs)
d355e46a 222 die(_("bad pack-int-id: %u (%u total packs)"),
cc6af73c 223 pack_int_id, m->num_packs);
3715a633
DS
224
225 if (m->packs[pack_int_id])
226 return 0;
227
228 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
229 m->pack_names[pack_int_id]);
230
af96fe33 231 p = add_packed_git(pack_name.buf, pack_name.len, m->local);
3715a633 232 strbuf_release(&pack_name);
af96fe33
DS
233
234 if (!p)
235 return 1;
236
237 p->multi_pack_index = 1;
238 m->packs[pack_int_id] = p;
239 install_packed_git(r, p);
240 list_add_tail(&p->mru, &r->objects->packed_git_mru);
241
242 return 0;
3715a633
DS
243}
244
245int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
246{
247 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
aaa95dfa 248 the_hash_algo->rawsz, result);
3715a633
DS
249}
250
8aac67a1
DS
251struct object_id *nth_midxed_object_oid(struct object_id *oid,
252 struct multi_pack_index *m,
253 uint32_t n)
254{
255 if (n >= m->num_objects)
256 return NULL;
257
258 hashcpy(oid->hash, m->chunk_oid_lookup + m->hash_len * n);
259 return oid;
260}
261
3715a633
DS
262static off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
263{
264 const unsigned char *offset_data;
265 uint32_t offset32;
266
267 offset_data = m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH;
268 offset32 = get_be32(offset_data + sizeof(uint32_t));
269
270 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
d8ac9ee1 271 if (sizeof(off_t) < sizeof(uint64_t))
3715a633
DS
272 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
273
274 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
275 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
276 }
277
278 return offset32;
279}
280
281static uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
282{
283 return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH);
284}
285
64404a24
DS
286static int nth_midxed_pack_entry(struct repository *r,
287 struct multi_pack_index *m,
288 struct pack_entry *e,
289 uint32_t pos)
3715a633
DS
290{
291 uint32_t pack_int_id;
292 struct packed_git *p;
293
294 if (pos >= m->num_objects)
295 return 0;
296
297 pack_int_id = nth_midxed_pack_int_id(m, pos);
298
64404a24 299 if (prepare_midx_pack(r, m, pack_int_id))
3715a633
DS
300 die(_("error preparing packfile from multi-pack-index"));
301 p = m->packs[pack_int_id];
302
303 /*
304 * We are about to tell the caller where they can locate the
305 * requested object. We better make sure the packfile is
306 * still here and can be accessed before supplying that
307 * answer, as it may have been deleted since the MIDX was
308 * loaded!
309 */
310 if (!is_pack_valid(p))
311 return 0;
312
c39b02ae
DS
313 if (p->num_bad_objects) {
314 uint32_t i;
315 struct object_id oid;
316 nth_midxed_object_oid(&oid, m, pos);
317 for (i = 0; i < p->num_bad_objects; i++)
e43d2dcc
JK
318 if (hasheq(oid.hash,
319 p->bad_object_sha1 + the_hash_algo->rawsz * i))
c39b02ae
DS
320 return 0;
321 }
322
3715a633
DS
323 e->offset = nth_midxed_offset(m, pos);
324 e->p = p;
325
326 return 1;
327}
328
64404a24
DS
329int fill_midx_entry(struct repository * r,
330 const struct object_id *oid,
331 struct pack_entry *e,
332 struct multi_pack_index *m)
3715a633
DS
333{
334 uint32_t pos;
335
336 if (!bsearch_midx(oid, m, &pos))
337 return 0;
338
64404a24 339 return nth_midxed_pack_entry(r, m, e, pos);
3715a633
DS
340}
341
013fd7ad
JK
342/* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
343static int cmp_idx_or_pack_name(const char *idx_or_pack_name,
344 const char *idx_name)
345{
346 /* Skip past any initial matching prefix. */
347 while (*idx_name && *idx_name == *idx_or_pack_name) {
348 idx_name++;
349 idx_or_pack_name++;
350 }
351
352 /*
353 * If we didn't match completely, we may have matched "pack-1234." and
354 * be left with "idx" and "pack" respectively, which is also OK. We do
355 * not have to check for "idx" and "idx", because that would have been
356 * a complete match (and in that case these strcmps will be false, but
357 * we'll correctly return 0 from the final strcmp() below.
358 *
359 * Technically this matches "fooidx" and "foopack", but we'd never have
360 * such names in the first place.
361 */
362 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
363 return 0;
364
365 /*
366 * This not only checks for a complete match, but also orders based on
367 * the first non-identical character, which means our ordering will
368 * match a raw strcmp(). That makes it OK to use this to binary search
369 * a naively-sorted list.
370 */
371 return strcmp(idx_or_pack_name, idx_name);
372}
373
374int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
a40498a1
DS
375{
376 uint32_t first = 0, last = m->num_packs;
377
378 while (first < last) {
379 uint32_t mid = first + (last - first) / 2;
380 const char *current;
381 int cmp;
382
383 current = m->pack_names[mid];
013fd7ad 384 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
a40498a1
DS
385 if (!cmp)
386 return 1;
387 if (cmp > 0) {
388 first = mid + 1;
389 continue;
390 }
391 last = mid;
392 }
393
394 return 0;
395}
396
2cf489a3 397int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
c4d25228 398{
29e2016b 399 struct multi_pack_index *m;
c4d25228
DS
400 struct multi_pack_index *m_search;
401 int config_value;
0465a505 402 static int env_value = -1;
c4d25228 403
0465a505
DS
404 if (env_value < 0)
405 env_value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);
c4d25228 406
0465a505
DS
407 if (!env_value &&
408 (repo_config_get_bool(r, "core.multipackindex", &config_value) ||
409 !config_value))
c4d25228
DS
410 return 0;
411
29e2016b 412 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
c4d25228
DS
413 if (!strcmp(object_dir, m_search->object_dir))
414 return 1;
415
29e2016b 416 m = load_multi_pack_index(object_dir, local);
c4d25228 417
29e2016b
DS
418 if (m) {
419 m->next = r->objects->multi_pack_index;
420 r->objects->multi_pack_index = m;
c4d25228
DS
421 return 1;
422 }
423
424 return 0;
425}
426
fc59e748
DS
427static size_t write_midx_header(struct hashfile *f,
428 unsigned char num_chunks,
429 uint32_t num_packs)
430{
fc59e748 431 hashwrite_be32(f, MIDX_SIGNATURE);
014f1447
RS
432 hashwrite_u8(f, MIDX_VERSION);
433 hashwrite_u8(f, oid_version());
434 hashwrite_u8(f, num_chunks);
435 hashwrite_u8(f, 0); /* unused */
fc59e748
DS
436 hashwrite_be32(f, num_packs);
437
438 return MIDX_HEADER_SIZE;
439}
440
d01bf2e6
DS
441struct pack_info {
442 uint32_t orig_pack_int_id;
443 char *pack_name;
444 struct packed_git *p;
19575c7c 445 unsigned expired : 1;
d01bf2e6
DS
446};
447
448static int pack_info_compare(const void *_a, const void *_b)
449{
450 struct pack_info *a = (struct pack_info *)_a;
451 struct pack_info *b = (struct pack_info *)_b;
452 return strcmp(a->pack_name, b->pack_name);
453}
454
396f2570 455struct pack_list {
d01bf2e6 456 struct pack_info *info;
396f2570 457 uint32_t nr;
d01bf2e6 458 uint32_t alloc;
a40498a1 459 struct multi_pack_index *m;
840cef0c
WB
460 struct progress *progress;
461 unsigned pack_paths_checked;
396f2570
DS
462};
463
464static void add_pack_to_midx(const char *full_path, size_t full_path_len,
465 const char *file_name, void *data)
466{
467 struct pack_list *packs = (struct pack_list *)data;
468
469 if (ends_with(file_name, ".idx")) {
840cef0c 470 display_progress(packs->progress, ++packs->pack_paths_checked);
a40498a1
DS
471 if (packs->m && midx_contains_pack(packs->m, file_name))
472 return;
473
d01bf2e6 474 ALLOC_GROW(packs->info, packs->nr + 1, packs->alloc);
396f2570 475
d01bf2e6
DS
476 packs->info[packs->nr].p = add_packed_git(full_path,
477 full_path_len,
478 0);
fe1ed56f 479
d01bf2e6 480 if (!packs->info[packs->nr].p) {
396f2570
DS
481 warning(_("failed to add packfile '%s'"),
482 full_path);
483 return;
484 }
485
d01bf2e6 486 if (open_pack_index(packs->info[packs->nr].p)) {
fe1ed56f
DS
487 warning(_("failed to open pack-index '%s'"),
488 full_path);
d01bf2e6
DS
489 close_pack(packs->info[packs->nr].p);
490 FREE_AND_NULL(packs->info[packs->nr].p);
fe1ed56f
DS
491 return;
492 }
493
d01bf2e6
DS
494 packs->info[packs->nr].pack_name = xstrdup(file_name);
495 packs->info[packs->nr].orig_pack_int_id = packs->nr;
19575c7c 496 packs->info[packs->nr].expired = 0;
396f2570
DS
497 packs->nr++;
498 }
499}
500
fe1ed56f
DS
501struct pack_midx_entry {
502 struct object_id oid;
503 uint32_t pack_int_id;
504 time_t pack_mtime;
505 uint64_t offset;
506};
507
508static int midx_oid_compare(const void *_a, const void *_b)
509{
510 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
511 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
512 int cmp = oidcmp(&a->oid, &b->oid);
513
514 if (cmp)
515 return cmp;
516
517 if (a->pack_mtime > b->pack_mtime)
518 return -1;
519 else if (a->pack_mtime < b->pack_mtime)
520 return 1;
521
522 return a->pack_int_id - b->pack_int_id;
523}
524
a40498a1 525static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
a40498a1
DS
526 struct pack_midx_entry *e,
527 uint32_t pos)
528{
529 if (pos >= m->num_objects)
530 return 1;
531
532 nth_midxed_object_oid(&e->oid, m, pos);
d01bf2e6 533 e->pack_int_id = nth_midxed_pack_int_id(m, pos);
a40498a1
DS
534 e->offset = nth_midxed_offset(m, pos);
535
536 /* consider objects in midx to be from "old" packs */
537 e->pack_mtime = 0;
538 return 0;
539}
540
fe1ed56f
DS
541static void fill_pack_entry(uint32_t pack_int_id,
542 struct packed_git *p,
543 uint32_t cur_object,
544 struct pack_midx_entry *entry)
545{
0763671b 546 if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
fe1ed56f
DS
547 die(_("failed to locate object %d in packfile"), cur_object);
548
549 entry->pack_int_id = pack_int_id;
550 entry->pack_mtime = p->mtime;
551
552 entry->offset = nth_packed_object_offset(p, cur_object);
553}
554
555/*
556 * It is possible to artificially get into a state where there are many
557 * duplicate copies of objects. That can create high memory pressure if
558 * we are to create a list of all objects before de-duplication. To reduce
559 * this memory pressure without a significant performance drop, automatically
560 * group objects by the first byte of their object id. Use the IDX fanout
561 * tables to group the data, copy to a local array, then sort.
562 *
563 * Copy only the de-duplicated entries (selected by most-recent modified time
564 * of a packfile containing the object).
565 */
a40498a1 566static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
d01bf2e6 567 struct pack_info *info,
fe1ed56f
DS
568 uint32_t nr_packs,
569 uint32_t *nr_objects)
570{
571 uint32_t cur_fanout, cur_pack, cur_object;
572 uint32_t alloc_fanout, alloc_objects, total_objects = 0;
573 struct pack_midx_entry *entries_by_fanout = NULL;
574 struct pack_midx_entry *deduplicated_entries = NULL;
a40498a1 575 uint32_t start_pack = m ? m->num_packs : 0;
fe1ed56f 576
a40498a1 577 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
d01bf2e6 578 total_objects += info[cur_pack].p->num_objects;
fe1ed56f
DS
579
580 /*
581 * As we de-duplicate by fanout value, we expect the fanout
582 * slices to be evenly distributed, with some noise. Hence,
583 * allocate slightly more than one 256th.
584 */
585 alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16;
586
587 ALLOC_ARRAY(entries_by_fanout, alloc_fanout);
588 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
589 *nr_objects = 0;
590
591 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
592 uint32_t nr_fanout = 0;
593
a40498a1
DS
594 if (m) {
595 uint32_t start = 0, end;
596
597 if (cur_fanout)
598 start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
599 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
600
601 for (cur_object = start; cur_object < end; cur_object++) {
602 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
d01bf2e6 603 nth_midxed_pack_midx_entry(m,
a40498a1
DS
604 &entries_by_fanout[nr_fanout],
605 cur_object);
606 nr_fanout++;
607 }
608 }
609
610 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
fe1ed56f
DS
611 uint32_t start = 0, end;
612
613 if (cur_fanout)
d01bf2e6
DS
614 start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1);
615 end = get_pack_fanout(info[cur_pack].p, cur_fanout);
fe1ed56f
DS
616
617 for (cur_object = start; cur_object < end; cur_object++) {
618 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
d01bf2e6 619 fill_pack_entry(cur_pack, info[cur_pack].p, cur_object, &entries_by_fanout[nr_fanout]);
fe1ed56f
DS
620 nr_fanout++;
621 }
622 }
623
624 QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
625
626 /*
627 * The batch is now sorted by OID and then mtime (descending).
628 * Take only the first duplicate.
629 */
630 for (cur_object = 0; cur_object < nr_fanout; cur_object++) {
e43d2dcc
JK
631 if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid,
632 &entries_by_fanout[cur_object].oid))
fe1ed56f
DS
633 continue;
634
635 ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
636 memcpy(&deduplicated_entries[*nr_objects],
637 &entries_by_fanout[cur_object],
638 sizeof(struct pack_midx_entry));
639 (*nr_objects)++;
640 }
641 }
642
643 free(entries_by_fanout);
644 return deduplicated_entries;
645}
646
32f3c541 647static size_t write_midx_pack_names(struct hashfile *f,
d01bf2e6 648 struct pack_info *info,
32f3c541
DS
649 uint32_t num_packs)
650{
651 uint32_t i;
652 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
653 size_t written = 0;
654
655 for (i = 0; i < num_packs; i++) {
19575c7c
DS
656 size_t writelen;
657
658 if (info[i].expired)
659 continue;
32f3c541 660
d01bf2e6 661 if (i && strcmp(info[i].pack_name, info[i - 1].pack_name) <= 0)
32f3c541 662 BUG("incorrect pack-file order: %s before %s",
d01bf2e6
DS
663 info[i - 1].pack_name,
664 info[i].pack_name);
32f3c541 665
19575c7c 666 writelen = strlen(info[i].pack_name) + 1;
d01bf2e6 667 hashwrite(f, info[i].pack_name, writelen);
32f3c541
DS
668 written += writelen;
669 }
670
671 /* add padding to be aligned */
672 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
673 if (i < MIDX_CHUNK_ALIGNMENT) {
674 memset(padding, 0, sizeof(padding));
675 hashwrite(f, padding, i);
676 written += i;
677 }
678
679 return written;
680}
681
d7cacf29
DS
682static size_t write_midx_oid_fanout(struct hashfile *f,
683 struct pack_midx_entry *objects,
684 uint32_t nr_objects)
685{
686 struct pack_midx_entry *list = objects;
687 struct pack_midx_entry *last = objects + nr_objects;
688 uint32_t count = 0;
689 uint32_t i;
690
691 /*
692 * Write the first-level table (the list is sorted,
693 * but we use a 256-entry lookup to be able to avoid
694 * having to do eight extra binary search iterations).
695 */
696 for (i = 0; i < 256; i++) {
697 struct pack_midx_entry *next = list;
698
699 while (next < last && next->oid.hash[0] == i) {
700 count++;
701 next++;
702 }
703
704 hashwrite_be32(f, count);
705 list = next;
706 }
707
708 return MIDX_CHUNK_FANOUT_SIZE;
709}
710
0d5b3a5e
DS
711static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len,
712 struct pack_midx_entry *objects,
713 uint32_t nr_objects)
714{
715 struct pack_midx_entry *list = objects;
716 uint32_t i;
717 size_t written = 0;
718
719 for (i = 0; i < nr_objects; i++) {
720 struct pack_midx_entry *obj = list++;
721
722 if (i < nr_objects - 1) {
723 struct pack_midx_entry *next = list;
724 if (oidcmp(&obj->oid, &next->oid) >= 0)
725 BUG("OIDs not in order: %s >= %s",
726 oid_to_hex(&obj->oid),
727 oid_to_hex(&next->oid));
728 }
729
730 hashwrite(f, obj->oid.hash, (int)hash_len);
731 written += hash_len;
732 }
733
734 return written;
735}
736
662148c4 737static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed,
d01bf2e6 738 uint32_t *perm,
662148c4
DS
739 struct pack_midx_entry *objects, uint32_t nr_objects)
740{
741 struct pack_midx_entry *list = objects;
742 uint32_t i, nr_large_offset = 0;
743 size_t written = 0;
744
745 for (i = 0; i < nr_objects; i++) {
746 struct pack_midx_entry *obj = list++;
747
19575c7c
DS
748 if (perm[obj->pack_int_id] == PACK_EXPIRED)
749 BUG("object %s is in an expired pack with int-id %d",
750 oid_to_hex(&obj->oid),
751 obj->pack_int_id);
752
d01bf2e6 753 hashwrite_be32(f, perm[obj->pack_int_id]);
662148c4
DS
754
755 if (large_offset_needed && obj->offset >> 31)
756 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
757 else if (!large_offset_needed && obj->offset >> 32)
758 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
759 oid_to_hex(&obj->oid),
760 obj->offset);
761 else
762 hashwrite_be32(f, (uint32_t)obj->offset);
763
764 written += MIDX_CHUNK_OFFSET_WIDTH;
765 }
766
767 return written;
768}
769
770static size_t write_midx_large_offsets(struct hashfile *f, uint32_t nr_large_offset,
771 struct pack_midx_entry *objects, uint32_t nr_objects)
772{
61b0fcbb 773 struct pack_midx_entry *list = objects, *end = objects + nr_objects;
662148c4
DS
774 size_t written = 0;
775
776 while (nr_large_offset) {
61b0fcbb
JK
777 struct pack_midx_entry *obj;
778 uint64_t offset;
779
780 if (list >= end)
781 BUG("too many large-offset objects");
782
783 obj = list++;
784 offset = obj->offset;
662148c4
DS
785
786 if (!(offset >> 31))
787 continue;
788
789 hashwrite_be32(f, offset >> 32);
790 hashwrite_be32(f, offset & 0xffffffffUL);
791 written += 2 * sizeof(uint32_t);
792
793 nr_large_offset--;
794 }
795
796 return written;
797}
798
19575c7c 799static int write_midx_internal(const char *object_dir, struct multi_pack_index *m,
840cef0c 800 struct string_list *packs_to_drop, unsigned flags)
a3407730 801{
32f3c541 802 unsigned char cur_chunk, num_chunks = 0;
fc59e748 803 char *midx_name;
396f2570 804 uint32_t i;
fc59e748
DS
805 struct hashfile *f = NULL;
806 struct lock_file lk;
396f2570 807 struct pack_list packs;
32f3c541
DS
808 uint32_t *pack_perm = NULL;
809 uint64_t written = 0;
810 uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
811 uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
662148c4 812 uint32_t nr_entries, num_large_offsets = 0;
fe1ed56f 813 struct pack_midx_entry *entries = NULL;
840cef0c 814 struct progress *progress = NULL;
662148c4 815 int large_offsets_needed = 0;
dba6175c 816 int pack_name_concat_len = 0;
19575c7c
DS
817 int dropped_packs = 0;
818 int result = 0;
fc59e748
DS
819
820 midx_name = get_midx_filename(object_dir);
d5e1961c 821 if (safe_create_leading_directories(midx_name))
fc59e748
DS
822 die_errno(_("unable to create leading directories of %s"),
823 midx_name);
fc59e748 824
19575c7c
DS
825 if (m)
826 packs.m = m;
827 else
828 packs.m = load_multi_pack_index(object_dir, 1);
a40498a1 829
396f2570 830 packs.nr = 0;
d01bf2e6
DS
831 packs.alloc = packs.m ? packs.m->num_packs : 16;
832 packs.info = NULL;
833 ALLOC_ARRAY(packs.info, packs.alloc);
396f2570 834
a40498a1
DS
835 if (packs.m) {
836 for (i = 0; i < packs.m->num_packs; i++) {
d01bf2e6 837 ALLOC_GROW(packs.info, packs.nr + 1, packs.alloc);
a40498a1 838
d01bf2e6
DS
839 packs.info[packs.nr].orig_pack_int_id = i;
840 packs.info[packs.nr].pack_name = xstrdup(packs.m->pack_names[i]);
841 packs.info[packs.nr].p = NULL;
19575c7c 842 packs.info[packs.nr].expired = 0;
a40498a1
DS
843 packs.nr++;
844 }
845 }
846
840cef0c
WB
847 packs.pack_paths_checked = 0;
848 if (flags & MIDX_PROGRESS)
849 packs.progress = start_progress(_("Adding packfiles to multi-pack-index"), 0);
850 else
851 packs.progress = NULL;
852
396f2570 853 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
840cef0c 854 stop_progress(&packs.progress);
396f2570 855
19575c7c 856 if (packs.m && packs.nr == packs.m->num_packs && !packs_to_drop)
a40498a1
DS
857 goto cleanup;
858
d01bf2e6 859 entries = get_sorted_entries(packs.m, packs.info, packs.nr, &nr_entries);
a40498a1 860
662148c4
DS
861 for (i = 0; i < nr_entries; i++) {
862 if (entries[i].offset > 0x7fffffff)
863 num_large_offsets++;
864 if (entries[i].offset > 0xffffffff)
865 large_offsets_needed = 1;
866 }
fe1ed56f 867
d01bf2e6
DS
868 QSORT(packs.info, packs.nr, pack_info_compare);
869
19575c7c
DS
870 if (packs_to_drop && packs_to_drop->nr) {
871 int drop_index = 0;
872 int missing_drops = 0;
873
874 for (i = 0; i < packs.nr && drop_index < packs_to_drop->nr; i++) {
875 int cmp = strcmp(packs.info[i].pack_name,
876 packs_to_drop->items[drop_index].string);
877
878 if (!cmp) {
879 drop_index++;
880 packs.info[i].expired = 1;
881 } else if (cmp > 0) {
882 error(_("did not see pack-file %s to drop"),
883 packs_to_drop->items[drop_index].string);
884 drop_index++;
885 missing_drops++;
886 i--;
887 } else {
888 packs.info[i].expired = 0;
889 }
890 }
891
892 if (missing_drops) {
893 result = 1;
894 goto cleanup;
895 }
896 }
897
d01bf2e6
DS
898 /*
899 * pack_perm stores a permutation between pack-int-ids from the
900 * previous multi-pack-index to the new one we are writing:
901 *
902 * pack_perm[old_id] = new_id
903 */
904 ALLOC_ARRAY(pack_perm, packs.nr);
905 for (i = 0; i < packs.nr; i++) {
19575c7c
DS
906 if (packs.info[i].expired) {
907 dropped_packs++;
908 pack_perm[packs.info[i].orig_pack_int_id] = PACK_EXPIRED;
909 } else {
910 pack_perm[packs.info[i].orig_pack_int_id] = i - dropped_packs;
911 }
d01bf2e6
DS
912 }
913
19575c7c
DS
914 for (i = 0; i < packs.nr; i++) {
915 if (!packs.info[i].expired)
916 pack_name_concat_len += strlen(packs.info[i].pack_name) + 1;
917 }
dba6175c
DS
918
919 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
920 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
921 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
922
fc59e748
DS
923 hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
924 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
925 FREE_AND_NULL(midx_name);
926
a40498a1
DS
927 if (packs.m)
928 close_midx(packs.m);
929
32f3c541 930 cur_chunk = 0;
662148c4 931 num_chunks = large_offsets_needed ? 5 : 4;
32f3c541 932
796d61cd
DR
933 if (packs.nr - dropped_packs == 0) {
934 error(_("no pack files to index."));
935 result = 1;
936 goto cleanup;
937 }
938
19575c7c 939 written = write_midx_header(f, num_chunks, packs.nr - dropped_packs);
32f3c541
DS
940
941 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
942 chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
943
944 cur_chunk++;
d7cacf29 945 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
dba6175c 946 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + pack_name_concat_len;
32f3c541 947
d7cacf29
DS
948 cur_chunk++;
949 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;
950 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE;
951
0d5b3a5e 952 cur_chunk++;
662148c4 953 chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS;
aaa95dfa 954 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * the_hash_algo->rawsz;
0d5b3a5e 955
662148c4
DS
956 cur_chunk++;
957 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_CHUNK_OFFSET_WIDTH;
958 if (large_offsets_needed) {
959 chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS;
960
961 cur_chunk++;
962 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] +
963 num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
964 }
965
966 chunk_ids[cur_chunk] = 0;
967
32f3c541
DS
968 for (i = 0; i <= num_chunks; i++) {
969 if (i && chunk_offsets[i] < chunk_offsets[i - 1])
970 BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64,
971 chunk_offsets[i - 1],
972 chunk_offsets[i]);
973
974 if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT)
975 BUG("chunk offset %"PRIu64" is not properly aligned",
976 chunk_offsets[i]);
977
978 hashwrite_be32(f, chunk_ids[i]);
979 hashwrite_be32(f, chunk_offsets[i] >> 32);
980 hashwrite_be32(f, chunk_offsets[i]);
981
982 written += MIDX_CHUNKLOOKUP_WIDTH;
983 }
984
840cef0c
WB
985 if (flags & MIDX_PROGRESS)
986 progress = start_progress(_("Writing chunks to multi-pack-index"),
987 num_chunks);
32f3c541
DS
988 for (i = 0; i < num_chunks; i++) {
989 if (written != chunk_offsets[i])
990 BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32,
991 chunk_offsets[i],
992 written,
993 chunk_ids[i]);
994
995 switch (chunk_ids[i]) {
996 case MIDX_CHUNKID_PACKNAMES:
d01bf2e6 997 written += write_midx_pack_names(f, packs.info, packs.nr);
32f3c541
DS
998 break;
999
d7cacf29
DS
1000 case MIDX_CHUNKID_OIDFANOUT:
1001 written += write_midx_oid_fanout(f, entries, nr_entries);
1002 break;
1003
0d5b3a5e 1004 case MIDX_CHUNKID_OIDLOOKUP:
aaa95dfa 1005 written += write_midx_oid_lookup(f, the_hash_algo->rawsz, entries, nr_entries);
0d5b3a5e
DS
1006 break;
1007
662148c4 1008 case MIDX_CHUNKID_OBJECTOFFSETS:
d01bf2e6 1009 written += write_midx_object_offsets(f, large_offsets_needed, pack_perm, entries, nr_entries);
662148c4
DS
1010 break;
1011
1012 case MIDX_CHUNKID_LARGEOFFSETS:
1013 written += write_midx_large_offsets(f, num_large_offsets, entries, nr_entries);
1014 break;
1015
32f3c541
DS
1016 default:
1017 BUG("trying to write unknown chunk id %"PRIx32,
1018 chunk_ids[i]);
1019 }
840cef0c
WB
1020
1021 display_progress(progress, i + 1);
32f3c541 1022 }
840cef0c 1023 stop_progress(&progress);
32f3c541
DS
1024
1025 if (written != chunk_offsets[num_chunks])
1026 BUG("incorrect final offset %"PRIu64" != %"PRIu64,
1027 written,
1028 chunk_offsets[num_chunks]);
fc59e748
DS
1029
1030 finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
1031 commit_lock_file(&lk);
1032
a40498a1 1033cleanup:
396f2570 1034 for (i = 0; i < packs.nr; i++) {
d01bf2e6
DS
1035 if (packs.info[i].p) {
1036 close_pack(packs.info[i].p);
1037 free(packs.info[i].p);
396f2570 1038 }
d01bf2e6 1039 free(packs.info[i].pack_name);
396f2570
DS
1040 }
1041
d01bf2e6 1042 free(packs.info);
fe1ed56f 1043 free(entries);
a40498a1
DS
1044 free(pack_perm);
1045 free(midx_name);
19575c7c
DS
1046 return result;
1047}
1048
efbc3aee 1049int write_midx_file(const char *object_dir, unsigned flags)
19575c7c 1050{
840cef0c 1051 return write_midx_internal(object_dir, NULL, NULL, flags);
a3407730 1052}
525e18c0 1053
1dcd9f20 1054void clear_midx_file(struct repository *r)
525e18c0 1055{
3b2f8a02 1056 char *midx = get_midx_filename(r->objects->odb->path);
1dcd9f20
DS
1057
1058 if (r->objects && r->objects->multi_pack_index) {
1059 close_midx(r->objects->multi_pack_index);
1060 r->objects->multi_pack_index = NULL;
1061 }
525e18c0 1062
d5e1961c 1063 if (remove_path(midx))
525e18c0 1064 die(_("failed to clear multi-pack-index at %s"), midx);
525e18c0
DS
1065
1066 free(midx);
1067}
56ee7ff1
DS
1068
1069static int verify_midx_error;
1070
d4bf1d88
DS
1071static void midx_report(const char *fmt, ...)
1072{
1073 va_list ap;
1074 verify_midx_error = 1;
1075 va_start(ap, fmt);
1076 vfprintf(stderr, fmt, ap);
1077 fprintf(stderr, "\n");
1078 va_end(ap);
1079}
1080
5ae18df9
JH
1081struct pair_pos_vs_id
1082{
1083 uint32_t pos;
1084 uint32_t pack_int_id;
1085};
1086
1087static int compare_pair_pos_vs_id(const void *_a, const void *_b)
1088{
1089 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
1090 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
1091
1092 return b->pack_int_id - a->pack_int_id;
1093}
1094
430efb8a
JH
1095/*
1096 * Limit calls to display_progress() for performance reasons.
1097 * The interval here was arbitrarily chosen.
1098 */
1099#define SPARSE_PROGRESS_INTERVAL (1 << 12)
1100#define midx_display_sparse_progress(progress, n) \
1101 do { \
1102 uint64_t _n = (n); \
1103 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1104 display_progress(progress, _n); \
1105 } while (0)
1106
efbc3aee 1107int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
56ee7ff1 1108{
5ae18df9 1109 struct pair_pos_vs_id *pairs = NULL;
d4bf1d88 1110 uint32_t i;
ad60096d 1111 struct progress *progress = NULL;
56ee7ff1
DS
1112 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1113 verify_midx_error = 0;
1114
d9607542
DS
1115 if (!m) {
1116 int result = 0;
1117 struct stat sb;
1118 char *filename = get_midx_filename(object_dir);
1119 if (!stat(filename, &sb)) {
1120 error(_("multi-pack-index file exists, but failed to parse"));
1121 result = 1;
1122 }
1123 free(filename);
1124 return result;
1125 }
56ee7ff1 1126
ad60096d
WB
1127 if (flags & MIDX_PROGRESS)
1128 progress = start_progress(_("Looking for referenced packfiles"),
1129 m->num_packs);
d4bf1d88 1130 for (i = 0; i < m->num_packs; i++) {
64404a24 1131 if (prepare_midx_pack(r, m, i))
d4bf1d88 1132 midx_report("failed to load pack in position %d", i);
430efb8a
JH
1133
1134 display_progress(progress, i + 1);
d4bf1d88 1135 }
430efb8a 1136 stop_progress(&progress);
d4bf1d88 1137
2f23d3f3
DS
1138 for (i = 0; i < 255; i++) {
1139 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
1140 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]);
1141
1142 if (oid_fanout1 > oid_fanout2)
1143 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
1144 i, oid_fanout1, oid_fanout2, i + 1);
1145 }
1146
796d61cd
DR
1147 if (m->num_objects == 0) {
1148 midx_report(_("the midx contains no oid"));
1149 /*
1150 * Remaining tests assume that we have objects, so we can
1151 * return here.
1152 */
1153 return verify_midx_error;
1154 }
1155
ad60096d
WB
1156 if (flags & MIDX_PROGRESS)
1157 progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1158 m->num_objects - 1);
55c5648d
DS
1159 for (i = 0; i < m->num_objects - 1; i++) {
1160 struct object_id oid1, oid2;
1161
1162 nth_midxed_object_oid(&oid1, m, i);
1163 nth_midxed_object_oid(&oid2, m, i + 1);
1164
1165 if (oidcmp(&oid1, &oid2) >= 0)
1166 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1167 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
430efb8a
JH
1168
1169 midx_display_sparse_progress(progress, i + 1);
55c5648d 1170 }
430efb8a 1171 stop_progress(&progress);
55c5648d 1172
5ae18df9
JH
1173 /*
1174 * Create an array mapping each object to its packfile id. Sort it
1175 * to group the objects by packfile. Use this permutation to visit
1176 * each of the objects and only require 1 packfile to be open at a
1177 * time.
1178 */
1179 ALLOC_ARRAY(pairs, m->num_objects);
1180 for (i = 0; i < m->num_objects; i++) {
1181 pairs[i].pos = i;
1182 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
1183 }
1184
ad60096d
WB
1185 if (flags & MIDX_PROGRESS)
1186 progress = start_sparse_progress(_("Sorting objects by packfile"),
1187 m->num_objects);
5ae18df9
JH
1188 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
1189 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
1190 stop_progress(&progress);
1191
ad60096d
WB
1192 if (flags & MIDX_PROGRESS)
1193 progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
cc6af73c
DS
1194 for (i = 0; i < m->num_objects; i++) {
1195 struct object_id oid;
1196 struct pack_entry e;
1197 off_t m_offset, p_offset;
1198
5ae18df9
JH
1199 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
1200 m->packs[pairs[i-1].pack_int_id])
1201 {
1202 close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
1203 close_pack_index(m->packs[pairs[i-1].pack_int_id]);
1204 }
1205
1206 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1207
64404a24 1208 if (!fill_midx_entry(r, &oid, &e, m)) {
cc6af73c 1209 midx_report(_("failed to load pack entry for oid[%d] = %s"),
5ae18df9 1210 pairs[i].pos, oid_to_hex(&oid));
cc6af73c
DS
1211 continue;
1212 }
1213
1214 if (open_pack_index(e.p)) {
1215 midx_report(_("failed to load pack-index for packfile %s"),
1216 e.p->pack_name);
1217 break;
1218 }
1219
1220 m_offset = e.offset;
1221 p_offset = find_pack_entry_one(oid.hash, e.p);
1222
1223 if (m_offset != p_offset)
1224 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
5ae18df9 1225 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
144d7033 1226
430efb8a 1227 midx_display_sparse_progress(progress, i + 1);
cc6af73c 1228 }
144d7033 1229 stop_progress(&progress);
cc6af73c 1230
5ae18df9
JH
1231 free(pairs);
1232
56ee7ff1
DS
1233 return verify_midx_error;
1234}
cff97116 1235
efbc3aee 1236int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
cff97116 1237{
19575c7c
DS
1238 uint32_t i, *count, result = 0;
1239 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1240 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
8dc18f89 1241 struct progress *progress = NULL;
19575c7c
DS
1242
1243 if (!m)
1244 return 0;
1245
1246 count = xcalloc(m->num_packs, sizeof(uint32_t));
8dc18f89
WB
1247
1248 if (flags & MIDX_PROGRESS)
1249 progress = start_progress(_("Counting referenced objects"),
1250 m->num_objects);
19575c7c
DS
1251 for (i = 0; i < m->num_objects; i++) {
1252 int pack_int_id = nth_midxed_pack_int_id(m, i);
1253 count[pack_int_id]++;
8dc18f89 1254 display_progress(progress, i + 1);
19575c7c 1255 }
8dc18f89 1256 stop_progress(&progress);
19575c7c 1257
8dc18f89
WB
1258 if (flags & MIDX_PROGRESS)
1259 progress = start_progress(_("Finding and deleting unreferenced packfiles"),
1260 m->num_packs);
19575c7c
DS
1261 for (i = 0; i < m->num_packs; i++) {
1262 char *pack_name;
8dc18f89 1263 display_progress(progress, i + 1);
19575c7c
DS
1264
1265 if (count[i])
1266 continue;
1267
1268 if (prepare_midx_pack(r, m, i))
1269 continue;
1270
1271 if (m->packs[i]->pack_keep)
1272 continue;
1273
1274 pack_name = xstrdup(m->packs[i]->pack_name);
1275 close_pack(m->packs[i]);
1276
1277 string_list_insert(&packs_to_drop, m->pack_names[i]);
1278 unlink_pack_path(pack_name, 0);
1279 free(pack_name);
1280 }
8dc18f89 1281 stop_progress(&progress);
19575c7c
DS
1282
1283 free(count);
1284
1285 if (packs_to_drop.nr)
840cef0c 1286 result = write_midx_internal(object_dir, m, &packs_to_drop, flags);
19575c7c
DS
1287
1288 string_list_clear(&packs_to_drop, 0);
1289 return result;
cff97116 1290}
2af890bb 1291
ce1e4a10
DS
1292struct repack_info {
1293 timestamp_t mtime;
1294 uint32_t referenced_objects;
1295 uint32_t pack_int_id;
1296};
1297
1298static int compare_by_mtime(const void *a_, const void *b_)
2af890bb 1299{
ce1e4a10
DS
1300 const struct repack_info *a, *b;
1301
1302 a = (const struct repack_info *)a_;
1303 b = (const struct repack_info *)b_;
1304
1305 if (a->mtime < b->mtime)
1306 return -1;
1307 if (a->mtime > b->mtime)
1308 return 1;
1309 return 0;
1310}
1311
3ce4ca0a
DS
1312static int fill_included_packs_all(struct repository *r,
1313 struct multi_pack_index *m,
ce1e4a10
DS
1314 unsigned char *include_pack)
1315{
3ce4ca0a
DS
1316 uint32_t i, count = 0;
1317 int pack_kept_objects = 0;
1318
1319 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1320
1321 for (i = 0; i < m->num_packs; i++) {
1322 if (prepare_midx_pack(r, m, i))
1323 continue;
1324 if (!pack_kept_objects && m->packs[i]->pack_keep)
1325 continue;
ce1e4a10 1326
ce1e4a10 1327 include_pack[i] = 1;
3ce4ca0a
DS
1328 count++;
1329 }
ce1e4a10 1330
3ce4ca0a 1331 return count < 2;
ce1e4a10
DS
1332}
1333
1334static int fill_included_packs_batch(struct repository *r,
1335 struct multi_pack_index *m,
1336 unsigned char *include_pack,
1337 size_t batch_size)
1338{
1339 uint32_t i, packs_to_repack;
1340 size_t total_size;
1341 struct repack_info *pack_info = xcalloc(m->num_packs, sizeof(struct repack_info));
3ce4ca0a
DS
1342 int pack_kept_objects = 0;
1343
1344 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
ce1e4a10
DS
1345
1346 for (i = 0; i < m->num_packs; i++) {
1347 pack_info[i].pack_int_id = i;
1348
1349 if (prepare_midx_pack(r, m, i))
1350 continue;
1351
1352 pack_info[i].mtime = m->packs[i]->mtime;
1353 }
1354
1355 for (i = 0; batch_size && i < m->num_objects; i++) {
1356 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1357 pack_info[pack_int_id].referenced_objects++;
1358 }
1359
1360 QSORT(pack_info, m->num_packs, compare_by_mtime);
1361
1362 total_size = 0;
1363 packs_to_repack = 0;
1364 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1365 int pack_int_id = pack_info[i].pack_int_id;
1366 struct packed_git *p = m->packs[pack_int_id];
1367 size_t expected_size;
1368
1369 if (!p)
1370 continue;
3ce4ca0a
DS
1371 if (!pack_kept_objects && p->pack_keep)
1372 continue;
ce1e4a10
DS
1373 if (open_pack_index(p) || !p->num_objects)
1374 continue;
1375
1376 expected_size = (size_t)(p->pack_size
1377 * pack_info[i].referenced_objects);
1378 expected_size /= p->num_objects;
1379
1380 if (expected_size >= batch_size)
1381 continue;
1382
1383 packs_to_repack++;
1384 total_size += expected_size;
1385 include_pack[pack_int_id] = 1;
1386 }
1387
1388 free(pack_info);
1389
1eb22c7d 1390 if (packs_to_repack < 2)
ce1e4a10
DS
1391 return 1;
1392
2af890bb
DS
1393 return 0;
1394}
ce1e4a10 1395
efbc3aee 1396int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
ce1e4a10
DS
1397{
1398 int result = 0;
1399 uint32_t i;
1400 unsigned char *include_pack;
1401 struct child_process cmd = CHILD_PROCESS_INIT;
6af3b00a 1402 FILE *cmd_in;
ce1e4a10
DS
1403 struct strbuf base_name = STRBUF_INIT;
1404 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1405
e11d86de
SLN
1406 /*
1407 * When updating the default for these configuration
1408 * variables in builtin/repack.c, these must be adjusted
1409 * to match.
1410 */
1411 int delta_base_offset = 1;
1412 int use_delta_islands = 0;
1413
ce1e4a10
DS
1414 if (!m)
1415 return 0;
1416
1417 include_pack = xcalloc(m->num_packs, sizeof(unsigned char));
1418
1419 if (batch_size) {
1420 if (fill_included_packs_batch(r, m, include_pack, batch_size))
1421 goto cleanup;
3ce4ca0a 1422 } else if (fill_included_packs_all(r, m, include_pack))
ce1e4a10
DS
1423 goto cleanup;
1424
e11d86de
SLN
1425 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
1426 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
1427
c972bf4c 1428 strvec_push(&cmd.args, "pack-objects");
ce1e4a10
DS
1429
1430 strbuf_addstr(&base_name, object_dir);
1431 strbuf_addstr(&base_name, "/pack/pack");
c972bf4c 1432 strvec_push(&cmd.args, base_name.buf);
64d80e7d 1433
e11d86de 1434 if (delta_base_offset)
c972bf4c 1435 strvec_push(&cmd.args, "--delta-base-offset");
e11d86de 1436 if (use_delta_islands)
c972bf4c 1437 strvec_push(&cmd.args, "--delta-islands");
e11d86de 1438
64d80e7d 1439 if (flags & MIDX_PROGRESS)
c972bf4c 1440 strvec_push(&cmd.args, "--progress");
64d80e7d 1441 else
c972bf4c 1442 strvec_push(&cmd.args, "-q");
64d80e7d 1443
ce1e4a10
DS
1444 strbuf_release(&base_name);
1445
1446 cmd.git_cmd = 1;
1447 cmd.in = cmd.out = -1;
1448
1449 if (start_command(&cmd)) {
1450 error(_("could not start pack-objects"));
1451 result = 1;
1452 goto cleanup;
1453 }
1454
6af3b00a
RS
1455 cmd_in = xfdopen(cmd.in, "w");
1456
ce1e4a10
DS
1457 for (i = 0; i < m->num_objects; i++) {
1458 struct object_id oid;
1459 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1460
1461 if (!include_pack[pack_int_id])
1462 continue;
1463
1464 nth_midxed_object_oid(&oid, m, i);
6af3b00a 1465 fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
ce1e4a10 1466 }
6af3b00a 1467 fclose(cmd_in);
ce1e4a10
DS
1468
1469 if (finish_command(&cmd)) {
1470 error(_("could not finish pack-objects"));
1471 result = 1;
1472 goto cleanup;
1473 }
1474
840cef0c 1475 result = write_midx_internal(object_dir, m, NULL, flags);
ce1e4a10
DS
1476 m = NULL;
1477
1478cleanup:
1479 if (m)
1480 close_midx(m);
1481 free(include_pack);
1482 return result;
1483}