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

[PATCH] Support empty blob in fsck --lost-found

From
BJ Hargrave <bj@bjhargrave.com>
Date
Sep 11, 2011, 15:40 UTC
Message-ID
<A3964281-B24B-46C0-AE73-0CCB4C12556F@bjhargrave.com>

fsck --lost-found died when attempting to write out the empty blob. Avoid calling fwrite when the blob size is zero since the call to fwrite returns 0 objects written which fails the check and caused fsck to die.

Signed-off-by: BJ Hargrave <bj@bjhargrave.com>
---
 builtin/fsck.c        |    7 ++++---
 t/t1420-lost-found.sh |   13 ++++++++-----
 2 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 5ae0366..ad6d713 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -232,9 +232,10 @@ static void check_unreachable_object(struct object *obj)
 				char *buf = read_sha1_file(obj->sha1,
 						&type, &size);
 				if (buf) {
-					if (fwrite(buf, size, 1, f) != 1)
-						die_errno("Could not write '%s'",
-							  filename);
+					if (size > 0)
+						if (fwrite(buf, size, 1, f) != 1)
+							die_errno("Could not write '%s'",
+								  filename);
 					free(buf);
 				}
 			} else
diff --git a/t/t1420-lost-found.sh b/t/t1420-lost-found.sh
index dc9e402..02323c9 100755
--- a/t/t1420-lost-found.sh
+++ b/t/t1420-lost-found.sh
@@ -8,7 +8,7 @@ test_description='Test fsck --lost-found'
 
 test_expect_success setup '
 	git config core.logAllRefUpdates 0 &&
-	: > file1 &&
+	echo x > file1 &&
 	git add file1 &&
 	test_tick &&
 	git commit -m initial &&
@@ -18,18 +18,21 @@ test_expect_success setup '
 	test_tick &&
 	git commit -m second &&
 	echo 3 > file3 &&
-	git add file3
+	: > file4 &&
+	git add file3 file4
 '
 
 test_expect_success 'lost and found something' '
 	git rev-parse HEAD > lost-commit &&
-	git rev-parse :file3 > lost-other &&
+	git rev-parse :file3 > lost-other3 &&
+	git rev-parse :file4 > lost-other4 &&
 	test_tick &&
 	git reset --hard HEAD^ &&
 	git fsck --lost-found &&
-	test 2 = $(ls .git/lost-found/*/* | wc -l) &&
+	test 3 = $(ls .git/lost-found/*/* | wc -l) &&
 	test -f .git/lost-found/commit/$(cat lost-commit) &&
-	test -f .git/lost-found/other/$(cat lost-other)
+	test -f .git/lost-found/other/$(cat lost-other3) &&
+	test -f .git/lost-found/other/$(cat lost-other4)
 '
 
 test_done
-- 
1.7.6.2
Next: Sverre Rabbelier
Message 1 of 7 in “Support empty blob in fsck --lost-found”
  1. Support empty blob in fsck --lost-foundBJ Hargrave, Sep 11, 2011
  2. Sverre RabbelierSep 11, 2011
  3. BJ HargraveSep 11, 2011
  4. Sverre RabbelierSep 11, 2011
  5. Junio C HamanoSep 11, 2011
  6. BJ HargraveSep 11, 2011
  7. Junio C HamanoSep 12, 2011

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.