midx: use existing midx when writing new one

Due to how Windows handles replacing a lockfile when there is an open handle, create the close_midx() method to close the existing midx before writing the new one. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 12, 2018 at 15:39 UTC a40498a12654259335995d785cc1da9f90f249c7
2 files changed +111 -6
midx.c
+110 -6
@@ -179,6 +179,23 @@ cleanup_fail:
179 return NULL;
180 }
181
182 +static void close_midx(struct multi_pack_index *m)
183 +{
184 + uint32_t i;
185 + munmap((unsigned char *)m->data, m->data_len);
186 + close(m->fd);
187 + m->fd = -1;
188 +
189 + for (i = 0; i < m->num_packs; i++) {
190 + if (m->packs[i]) {
191 + close_pack(m->packs[i]);
192 + free(m->packs);
193 + }
194 + }
195 + FREE_AND_NULL(m->packs);
196 + FREE_AND_NULL(m->pack_names);
197 +}
198 +
199 static int prepare_midx_pack(struct multi_pack_index *m, uint32_t pack_int_id)
200 {
201 struct strbuf pack_name = STRBUF_INIT;
@@ -278,6 +295,29 @@ int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct mu
295 return nth_midxed_pack_entry(m, e, pos);
296 }
297
298 +int midx_contains_pack(struct multi_pack_index *m, const char *idx_name)
299 +{
300 + uint32_t first = 0, last = m->num_packs;
301 +
302 + while (first < last) {
303 + uint32_t mid = first + (last - first) / 2;
304 + const char *current;
305 + int cmp;
306 +
307 + current = m->pack_names[mid];
308 + cmp = strcmp(idx_name, current);
309 + if (!cmp)
310 + return 1;
311 + if (cmp > 0) {
312 + first = mid + 1;
313 + continue;
314 + }
315 + last = mid;
316 + }
317 +
318 + return 0;
319 +}
320 +
321 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir)
322 {
323 struct multi_pack_index *m = r->objects->multi_pack_index;
@@ -326,6 +366,7 @@ struct pack_list {
366 uint32_t alloc_list;
367 uint32_t alloc_names;
368 size_t pack_name_concat_len;
369 + struct multi_pack_index *m;
370 };
371
372 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
@@ -334,6 +375,9 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
375 struct pack_list *packs = (struct pack_list *)data;
376
377 if (ends_with(file_name, ".idx")) {
378 + if (packs->m && midx_contains_pack(packs->m, file_name))
379 + return;
380 +
381 ALLOC_GROW(packs->list, packs->nr + 1, packs->alloc_list);
382 ALLOC_GROW(packs->names, packs->nr + 1, packs->alloc_names);
383
@@ -419,6 +463,23 @@ static int midx_oid_compare(const void *_a, const void *_b)
463 return a->pack_int_id - b->pack_int_id;
464 }
465
466 +static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
467 + uint32_t *pack_perm,
468 + struct pack_midx_entry *e,
469 + uint32_t pos)
470 +{
471 + if (pos >= m->num_objects)
472 + return 1;
473 +
474 + nth_midxed_object_oid(&e->oid, m, pos);
475 + e->pack_int_id = pack_perm[nth_midxed_pack_int_id(m, pos)];
476 + e->offset = nth_midxed_offset(m, pos);
477 +
478 + /* consider objects in midx to be from "old" packs */
479 + e->pack_mtime = 0;
480 + return 0;
481 +}
482 +
483 static void fill_pack_entry(uint32_t pack_int_id,
484 struct packed_git *p,
485 uint32_t cur_object,
@@ -444,7 +505,8 @@ static void fill_pack_entry(uint32_t pack_int_id,
505 * Copy only the de-duplicated entries (selected by most-recent modified time
506 * of a packfile containing the object).
507 */
447 -static struct pack_midx_entry *get_sorted_entries(struct packed_git **p,
508 +static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
509 + struct packed_git **p,
510 uint32_t *perm,
511 uint32_t nr_packs,
512 uint32_t *nr_objects)
@@ -453,8 +515,9 @@ static struct pack_midx_entry *get_sorted_entries(struct packed_git **p,
515 uint32_t alloc_fanout, alloc_objects, total_objects = 0;
516 struct pack_midx_entry *entries_by_fanout = NULL;
517 struct pack_midx_entry *deduplicated_entries = NULL;
518 + uint32_t start_pack = m ? m->num_packs : 0;
519
457 - for (cur_pack = 0; cur_pack < nr_packs; cur_pack++)
520 + for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
521 total_objects += p[cur_pack]->num_objects;
522
523 /*
@@ -471,7 +534,23 @@ static struct pack_midx_entry *get_sorted_entries(struct packed_git **p,
534 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
535 uint32_t nr_fanout = 0;
536
474 - for (cur_pack = 0; cur_pack < nr_packs; cur_pack++) {
537 + if (m) {
538 + uint32_t start = 0, end;
539 +
540 + if (cur_fanout)
541 + start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
542 + end = ntohl(m->chunk_oid_fanout[cur_fanout]);
543 +
544 + for (cur_object = start; cur_object < end; cur_object++) {
545 + ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
546 + nth_midxed_pack_midx_entry(m, perm,
547 + &entries_by_fanout[nr_fanout],
548 + cur_object);
549 + nr_fanout++;
550 + }
551 + }
552 +
553 + for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
554 uint32_t start = 0, end;
555
556 if (cur_fanout)
@@ -667,16 +746,34 @@ int write_midx_file(const char *object_dir)
746 midx_name);
747 }
748
749 + packs.m = load_multi_pack_index(object_dir);
750 +
751 packs.nr = 0;
671 - packs.alloc_list = 16;
672 - packs.alloc_names = 16;
752 + packs.alloc_list = packs.m ? packs.m->num_packs : 16;
753 + packs.alloc_names = packs.alloc_list;
754 packs.list = NULL;
755 + packs.names = NULL;
756 packs.pack_name_concat_len = 0;
757 ALLOC_ARRAY(packs.list, packs.alloc_list);
758 ALLOC_ARRAY(packs.names, packs.alloc_names);
759
760 + if (packs.m) {
761 + for (i = 0; i < packs.m->num_packs; i++) {
762 + ALLOC_GROW(packs.list, packs.nr + 1, packs.alloc_list);
763 + ALLOC_GROW(packs.names, packs.nr + 1, packs.alloc_names);
764 +
765 + packs.list[packs.nr] = NULL;
766 + packs.names[packs.nr] = xstrdup(packs.m->pack_names[i]);
767 + packs.pack_name_concat_len += strlen(packs.names[packs.nr]) + 1;
768 + packs.nr++;
769 + }
770 + }
771 +
772 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
773
774 + if (packs.m && packs.nr == packs.m->num_packs)
775 + goto cleanup;
776 +
777 if (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
778 packs.pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
779 (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
@@ -684,7 +781,8 @@ int write_midx_file(const char *object_dir)
781 ALLOC_ARRAY(pack_perm, packs.nr);
782 sort_packs_by_name(packs.names, packs.nr, pack_perm);
783
687 - entries = get_sorted_entries(packs.list, pack_perm, packs.nr, &nr_entries);
784 + entries = get_sorted_entries(packs.m, packs.list, pack_perm, packs.nr, &nr_entries);
785 +
786 for (i = 0; i < nr_entries; i++) {
787 if (entries[i].offset > 0x7fffffff)
788 num_large_offsets++;
@@ -696,6 +794,9 @@ int write_midx_file(const char *object_dir)
794 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
795 FREE_AND_NULL(midx_name);
796
797 + if (packs.m)
798 + close_midx(packs.m);
799 +
800 cur_chunk = 0;
801 num_chunks = large_offsets_needed ? 5 : 4;
802
@@ -787,6 +888,7 @@ int write_midx_file(const char *object_dir)
888 finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
889 commit_lock_file(&lk);
890
891 +cleanup:
892 for (i = 0; i < packs.nr; i++) {
893 if (packs.list[i]) {
894 close_pack(packs.list[i]);
@@ -798,5 +900,7 @@ int write_midx_file(const char *object_dir)
900 free(packs.list);
901 free(packs.names);
902 free(entries);
903 + free(pack_perm);
904 + free(midx_name);
905 return 0;
906 }
midx.h
+1
@@ -35,6 +35,7 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
35 struct multi_pack_index *m,
36 uint32_t n);
37 int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct multi_pack_index *m);
38 +int midx_contains_pack(struct multi_pack_index *m, const char *idx_name);
39 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir);
40
41 int write_midx_file(const char *object_dir);