Re: [PATCH 66/68] fsck: use for_each_loose_file_in_objdir

2015-09-25 Thread Jeff King
On Thu, Sep 24, 2015 at 05:08:32PM -0400, Jeff King wrote:

> +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)
> @@ -501,12 +481,9 @@ static void fsck_object_dir(const char *path)
>  
>   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);
>   stop_progress();

I happened to be running git-fsck today and noticed that it finished
with the progress bar still reading 94%. The problem is that we update
the progress when we finish a subdir, but of course we do not
necessarily have all 256 subdirs, and the for_each_loose code only
triggers our callback for ones that exist.

So we need this on top:

diff --git a/builtin/fsck.c b/builtin/fsck.c
index 2fe6a31..d50efd5 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -484,6 +484,7 @@ static void fsck_object_dir(const char *path)
 
for_each_loose_file_in_objdir(path, fsck_loose, fsck_cruft, fsck_subdir,
  progress);
+   display_progress(progress, 256);
stop_progress();
 }
 

to make things pretty.

-Peff
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 66/68] fsck: use for_each_loose_file_in_objdir

2015-09-24 Thread Jeff King
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 
---
 builtin/fsck.c | 69 --
 1 file changed, 23 insertions(+), 46 deletions(-)

diff --git a/builtin/fsck.c b/builtin/fsck.c
index 73c3596..2fe6a31 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -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)
@@ -491,9 +452,28 @@ 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)
@@ -501,12 +481,9 @@ static void fsck_object_dir(const char *path)
 
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);
stop_progress();
 }
 
-- 
2.6.0.rc3.454.g204ad51

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html