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>

Jeff King committed Sep 24, 2015 at 17:08 UTC f0766bf94ec7a398a5a524053ea5334883c16a2e
1 file changed +24 -46
builtin/fsck.c
+24 -46
@@ -365,45 +365,6 @@ static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
365 return fsck_obj(obj);
366 }
367
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 -
368 static int default_refs;
369
370 static void fsck_handle_reflog_sha1(const char *refname, unsigned char *sha1)
@@ -491,9 +452,28 @@ static void get_default_heads(void)
452 }
453 }
454
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 +
475 static void fsck_object_dir(const char *path)
476 {
496 - int i;
477 struct progress *progress = NULL;
478
479 if (verbose)
@@ -501,12 +481,10 @@ static void fsck_object_dir(const char *path)
481
482 if (show_progress)
483 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);
488 stop_progress(&progress);
489 }
490