midx: close multi-pack-index on repack
authorDerrick Stolee <dstolee@microsoft.com>
Fri, 12 Oct 2018 17:34:19 +0000 (10:34 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Oct 2018 01:42:46 +0000 (10:42 +0900)
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 <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/repack.c
midx.c
midx.h

index c6a7943..44965cb 100644 (file)
@@ -431,8 +431,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
                        char *fname, *fname_old;
 
                        if (!midx_cleared) {
                        char *fname, *fname_old;
 
                        if (!midx_cleared) {
-                               /* if we move a packfile, it will invalidated the midx */
-                               clear_midx_file(get_object_directory());
+                               clear_midx_file(the_repository);
                                midx_cleared = 1;
                        }
 
                                midx_cleared = 1;
                        }
 
diff --git a/midx.c b/midx.c
index 999717b..2243c91 100644 (file)
--- a/midx.c
+++ b/midx.c
@@ -180,9 +180,13 @@ cleanup_fail:
        return NULL;
 }
 
        return NULL;
 }
 
-static void close_midx(struct multi_pack_index *m)
+void close_midx(struct multi_pack_index *m)
 {
        uint32_t i;
 {
        uint32_t i;
+
+       if (!m)
+               return;
+
        munmap((unsigned char *)m->data, m->data_len);
        close(m->fd);
        m->fd = -1;
        munmap((unsigned char *)m->data, m->data_len);
        close(m->fd);
        m->fd = -1;
@@ -917,9 +921,14 @@ cleanup:
        return 0;
 }
 
        return 0;
 }
 
-void clear_midx_file(const char *object_dir)
+void clear_midx_file(struct repository *r)
 {
 {
-       char *midx = get_midx_filename(object_dir);
+       char *midx = get_midx_filename(r->objects->objectdir);
+
+       if (r->objects && r->objects->multi_pack_index) {
+               close_midx(r->objects->multi_pack_index);
+               r->objects->multi_pack_index = NULL;
+       }
 
        if (remove_path(midx)) {
                UNLEAK(midx);
 
        if (remove_path(midx)) {
                UNLEAK(midx);
diff --git a/midx.h b/midx.h
index a210f1a..7e60907 100644 (file)
--- a/midx.h
+++ b/midx.h
@@ -42,6 +42,8 @@ int midx_contains_pack(struct multi_pack_index *m, const char *idx_name);
 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local);
 
 int write_midx_file(const char *object_dir);
 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local);
 
 int write_midx_file(const char *object_dir);
-void clear_midx_file(const char *object_dir);
+void clear_midx_file(struct repository *r);
+
+void close_midx(struct multi_pack_index *m);
 
 #endif
 
 #endif