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