git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: James Melvin <jmelvin@codeaurora.org>
To: gitster@pobox.com
Cc: git@vger.kernel.org, nasserg@codeaurora.org,
	mfick@codeaurora.org, peff@peff.net, sbeller@google.com,
	James Melvin <jmelvin@codeaurora.org>
Subject: [PATCH v2] repack: Add option to preserve and prune old pack files
Date: Fri, 10 Mar 2017 15:00:20 -0700	[thread overview]
Message-ID: <20170310220020.2666-1-jmelvin@codeaurora.org> (raw)

The new --preserve-and-prune option renames old pack files
instead of deleting them after repacking and prunes previously
preserved pack files.

This option is designed to prevent stale file handle exceptions
during git operations which can happen on users of NFS repos when
repacking is done on them. The strategy is to preserve old pack files
around until the next repack with the hopes that they will become
unreferenced by then and not cause any exceptions to running processes
when they are finally deleted (pruned).

Signed-off-by: James Melvin <jmelvin@codeaurora.org>
---
 Documentation/git-repack.txt |  4 +++
 builtin/repack.c             | 66 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-repack.txt b/Documentation/git-repack.txt
index 26afe6ed5..effd98a43 100644
--- a/Documentation/git-repack.txt
+++ b/Documentation/git-repack.txt
@@ -143,6 +143,10 @@ other objects in that pack they already have locally.
 	being removed. In addition, any unreachable loose objects will
 	be packed (and their loose counterparts removed).
 
+--preserve-and-prune::
+	 Preserve old pack files by renaming them instead of deleting. Prune any
+	 previously preserved pack files before preserving new ones.
+
 Configuration
 -------------
 
diff --git a/builtin/repack.c b/builtin/repack.c
index 677bc7c81..78fad8f1a 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -10,6 +10,7 @@
 
 static int delta_base_offset = 1;
 static int pack_kept_objects = -1;
+static int preserve_and_prune;
 static int write_bitmaps;
 static char *packdir, *packtmp;
 
@@ -108,6 +109,60 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list)
 	closedir(dir);
 }
 
+/*
+ * Adds all preserved packs hex strings to the fname list
+ */
+static void get_preserved_pack_filenames(struct string_list *fname_list)
+{
+	DIR *dir;
+	struct dirent *e;
+	char *fname;
+
+	if (!(dir = opendir(packdir)))
+		return;
+
+	while ((e = readdir(dir)) != NULL) {
+		size_t len;
+		if (!strip_suffix(e->d_name, ".old-pack", &len))
+			continue;
+
+		fname = xmemdupz(e->d_name, len);
+		string_list_append_nodup(fname_list, fname);
+	}
+	closedir(dir);
+}
+
+static void remove_preserved_packs(void) {
+	const char *exts[] = {"pack", "idx", "keep", "bitmap"};
+	int i;
+	struct string_list names = STRING_LIST_INIT_DUP;
+	struct string_list_item *item;
+
+	get_preserved_pack_filenames(&names);
+
+	for_each_string_list_item(item, &names) {
+		for (i = 0; i < ARRAY_SIZE(exts); i++) {
+			char *fname;
+			fname = mkpathdup("%s/%s.old-%s",
+					  packdir,
+					  item->string,
+					  exts[i]);
+			if (remove_path(fname))
+				warning(_("failed to remove '%s'"), fname);
+			free(fname);
+		}
+	}
+}
+
+static void preserve_pack(const char *file_path, const char *file_name,  const char *file_ext)
+{
+	char *fname_old;
+
+	fname_old = mkpathdup("%s/%s.old-%s", packdir, file_name, ++file_ext);
+	rename(file_path, fname_old);
+	free(fname_old);
+}
+
 static void remove_redundant_pack(const char *dir_name, const char *base_name)
 {
 	const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
@@ -121,7 +176,10 @@ static void remove_redundant_pack(const char *dir_name, const char *base_name)
 	for (i = 0; i < ARRAY_SIZE(exts); i++) {
 		strbuf_setlen(&buf, plen);
 		strbuf_addstr(&buf, exts[i]);
-		unlink(buf.buf);
+		if (preserve_and_prune)
+			preserve_pack(buf.buf, base_name, exts[i]);
+		else
+			unlink(buf.buf);
 	}
 	strbuf_release(&buf);
 }
@@ -194,6 +252,9 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 				N_("maximum size of each packfile")),
 		OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
 				N_("repack objects in packs marked with .keep")),
+		OPT_BOOL(0, "preserve-and-prune", &preserve_and_prune,
+				N_("preserve old pack files by renaming them instead of deleting, prune any "
+						"previously preserved pack files before preserving new ones")),
 		OPT_END()
 	};
 
@@ -404,6 +465,9 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
 
 	/* End of pack replacement. */
 
+	if (preserve_and_prune)
+		remove_preserved_packs();
+
 	if (delete_redundant) {
 		int opts = 0;
 		string_list_sort(&names);
-- 
2.12.0.191.ga15447d9f


             reply	other threads:[~2017-03-10 22:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-10 22:00 James Melvin [this message]
2017-03-10 23:43 ` [PATCH v2] repack: Add option to preserve and prune old pack files Junio C Hamano
2017-03-12 12:24   ` Jeff King
2017-03-12 18:03     ` Junio C Hamano
2017-03-13 16:39       ` Martin Fick
2017-03-11 12:38 ` Ævar Arnfjörð Bjarmason

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170310220020.2666-1-jmelvin@codeaurora.org \
    --to=jmelvin@codeaurora.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mfick@codeaurora.org \
    --cc=nasserg@codeaurora.org \
    --cc=peff@peff.net \
    --cc=sbeller@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).