Skip to content

Commit a6e7b60

Browse files
committed
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 <[email protected]>
1 parent 7dc830e commit a6e7b60

File tree

2 files changed

+111
-6
lines changed

2 files changed

+111
-6
lines changed

midx.c

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir)
181181
return NULL;
182182
}
183183

184+
static void close_midx(struct multi_pack_index *m)
185+
{
186+
uint32_t i;
187+
munmap((unsigned char *)m->data, m->data_len);
188+
close(m->fd);
189+
m->fd = -1;
190+
191+
for (i = 0; i < m->num_packs; i++) {
192+
if (m->packs[i]) {
193+
close_pack(m->packs[i]);
194+
free(m->packs);
195+
}
196+
}
197+
FREE_AND_NULL(m->packs);
198+
FREE_AND_NULL(m->pack_names);
199+
}
200+
184201
static int prepare_midx_pack(struct multi_pack_index *m, uint32_t pack_int_id)
185202
{
186203
struct strbuf pack_name = STRBUF_INIT;
@@ -280,6 +297,29 @@ int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct mu
280297
return nth_midxed_pack_entry(m, e, pos);
281298
}
282299

300+
int midx_contains_pack(struct multi_pack_index *m, const char *idx_name)
301+
{
302+
uint32_t first = 0, last = m->num_packs;
303+
304+
while (first < last) {
305+
uint32_t mid = first + (last - first) / 2;
306+
const char *current;
307+
int cmp;
308+
309+
current = m->pack_names[mid];
310+
cmp = strcmp(idx_name, current);
311+
if (!cmp)
312+
return 1;
313+
if (cmp > 0) {
314+
first = mid + 1;
315+
continue;
316+
}
317+
last = mid;
318+
}
319+
320+
return 0;
321+
}
322+
283323
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir)
284324
{
285325
struct multi_pack_index *m = r->objects->multi_pack_index;
@@ -326,6 +366,7 @@ struct pack_list {
326366
uint32_t alloc_list;
327367
uint32_t alloc_names;
328368
size_t pack_name_concat_len;
369+
struct multi_pack_index *m;
329370
};
330371

331372
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,
334375
struct pack_list *packs = (struct pack_list *)data;
335376

336377
if (ends_with(file_name, ".idx")) {
378+
if (packs->m && midx_contains_pack(packs->m, file_name))
379+
return;
380+
337381
ALLOC_GROW(packs->list, packs->nr + 1, packs->alloc_list);
338382
ALLOC_GROW(packs->names, packs->nr + 1, packs->alloc_names);
339383

@@ -418,6 +462,23 @@ static int midx_oid_compare(const void *_a, const void *_b)
418462
return a->pack_int_id - b->pack_int_id;
419463
}
420464

465+
static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
466+
uint32_t *pack_perm,
467+
struct pack_midx_entry *e,
468+
uint32_t pos)
469+
{
470+
if (pos >= m->num_objects)
471+
return 1;
472+
473+
nth_midxed_object_oid(&e->oid, m, pos);
474+
e->pack_int_id = pack_perm[nth_midxed_pack_int_id(m, pos)];
475+
e->offset = nth_midxed_offset(m, pos);
476+
477+
/* consider objects in midx to be from "old" packs */
478+
e->pack_mtime = 0;
479+
return 0;
480+
}
481+
421482
static void fill_pack_entry(uint32_t pack_int_id,
422483
struct packed_git *p,
423484
uint32_t cur_object,
@@ -443,7 +504,8 @@ static void fill_pack_entry(uint32_t pack_int_id,
443504
* Copy only the de-duplicated entries (selected by most-recent modified time
444505
* of a packfile containing the object).
445506
*/
446-
static struct pack_midx_entry *get_sorted_entries(struct packed_git **p,
507+
static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
508+
struct packed_git **p,
447509
uint32_t *perm,
448510
uint32_t nr_packs,
449511
uint32_t *nr_objects)
@@ -452,8 +514,9 @@ static struct pack_midx_entry *get_sorted_entries(struct packed_git **p,
452514
uint32_t alloc_fanout, alloc_objects, total_objects = 0;
453515
struct pack_midx_entry *entries_by_fanout = NULL;
454516
struct pack_midx_entry *deduplicated_entries = NULL;
517+
uint32_t start_pack = m ? m->num_packs : 0;
455518

456-
for (cur_pack = 0; cur_pack < nr_packs; cur_pack++)
519+
for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
457520
total_objects += p[cur_pack]->num_objects;
458521

459522
/*
@@ -470,7 +533,23 @@ static struct pack_midx_entry *get_sorted_entries(struct packed_git **p,
470533
for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
471534
uint32_t nr_fanout = 0;
472535

473-
for (cur_pack = 0; cur_pack < nr_packs; cur_pack++) {
536+
if (m) {
537+
uint32_t start = 0, end;
538+
539+
if (cur_fanout)
540+
start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
541+
end = ntohl(m->chunk_oid_fanout[cur_fanout]);
542+
543+
for (cur_object = start; cur_object < end; cur_object++) {
544+
ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
545+
nth_midxed_pack_midx_entry(m, perm,
546+
&entries_by_fanout[nr_fanout],
547+
cur_object);
548+
nr_fanout++;
549+
}
550+
}
551+
552+
for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
474553
uint32_t start = 0, end;
475554

476555
if (cur_fanout)
@@ -666,24 +745,43 @@ int write_midx_file(const char *object_dir)
666745
midx_name);
667746
}
668747

748+
packs.m = load_multi_pack_index(object_dir);
749+
669750
packs.nr = 0;
670-
packs.alloc_list = 16;
671-
packs.alloc_names = 16;
751+
packs.alloc_list = packs.m ? packs.m->num_packs : 16;
752+
packs.alloc_names = packs.alloc_list;
672753
packs.list = NULL;
754+
packs.names = NULL;
673755
packs.pack_name_concat_len = 0;
674756
ALLOC_ARRAY(packs.list, packs.alloc_list);
675757
ALLOC_ARRAY(packs.names, packs.alloc_names);
676758

759+
if (packs.m) {
760+
for (i = 0; i < packs.m->num_packs; i++) {
761+
ALLOC_GROW(packs.list, packs.nr + 1, packs.alloc_list);
762+
ALLOC_GROW(packs.names, packs.nr + 1, packs.alloc_names);
763+
764+
packs.list[packs.nr] = NULL;
765+
packs.names[packs.nr] = xstrdup(packs.m->pack_names[i]);
766+
packs.pack_name_concat_len += strlen(packs.names[packs.nr]) + 1;
767+
packs.nr++;
768+
}
769+
}
770+
677771
for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
678772

773+
if (packs.m && packs.nr == packs.m->num_packs)
774+
goto cleanup;
775+
679776
if (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
680777
packs.pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
681778
(packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
682779

683780
ALLOC_ARRAY(pack_perm, packs.nr);
684781
sort_packs_by_name(packs.names, packs.nr, pack_perm);
685782

686-
entries = get_sorted_entries(packs.list, pack_perm, packs.nr, &nr_entries);
783+
entries = get_sorted_entries(packs.m, packs.list, pack_perm, packs.nr, &nr_entries);
784+
687785
for (i = 0; i < nr_entries; i++) {
688786
if (entries[i].offset > 0x7fffffff)
689787
num_large_offsets++;
@@ -695,6 +793,9 @@ int write_midx_file(const char *object_dir)
695793
f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
696794
FREE_AND_NULL(midx_name);
697795

796+
if (packs.m)
797+
close_midx(packs.m);
798+
698799
cur_chunk = 0;
699800
num_chunks = large_offsets_needed ? 5 : 4;
700801

@@ -786,6 +887,7 @@ int write_midx_file(const char *object_dir)
786887
finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
787888
commit_lock_file(&lk);
788889

890+
cleanup:
789891
for (i = 0; i < packs.nr; i++) {
790892
if (packs.list[i]) {
791893
close_pack(packs.list[i]);
@@ -797,5 +899,7 @@ int write_midx_file(const char *object_dir)
797899
free(packs.list);
798900
free(packs.names);
799901
free(entries);
902+
free(pack_perm);
903+
free(midx_name);
800904
return 0;
801905
}

midx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
1111
struct multi_pack_index *m,
1212
uint32_t n);
1313
int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct multi_pack_index *m);
14+
int midx_contains_pack(struct multi_pack_index *m, const char *idx_name);
1415
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir);
1516

1617
int write_midx_file(const char *object_dir);

0 commit comments

Comments
 (0)