Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Oct 5, 2015
1 parent e23a91b commit f0766bf
Showing 1 changed file with 24 additions and 46 deletions.
70 changes: 24 additions & 46 deletions builtin/fsck.c
Expand Up @@ -365,45 +365,6 @@ static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
return fsck_obj(obj);
}

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;

if (verbose)
fprintf(stderr, "Checking directory %s\n", path);

sprintf(name, "%02x", i);
while ((de = readdir(dir)) != NULL) {
unsigned char sha1[20];

if (is_dot_or_dotdot(de->d_name))
continue;
if (is_loose_object_file(de, name, sha1)) {
if (fsck_sha1(sha1))
errors_found |= ERROR_OBJECT;
continue;
}
if (starts_with(de->d_name, "tmp_obj_"))
continue;
fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
}
closedir(dir);
}

static int default_refs;

static void fsck_handle_reflog_sha1(const char *refname, unsigned char *sha1)
Expand Down Expand Up @@ -491,22 +452,39 @@ static void get_default_heads(void)
}
}

static int fsck_loose(const unsigned char *sha1, const char *path, void *data)
{
if (fsck_sha1(sha1))
errors_found |= ERROR_OBJECT;
return 0;
}

static int fsck_cruft(const char *basename, const char *path, void *data)
{
if (!starts_with(basename, "tmp_obj_"))
fprintf(stderr, "bad sha1 file: %s\n", path);
return 0;
}

static int fsck_subdir(int nr, const char *path, void *progress)
{
display_progress(progress, nr + 1);
return 0;
}

static void fsck_object_dir(const char *path)
{
int i;
struct progress *progress = NULL;

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

if (show_progress)
progress = start_progress(_("Checking object directories"), 256);
for (i = 0; i < 256; i++) {
static char dir[4096];
sprintf(dir, "%s/%02x", path, i);
fsck_dir(i, dir);
display_progress(progress, i+1);
}

for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
progress);
display_progress(progress, 256);
stop_progress(&progress);
}

Expand Down

0 comments on commit f0766bf

Please sign in to comment.