git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/3] midx: close multi-pack-index on repack

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Oct 12, 2018, 17:34 UTC
Message-ID
<2d8f26679d4ba18d7a080a5e6be993198f9b1da2.1539365654.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.27.v2.git.gitgitgadget@gmail.com>
From: Derrick Stolee <dstolee@microsoft.com>

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>
---
 builtin/repack.c |  3 +--
 midx.c           | 15 ++++++++++++---
 midx.h           |  4 +++-
 3 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/builtin/repack.c b/builtin/repack.c
index c6a7943d5c..44965cbaa3 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -431,8 +431,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 			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;
 			}
 
diff --git a/midx.c b/midx.c
index bf1f511862..22247a30ab 100644
--- a/midx.c
+++ b/midx.c
@@ -176,9 +176,13 @@ cleanup_fail:
 	return NULL;
 }
 
-static void close_midx(struct multi_pack_index *m)
+void close_midx(struct multi_pack_index *m)
 {
 	uint32_t i;
+
+	if (!m)
+		return;
+
 	munmap((unsigned char *)m->data, m->data_len);
 	close(m->fd);
 	m->fd = -1;
@@ -914,9 +918,14 @@ cleanup:
 	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);
diff --git a/midx.h b/midx.h
index ce80b91c68..0f68bccdd5 100644
--- a/midx.h
+++ b/midx.h
@@ -42,7 +42,9 @@ 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);
-void clear_midx_file(const char *object_dir);
+void clear_midx_file(struct repository *r);
 int verify_midx_file(const char *object_dir);
 
+void close_midx(struct multi_pack_index *m);
+
 #endif
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Derrick Stolee
Message 12 of 14 in “Add GIT_TEST_MULTI_PACK_INDEX environment variable”
  1. 0/3 Add GIT_TEST_MULTI_PACK_INDEX environment variableDerrick Stolee via GitGitGadget, Oct 8, 2018
  2. 1/3 midx: fix broken free() in close_midx()Derrick Stolee via GitGitGadget, Oct 8, 2018
  3. Junio C HamanoOct 9, 2018
  4. 2/3 midx: close multi-pack-index on repackDerrick Stolee via GitGitGadget, Oct 8, 2018
  5. Junio C HamanoOct 9, 2018
  6. Derrick StoleeOct 9, 2018
  7. Stefan BellerOct 9, 2018
  8. 3/3 multi-pack-index: define GIT_TEST_MULTI_PACK_INDEXDerrick Stolee via GitGitGadget, Oct 8, 2018
  9. 0/3 Add GIT_TEST_MULTI_PACK_INDEX environment variableDerrick Stolee via GitGitGadget, Oct 12, 2018
  10. 1/3 midx: fix broken free() in close_midx()Derrick Stolee via GitGitGadget, Oct 12, 2018
  11. 3/3 multi-pack-index: define GIT_TEST_MULTI_PACK_INDEXDerrick Stolee via GitGitGadget, Oct 12, 2018
  12. 2/3 midx: close multi-pack-index on repackDerrick Stolee via GitGitGadget, Oct 12, 2018
  13. Derrick StoleeOct 12, 2018
  14. Junio C HamanoOct 22, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.