blob: 6d1584ca51d313343bbb65f9baa7c794259c7bf0 [file] [log] [blame]
Derrick Stoleea3407732018-07-12 15:39:21 -04001#include "cache.h"
Derrick Stoleec4d25222018-07-12 15:39:33 -04002#include "config.h"
Derrick Stoleefc59e742018-07-12 15:39:22 -04003#include "csum-file.h"
Derrick Stolee396f2572018-07-12 15:39:26 -04004#include "dir.h"
Derrick Stoleefc59e742018-07-12 15:39:22 -04005#include "lockfile.h"
Derrick Stolee396f2572018-07-12 15:39:26 -04006#include "packfile.h"
Derrick Stolee4d805602018-07-12 15:39:23 -04007#include "object-store.h"
Derrick Stolee3715a632018-07-12 15:39:34 -04008#include "sha1-lookup.h"
Derrick Stoleea3407732018-07-12 15:39:21 -04009#include "midx.h"
Derrick Stolee144d7032018-09-13 11:02:26 -070010#include "progress.h"
Jeff Hostetlerd8292232019-03-21 12:36:13 -070011#include "trace2.h"
Derrick Stoleece1e4a12019-06-10 16:35:27 -070012#include "run-command.h"
Derrick Stoleea3407732018-07-12 15:39:21 -040013
Derrick Stoleefc59e742018-07-12 15:39:22 -040014#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
15#define MIDX_VERSION 1
Derrick Stolee4d805602018-07-12 15:39:23 -040016#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
Derrick Stoleefc59e742018-07-12 15:39:22 -040020#define MIDX_HASH_VERSION 1
21#define MIDX_HEADER_SIZE 12
brian m. carlsonaaa95df2019-08-18 20:04:27 +000022#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
Derrick Stoleefc59e742018-07-12 15:39:22 -040023
Derrick Stolee662148c2018-07-12 15:39:32 -040024#define MIDX_MAX_CHUNKS 5
Derrick Stolee32f3c542018-07-12 15:39:27 -040025#define MIDX_CHUNK_ALIGNMENT 4
26#define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
Derrick Stoleed7cacf22018-07-12 15:39:31 -040027#define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
Derrick Stolee0d5b3a52018-07-12 15:39:30 -040028#define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
Derrick Stolee662148c2018-07-12 15:39:32 -040029#define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
30#define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
Derrick Stolee32f3c542018-07-12 15:39:27 -040031#define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
Derrick Stoleed7cacf22018-07-12 15:39:31 -040032#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
Derrick Stolee662148c2018-07-12 15:39:32 -040033#define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
34#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
35#define MIDX_LARGE_OFFSET_NEEDED 0x80000000
Derrick Stolee32f3c542018-07-12 15:39:27 -040036
Derrick Stolee19575c72019-06-10 16:35:25 -070037#define PACK_EXPIRED UINT_MAX
38
Derrick Stoleefc59e742018-07-12 15:39:22 -040039static char *get_midx_filename(const char *object_dir)
40{
41 return xstrfmt("%s/pack/multi-pack-index", object_dir);
42}
43
Derrick Stolee2cf489a2018-08-20 16:51:55 +000044struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
Derrick Stolee4d805602018-07-12 15:39:23 -040045{
46 struct multi_pack_index *m = NULL;
47 int fd;
48 struct stat st;
49 size_t midx_size;
50 void *midx_map = NULL;
51 uint32_t hash_version;
52 char *midx_name = get_midx_filename(object_dir);
Derrick Stolee32f3c542018-07-12 15:39:27 -040053 uint32_t i;
Derrick Stolee32275652018-07-12 15:39:28 -040054 const char *cur_pack_name;
Derrick Stolee4d805602018-07-12 15:39:23 -040055
56 fd = git_open(midx_name);
57
58 if (fd < 0)
59 goto cleanup_fail;
60 if (fstat(fd, &st)) {
61 error_errno(_("failed to read %s"), midx_name);
62 goto cleanup_fail;
63 }
64
65 midx_size = xsize_t(st.st_size);
66
67 if (midx_size < MIDX_MIN_SIZE) {
68 error(_("multi-pack-index file %s is too small"), midx_name);
69 goto cleanup_fail;
70 }
71
72 FREE_AND_NULL(midx_name);
73
74 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
Derrick Stolee6c7ff7c2020-04-24 09:17:16 -040075 close(fd);
Derrick Stolee4d805602018-07-12 15:39:23 -040076
Denton Liu577314c2019-04-03 15:00:05 -070077 FLEX_ALLOC_STR(m, object_dir, object_dir);
Derrick Stolee4d805602018-07-12 15:39:23 -040078 m->data = midx_map;
79 m->data_len = midx_size;
Derrick Stolee2cf489a2018-08-20 16:51:55 +000080 m->local = local;
Derrick Stolee4d805602018-07-12 15:39:23 -040081
82 m->signature = get_be32(m->data);
Derrick Stolee53ad0402018-09-13 11:02:15 -070083 if (m->signature != MIDX_SIGNATURE)
84 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
Derrick Stolee4d805602018-07-12 15:39:23 -040085 m->signature, MIDX_SIGNATURE);
Derrick Stolee4d805602018-07-12 15:39:23 -040086
87 m->version = m->data[MIDX_BYTE_FILE_VERSION];
Derrick Stolee53ad0402018-09-13 11:02:15 -070088 if (m->version != MIDX_VERSION)
89 die(_("multi-pack-index version %d not recognized"),
Derrick Stolee4d805602018-07-12 15:39:23 -040090 m->version);
Derrick Stolee4d805602018-07-12 15:39:23 -040091
92 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
Derrick Stolee53ad0402018-09-13 11:02:15 -070093 if (hash_version != MIDX_HASH_VERSION)
94 die(_("hash version %u does not match"), hash_version);
brian m. carlsonaaa95df2019-08-18 20:04:27 +000095 m->hash_len = the_hash_algo->rawsz;
Derrick Stolee4d805602018-07-12 15:39:23 -040096
97 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
98
99 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
100
Derrick Stolee32f3c542018-07-12 15:39:27 -0400101 for (i = 0; i < m->num_chunks; i++) {
102 uint32_t chunk_id = get_be32(m->data + MIDX_HEADER_SIZE +
103 MIDX_CHUNKLOOKUP_WIDTH * i);
104 uint64_t chunk_offset = get_be64(m->data + MIDX_HEADER_SIZE + 4 +
105 MIDX_CHUNKLOOKUP_WIDTH * i);
106
Derrick Stoleed3f8e212018-09-13 11:02:16 -0700107 if (chunk_offset >= m->data_len)
108 die(_("invalid chunk offset (too large)"));
109
Derrick Stolee32f3c542018-07-12 15:39:27 -0400110 switch (chunk_id) {
111 case MIDX_CHUNKID_PACKNAMES:
112 m->chunk_pack_names = m->data + chunk_offset;
113 break;
114
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400115 case MIDX_CHUNKID_OIDFANOUT:
116 m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset);
117 break;
118
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400119 case MIDX_CHUNKID_OIDLOOKUP:
120 m->chunk_oid_lookup = m->data + chunk_offset;
121 break;
122
Derrick Stolee662148c2018-07-12 15:39:32 -0400123 case MIDX_CHUNKID_OBJECTOFFSETS:
124 m->chunk_object_offsets = m->data + chunk_offset;
125 break;
126
127 case MIDX_CHUNKID_LARGEOFFSETS:
128 m->chunk_large_offsets = m->data + chunk_offset;
129 break;
130
Derrick Stolee32f3c542018-07-12 15:39:27 -0400131 case 0:
132 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
133 break;
134
135 default:
136 /*
137 * Do nothing on unrecognized chunks, allowing future
138 * extensions to add optional chunks.
139 */
140 break;
141 }
142 }
143
144 if (!m->chunk_pack_names)
145 die(_("multi-pack-index missing required pack-name chunk"));
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400146 if (!m->chunk_oid_fanout)
147 die(_("multi-pack-index missing required OID fanout chunk"));
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400148 if (!m->chunk_oid_lookup)
149 die(_("multi-pack-index missing required OID lookup chunk"));
Derrick Stolee662148c2018-07-12 15:39:32 -0400150 if (!m->chunk_object_offsets)
151 die(_("multi-pack-index missing required object offsets chunk"));
Derrick Stolee32f3c542018-07-12 15:39:27 -0400152
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400153 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
154
Derrick Stolee32275652018-07-12 15:39:28 -0400155 m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
Derrick Stolee3715a632018-07-12 15:39:34 -0400156 m->packs = xcalloc(m->num_packs, sizeof(*m->packs));
Derrick Stolee32275652018-07-12 15:39:28 -0400157
158 cur_pack_name = (const char *)m->chunk_pack_names;
159 for (i = 0; i < m->num_packs; i++) {
160 m->pack_names[i] = cur_pack_name;
161
162 cur_pack_name += strlen(cur_pack_name) + 1;
163
Derrick Stolee8e72a3c2018-09-13 11:02:18 -0700164 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
165 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
Derrick Stolee32275652018-07-12 15:39:28 -0400166 m->pack_names[i - 1],
167 m->pack_names[i]);
Derrick Stolee32275652018-07-12 15:39:28 -0400168 }
169
Jeff Hostetlerd8292232019-03-21 12:36:13 -0700170 trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
171 trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
172
Derrick Stolee4d805602018-07-12 15:39:23 -0400173 return m;
174
175cleanup_fail:
176 free(m);
177 free(midx_name);
178 if (midx_map)
179 munmap(midx_map, midx_size);
180 if (0 <= fd)
181 close(fd);
182 return NULL;
183}
184
Derrick Stolee1dcd9f22018-10-12 10:34:19 -0700185void close_midx(struct multi_pack_index *m)
Derrick Stoleea40498a2018-07-12 15:39:36 -0400186{
187 uint32_t i;
Derrick Stolee1dcd9f22018-10-12 10:34:19 -0700188
189 if (!m)
190 return;
191
Derrick Stoleea40498a2018-07-12 15:39:36 -0400192 munmap((unsigned char *)m->data, m->data_len);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400193
194 for (i = 0; i < m->num_packs; i++) {
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700195 if (m->packs[i])
196 m->packs[i]->multi_pack_index = 0;
Derrick Stoleea40498a2018-07-12 15:39:36 -0400197 }
198 FREE_AND_NULL(m->packs);
199 FREE_AND_NULL(m->pack_names);
200}
201
Derrick Stolee64404a22019-04-29 09:18:55 -0700202int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
Derrick Stolee3715a632018-07-12 15:39:34 -0400203{
204 struct strbuf pack_name = STRBUF_INIT;
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700205 struct packed_git *p;
Derrick Stolee3715a632018-07-12 15:39:34 -0400206
207 if (pack_int_id >= m->num_packs)
Jean-Noël Avilad355e462018-11-28 22:43:09 +0100208 die(_("bad pack-int-id: %u (%u total packs)"),
Derrick Stoleecc6af732018-09-13 11:02:25 -0700209 pack_int_id, m->num_packs);
Derrick Stolee3715a632018-07-12 15:39:34 -0400210
211 if (m->packs[pack_int_id])
212 return 0;
213
214 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
215 m->pack_names[pack_int_id]);
216
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700217 p = add_packed_git(pack_name.buf, pack_name.len, m->local);
Derrick Stolee3715a632018-07-12 15:39:34 -0400218 strbuf_release(&pack_name);
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700219
220 if (!p)
221 return 1;
222
223 p->multi_pack_index = 1;
224 m->packs[pack_int_id] = p;
225 install_packed_git(r, p);
226 list_add_tail(&p->mru, &r->objects->packed_git_mru);
227
228 return 0;
Derrick Stolee3715a632018-07-12 15:39:34 -0400229}
230
231int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
232{
233 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
brian m. carlsonaaa95df2019-08-18 20:04:27 +0000234 the_hash_algo->rawsz, result);
Derrick Stolee3715a632018-07-12 15:39:34 -0400235}
236
Derrick Stolee8aac67a2018-07-12 15:39:35 -0400237struct object_id *nth_midxed_object_oid(struct object_id *oid,
238 struct multi_pack_index *m,
239 uint32_t n)
240{
241 if (n >= m->num_objects)
242 return NULL;
243
244 hashcpy(oid->hash, m->chunk_oid_lookup + m->hash_len * n);
245 return oid;
246}
247
Derrick Stolee3715a632018-07-12 15:39:34 -0400248static off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
249{
250 const unsigned char *offset_data;
251 uint32_t offset32;
252
253 offset_data = m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH;
254 offset32 = get_be32(offset_data + sizeof(uint32_t));
255
256 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
Derrick Stoleed8ac9ee2018-09-13 11:02:23 -0700257 if (sizeof(off_t) < sizeof(uint64_t))
Derrick Stolee3715a632018-07-12 15:39:34 -0400258 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
259
260 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
261 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
262 }
263
264 return offset32;
265}
266
267static uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
268{
269 return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH);
270}
271
Derrick Stolee64404a22019-04-29 09:18:55 -0700272static int nth_midxed_pack_entry(struct repository *r,
273 struct multi_pack_index *m,
274 struct pack_entry *e,
275 uint32_t pos)
Derrick Stolee3715a632018-07-12 15:39:34 -0400276{
277 uint32_t pack_int_id;
278 struct packed_git *p;
279
280 if (pos >= m->num_objects)
281 return 0;
282
283 pack_int_id = nth_midxed_pack_int_id(m, pos);
284
Derrick Stolee64404a22019-04-29 09:18:55 -0700285 if (prepare_midx_pack(r, m, pack_int_id))
Derrick Stolee3715a632018-07-12 15:39:34 -0400286 die(_("error preparing packfile from multi-pack-index"));
287 p = m->packs[pack_int_id];
288
289 /*
290 * We are about to tell the caller where they can locate the
291 * requested object. We better make sure the packfile is
292 * still here and can be accessed before supplying that
293 * answer, as it may have been deleted since the MIDX was
294 * loaded!
295 */
296 if (!is_pack_valid(p))
297 return 0;
298
Derrick Stoleec39b02a2018-08-20 16:51:57 +0000299 if (p->num_bad_objects) {
300 uint32_t i;
301 struct object_id oid;
302 nth_midxed_object_oid(&oid, m, pos);
303 for (i = 0; i < p->num_bad_objects; i++)
Jeff Kinge43d2dc2018-10-02 17:19:21 -0400304 if (hasheq(oid.hash,
305 p->bad_object_sha1 + the_hash_algo->rawsz * i))
Derrick Stoleec39b02a2018-08-20 16:51:57 +0000306 return 0;
307 }
308
Derrick Stolee3715a632018-07-12 15:39:34 -0400309 e->offset = nth_midxed_offset(m, pos);
310 e->p = p;
311
312 return 1;
313}
314
Derrick Stolee64404a22019-04-29 09:18:55 -0700315int fill_midx_entry(struct repository * r,
316 const struct object_id *oid,
317 struct pack_entry *e,
318 struct multi_pack_index *m)
Derrick Stolee3715a632018-07-12 15:39:34 -0400319{
320 uint32_t pos;
321
322 if (!bsearch_midx(oid, m, &pos))
323 return 0;
324
Derrick Stolee64404a22019-04-29 09:18:55 -0700325 return nth_midxed_pack_entry(r, m, e, pos);
Derrick Stolee3715a632018-07-12 15:39:34 -0400326}
327
Jeff King013fd7a2019-04-05 14:06:04 -0400328/* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
329static int cmp_idx_or_pack_name(const char *idx_or_pack_name,
330 const char *idx_name)
331{
332 /* Skip past any initial matching prefix. */
333 while (*idx_name && *idx_name == *idx_or_pack_name) {
334 idx_name++;
335 idx_or_pack_name++;
336 }
337
338 /*
339 * If we didn't match completely, we may have matched "pack-1234." and
340 * be left with "idx" and "pack" respectively, which is also OK. We do
341 * not have to check for "idx" and "idx", because that would have been
342 * a complete match (and in that case these strcmps will be false, but
343 * we'll correctly return 0 from the final strcmp() below.
344 *
345 * Technically this matches "fooidx" and "foopack", but we'd never have
346 * such names in the first place.
347 */
348 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
349 return 0;
350
351 /*
352 * This not only checks for a complete match, but also orders based on
353 * the first non-identical character, which means our ordering will
354 * match a raw strcmp(). That makes it OK to use this to binary search
355 * a naively-sorted list.
356 */
357 return strcmp(idx_or_pack_name, idx_name);
358}
359
360int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
Derrick Stoleea40498a2018-07-12 15:39:36 -0400361{
362 uint32_t first = 0, last = m->num_packs;
363
364 while (first < last) {
365 uint32_t mid = first + (last - first) / 2;
366 const char *current;
367 int cmp;
368
369 current = m->pack_names[mid];
Jeff King013fd7a2019-04-05 14:06:04 -0400370 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400371 if (!cmp)
372 return 1;
373 if (cmp > 0) {
374 first = mid + 1;
375 continue;
376 }
377 last = mid;
378 }
379
380 return 0;
381}
382
Derrick Stolee2cf489a2018-08-20 16:51:55 +0000383int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
Derrick Stoleec4d25222018-07-12 15:39:33 -0400384{
Derrick Stolee29e20162018-08-20 16:52:00 +0000385 struct multi_pack_index *m;
Derrick Stoleec4d25222018-07-12 15:39:33 -0400386 struct multi_pack_index *m_search;
387 int config_value;
Derrick Stolee0465a502018-10-12 10:34:20 -0700388 static int env_value = -1;
Derrick Stoleec4d25222018-07-12 15:39:33 -0400389
Derrick Stolee0465a502018-10-12 10:34:20 -0700390 if (env_value < 0)
391 env_value = git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0);
392
393 if (!env_value &&
394 (repo_config_get_bool(r, "core.multipackindex", &config_value) ||
395 !config_value))
Derrick Stoleec4d25222018-07-12 15:39:33 -0400396 return 0;
397
Derrick Stolee29e20162018-08-20 16:52:00 +0000398 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
Derrick Stoleec4d25222018-07-12 15:39:33 -0400399 if (!strcmp(object_dir, m_search->object_dir))
400 return 1;
401
Derrick Stolee29e20162018-08-20 16:52:00 +0000402 m = load_multi_pack_index(object_dir, local);
Derrick Stoleec4d25222018-07-12 15:39:33 -0400403
Derrick Stolee29e20162018-08-20 16:52:00 +0000404 if (m) {
405 m->next = r->objects->multi_pack_index;
406 r->objects->multi_pack_index = m;
Derrick Stoleec4d25222018-07-12 15:39:33 -0400407 return 1;
408 }
409
410 return 0;
411}
412
Derrick Stoleefc59e742018-07-12 15:39:22 -0400413static size_t write_midx_header(struct hashfile *f,
414 unsigned char num_chunks,
415 uint32_t num_packs)
416{
417 unsigned char byte_values[4];
418
419 hashwrite_be32(f, MIDX_SIGNATURE);
420 byte_values[0] = MIDX_VERSION;
421 byte_values[1] = MIDX_HASH_VERSION;
422 byte_values[2] = num_chunks;
423 byte_values[3] = 0; /* unused */
424 hashwrite(f, byte_values, sizeof(byte_values));
425 hashwrite_be32(f, num_packs);
426
427 return MIDX_HEADER_SIZE;
428}
429
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700430struct pack_info {
431 uint32_t orig_pack_int_id;
432 char *pack_name;
433 struct packed_git *p;
Derrick Stolee19575c72019-06-10 16:35:25 -0700434 unsigned expired : 1;
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700435};
436
437static int pack_info_compare(const void *_a, const void *_b)
438{
439 struct pack_info *a = (struct pack_info *)_a;
440 struct pack_info *b = (struct pack_info *)_b;
441 return strcmp(a->pack_name, b->pack_name);
442}
443
Derrick Stolee396f2572018-07-12 15:39:26 -0400444struct pack_list {
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700445 struct pack_info *info;
Derrick Stolee396f2572018-07-12 15:39:26 -0400446 uint32_t nr;
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700447 uint32_t alloc;
Derrick Stoleea40498a2018-07-12 15:39:36 -0400448 struct multi_pack_index *m;
William Baker840cef02019-10-21 18:39:59 +0000449 struct progress *progress;
450 unsigned pack_paths_checked;
Derrick Stolee396f2572018-07-12 15:39:26 -0400451};
452
453static void add_pack_to_midx(const char *full_path, size_t full_path_len,
454 const char *file_name, void *data)
455{
456 struct pack_list *packs = (struct pack_list *)data;
457
458 if (ends_with(file_name, ".idx")) {
William Baker840cef02019-10-21 18:39:59 +0000459 display_progress(packs->progress, ++packs->pack_paths_checked);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400460 if (packs->m && midx_contains_pack(packs->m, file_name))
461 return;
462
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700463 ALLOC_GROW(packs->info, packs->nr + 1, packs->alloc);
Derrick Stolee396f2572018-07-12 15:39:26 -0400464
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700465 packs->info[packs->nr].p = add_packed_git(full_path,
466 full_path_len,
467 0);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400468
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700469 if (!packs->info[packs->nr].p) {
Derrick Stolee396f2572018-07-12 15:39:26 -0400470 warning(_("failed to add packfile '%s'"),
471 full_path);
472 return;
473 }
474
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700475 if (open_pack_index(packs->info[packs->nr].p)) {
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400476 warning(_("failed to open pack-index '%s'"),
477 full_path);
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700478 close_pack(packs->info[packs->nr].p);
479 FREE_AND_NULL(packs->info[packs->nr].p);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400480 return;
481 }
482
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700483 packs->info[packs->nr].pack_name = xstrdup(file_name);
484 packs->info[packs->nr].orig_pack_int_id = packs->nr;
Derrick Stolee19575c72019-06-10 16:35:25 -0700485 packs->info[packs->nr].expired = 0;
Derrick Stolee396f2572018-07-12 15:39:26 -0400486 packs->nr++;
487 }
488}
489
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400490struct pack_midx_entry {
491 struct object_id oid;
492 uint32_t pack_int_id;
493 time_t pack_mtime;
494 uint64_t offset;
495};
496
497static int midx_oid_compare(const void *_a, const void *_b)
498{
499 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
500 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
501 int cmp = oidcmp(&a->oid, &b->oid);
502
503 if (cmp)
504 return cmp;
505
506 if (a->pack_mtime > b->pack_mtime)
507 return -1;
508 else if (a->pack_mtime < b->pack_mtime)
509 return 1;
510
511 return a->pack_int_id - b->pack_int_id;
512}
513
Derrick Stoleea40498a2018-07-12 15:39:36 -0400514static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
Derrick Stoleea40498a2018-07-12 15:39:36 -0400515 struct pack_midx_entry *e,
516 uint32_t pos)
517{
518 if (pos >= m->num_objects)
519 return 1;
520
521 nth_midxed_object_oid(&e->oid, m, pos);
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700522 e->pack_int_id = nth_midxed_pack_int_id(m, pos);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400523 e->offset = nth_midxed_offset(m, pos);
524
525 /* consider objects in midx to be from "old" packs */
526 e->pack_mtime = 0;
527 return 0;
528}
529
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400530static void fill_pack_entry(uint32_t pack_int_id,
531 struct packed_git *p,
532 uint32_t cur_object,
533 struct pack_midx_entry *entry)
534{
Jeff King07636712020-02-23 23:27:36 -0500535 if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400536 die(_("failed to locate object %d in packfile"), cur_object);
537
538 entry->pack_int_id = pack_int_id;
539 entry->pack_mtime = p->mtime;
540
541 entry->offset = nth_packed_object_offset(p, cur_object);
542}
543
544/*
545 * It is possible to artificially get into a state where there are many
546 * duplicate copies of objects. That can create high memory pressure if
547 * we are to create a list of all objects before de-duplication. To reduce
548 * this memory pressure without a significant performance drop, automatically
549 * group objects by the first byte of their object id. Use the IDX fanout
550 * tables to group the data, copy to a local array, then sort.
551 *
552 * Copy only the de-duplicated entries (selected by most-recent modified time
553 * of a packfile containing the object).
554 */
Derrick Stoleea40498a2018-07-12 15:39:36 -0400555static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700556 struct pack_info *info,
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400557 uint32_t nr_packs,
558 uint32_t *nr_objects)
559{
560 uint32_t cur_fanout, cur_pack, cur_object;
561 uint32_t alloc_fanout, alloc_objects, total_objects = 0;
562 struct pack_midx_entry *entries_by_fanout = NULL;
563 struct pack_midx_entry *deduplicated_entries = NULL;
Derrick Stoleea40498a2018-07-12 15:39:36 -0400564 uint32_t start_pack = m ? m->num_packs : 0;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400565
Derrick Stoleea40498a2018-07-12 15:39:36 -0400566 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700567 total_objects += info[cur_pack].p->num_objects;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400568
569 /*
570 * As we de-duplicate by fanout value, we expect the fanout
571 * slices to be evenly distributed, with some noise. Hence,
572 * allocate slightly more than one 256th.
573 */
574 alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16;
575
576 ALLOC_ARRAY(entries_by_fanout, alloc_fanout);
577 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
578 *nr_objects = 0;
579
580 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
581 uint32_t nr_fanout = 0;
582
Derrick Stoleea40498a2018-07-12 15:39:36 -0400583 if (m) {
584 uint32_t start = 0, end;
585
586 if (cur_fanout)
587 start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
588 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
589
590 for (cur_object = start; cur_object < end; cur_object++) {
591 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700592 nth_midxed_pack_midx_entry(m,
Derrick Stoleea40498a2018-07-12 15:39:36 -0400593 &entries_by_fanout[nr_fanout],
594 cur_object);
595 nr_fanout++;
596 }
597 }
598
599 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400600 uint32_t start = 0, end;
601
602 if (cur_fanout)
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700603 start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1);
604 end = get_pack_fanout(info[cur_pack].p, cur_fanout);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400605
606 for (cur_object = start; cur_object < end; cur_object++) {
607 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700608 fill_pack_entry(cur_pack, info[cur_pack].p, cur_object, &entries_by_fanout[nr_fanout]);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400609 nr_fanout++;
610 }
611 }
612
613 QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
614
615 /*
616 * The batch is now sorted by OID and then mtime (descending).
617 * Take only the first duplicate.
618 */
619 for (cur_object = 0; cur_object < nr_fanout; cur_object++) {
Jeff Kinge43d2dc2018-10-02 17:19:21 -0400620 if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid,
621 &entries_by_fanout[cur_object].oid))
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400622 continue;
623
624 ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
625 memcpy(&deduplicated_entries[*nr_objects],
626 &entries_by_fanout[cur_object],
627 sizeof(struct pack_midx_entry));
628 (*nr_objects)++;
629 }
630 }
631
632 free(entries_by_fanout);
633 return deduplicated_entries;
634}
635
Derrick Stolee32f3c542018-07-12 15:39:27 -0400636static size_t write_midx_pack_names(struct hashfile *f,
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700637 struct pack_info *info,
Derrick Stolee32f3c542018-07-12 15:39:27 -0400638 uint32_t num_packs)
639{
640 uint32_t i;
641 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
642 size_t written = 0;
643
644 for (i = 0; i < num_packs; i++) {
Derrick Stolee19575c72019-06-10 16:35:25 -0700645 size_t writelen;
646
647 if (info[i].expired)
648 continue;
Derrick Stolee32f3c542018-07-12 15:39:27 -0400649
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700650 if (i && strcmp(info[i].pack_name, info[i - 1].pack_name) <= 0)
Derrick Stolee32f3c542018-07-12 15:39:27 -0400651 BUG("incorrect pack-file order: %s before %s",
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700652 info[i - 1].pack_name,
653 info[i].pack_name);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400654
Derrick Stolee19575c72019-06-10 16:35:25 -0700655 writelen = strlen(info[i].pack_name) + 1;
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700656 hashwrite(f, info[i].pack_name, writelen);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400657 written += writelen;
658 }
659
660 /* add padding to be aligned */
661 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
662 if (i < MIDX_CHUNK_ALIGNMENT) {
663 memset(padding, 0, sizeof(padding));
664 hashwrite(f, padding, i);
665 written += i;
666 }
667
668 return written;
669}
670
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400671static size_t write_midx_oid_fanout(struct hashfile *f,
672 struct pack_midx_entry *objects,
673 uint32_t nr_objects)
674{
675 struct pack_midx_entry *list = objects;
676 struct pack_midx_entry *last = objects + nr_objects;
677 uint32_t count = 0;
678 uint32_t i;
679
680 /*
681 * Write the first-level table (the list is sorted,
682 * but we use a 256-entry lookup to be able to avoid
683 * having to do eight extra binary search iterations).
684 */
685 for (i = 0; i < 256; i++) {
686 struct pack_midx_entry *next = list;
687
688 while (next < last && next->oid.hash[0] == i) {
689 count++;
690 next++;
691 }
692
693 hashwrite_be32(f, count);
694 list = next;
695 }
696
697 return MIDX_CHUNK_FANOUT_SIZE;
698}
699
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400700static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len,
701 struct pack_midx_entry *objects,
702 uint32_t nr_objects)
703{
704 struct pack_midx_entry *list = objects;
705 uint32_t i;
706 size_t written = 0;
707
708 for (i = 0; i < nr_objects; i++) {
709 struct pack_midx_entry *obj = list++;
710
711 if (i < nr_objects - 1) {
712 struct pack_midx_entry *next = list;
713 if (oidcmp(&obj->oid, &next->oid) >= 0)
714 BUG("OIDs not in order: %s >= %s",
715 oid_to_hex(&obj->oid),
716 oid_to_hex(&next->oid));
717 }
718
719 hashwrite(f, obj->oid.hash, (int)hash_len);
720 written += hash_len;
721 }
722
723 return written;
724}
725
Derrick Stolee662148c2018-07-12 15:39:32 -0400726static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed,
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700727 uint32_t *perm,
Derrick Stolee662148c2018-07-12 15:39:32 -0400728 struct pack_midx_entry *objects, uint32_t nr_objects)
729{
730 struct pack_midx_entry *list = objects;
731 uint32_t i, nr_large_offset = 0;
732 size_t written = 0;
733
734 for (i = 0; i < nr_objects; i++) {
735 struct pack_midx_entry *obj = list++;
736
Derrick Stolee19575c72019-06-10 16:35:25 -0700737 if (perm[obj->pack_int_id] == PACK_EXPIRED)
738 BUG("object %s is in an expired pack with int-id %d",
739 oid_to_hex(&obj->oid),
740 obj->pack_int_id);
741
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700742 hashwrite_be32(f, perm[obj->pack_int_id]);
Derrick Stolee662148c2018-07-12 15:39:32 -0400743
744 if (large_offset_needed && obj->offset >> 31)
745 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
746 else if (!large_offset_needed && obj->offset >> 32)
747 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
748 oid_to_hex(&obj->oid),
749 obj->offset);
750 else
751 hashwrite_be32(f, (uint32_t)obj->offset);
752
753 written += MIDX_CHUNK_OFFSET_WIDTH;
754 }
755
756 return written;
757}
758
759static size_t write_midx_large_offsets(struct hashfile *f, uint32_t nr_large_offset,
760 struct pack_midx_entry *objects, uint32_t nr_objects)
761{
Jeff King61b0fcb2018-11-03 22:27:46 -0400762 struct pack_midx_entry *list = objects, *end = objects + nr_objects;
Derrick Stolee662148c2018-07-12 15:39:32 -0400763 size_t written = 0;
764
765 while (nr_large_offset) {
Jeff King61b0fcb2018-11-03 22:27:46 -0400766 struct pack_midx_entry *obj;
767 uint64_t offset;
768
769 if (list >= end)
770 BUG("too many large-offset objects");
771
772 obj = list++;
773 offset = obj->offset;
Derrick Stolee662148c2018-07-12 15:39:32 -0400774
775 if (!(offset >> 31))
776 continue;
777
778 hashwrite_be32(f, offset >> 32);
779 hashwrite_be32(f, offset & 0xffffffffUL);
780 written += 2 * sizeof(uint32_t);
781
782 nr_large_offset--;
783 }
784
785 return written;
786}
787
Derrick Stolee19575c72019-06-10 16:35:25 -0700788static int write_midx_internal(const char *object_dir, struct multi_pack_index *m,
William Baker840cef02019-10-21 18:39:59 +0000789 struct string_list *packs_to_drop, unsigned flags)
Derrick Stoleea3407732018-07-12 15:39:21 -0400790{
Derrick Stolee32f3c542018-07-12 15:39:27 -0400791 unsigned char cur_chunk, num_chunks = 0;
Derrick Stoleefc59e742018-07-12 15:39:22 -0400792 char *midx_name;
Derrick Stolee396f2572018-07-12 15:39:26 -0400793 uint32_t i;
Derrick Stoleefc59e742018-07-12 15:39:22 -0400794 struct hashfile *f = NULL;
795 struct lock_file lk;
Derrick Stolee396f2572018-07-12 15:39:26 -0400796 struct pack_list packs;
Derrick Stolee32f3c542018-07-12 15:39:27 -0400797 uint32_t *pack_perm = NULL;
798 uint64_t written = 0;
799 uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
800 uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
Derrick Stolee662148c2018-07-12 15:39:32 -0400801 uint32_t nr_entries, num_large_offsets = 0;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400802 struct pack_midx_entry *entries = NULL;
William Baker840cef02019-10-21 18:39:59 +0000803 struct progress *progress = NULL;
Derrick Stolee662148c2018-07-12 15:39:32 -0400804 int large_offsets_needed = 0;
Derrick Stoleedba61752019-06-10 16:35:24 -0700805 int pack_name_concat_len = 0;
Derrick Stolee19575c72019-06-10 16:35:25 -0700806 int dropped_packs = 0;
807 int result = 0;
Derrick Stoleefc59e742018-07-12 15:39:22 -0400808
809 midx_name = get_midx_filename(object_dir);
810 if (safe_create_leading_directories(midx_name)) {
811 UNLEAK(midx_name);
812 die_errno(_("unable to create leading directories of %s"),
813 midx_name);
814 }
815
Derrick Stolee19575c72019-06-10 16:35:25 -0700816 if (m)
817 packs.m = m;
818 else
819 packs.m = load_multi_pack_index(object_dir, 1);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400820
Derrick Stolee396f2572018-07-12 15:39:26 -0400821 packs.nr = 0;
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700822 packs.alloc = packs.m ? packs.m->num_packs : 16;
823 packs.info = NULL;
824 ALLOC_ARRAY(packs.info, packs.alloc);
Derrick Stolee396f2572018-07-12 15:39:26 -0400825
Derrick Stoleea40498a2018-07-12 15:39:36 -0400826 if (packs.m) {
827 for (i = 0; i < packs.m->num_packs; i++) {
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700828 ALLOC_GROW(packs.info, packs.nr + 1, packs.alloc);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400829
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700830 packs.info[packs.nr].orig_pack_int_id = i;
831 packs.info[packs.nr].pack_name = xstrdup(packs.m->pack_names[i]);
832 packs.info[packs.nr].p = NULL;
Derrick Stolee19575c72019-06-10 16:35:25 -0700833 packs.info[packs.nr].expired = 0;
Derrick Stoleea40498a2018-07-12 15:39:36 -0400834 packs.nr++;
835 }
836 }
837
William Baker840cef02019-10-21 18:39:59 +0000838 packs.pack_paths_checked = 0;
839 if (flags & MIDX_PROGRESS)
840 packs.progress = start_progress(_("Adding packfiles to multi-pack-index"), 0);
841 else
842 packs.progress = NULL;
843
Derrick Stolee396f2572018-07-12 15:39:26 -0400844 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
William Baker840cef02019-10-21 18:39:59 +0000845 stop_progress(&packs.progress);
Derrick Stolee396f2572018-07-12 15:39:26 -0400846
Derrick Stolee19575c72019-06-10 16:35:25 -0700847 if (packs.m && packs.nr == packs.m->num_packs && !packs_to_drop)
Derrick Stoleea40498a2018-07-12 15:39:36 -0400848 goto cleanup;
849
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700850 entries = get_sorted_entries(packs.m, packs.info, packs.nr, &nr_entries);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400851
Derrick Stolee662148c2018-07-12 15:39:32 -0400852 for (i = 0; i < nr_entries; i++) {
853 if (entries[i].offset > 0x7fffffff)
854 num_large_offsets++;
855 if (entries[i].offset > 0xffffffff)
856 large_offsets_needed = 1;
857 }
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400858
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700859 QSORT(packs.info, packs.nr, pack_info_compare);
860
Derrick Stolee19575c72019-06-10 16:35:25 -0700861 if (packs_to_drop && packs_to_drop->nr) {
862 int drop_index = 0;
863 int missing_drops = 0;
864
865 for (i = 0; i < packs.nr && drop_index < packs_to_drop->nr; i++) {
866 int cmp = strcmp(packs.info[i].pack_name,
867 packs_to_drop->items[drop_index].string);
868
869 if (!cmp) {
870 drop_index++;
871 packs.info[i].expired = 1;
872 } else if (cmp > 0) {
873 error(_("did not see pack-file %s to drop"),
874 packs_to_drop->items[drop_index].string);
875 drop_index++;
876 missing_drops++;
877 i--;
878 } else {
879 packs.info[i].expired = 0;
880 }
881 }
882
883 if (missing_drops) {
884 result = 1;
885 goto cleanup;
886 }
887 }
888
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700889 /*
890 * pack_perm stores a permutation between pack-int-ids from the
891 * previous multi-pack-index to the new one we are writing:
892 *
893 * pack_perm[old_id] = new_id
894 */
895 ALLOC_ARRAY(pack_perm, packs.nr);
896 for (i = 0; i < packs.nr; i++) {
Derrick Stolee19575c72019-06-10 16:35:25 -0700897 if (packs.info[i].expired) {
898 dropped_packs++;
899 pack_perm[packs.info[i].orig_pack_int_id] = PACK_EXPIRED;
900 } else {
901 pack_perm[packs.info[i].orig_pack_int_id] = i - dropped_packs;
902 }
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700903 }
904
Derrick Stolee19575c72019-06-10 16:35:25 -0700905 for (i = 0; i < packs.nr; i++) {
906 if (!packs.info[i].expired)
907 pack_name_concat_len += strlen(packs.info[i].pack_name) + 1;
908 }
Derrick Stoleedba61752019-06-10 16:35:24 -0700909
910 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
911 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
912 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
913
Derrick Stoleefc59e742018-07-12 15:39:22 -0400914 hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
915 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
916 FREE_AND_NULL(midx_name);
917
Derrick Stoleea40498a2018-07-12 15:39:36 -0400918 if (packs.m)
919 close_midx(packs.m);
920
Derrick Stolee32f3c542018-07-12 15:39:27 -0400921 cur_chunk = 0;
Derrick Stolee662148c2018-07-12 15:39:32 -0400922 num_chunks = large_offsets_needed ? 5 : 4;
Derrick Stolee32f3c542018-07-12 15:39:27 -0400923
Damien Robert796d61c2020-03-28 23:18:22 +0100924 if (packs.nr - dropped_packs == 0) {
925 error(_("no pack files to index."));
926 result = 1;
927 goto cleanup;
928 }
929
Derrick Stolee19575c72019-06-10 16:35:25 -0700930 written = write_midx_header(f, num_chunks, packs.nr - dropped_packs);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400931
932 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
933 chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
934
935 cur_chunk++;
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400936 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
Derrick Stoleedba61752019-06-10 16:35:24 -0700937 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + pack_name_concat_len;
Derrick Stolee32f3c542018-07-12 15:39:27 -0400938
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400939 cur_chunk++;
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400940 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;
941 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE;
942
943 cur_chunk++;
Derrick Stolee662148c2018-07-12 15:39:32 -0400944 chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS;
brian m. carlsonaaa95df2019-08-18 20:04:27 +0000945 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * the_hash_algo->rawsz;
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400946
Derrick Stolee662148c2018-07-12 15:39:32 -0400947 cur_chunk++;
948 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_CHUNK_OFFSET_WIDTH;
949 if (large_offsets_needed) {
950 chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS;
951
952 cur_chunk++;
953 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] +
954 num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH;
955 }
956
957 chunk_ids[cur_chunk] = 0;
958
Derrick Stolee32f3c542018-07-12 15:39:27 -0400959 for (i = 0; i <= num_chunks; i++) {
960 if (i && chunk_offsets[i] < chunk_offsets[i - 1])
961 BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64,
962 chunk_offsets[i - 1],
963 chunk_offsets[i]);
964
965 if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT)
966 BUG("chunk offset %"PRIu64" is not properly aligned",
967 chunk_offsets[i]);
968
969 hashwrite_be32(f, chunk_ids[i]);
970 hashwrite_be32(f, chunk_offsets[i] >> 32);
971 hashwrite_be32(f, chunk_offsets[i]);
972
973 written += MIDX_CHUNKLOOKUP_WIDTH;
974 }
975
William Baker840cef02019-10-21 18:39:59 +0000976 if (flags & MIDX_PROGRESS)
977 progress = start_progress(_("Writing chunks to multi-pack-index"),
978 num_chunks);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400979 for (i = 0; i < num_chunks; i++) {
980 if (written != chunk_offsets[i])
981 BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32,
982 chunk_offsets[i],
983 written,
984 chunk_ids[i]);
985
986 switch (chunk_ids[i]) {
987 case MIDX_CHUNKID_PACKNAMES:
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700988 written += write_midx_pack_names(f, packs.info, packs.nr);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400989 break;
990
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400991 case MIDX_CHUNKID_OIDFANOUT:
992 written += write_midx_oid_fanout(f, entries, nr_entries);
993 break;
994
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400995 case MIDX_CHUNKID_OIDLOOKUP:
brian m. carlsonaaa95df2019-08-18 20:04:27 +0000996 written += write_midx_oid_lookup(f, the_hash_algo->rawsz, entries, nr_entries);
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400997 break;
998
Derrick Stolee662148c2018-07-12 15:39:32 -0400999 case MIDX_CHUNKID_OBJECTOFFSETS:
Derrick Stoleed01bf2e2019-06-10 16:35:24 -07001000 written += write_midx_object_offsets(f, large_offsets_needed, pack_perm, entries, nr_entries);
Derrick Stolee662148c2018-07-12 15:39:32 -04001001 break;
1002
1003 case MIDX_CHUNKID_LARGEOFFSETS:
1004 written += write_midx_large_offsets(f, num_large_offsets, entries, nr_entries);
1005 break;
1006
Derrick Stolee32f3c542018-07-12 15:39:27 -04001007 default:
1008 BUG("trying to write unknown chunk id %"PRIx32,
1009 chunk_ids[i]);
1010 }
William Baker840cef02019-10-21 18:39:59 +00001011
1012 display_progress(progress, i + 1);
Derrick Stolee32f3c542018-07-12 15:39:27 -04001013 }
William Baker840cef02019-10-21 18:39:59 +00001014 stop_progress(&progress);
Derrick Stolee32f3c542018-07-12 15:39:27 -04001015
1016 if (written != chunk_offsets[num_chunks])
1017 BUG("incorrect final offset %"PRIu64" != %"PRIu64,
1018 written,
1019 chunk_offsets[num_chunks]);
Derrick Stoleefc59e742018-07-12 15:39:22 -04001020
1021 finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
1022 commit_lock_file(&lk);
1023
Derrick Stoleea40498a2018-07-12 15:39:36 -04001024cleanup:
Derrick Stolee396f2572018-07-12 15:39:26 -04001025 for (i = 0; i < packs.nr; i++) {
Derrick Stoleed01bf2e2019-06-10 16:35:24 -07001026 if (packs.info[i].p) {
1027 close_pack(packs.info[i].p);
1028 free(packs.info[i].p);
Derrick Stolee396f2572018-07-12 15:39:26 -04001029 }
Derrick Stoleed01bf2e2019-06-10 16:35:24 -07001030 free(packs.info[i].pack_name);
Derrick Stolee396f2572018-07-12 15:39:26 -04001031 }
1032
Derrick Stoleed01bf2e2019-06-10 16:35:24 -07001033 free(packs.info);
Derrick Stoleefe1ed562018-07-12 15:39:29 -04001034 free(entries);
Derrick Stoleea40498a2018-07-12 15:39:36 -04001035 free(pack_perm);
1036 free(midx_name);
Derrick Stolee19575c72019-06-10 16:35:25 -07001037 return result;
1038}
1039
William Bakerefbc3ae2019-10-21 18:39:58 +00001040int write_midx_file(const char *object_dir, unsigned flags)
Derrick Stolee19575c72019-06-10 16:35:25 -07001041{
William Baker840cef02019-10-21 18:39:59 +00001042 return write_midx_internal(object_dir, NULL, NULL, flags);
Derrick Stoleea3407732018-07-12 15:39:21 -04001043}
Derrick Stolee525e18c2018-07-12 15:39:40 -04001044
Derrick Stolee1dcd9f22018-10-12 10:34:19 -07001045void clear_midx_file(struct repository *r)
Derrick Stolee525e18c2018-07-12 15:39:40 -04001046{
Junio C Hamano3b2f8a02019-01-04 13:33:32 -08001047 char *midx = get_midx_filename(r->objects->odb->path);
Derrick Stolee1dcd9f22018-10-12 10:34:19 -07001048
1049 if (r->objects && r->objects->multi_pack_index) {
1050 close_midx(r->objects->multi_pack_index);
1051 r->objects->multi_pack_index = NULL;
1052 }
Derrick Stolee525e18c2018-07-12 15:39:40 -04001053
1054 if (remove_path(midx)) {
1055 UNLEAK(midx);
1056 die(_("failed to clear multi-pack-index at %s"), midx);
1057 }
1058
1059 free(midx);
1060}
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001061
1062static int verify_midx_error;
1063
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001064static void midx_report(const char *fmt, ...)
1065{
1066 va_list ap;
1067 verify_midx_error = 1;
1068 va_start(ap, fmt);
1069 vfprintf(stderr, fmt, ap);
1070 fprintf(stderr, "\n");
1071 va_end(ap);
1072}
1073
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001074struct pair_pos_vs_id
1075{
1076 uint32_t pos;
1077 uint32_t pack_int_id;
1078};
1079
1080static int compare_pair_pos_vs_id(const void *_a, const void *_b)
1081{
1082 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
1083 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
1084
1085 return b->pack_int_id - a->pack_int_id;
1086}
1087
Jeff Hostetler430efb82019-03-21 12:36:14 -07001088/*
1089 * Limit calls to display_progress() for performance reasons.
1090 * The interval here was arbitrarily chosen.
1091 */
1092#define SPARSE_PROGRESS_INTERVAL (1 << 12)
1093#define midx_display_sparse_progress(progress, n) \
1094 do { \
1095 uint64_t _n = (n); \
1096 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1097 display_progress(progress, _n); \
1098 } while (0)
1099
William Bakerefbc3ae2019-10-21 18:39:58 +00001100int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001101{
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001102 struct pair_pos_vs_id *pairs = NULL;
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001103 uint32_t i;
William Bakerad600962019-10-21 18:40:01 +00001104 struct progress *progress = NULL;
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001105 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1106 verify_midx_error = 0;
1107
1108 if (!m)
1109 return 0;
1110
William Bakerad600962019-10-21 18:40:01 +00001111 if (flags & MIDX_PROGRESS)
1112 progress = start_progress(_("Looking for referenced packfiles"),
1113 m->num_packs);
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001114 for (i = 0; i < m->num_packs; i++) {
Derrick Stolee64404a22019-04-29 09:18:55 -07001115 if (prepare_midx_pack(r, m, i))
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001116 midx_report("failed to load pack in position %d", i);
Jeff Hostetler430efb82019-03-21 12:36:14 -07001117
1118 display_progress(progress, i + 1);
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001119 }
Jeff Hostetler430efb82019-03-21 12:36:14 -07001120 stop_progress(&progress);
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001121
Derrick Stolee2f23d3f2018-09-13 11:02:20 -07001122 for (i = 0; i < 255; i++) {
1123 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
1124 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]);
1125
1126 if (oid_fanout1 > oid_fanout2)
1127 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
1128 i, oid_fanout1, oid_fanout2, i + 1);
1129 }
1130
Damien Robert796d61c2020-03-28 23:18:22 +01001131 if (m->num_objects == 0) {
1132 midx_report(_("the midx contains no oid"));
1133 /*
1134 * Remaining tests assume that we have objects, so we can
1135 * return here.
1136 */
1137 return verify_midx_error;
1138 }
1139
William Bakerad600962019-10-21 18:40:01 +00001140 if (flags & MIDX_PROGRESS)
1141 progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1142 m->num_objects - 1);
Derrick Stolee55c56482018-09-13 11:02:22 -07001143 for (i = 0; i < m->num_objects - 1; i++) {
1144 struct object_id oid1, oid2;
1145
1146 nth_midxed_object_oid(&oid1, m, i);
1147 nth_midxed_object_oid(&oid2, m, i + 1);
1148
1149 if (oidcmp(&oid1, &oid2) >= 0)
1150 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1151 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
Derrick Stolee55c56482018-09-13 11:02:22 -07001152
Jeff Hostetler430efb82019-03-21 12:36:14 -07001153 midx_display_sparse_progress(progress, i + 1);
1154 }
1155 stop_progress(&progress);
1156
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001157 /*
1158 * Create an array mapping each object to its packfile id. Sort it
1159 * to group the objects by packfile. Use this permutation to visit
1160 * each of the objects and only require 1 packfile to be open at a
1161 * time.
1162 */
1163 ALLOC_ARRAY(pairs, m->num_objects);
1164 for (i = 0; i < m->num_objects; i++) {
1165 pairs[i].pos = i;
1166 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
1167 }
1168
William Bakerad600962019-10-21 18:40:01 +00001169 if (flags & MIDX_PROGRESS)
1170 progress = start_sparse_progress(_("Sorting objects by packfile"),
1171 m->num_objects);
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001172 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
1173 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
1174 stop_progress(&progress);
1175
William Bakerad600962019-10-21 18:40:01 +00001176 if (flags & MIDX_PROGRESS)
1177 progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
Derrick Stoleecc6af732018-09-13 11:02:25 -07001178 for (i = 0; i < m->num_objects; i++) {
1179 struct object_id oid;
1180 struct pack_entry e;
1181 off_t m_offset, p_offset;
1182
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001183 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
1184 m->packs[pairs[i-1].pack_int_id])
1185 {
1186 close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
1187 close_pack_index(m->packs[pairs[i-1].pack_int_id]);
1188 }
1189
1190 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1191
Derrick Stolee64404a22019-04-29 09:18:55 -07001192 if (!fill_midx_entry(r, &oid, &e, m)) {
Derrick Stoleecc6af732018-09-13 11:02:25 -07001193 midx_report(_("failed to load pack entry for oid[%d] = %s"),
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001194 pairs[i].pos, oid_to_hex(&oid));
Derrick Stoleecc6af732018-09-13 11:02:25 -07001195 continue;
1196 }
1197
1198 if (open_pack_index(e.p)) {
1199 midx_report(_("failed to load pack-index for packfile %s"),
1200 e.p->pack_name);
1201 break;
1202 }
1203
1204 m_offset = e.offset;
1205 p_offset = find_pack_entry_one(oid.hash, e.p);
1206
1207 if (m_offset != p_offset)
1208 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001209 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
Derrick Stolee144d7032018-09-13 11:02:26 -07001210
Jeff Hostetler430efb82019-03-21 12:36:14 -07001211 midx_display_sparse_progress(progress, i + 1);
Derrick Stoleecc6af732018-09-13 11:02:25 -07001212 }
Derrick Stolee144d7032018-09-13 11:02:26 -07001213 stop_progress(&progress);
Derrick Stoleecc6af732018-09-13 11:02:25 -07001214
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001215 free(pairs);
1216
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001217 return verify_midx_error;
1218}
Derrick Stoleecff97112019-06-10 16:35:23 -07001219
William Bakerefbc3ae2019-10-21 18:39:58 +00001220int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
Derrick Stoleecff97112019-06-10 16:35:23 -07001221{
Derrick Stolee19575c72019-06-10 16:35:25 -07001222 uint32_t i, *count, result = 0;
1223 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1224 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
William Baker8dc18f82019-10-21 18:40:00 +00001225 struct progress *progress = NULL;
Derrick Stolee19575c72019-06-10 16:35:25 -07001226
1227 if (!m)
1228 return 0;
1229
1230 count = xcalloc(m->num_packs, sizeof(uint32_t));
William Baker8dc18f82019-10-21 18:40:00 +00001231
1232 if (flags & MIDX_PROGRESS)
1233 progress = start_progress(_("Counting referenced objects"),
1234 m->num_objects);
Derrick Stolee19575c72019-06-10 16:35:25 -07001235 for (i = 0; i < m->num_objects; i++) {
1236 int pack_int_id = nth_midxed_pack_int_id(m, i);
1237 count[pack_int_id]++;
William Baker8dc18f82019-10-21 18:40:00 +00001238 display_progress(progress, i + 1);
Derrick Stolee19575c72019-06-10 16:35:25 -07001239 }
William Baker8dc18f82019-10-21 18:40:00 +00001240 stop_progress(&progress);
Derrick Stolee19575c72019-06-10 16:35:25 -07001241
William Baker8dc18f82019-10-21 18:40:00 +00001242 if (flags & MIDX_PROGRESS)
1243 progress = start_progress(_("Finding and deleting unreferenced packfiles"),
1244 m->num_packs);
Derrick Stolee19575c72019-06-10 16:35:25 -07001245 for (i = 0; i < m->num_packs; i++) {
1246 char *pack_name;
William Baker8dc18f82019-10-21 18:40:00 +00001247 display_progress(progress, i + 1);
Derrick Stolee19575c72019-06-10 16:35:25 -07001248
1249 if (count[i])
1250 continue;
1251
1252 if (prepare_midx_pack(r, m, i))
1253 continue;
1254
1255 if (m->packs[i]->pack_keep)
1256 continue;
1257
1258 pack_name = xstrdup(m->packs[i]->pack_name);
1259 close_pack(m->packs[i]);
1260
1261 string_list_insert(&packs_to_drop, m->pack_names[i]);
1262 unlink_pack_path(pack_name, 0);
1263 free(pack_name);
1264 }
William Baker8dc18f82019-10-21 18:40:00 +00001265 stop_progress(&progress);
Derrick Stolee19575c72019-06-10 16:35:25 -07001266
1267 free(count);
1268
1269 if (packs_to_drop.nr)
William Baker840cef02019-10-21 18:39:59 +00001270 result = write_midx_internal(object_dir, m, &packs_to_drop, flags);
Derrick Stolee19575c72019-06-10 16:35:25 -07001271
1272 string_list_clear(&packs_to_drop, 0);
1273 return result;
Derrick Stoleecff97112019-06-10 16:35:23 -07001274}
Derrick Stolee2af890b2019-06-10 16:35:26 -07001275
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001276struct repack_info {
1277 timestamp_t mtime;
1278 uint32_t referenced_objects;
1279 uint32_t pack_int_id;
1280};
1281
1282static int compare_by_mtime(const void *a_, const void *b_)
1283{
1284 const struct repack_info *a, *b;
1285
1286 a = (const struct repack_info *)a_;
1287 b = (const struct repack_info *)b_;
1288
1289 if (a->mtime < b->mtime)
1290 return -1;
1291 if (a->mtime > b->mtime)
1292 return 1;
1293 return 0;
1294}
1295
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001296static int fill_included_packs_all(struct repository *r,
1297 struct multi_pack_index *m,
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001298 unsigned char *include_pack)
1299{
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001300 uint32_t i, count = 0;
1301 int pack_kept_objects = 0;
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001302
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001303 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1304
1305 for (i = 0; i < m->num_packs; i++) {
1306 if (prepare_midx_pack(r, m, i))
1307 continue;
1308 if (!pack_kept_objects && m->packs[i]->pack_keep)
1309 continue;
1310
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001311 include_pack[i] = 1;
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001312 count++;
1313 }
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001314
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001315 return count < 2;
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001316}
1317
1318static int fill_included_packs_batch(struct repository *r,
1319 struct multi_pack_index *m,
1320 unsigned char *include_pack,
1321 size_t batch_size)
1322{
1323 uint32_t i, packs_to_repack;
1324 size_t total_size;
1325 struct repack_info *pack_info = xcalloc(m->num_packs, sizeof(struct repack_info));
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001326 int pack_kept_objects = 0;
1327
1328 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001329
1330 for (i = 0; i < m->num_packs; i++) {
1331 pack_info[i].pack_int_id = i;
1332
1333 if (prepare_midx_pack(r, m, i))
1334 continue;
1335
1336 pack_info[i].mtime = m->packs[i]->mtime;
1337 }
1338
1339 for (i = 0; batch_size && i < m->num_objects; i++) {
1340 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1341 pack_info[pack_int_id].referenced_objects++;
1342 }
1343
1344 QSORT(pack_info, m->num_packs, compare_by_mtime);
1345
1346 total_size = 0;
1347 packs_to_repack = 0;
1348 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1349 int pack_int_id = pack_info[i].pack_int_id;
1350 struct packed_git *p = m->packs[pack_int_id];
1351 size_t expected_size;
1352
1353 if (!p)
1354 continue;
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001355 if (!pack_kept_objects && p->pack_keep)
1356 continue;
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001357 if (open_pack_index(p) || !p->num_objects)
1358 continue;
1359
1360 expected_size = (size_t)(p->pack_size
1361 * pack_info[i].referenced_objects);
1362 expected_size /= p->num_objects;
1363
1364 if (expected_size >= batch_size)
1365 continue;
1366
1367 packs_to_repack++;
1368 total_size += expected_size;
1369 include_pack[pack_int_id] = 1;
1370 }
1371
1372 free(pack_info);
1373
1374 if (total_size < batch_size || packs_to_repack < 2)
1375 return 1;
1376
1377 return 0;
1378}
1379
William Bakerefbc3ae2019-10-21 18:39:58 +00001380int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
Derrick Stolee2af890b2019-06-10 16:35:26 -07001381{
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001382 int result = 0;
1383 uint32_t i;
1384 unsigned char *include_pack;
1385 struct child_process cmd = CHILD_PROCESS_INIT;
1386 struct strbuf base_name = STRBUF_INIT;
1387 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1388
Son Luong Ngoce11d86d2020-05-10 16:07:33 +00001389 /*
1390 * When updating the default for these configuration
1391 * variables in builtin/repack.c, these must be adjusted
1392 * to match.
1393 */
1394 int delta_base_offset = 1;
1395 int use_delta_islands = 0;
1396
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001397 if (!m)
1398 return 0;
1399
1400 include_pack = xcalloc(m->num_packs, sizeof(unsigned char));
1401
1402 if (batch_size) {
1403 if (fill_included_packs_batch(r, m, include_pack, batch_size))
1404 goto cleanup;
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001405 } else if (fill_included_packs_all(r, m, include_pack))
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001406 goto cleanup;
1407
Son Luong Ngoce11d86d2020-05-10 16:07:33 +00001408 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
1409 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
1410
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001411 argv_array_push(&cmd.args, "pack-objects");
1412
1413 strbuf_addstr(&base_name, object_dir);
1414 strbuf_addstr(&base_name, "/pack/pack");
1415 argv_array_push(&cmd.args, base_name.buf);
William Baker64d80e72019-10-21 18:40:02 +00001416
Son Luong Ngoce11d86d2020-05-10 16:07:33 +00001417 if (delta_base_offset)
1418 argv_array_push(&cmd.args, "--delta-base-offset");
1419 if (use_delta_islands)
1420 argv_array_push(&cmd.args, "--delta-islands");
1421
William Baker64d80e72019-10-21 18:40:02 +00001422 if (flags & MIDX_PROGRESS)
1423 argv_array_push(&cmd.args, "--progress");
1424 else
1425 argv_array_push(&cmd.args, "-q");
1426
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001427 strbuf_release(&base_name);
1428
1429 cmd.git_cmd = 1;
1430 cmd.in = cmd.out = -1;
1431
1432 if (start_command(&cmd)) {
1433 error(_("could not start pack-objects"));
1434 result = 1;
1435 goto cleanup;
1436 }
1437
1438 for (i = 0; i < m->num_objects; i++) {
1439 struct object_id oid;
1440 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1441
1442 if (!include_pack[pack_int_id])
1443 continue;
1444
1445 nth_midxed_object_oid(&oid, m, i);
1446 xwrite(cmd.in, oid_to_hex(&oid), the_hash_algo->hexsz);
1447 xwrite(cmd.in, "\n", 1);
1448 }
1449 close(cmd.in);
1450
1451 if (finish_command(&cmd)) {
1452 error(_("could not finish pack-objects"));
1453 result = 1;
1454 goto cleanup;
1455 }
1456
William Baker840cef02019-10-21 18:39:59 +00001457 result = write_midx_internal(object_dir, m, NULL, flags);
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001458 m = NULL;
1459
1460cleanup:
1461 if (m)
1462 close_midx(m);
1463 free(include_pack);
1464 return result;
Derrick Stolee2af890b2019-06-10 16:35:26 -07001465}