Skip to content

Commit 1dcd9f2

Browse files
derrickstoleegitster
authored andcommitted
midx: close multi-pack-index on repack
When repacking, we may remove pack-files. This invalidates the multi-pack-index (if it exists). Previously, we removed the multi-pack-index file before removing any pack-file. In some cases, the repack command may load the multi-pack-index into memory. This may lead to later in-memory references to the non-existent pack- files. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0ce4ff9 commit 1dcd9f2

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

builtin/repack.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
431431
char *fname, *fname_old;
432432

433433
if (!midx_cleared) {
434-
/* if we move a packfile, it will invalidated the midx */
435-
clear_midx_file(get_object_directory());
434+
clear_midx_file(the_repository);
436435
midx_cleared = 1;
437436
}
438437

midx.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,13 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
180180
return NULL;
181181
}
182182

183-
static void close_midx(struct multi_pack_index *m)
183+
void close_midx(struct multi_pack_index *m)
184184
{
185185
uint32_t i;
186+
187+
if (!m)
188+
return;
189+
186190
munmap((unsigned char *)m->data, m->data_len);
187191
close(m->fd);
188192
m->fd = -1;
@@ -917,9 +921,14 @@ int write_midx_file(const char *object_dir)
917921
return 0;
918922
}
919923

920-
void clear_midx_file(const char *object_dir)
924+
void clear_midx_file(struct repository *r)
921925
{
922-
char *midx = get_midx_filename(object_dir);
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+
}
923932

924933
if (remove_path(midx)) {
925934
UNLEAK(midx);

midx.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ int midx_contains_pack(struct multi_pack_index *m, const char *idx_name);
4242
int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local);
4343

4444
int write_midx_file(const char *object_dir);
45-
void clear_midx_file(const char *object_dir);
45+
void clear_midx_file(struct repository *r);
46+
47+
void close_midx(struct multi_pack_index *m);
4648

4749
#endif

0 commit comments

Comments
 (0)