fsck: do not give up too early in fsck_dir()
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>
Junio C Hamano committed
Jan 26, 2011 at 13:01 UTC
ea6f0a23ac63b3a19202db5441704e3d79236ad4
1 file changed
+12
-6
builtin/fsck.c
+12
-6
@@ -392,10 +392,20 @@ static void add_sha1_list(unsigned char *sha1, unsigned long ino)
392
sha1_list.nr = ++nr;
393
}
394
395
+static inline int is_loose_object_file(struct dirent *de,
396
+ char *name, unsigned char *sha1)
397
+{
398
+ if (strlen(de->d_name) != 38)
399
+ return 0;
400
+ memcpy(name + 2, de->d_name, 39);
401
+ return !get_sha1_hex(name, sha1);
402
+}
403
+
404
static void fsck_dir(int i, char *path)
405
{
406
DIR *dir = opendir(path);
407
struct dirent *de;
408
+ char name[100];
409
410
if (!dir)
411
return;
@@ -403,17 +413,13 @@ static void fsck_dir(int i, char *path)
413
if (verbose)
414
fprintf(stderr, "Checking directory %s\n", path);
415
416
+ sprintf(name, "%02x", i);
417
while ((de = readdir(dir)) != NULL) {
407
- char name[100];
418
unsigned char sha1[20];
419
420
if (is_dot_or_dotdot(de->d_name))
421
continue;
412
- if (strlen(de->d_name) == 38) {
413
- sprintf(name, "%02x", i);
414
- memcpy(name+2, de->d_name, 39);
415
- if (get_sha1_hex(name, sha1) < 0)
416
- break;
422
+ if (is_loose_object_file(de, name, sha1)) {
423
add_sha1_list(sha1, DIRENT_SORT_HINT(de));
424
continue;
425
}