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

[PATCH] fsck: do not give up too early in fsck_dir()

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 26, 2011, 21:01 UTC
Message-ID
<7vlj27js59.fsf@alter.siamese.dyndns.org>

When there is a random garbage file whose name happens to be 38-byte long in a .git/objects/??/ directory, the loop terminated prematurely without marking all the other files that it hasn't checked in the readdir() loop.

Treat such a file just like any other garbage file, and do not break out of the readdir() loop.

While at it, replace repeated sprintf() calls to a single one outside the loop.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * For readability, I think it also makes sense to move the surrounding
   code so that "tmp_obj_" check and the "is_dot_or_dotdot" check are next
   to each other, as they are both in "we know this is not a loose object
   file, and we don't want to warn about its existence" category, but that
   is a minor point.
 builtin/fsck.c |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 91409a0..795aba0 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -392,10 +392,20 @@ static void add_sha1_list(unsigned char *sha1, unsigned long ino)
 	sha1_list.nr = ++nr;
 }
 
+static inline int is_loose_object_file(struct dirent *de,
+				       char *name, unsigned char *sha1)
+{
+	if (strlen(de->d_name) != 38)
+		return 0;
+	memcpy(name + 2, de->d_name, 39);
+	return !get_sha1_hex(name, sha1);
+}
+
 static void fsck_dir(int i, char *path)
 {
 	DIR *dir = opendir(path);
 	struct dirent *de;
+	char name[100];
 
 	if (!dir)
 		return;
@@ -403,17 +413,13 @@ static void fsck_dir(int i, char *path)
 	if (verbose)
 		fprintf(stderr, "Checking directory %s\n", path);
 
+	sprintf(name, "%02x", i);
 	while ((de = readdir(dir)) != NULL) {
-		char name[100];
 		unsigned char sha1[20];
 
 		if (is_dot_or_dotdot(de->d_name))
 			continue;
-		if (strlen(de->d_name) == 38) {
-			sprintf(name, "%02x", i);
-			memcpy(name+2, de->d_name, 39);
-			if (get_sha1_hex(name, sha1) < 0)
-				break;
+		if (is_loose_object_file(de, name, sha1)) {
 			add_sha1_list(sha1, DIRENT_SORT_HINT(de));
 			continue;
 		}
Message 1 of 1 in “fsck: do not give up too early in fsck_dir()”
  1. fsck: do not give up too early in fsck_dir()Junio C Hamano, Jan 26, 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.