]>
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 DS |
9 | #include "midx.h" |
10 | ||
fc59e748 DS |
11 | #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */ |
12 | #define MIDX_VERSION 1 | |
4d80560c DS |
13 | #define MIDX_BYTE_FILE_VERSION 4 |
14 | #define MIDX_BYTE_HASH_VERSION 5 | |
15 | #define MIDX_BYTE_NUM_CHUNKS 6 | |
16 | #define MIDX_BYTE_NUM_PACKS 8 | |
fc59e748 DS |
17 | #define MIDX_HASH_VERSION 1 |
18 | #define MIDX_HEADER_SIZE 12 | |
4d80560c DS |
19 | #define MIDX_HASH_LEN 20 |
20 | #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + MIDX_HASH_LEN) | |
fc59e748 | 21 | |
662148c4 | 22 | #define MIDX_MAX_CHUNKS 5 |
32f3c541 DS |
23 | #define MIDX_CHUNK_ALIGNMENT 4 |
24 | #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */ | |
d7cacf29 | 25 | #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ |
0d5b3a5e | 26 | #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ |
662148c4 DS |
27 | #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */ |
28 | #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */ | |
32f3c541 | 29 | #define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t)) |
d7cacf29 | 30 | #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256) |
662148c4 DS |
31 | #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t)) |
32 | #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t)) | |
33 | #define MIDX_LARGE_OFFSET_NEEDED 0x80000000 | |
32f3c541 | 34 | |
fc59e748 DS |
35 | static char *get_midx_filename(const char *object_dir) |
36 | { | |
37 | return xstrfmt("%s/pack/multi-pack-index", object_dir); | |
38 | } | |
39 | ||
2cf489a3 | 40 | struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local) |
4d80560c DS |
41 | { |
42 | struct multi_pack_index *m = NULL; | |
43 | int fd; | |
44 | struct stat st; | |
45 | size_t midx_size; | |
46 | void *midx_map = NULL; | |
47 | uint32_t hash_version; | |
48 | char *midx_name = get_midx_filename(object_dir); | |
32f3c541 | 49 | uint32_t i; |
3227565c | 50 | const char *cur_pack_name; |
4d80560c DS |
51 | |
52 | fd = git_open(midx_name); | |
53 | ||
54 | if (fd < 0) | |
55 | goto cleanup_fail; | |
56 | if (fstat(fd, &st)) { | |
57 | error_errno(_("failed to read %s"), midx_name); | |
58 | goto cleanup_fail; | |
59 | } | |
60 | ||
61 | midx_size = xsize_t(st.st_size); | |
62 | ||
63 | if (midx_size < MIDX_MIN_SIZE) { | |
64 | error(_("multi-pack-index file %s is too small"), midx_name); | |
65 | goto cleanup_fail; | |
66 | } | |
67 | ||
68 | FREE_AND_NULL(midx_name); | |
69 | ||
70 | midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
71 | ||
72 | FLEX_ALLOC_MEM(m, object_dir, object_dir, strlen(object_dir)); | |
73 | m->fd = fd; | |
74 | m->data = midx_map; | |
75 | m->data_len = midx_size; | |
2cf489a3 | 76 | m->local = local; |
4d80560c DS |
77 | |
78 | m->signature = get_be32(m->data); | |
79 | if (m->signature != MIDX_SIGNATURE) { | |
80 | error(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"), | |
81 | m->signature, MIDX_SIGNATURE); | |
82 | goto cleanup_fail; | |
83 | } | |
84 | ||
85 | m->version = m->data[MIDX_BYTE_FILE_VERSION]; | |
86 | if (m->version != MIDX_VERSION) { | |
87 | error(_("multi-pack-index version %d not recognized"), | |
88 | m->version); | |
89 | goto cleanup_fail; | |
90 | } | |
91 | ||
92 | hash_version = m->data[MIDX_BYTE_HASH_VERSION]; | |
93 | if (hash_version != MIDX_HASH_VERSION) { | |
94 | error(_("hash version %u does not match"), hash_version); | |
95 | goto cleanup_fail; | |
96 | } | |
97 | m->hash_len = MIDX_HASH_LEN; | |
98 | ||
99 | m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS]; | |
100 | ||
101 | m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS); | |
102 | ||
32f3c541 DS |
103 | for (i = 0; i < m->num_chunks; i++) { |
104 | uint32_t chunk_id = get_be32(m->data + MIDX_HEADER_SIZE + | |
105 | MIDX_CHUNKLOOKUP_WIDTH * i); | |
106 | uint64_t chunk_offset = get_be64(m->data + MIDX_HEADER_SIZE + 4 + | |
107 | MIDX_CHUNKLOOKUP_WIDTH * i); | |
108 | ||
109 | switch (chunk_id) { | |
110 | case MIDX_CHUNKID_PACKNAMES: | |
111 | m->chunk_pack_names = m->data + chunk_offset; | |
112 | break; | |
113 | ||
d7cacf29 DS |
114 | case MIDX_CHUNKID_OIDFANOUT: |
115 | m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset); | |
116 | break; | |
117 | ||
0d5b3a5e DS |
118 | case MIDX_CHUNKID_OIDLOOKUP: |
119 | m->chunk_oid_lookup = m->data + chunk_offset; | |
120 | break; | |
121 | ||
662148c4 DS |
122 | case MIDX_CHUNKID_OBJECTOFFSETS: |
123 | m->chunk_object_offsets = m->data + chunk_offset; | |
124 | break; | |
125 | ||
126 | case MIDX_CHUNKID_LARGEOFFSETS: | |
127 | m->chunk_large_offsets = m->data + chunk_offset; | |
128 | break; | |
129 | ||
32f3c541 DS |
130 | case 0: |
131 | die(_("terminating multi-pack-index chunk id appears earlier than expected")); | |
132 | break; | |
133 | ||
134 | default: | |
135 | /* | |
136 | * Do nothing on unrecognized chunks, allowing future | |
137 | * extensions to add optional chunks. | |
138 | */ | |
139 | break; | |
140 | } | |
141 | } | |
142 | ||
143 | if (!m->chunk_pack_names) | |
144 | die(_("multi-pack-index missing required pack-name chunk")); | |
d7cacf29 DS |
145 | if (!m->chunk_oid_fanout) |
146 | die(_("multi-pack-index missing required OID fanout chunk")); | |
0d5b3a5e DS |
147 | if (!m->chunk_oid_lookup) |
148 | die(_("multi-pack-index missing required OID lookup chunk")); | |
662148c4 DS |
149 | if (!m->chunk_object_offsets) |
150 | die(_("multi-pack-index missing required object offsets chunk")); | |
32f3c541 | 151 | |
d7cacf29 DS |
152 | m->num_objects = ntohl(m->chunk_oid_fanout[255]); |
153 | ||
3227565c | 154 | m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names)); |
3715a633 | 155 | m->packs = xcalloc(m->num_packs, sizeof(*m->packs)); |
3227565c DS |
156 | |
157 | cur_pack_name = (const char *)m->chunk_pack_names; | |
158 | for (i = 0; i < m->num_packs; i++) { | |
159 | m->pack_names[i] = cur_pack_name; | |
160 | ||
161 | cur_pack_name += strlen(cur_pack_name) + 1; | |
162 | ||
163 | if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0) { | |
164 | error(_("multi-pack-index pack names out of order: '%s' before '%s'"), | |
165 | m->pack_names[i - 1], | |
166 | m->pack_names[i]); | |
167 | goto cleanup_fail; | |
168 | } | |
169 | } | |
170 | ||
4d80560c DS |
171 | return m; |
172 | ||
173 | cleanup_fail: | |
174 | free(m); | |
175 | free(midx_name); | |
176 | if (midx_map) | |
177 | munmap(midx_map, midx_size); | |
178 | if (0 <= fd) | |
179 | close(fd); | |
180 | return NULL; | |
181 | } | |
182 | ||
1dcd9f20 | 183 | void close_midx(struct multi_pack_index *m) |
a40498a1 DS |
184 | { |
185 | uint32_t i; | |
1dcd9f20 DS |
186 | |
187 | if (!m) | |
188 | return; | |
189 | ||
a40498a1 DS |
190 | munmap((unsigned char *)m->data, m->data_len); |
191 | close(m->fd); | |
192 | m->fd = -1; | |
193 | ||
194 | for (i = 0; i < m->num_packs; i++) { | |
195 | if (m->packs[i]) { | |
196 | close_pack(m->packs[i]); | |
0ce4ff94 | 197 | free(m->packs[i]); |
a40498a1 DS |
198 | } |
199 | } | |
200 | FREE_AND_NULL(m->packs); | |
201 | FREE_AND_NULL(m->pack_names); | |
202 | } | |
203 | ||
0bff5269 | 204 | int prepare_midx_pack(struct multi_pack_index *m, uint32_t pack_int_id) |
3715a633 DS |
205 | { |
206 | struct strbuf pack_name = STRBUF_INIT; | |
207 | ||
208 | if (pack_int_id >= m->num_packs) | |
209 | BUG("bad pack-int-id"); | |
210 | ||
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 | ||
2cf489a3 | 217 | m->packs[pack_int_id] = add_packed_git(pack_name.buf, pack_name.len, m->local); |
3715a633 DS |
218 | strbuf_release(&pack_name); |
219 | return !m->packs[pack_int_id]; | |
220 | } | |
221 | ||
222 | int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result) | |
223 | { | |
224 | return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup, | |
225 | MIDX_HASH_LEN, result); | |
226 | } | |
227 | ||
8aac67a1 DS |
228 | struct object_id *nth_midxed_object_oid(struct object_id *oid, |
229 | struct multi_pack_index *m, | |
230 | uint32_t n) | |
231 | { | |
232 | if (n >= m->num_objects) | |
233 | return NULL; | |
234 | ||
235 | hashcpy(oid->hash, m->chunk_oid_lookup + m->hash_len * n); | |
236 | return oid; | |
237 | } | |
238 | ||
3715a633 DS |
239 | static off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos) |
240 | { | |
241 | const unsigned char *offset_data; | |
242 | uint32_t offset32; | |
243 | ||
244 | offset_data = m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH; | |
245 | offset32 = get_be32(offset_data + sizeof(uint32_t)); | |
246 | ||
247 | if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) { | |
248 | if (sizeof(offset32) < sizeof(uint64_t)) | |
249 | die(_("multi-pack-index stores a 64-bit offset, but off_t is too small")); | |
250 | ||
251 | offset32 ^= MIDX_LARGE_OFFSET_NEEDED; | |
252 | return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32); | |
253 | } | |
254 | ||
255 | return offset32; | |
256 | } | |
257 | ||
258 | static uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos) | |
259 | { | |
260 | return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH); | |
261 | } | |
262 | ||
263 | static int nth_midxed_pack_entry(struct multi_pack_index *m, struct pack_entry *e, uint32_t pos) | |
264 | { | |
265 | uint32_t pack_int_id; | |
266 | struct packed_git *p; | |
267 | ||
268 | if (pos >= m->num_objects) | |
269 | return 0; | |
270 | ||
271 | pack_int_id = nth_midxed_pack_int_id(m, pos); | |
272 | ||
273 | if (prepare_midx_pack(m, pack_int_id)) | |
274 | die(_("error preparing packfile from multi-pack-index")); | |
275 | p = m->packs[pack_int_id]; | |
276 | ||
277 | /* | |
278 | * We are about to tell the caller where they can locate the | |
279 | * requested object. We better make sure the packfile is | |
280 | * still here and can be accessed before supplying that | |
281 | * answer, as it may have been deleted since the MIDX was | |
282 | * loaded! | |
283 | */ | |
284 | if (!is_pack_valid(p)) | |
285 | return 0; | |
286 | ||
c39b02ae DS |
287 | if (p->num_bad_objects) { |
288 | uint32_t i; | |
289 | struct object_id oid; | |
290 | nth_midxed_object_oid(&oid, m, pos); | |
291 | for (i = 0; i < p->num_bad_objects; i++) | |
292 | if (!hashcmp(oid.hash, | |
293 | p->bad_object_sha1 + the_hash_algo->rawsz * i)) | |
294 | return 0; | |
295 | } | |
296 | ||
3715a633 DS |
297 | e->offset = nth_midxed_offset(m, pos); |
298 | e->p = p; | |
299 | ||
300 | return 1; | |
301 | } | |
302 | ||
303 | int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct multi_pack_index *m) | |
304 | { | |
305 | uint32_t pos; | |
306 | ||
307 | if (!bsearch_midx(oid, m, &pos)) | |
308 | return 0; | |
309 | ||
310 | return nth_midxed_pack_entry(m, e, pos); | |
311 | } | |
312 | ||
a40498a1 DS |
313 | int midx_contains_pack(struct multi_pack_index *m, const char *idx_name) |
314 | { | |
315 | uint32_t first = 0, last = m->num_packs; | |
316 | ||
317 | while (first < last) { | |
318 | uint32_t mid = first + (last - first) / 2; | |
319 | const char *current; | |
320 | int cmp; | |
321 | ||
322 | current = m->pack_names[mid]; | |
323 | cmp = strcmp(idx_name, current); | |
324 | if (!cmp) | |
325 | return 1; | |
326 | if (cmp > 0) { | |
327 | first = mid + 1; | |
328 | continue; | |
329 | } | |
330 | last = mid; | |
331 | } | |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
2cf489a3 | 336 | int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local) |
c4d25228 | 337 | { |
29e2016b | 338 | struct multi_pack_index *m; |
c4d25228 DS |
339 | struct multi_pack_index *m_search; |
340 | int config_value; | |
341 | ||
342 | if (repo_config_get_bool(r, "core.multipackindex", &config_value) || | |
343 | !config_value) | |
344 | return 0; | |
345 | ||
29e2016b | 346 | for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next) |
c4d25228 DS |
347 | if (!strcmp(object_dir, m_search->object_dir)) |
348 | return 1; | |
349 | ||
29e2016b | 350 | m = load_multi_pack_index(object_dir, local); |
c4d25228 | 351 | |
29e2016b DS |
352 | if (m) { |
353 | m->next = r->objects->multi_pack_index; | |
354 | r->objects->multi_pack_index = m; | |
c4d25228 DS |
355 | return 1; |
356 | } | |
357 | ||
358 | return 0; | |
359 | } | |
360 | ||
fc59e748 DS |
361 | static size_t write_midx_header(struct hashfile *f, |
362 | unsigned char num_chunks, | |
363 | uint32_t num_packs) | |
364 | { | |
365 | unsigned char byte_values[4]; | |
366 | ||
367 | hashwrite_be32(f, MIDX_SIGNATURE); | |
368 | byte_values[0] = MIDX_VERSION; | |
369 | byte_values[1] = MIDX_HASH_VERSION; | |
370 | byte_values[2] = num_chunks; | |
371 | byte_values[3] = 0; /* unused */ | |
372 | hashwrite(f, byte_values, sizeof(byte_values)); | |
373 | hashwrite_be32(f, num_packs); | |
374 | ||
375 | return MIDX_HEADER_SIZE; | |
376 | } | |
377 | ||
396f2570 DS |
378 | struct pack_list { |
379 | struct packed_git **list; | |
32f3c541 | 380 | char **names; |
396f2570 DS |
381 | uint32_t nr; |
382 | uint32_t alloc_list; | |
32f3c541 DS |
383 | uint32_t alloc_names; |
384 | size_t pack_name_concat_len; | |
a40498a1 | 385 | struct multi_pack_index *m; |
396f2570 DS |
386 | }; |
387 | ||
388 | static void add_pack_to_midx(const char *full_path, size_t full_path_len, | |
389 | const char *file_name, void *data) | |
390 | { | |
391 | struct pack_list *packs = (struct pack_list *)data; | |
392 | ||
393 | if (ends_with(file_name, ".idx")) { | |
a40498a1 DS |
394 | if (packs->m && midx_contains_pack(packs->m, file_name)) |
395 | return; | |
396 | ||
396f2570 | 397 | ALLOC_GROW(packs->list, packs->nr + 1, packs->alloc_list); |
32f3c541 | 398 | ALLOC_GROW(packs->names, packs->nr + 1, packs->alloc_names); |
396f2570 DS |
399 | |
400 | packs->list[packs->nr] = add_packed_git(full_path, | |
401 | full_path_len, | |
402 | 0); | |
fe1ed56f | 403 | |
396f2570 DS |
404 | if (!packs->list[packs->nr]) { |
405 | warning(_("failed to add packfile '%s'"), | |
406 | full_path); | |
407 | return; | |
408 | } | |
409 | ||
fe1ed56f DS |
410 | if (open_pack_index(packs->list[packs->nr])) { |
411 | warning(_("failed to open pack-index '%s'"), | |
412 | full_path); | |
413 | close_pack(packs->list[packs->nr]); | |
414 | FREE_AND_NULL(packs->list[packs->nr]); | |
415 | return; | |
416 | } | |
417 | ||
32f3c541 DS |
418 | packs->names[packs->nr] = xstrdup(file_name); |
419 | packs->pack_name_concat_len += strlen(file_name) + 1; | |
396f2570 DS |
420 | packs->nr++; |
421 | } | |
422 | } | |
423 | ||
32f3c541 DS |
424 | struct pack_pair { |
425 | uint32_t pack_int_id; | |
426 | char *pack_name; | |
427 | }; | |
428 | ||
429 | static int pack_pair_compare(const void *_a, const void *_b) | |
430 | { | |
431 | struct pack_pair *a = (struct pack_pair *)_a; | |
432 | struct pack_pair *b = (struct pack_pair *)_b; | |
433 | return strcmp(a->pack_name, b->pack_name); | |
434 | } | |
435 | ||
436 | static void sort_packs_by_name(char **pack_names, uint32_t nr_packs, uint32_t *perm) | |
437 | { | |
438 | uint32_t i; | |
439 | struct pack_pair *pairs; | |
440 | ||
441 | ALLOC_ARRAY(pairs, nr_packs); | |
442 | ||
443 | for (i = 0; i < nr_packs; i++) { | |
444 | pairs[i].pack_int_id = i; | |
445 | pairs[i].pack_name = pack_names[i]; | |
446 | } | |
447 | ||
448 | QSORT(pairs, nr_packs, pack_pair_compare); | |
449 | ||
450 | for (i = 0; i < nr_packs; i++) { | |
451 | pack_names[i] = pairs[i].pack_name; | |
452 | perm[pairs[i].pack_int_id] = i; | |
453 | } | |
454 | ||
455 | free(pairs); | |
456 | } | |
457 | ||
fe1ed56f DS |
458 | struct pack_midx_entry { |
459 | struct object_id oid; | |
460 | uint32_t pack_int_id; | |
461 | time_t pack_mtime; | |
462 | uint64_t offset; | |
463 | }; | |
464 | ||
465 | static int midx_oid_compare(const void *_a, const void *_b) | |
466 | { | |
467 | const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a; | |
468 | const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b; | |
469 | int cmp = oidcmp(&a->oid, &b->oid); | |
470 | ||
471 | if (cmp) | |
472 | return cmp; | |
473 | ||
474 | if (a->pack_mtime > b->pack_mtime) | |
475 | return -1; | |
476 | else if (a->pack_mtime < b->pack_mtime) | |
477 | return 1; | |
478 | ||
479 | return a->pack_int_id - b->pack_int_id; | |
480 | } | |
481 | ||
a40498a1 DS |
482 | static int nth_midxed_pack_midx_entry(struct multi_pack_index *m, |
483 | uint32_t *pack_perm, | |
484 | struct pack_midx_entry *e, | |
485 | uint32_t pos) | |
486 | { | |
487 | if (pos >= m->num_objects) | |
488 | return 1; | |
489 | ||
490 | nth_midxed_object_oid(&e->oid, m, pos); | |
491 | e->pack_int_id = pack_perm[nth_midxed_pack_int_id(m, pos)]; | |
492 | e->offset = nth_midxed_offset(m, pos); | |
493 | ||
494 | /* consider objects in midx to be from "old" packs */ | |
495 | e->pack_mtime = 0; | |
496 | return 0; | |
497 | } | |
498 | ||
fe1ed56f DS |
499 | static void fill_pack_entry(uint32_t pack_int_id, |
500 | struct packed_git *p, | |
501 | uint32_t cur_object, | |
502 | struct pack_midx_entry *entry) | |
503 | { | |
504 | if (!nth_packed_object_oid(&entry->oid, p, cur_object)) | |
505 | die(_("failed to locate object %d in packfile"), cur_object); | |
506 | ||
507 | entry->pack_int_id = pack_int_id; | |
508 | entry->pack_mtime = p->mtime; | |
509 | ||
510 | entry->offset = nth_packed_object_offset(p, cur_object); | |
511 | } | |
512 | ||
513 | /* | |
514 | * It is possible to artificially get into a state where there are many | |
515 | * duplicate copies of objects. That can create high memory pressure if | |
516 | * we are to create a list of all objects before de-duplication. To reduce | |
517 | * this memory pressure without a significant performance drop, automatically | |
518 | * group objects by the first byte of their object id. Use the IDX fanout | |
519 | * tables to group the data, copy to a local array, then sort. | |
520 | * | |
521 | * Copy only the de-duplicated entries (selected by most-recent modified time | |
522 | * of a packfile containing the object). | |
523 | */ | |
a40498a1 DS |
524 | static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m, |
525 | struct packed_git **p, | |
fe1ed56f DS |
526 | uint32_t *perm, |
527 | uint32_t nr_packs, | |
528 | uint32_t *nr_objects) | |
529 | { | |
530 | uint32_t cur_fanout, cur_pack, cur_object; | |
531 | uint32_t alloc_fanout, alloc_objects, total_objects = 0; | |
532 | struct pack_midx_entry *entries_by_fanout = NULL; | |
533 | struct pack_midx_entry *deduplicated_entries = NULL; | |
a40498a1 | 534 | uint32_t start_pack = m ? m->num_packs : 0; |
fe1ed56f | 535 | |
a40498a1 | 536 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) |
fe1ed56f DS |
537 | total_objects += p[cur_pack]->num_objects; |
538 | ||
539 | /* | |
540 | * As we de-duplicate by fanout value, we expect the fanout | |
541 | * slices to be evenly distributed, with some noise. Hence, | |
542 | * allocate slightly more than one 256th. | |
543 | */ | |
544 | alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16; | |
545 | ||
546 | ALLOC_ARRAY(entries_by_fanout, alloc_fanout); | |
547 | ALLOC_ARRAY(deduplicated_entries, alloc_objects); | |
548 | *nr_objects = 0; | |
549 | ||
550 | for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) { | |
551 | uint32_t nr_fanout = 0; | |
552 | ||
a40498a1 DS |
553 | if (m) { |
554 | uint32_t start = 0, end; | |
555 | ||
556 | if (cur_fanout) | |
557 | start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]); | |
558 | end = ntohl(m->chunk_oid_fanout[cur_fanout]); | |
559 | ||
560 | for (cur_object = start; cur_object < end; cur_object++) { | |
561 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); | |
562 | nth_midxed_pack_midx_entry(m, perm, | |
563 | &entries_by_fanout[nr_fanout], | |
564 | cur_object); | |
565 | nr_fanout++; | |
566 | } | |
567 | } | |
568 | ||
569 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) { | |
fe1ed56f DS |
570 | uint32_t start = 0, end; |
571 | ||
572 | if (cur_fanout) | |
573 | start = get_pack_fanout(p[cur_pack], cur_fanout - 1); | |
574 | end = get_pack_fanout(p[cur_pack], cur_fanout); | |
575 | ||
576 | for (cur_object = start; cur_object < end; cur_object++) { | |
577 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); | |
578 | fill_pack_entry(perm[cur_pack], p[cur_pack], cur_object, &entries_by_fanout[nr_fanout]); | |
579 | nr_fanout++; | |
580 | } | |
581 | } | |
582 | ||
583 | QSORT(entries_by_fanout, nr_fanout, midx_oid_compare); | |
584 | ||
585 | /* | |
586 | * The batch is now sorted by OID and then mtime (descending). | |
587 | * Take only the first duplicate. | |
588 | */ | |
589 | for (cur_object = 0; cur_object < nr_fanout; cur_object++) { | |
590 | if (cur_object && !oidcmp(&entries_by_fanout[cur_object - 1].oid, | |
591 | &entries_by_fanout[cur_object].oid)) | |
592 | continue; | |
593 | ||
594 | ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects); | |
595 | memcpy(&deduplicated_entries[*nr_objects], | |
596 | &entries_by_fanout[cur_object], | |
597 | sizeof(struct pack_midx_entry)); | |
598 | (*nr_objects)++; | |
599 | } | |
600 | } | |
601 | ||
602 | free(entries_by_fanout); | |
603 | return deduplicated_entries; | |
604 | } | |
605 | ||
32f3c541 DS |
606 | static size_t write_midx_pack_names(struct hashfile *f, |
607 | char **pack_names, | |
608 | uint32_t num_packs) | |
609 | { | |
610 | uint32_t i; | |
611 | unsigned char padding[MIDX_CHUNK_ALIGNMENT]; | |
612 | size_t written = 0; | |
613 | ||
614 | for (i = 0; i < num_packs; i++) { | |
615 | size_t writelen = strlen(pack_names[i]) + 1; | |
616 | ||
617 | if (i && strcmp(pack_names[i], pack_names[i - 1]) <= 0) | |
618 | BUG("incorrect pack-file order: %s before %s", | |
619 | pack_names[i - 1], | |
620 | pack_names[i]); | |
621 | ||
622 | hashwrite(f, pack_names[i], writelen); | |
623 | written += writelen; | |
624 | } | |
625 | ||
626 | /* add padding to be aligned */ | |
627 | i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT); | |
628 | if (i < MIDX_CHUNK_ALIGNMENT) { | |
629 | memset(padding, 0, sizeof(padding)); | |
630 | hashwrite(f, padding, i); | |
631 | written += i; | |
632 | } | |
633 | ||
634 | return written; | |
635 | } | |
636 | ||
d7cacf29 DS |
637 | static size_t write_midx_oid_fanout(struct hashfile *f, |
638 | struct pack_midx_entry *objects, | |
639 | uint32_t nr_objects) | |
640 | { | |
641 | struct pack_midx_entry *list = objects; | |
642 | struct pack_midx_entry *last = objects + nr_objects; | |
643 | uint32_t count = 0; | |
644 | uint32_t i; | |
645 | ||
646 | /* | |
647 | * Write the first-level table (the list is sorted, | |
648 | * but we use a 256-entry lookup to be able to avoid | |
649 | * having to do eight extra binary search iterations). | |
650 | */ | |
651 | for (i = 0; i < 256; i++) { | |
652 | struct pack_midx_entry *next = list; | |
653 | ||
654 | while (next < last && next->oid.hash[0] == i) { | |
655 | count++; | |
656 | next++; | |
657 | } | |
658 | ||
659 | hashwrite_be32(f, count); | |
660 | list = next; | |
661 | } | |
662 | ||
663 | return MIDX_CHUNK_FANOUT_SIZE; | |
664 | } | |
665 | ||
0d5b3a5e DS |
666 | static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len, |
667 | struct pack_midx_entry *objects, | |
668 | uint32_t nr_objects) | |
669 | { | |
670 | struct pack_midx_entry *list = objects; | |
671 | uint32_t i; | |
672 | size_t written = 0; | |
673 | ||
674 | for (i = 0; i < nr_objects; i++) { | |
675 | struct pack_midx_entry *obj = list++; | |
676 | ||
677 | if (i < nr_objects - 1) { | |
678 | struct pack_midx_entry *next = list; | |
679 | if (oidcmp(&obj->oid, &next->oid) >= 0) | |
680 | BUG("OIDs not in order: %s >= %s", | |
681 | oid_to_hex(&obj->oid), | |
682 | oid_to_hex(&next->oid)); | |
683 | } | |
684 | ||
685 | hashwrite(f, obj->oid.hash, (int)hash_len); | |
686 | written += hash_len; | |
687 | } | |
688 | ||
689 | return written; | |
690 | } | |
691 | ||
662148c4 DS |
692 | static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed, |
693 | struct pack_midx_entry *objects, uint32_t nr_objects) | |
694 | { | |
695 | struct pack_midx_entry *list = objects; | |
696 | uint32_t i, nr_large_offset = 0; | |
697 | size_t written = 0; | |
698 | ||
699 | for (i = 0; i < nr_objects; i++) { | |
700 | struct pack_midx_entry *obj = list++; | |
701 | ||
702 | hashwrite_be32(f, obj->pack_int_id); | |
703 | ||
704 | if (large_offset_needed && obj->offset >> 31) | |
705 | hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++); | |
706 | else if (!large_offset_needed && obj->offset >> 32) | |
707 | BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!", | |
708 | oid_to_hex(&obj->oid), | |
709 | obj->offset); | |
710 | else | |
711 | hashwrite_be32(f, (uint32_t)obj->offset); | |
712 | ||
713 | written += MIDX_CHUNK_OFFSET_WIDTH; | |
714 | } | |
715 | ||
716 | return written; | |
717 | } | |
718 | ||
719 | static size_t write_midx_large_offsets(struct hashfile *f, uint32_t nr_large_offset, | |
720 | struct pack_midx_entry *objects, uint32_t nr_objects) | |
721 | { | |
722 | struct pack_midx_entry *list = objects; | |
723 | size_t written = 0; | |
724 | ||
725 | while (nr_large_offset) { | |
726 | struct pack_midx_entry *obj = list++; | |
727 | uint64_t offset = obj->offset; | |
728 | ||
729 | if (!(offset >> 31)) | |
730 | continue; | |
731 | ||
732 | hashwrite_be32(f, offset >> 32); | |
733 | hashwrite_be32(f, offset & 0xffffffffUL); | |
734 | written += 2 * sizeof(uint32_t); | |
735 | ||
736 | nr_large_offset--; | |
737 | } | |
738 | ||
739 | return written; | |
740 | } | |
741 | ||
a3407730 DS |
742 | int write_midx_file(const char *object_dir) |
743 | { | |
32f3c541 | 744 | unsigned char cur_chunk, num_chunks = 0; |
fc59e748 | 745 | char *midx_name; |
396f2570 | 746 | uint32_t i; |
fc59e748 DS |
747 | struct hashfile *f = NULL; |
748 | struct lock_file lk; | |
396f2570 | 749 | struct pack_list packs; |
32f3c541 DS |
750 | uint32_t *pack_perm = NULL; |
751 | uint64_t written = 0; | |
752 | uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1]; | |
753 | uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1]; | |
662148c4 | 754 | uint32_t nr_entries, num_large_offsets = 0; |
fe1ed56f | 755 | struct pack_midx_entry *entries = NULL; |
662148c4 | 756 | int large_offsets_needed = 0; |
fc59e748 DS |
757 | |
758 | midx_name = get_midx_filename(object_dir); | |
759 | if (safe_create_leading_directories(midx_name)) { | |
760 | UNLEAK(midx_name); | |
761 | die_errno(_("unable to create leading directories of %s"), | |
762 | midx_name); | |
763 | } | |
764 | ||
2cf489a3 | 765 | packs.m = load_multi_pack_index(object_dir, 1); |
a40498a1 | 766 | |
396f2570 | 767 | packs.nr = 0; |
a40498a1 DS |
768 | packs.alloc_list = packs.m ? packs.m->num_packs : 16; |
769 | packs.alloc_names = packs.alloc_list; | |
396f2570 | 770 | packs.list = NULL; |
a40498a1 | 771 | packs.names = NULL; |
32f3c541 | 772 | packs.pack_name_concat_len = 0; |
396f2570 | 773 | ALLOC_ARRAY(packs.list, packs.alloc_list); |
32f3c541 | 774 | ALLOC_ARRAY(packs.names, packs.alloc_names); |
396f2570 | 775 | |
a40498a1 DS |
776 | if (packs.m) { |
777 | for (i = 0; i < packs.m->num_packs; i++) { | |
778 | ALLOC_GROW(packs.list, packs.nr + 1, packs.alloc_list); | |
779 | ALLOC_GROW(packs.names, packs.nr + 1, packs.alloc_names); | |
780 | ||
781 | packs.list[packs.nr] = NULL; | |
782 | packs.names[packs.nr] = xstrdup(packs.m->pack_names[i]); | |
783 | packs.pack_name_concat_len += strlen(packs.names[packs.nr]) + 1; | |
784 | packs.nr++; | |
785 | } | |
786 | } | |
787 | ||
396f2570 DS |
788 | for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs); |
789 | ||
a40498a1 DS |
790 | if (packs.m && packs.nr == packs.m->num_packs) |
791 | goto cleanup; | |
792 | ||
32f3c541 DS |
793 | if (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT) |
794 | packs.pack_name_concat_len += MIDX_CHUNK_ALIGNMENT - | |
795 | (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT); | |
796 | ||
797 | ALLOC_ARRAY(pack_perm, packs.nr); | |
798 | sort_packs_by_name(packs.names, packs.nr, pack_perm); | |
799 | ||
a40498a1 DS |
800 | entries = get_sorted_entries(packs.m, packs.list, pack_perm, packs.nr, &nr_entries); |
801 | ||
662148c4 DS |
802 | for (i = 0; i < nr_entries; i++) { |
803 | if (entries[i].offset > 0x7fffffff) | |
804 | num_large_offsets++; | |
805 | if (entries[i].offset > 0xffffffff) | |
806 | large_offsets_needed = 1; | |
807 | } | |
fe1ed56f | 808 | |
fc59e748 DS |
809 | hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR); |
810 | f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf); | |
811 | FREE_AND_NULL(midx_name); | |
812 | ||
a40498a1 DS |
813 | if (packs.m) |
814 | close_midx(packs.m); | |
815 | ||
32f3c541 | 816 | cur_chunk = 0; |
662148c4 | 817 | num_chunks = large_offsets_needed ? 5 : 4; |
32f3c541 DS |
818 | |
819 | written = write_midx_header(f, num_chunks, packs.nr); | |
820 | ||
821 | chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES; | |
822 | chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH; | |
823 | ||
824 | cur_chunk++; | |
d7cacf29 | 825 | chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT; |
32f3c541 DS |
826 | chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + packs.pack_name_concat_len; |
827 | ||
d7cacf29 DS |
828 | cur_chunk++; |
829 | chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP; | |
830 | chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE; | |
831 | ||
0d5b3a5e | 832 | cur_chunk++; |
662148c4 | 833 | chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS; |
0d5b3a5e DS |
834 | chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_HASH_LEN; |
835 | ||
662148c4 DS |
836 | cur_chunk++; |
837 | chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_CHUNK_OFFSET_WIDTH; | |
838 | if (large_offsets_needed) { | |
839 | chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS; | |
840 | ||
841 | cur_chunk++; | |
842 | chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + | |
843 | num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH; | |
844 | } | |
845 | ||
846 | chunk_ids[cur_chunk] = 0; | |
847 | ||
32f3c541 DS |
848 | for (i = 0; i <= num_chunks; i++) { |
849 | if (i && chunk_offsets[i] < chunk_offsets[i - 1]) | |
850 | BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64, | |
851 | chunk_offsets[i - 1], | |
852 | chunk_offsets[i]); | |
853 | ||
854 | if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT) | |
855 | BUG("chunk offset %"PRIu64" is not properly aligned", | |
856 | chunk_offsets[i]); | |
857 | ||
858 | hashwrite_be32(f, chunk_ids[i]); | |
859 | hashwrite_be32(f, chunk_offsets[i] >> 32); | |
860 | hashwrite_be32(f, chunk_offsets[i]); | |
861 | ||
862 | written += MIDX_CHUNKLOOKUP_WIDTH; | |
863 | } | |
864 | ||
865 | for (i = 0; i < num_chunks; i++) { | |
866 | if (written != chunk_offsets[i]) | |
867 | BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32, | |
868 | chunk_offsets[i], | |
869 | written, | |
870 | chunk_ids[i]); | |
871 | ||
872 | switch (chunk_ids[i]) { | |
873 | case MIDX_CHUNKID_PACKNAMES: | |
874 | written += write_midx_pack_names(f, packs.names, packs.nr); | |
875 | break; | |
876 | ||
d7cacf29 DS |
877 | case MIDX_CHUNKID_OIDFANOUT: |
878 | written += write_midx_oid_fanout(f, entries, nr_entries); | |
879 | break; | |
880 | ||
0d5b3a5e DS |
881 | case MIDX_CHUNKID_OIDLOOKUP: |
882 | written += write_midx_oid_lookup(f, MIDX_HASH_LEN, entries, nr_entries); | |
883 | break; | |
884 | ||
662148c4 DS |
885 | case MIDX_CHUNKID_OBJECTOFFSETS: |
886 | written += write_midx_object_offsets(f, large_offsets_needed, entries, nr_entries); | |
887 | break; | |
888 | ||
889 | case MIDX_CHUNKID_LARGEOFFSETS: | |
890 | written += write_midx_large_offsets(f, num_large_offsets, entries, nr_entries); | |
891 | break; | |
892 | ||
32f3c541 DS |
893 | default: |
894 | BUG("trying to write unknown chunk id %"PRIx32, | |
895 | chunk_ids[i]); | |
896 | } | |
897 | } | |
898 | ||
899 | if (written != chunk_offsets[num_chunks]) | |
900 | BUG("incorrect final offset %"PRIu64" != %"PRIu64, | |
901 | written, | |
902 | chunk_offsets[num_chunks]); | |
fc59e748 DS |
903 | |
904 | finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM); | |
905 | commit_lock_file(&lk); | |
906 | ||
a40498a1 | 907 | cleanup: |
396f2570 DS |
908 | for (i = 0; i < packs.nr; i++) { |
909 | if (packs.list[i]) { | |
910 | close_pack(packs.list[i]); | |
911 | free(packs.list[i]); | |
912 | } | |
32f3c541 | 913 | free(packs.names[i]); |
396f2570 DS |
914 | } |
915 | ||
916 | free(packs.list); | |
32f3c541 | 917 | free(packs.names); |
fe1ed56f | 918 | free(entries); |
a40498a1 DS |
919 | free(pack_perm); |
920 | free(midx_name); | |
a3407730 DS |
921 | return 0; |
922 | } | |
525e18c0 | 923 | |
1dcd9f20 | 924 | void clear_midx_file(struct repository *r) |
525e18c0 | 925 | { |
1dcd9f20 DS |
926 | char *midx = get_midx_filename(r->objects->objectdir); |
927 | ||
928 | if (r->objects && r->objects->multi_pack_index) { | |
929 | close_midx(r->objects->multi_pack_index); | |
930 | r->objects->multi_pack_index = NULL; | |
931 | } | |
525e18c0 DS |
932 | |
933 | if (remove_path(midx)) { | |
934 | UNLEAK(midx); | |
935 | die(_("failed to clear multi-pack-index at %s"), midx); | |
936 | } | |
937 | ||
938 | free(midx); | |
939 | } |