Skip to content

Commit f0766bf

Browse files
peffgitster
authored andcommitted
fsck: use for_each_loose_file_in_objdir
Since 27e1e22 (prune: factor out loose-object directory traversal, 2014-10-15), we now have a generic callback system for iterating over the loose object directories. This is used by prune, count-objects, etc. We did not convert git-fsck at the time because it implemented an inode-sorting scheme that was not part of the generic code. Now that the inode-sorting code is gone, we can reuse the generic code. The result is shorter, hopefully more readable, and drops some unchecked sprintf calls. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e23a91b commit f0766bf

File tree

1 file changed

+24
-46
lines changed

1 file changed

+24
-46
lines changed

builtin/fsck.c

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -365,45 +365,6 @@ static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
365365
return fsck_obj(obj);
366366
}
367367

368-
static inline int is_loose_object_file(struct dirent *de,
369-
char *name, unsigned char *sha1)
370-
{
371-
if (strlen(de->d_name) != 38)
372-
return 0;
373-
memcpy(name + 2, de->d_name, 39);
374-
return !get_sha1_hex(name, sha1);
375-
}
376-
377-
static void fsck_dir(int i, char *path)
378-
{
379-
DIR *dir = opendir(path);
380-
struct dirent *de;
381-
char name[100];
382-
383-
if (!dir)
384-
return;
385-
386-
if (verbose)
387-
fprintf(stderr, "Checking directory %s\n", path);
388-
389-
sprintf(name, "%02x", i);
390-
while ((de = readdir(dir)) != NULL) {
391-
unsigned char sha1[20];
392-
393-
if (is_dot_or_dotdot(de->d_name))
394-
continue;
395-
if (is_loose_object_file(de, name, sha1)) {
396-
if (fsck_sha1(sha1))
397-
errors_found |= ERROR_OBJECT;
398-
continue;
399-
}
400-
if (starts_with(de->d_name, "tmp_obj_"))
401-
continue;
402-
fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
403-
}
404-
closedir(dir);
405-
}
406-
407368
static int default_refs;
408369

409370
static void fsck_handle_reflog_sha1(const char *refname, unsigned char *sha1)
@@ -491,22 +452,39 @@ static void get_default_heads(void)
491452
}
492453
}
493454

455+
static int fsck_loose(const unsigned char *sha1, const char *path, void *data)
456+
{
457+
if (fsck_sha1(sha1))
458+
errors_found |= ERROR_OBJECT;
459+
return 0;
460+
}
461+
462+
static int fsck_cruft(const char *basename, const char *path, void *data)
463+
{
464+
if (!starts_with(basename, "tmp_obj_"))
465+
fprintf(stderr, "bad sha1 file: %s\n", path);
466+
return 0;
467+
}
468+
469+
static int fsck_subdir(int nr, const char *path, void *progress)
470+
{
471+
display_progress(progress, nr + 1);
472+
return 0;
473+
}
474+
494475
static void fsck_object_dir(const char *path)
495476
{
496-
int i;
497477
struct progress *progress = NULL;
498478

499479
if (verbose)
500480
fprintf(stderr, "Checking object directory\n");
501481

502482
if (show_progress)
503483
progress = start_progress(_("Checking object directories"), 256);
504-
for (i = 0; i < 256; i++) {
505-
static char dir[4096];
506-
sprintf(dir, "%s/%02x", path, i);
507-
fsck_dir(i, dir);
508-
display_progress(progress, i+1);
509-
}
484+
485+
for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
486+
progress);
487+
display_progress(progress, 256);
510488
stop_progress(&progress);
511489
}
512490

0 commit comments

Comments
 (0)