git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Make repack less likely to corrupt repository
@ 2009-02-09  0:44 Robin Rosenberg
  2009-02-09  6:04 ` Junio C Hamano
  0 siblings, 1 reply; 20+ messages in thread
From: Robin Rosenberg @ 2009-02-09  0:44 UTC (permalink / raw)
  To: gitster; +Cc: git, Johannes.Schindelin, spearce, Robin Rosenberg

Repack could easily leave a repo in bad state after a failed
repack on Windows if a pack file that repack wanted to replace
was locked by a reader. A second attempt at running repack
would then destroy the repo by removing the pack file. This
attempts to make repack leave the repo in a good state, although
not optimal, in order to avoid disasters.

If renaming an old pack fails we will try to restore halfway renames
before exiting repack.

For severe situations we encourage the user to seek advice.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 git-repack.sh |   43 ++++++++++++++++++++++++++++++++-----------
 1 files changed, 32 insertions(+), 11 deletions(-)

Here is an attempt at fixing this It was tested by making sure the
idx or pack file was opened by some process and verifying that repo
was ok after git repack -a -d. That was a manual test. We should
probably add an automatic one too of some sort but I submit this 
for reading  and ad-hoc testing by interested parties.

-- robin

diff --git a/git-repack.sh b/git-repack.sh
index 458a497..6a7ba90 100755
--- a/git-repack.sh
+++ b/git-repack.sh
@@ -93,22 +93,43 @@ for name in $names ; do
 	chmod a-w "$PACKTMP-$name.pack"
 	chmod a-w "$PACKTMP-$name.idx"
 	mkdir -p "$PACKDIR" || exit
-
-	for sfx in pack idx
-	do
-		if test -f "$PACKDIR/pack-$name.$sfx"
-		then
-			mv -f "$PACKDIR/pack-$name.$sfx" \
-				"$PACKDIR/old-pack-$name.$sfx"
-		fi
-	done &&
+	ok=t
+	if test -f "$PACKDIR/pack-$name.pack"
+	then
+		mv -f "$PACKDIR/pack-$name.pack" \
+			"$PACKDIR/old-pack-$name.pack"
+	fi &&
+	if test -f "$PACKDIR/pack-$name.idx"
+	then
+		mv -f "$PACKDIR/pack-$name.idx" \
+			"$PACKDIR/old-pack-$name.idx" ||
+		(
+			mv -f "$PACKDIR/old-pack-$name.pack" \
+			"$PACKDIR/pack-$name.pack" || (
+				echo >&2 "Failed to restore after a failure to rename"\
+					"pack-$name{pack,idx} to old-$pack{pack,idx} in $PACKDIR"
+				echo >&2 "Please acquire advice on how to recover from this"\
+					"situation before you proceed."
+				exit 1
+			) || false
+		) || (
+			echo >&2 "Failed to replace the existing pack with updated one."
+			echo >&2 "We recovered from the situation, but cannot continue".
+			echo >&2 "repacking."
+			exit 0
+		)
+	fi &&
 	mv -f "$PACKTMP-$name.pack" "$PACKDIR/pack-$name.pack" &&
 	mv -f "$PACKTMP-$name.idx"  "$PACKDIR/pack-$name.idx" &&
 	test -f "$PACKDIR/pack-$name.pack" &&
 	test -f "$PACKDIR/pack-$name.idx" || {
 		echo >&2 "Couldn't replace the existing pack with updated one."
-		echo >&2 "The original set of packs have been saved as"
-		echo >&2 "old-pack-$name.{pack,idx} in $PACKDIR."
+		if (test -f "$PACKDIR/old-pack-$name.pack" ||
+			test -f "$PACKDIR/old-pack-$name.idx")
+		then
+			echo >&2 "The original set of packs have been saved as"
+			echo >&2 "old-pack-$name.{pack,idx} in $PACKDIR."
+		fi
 		exit 1
 	}
 	rm -f "$PACKDIR/old-pack-$name.pack" "$PACKDIR/old-pack-$name.idx"
-- 
1.6.1.285.g35d8b

^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2009-02-20  0:11 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-09  0:44 [PATCH] Make repack less likely to corrupt repository Robin Rosenberg
2009-02-09  6:04 ` Junio C Hamano
2009-02-10  7:07   ` Robin Rosenberg
2009-02-10 15:59     ` Junio C Hamano
2009-02-10 16:57       ` Robin Rosenberg
2009-02-10 20:16         ` Junio C Hamano
2009-02-10 23:51           ` Robin Rosenberg
2009-02-10 23:56             ` Junio C Hamano
2009-02-11  0:27               ` Robin Rosenberg
2009-02-11  0:31                 ` Junio C Hamano
2009-02-11 17:08                   ` Robin Rosenberg
2009-02-15 16:15                   ` Robin Rosenberg
2009-02-15 16:46                     ` Johannes Schindelin
2009-02-15 18:42                       ` Robin Rosenberg
2009-02-15 20:09                         ` Junio C Hamano
2009-02-16  5:17                           ` Robin Rosenberg
2009-02-19 22:21                           ` Robin Rosenberg
2009-02-19 22:44                             ` Johannes Schindelin
2009-02-20  0:09                               ` Junio C Hamano
2009-02-20  0:09                               ` Junio C Hamano

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).