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