restart the check after images are removed
authorHeiko Schlittermann (JUMPER) <hs@schlittermann.de>
Tue, 16 Aug 2011 16:04:02 +0200
changeset 66 35a0ea276176
parent 65 a10f9c6a0b42
child 67 c0a522905faf
restart the check after images are removed
bin/imager.check
--- a/bin/imager.check	Tue Aug 16 14:55:31 2011 +0200
+++ b/bin/imager.check	Tue Aug 16 16:04:02 2011 +0200
@@ -45,22 +45,22 @@
       and @ARGV
       or pod2usage;
     my $dir = shift;
-    my $tmp = File::Temp->new;
-
-    # load the index files, remember the latest
-    # timestamp we see
-    #tie %idx, "DB_File" => $tmp->filename;
-    verbose("# reading index files");
-    my %block = get_block_list($dir);
+    
+    while (1) {
+        my %block = get_block_list($dir);
 
-    verbose("# indexed: "
-          . scalar(@{ $block{""} // [] })
-          . " images with "
-          . (grep !/^\.idx$/ => keys(%block))
-          . " blocks");
+        verbose("# reading index files");
+        verbose("# indexed: "
+              . scalar(@{ $block{""} // [] })
+              . " images with "
+              . (grep !/^\.idx$/ => keys(%block))
+              . " blocks");
 
-    purge_unused($dir => %block);
-    check_images($dir => %block);
+        purge_unused($dir => %block);
+        check_images($dir => %block) and last;
+
+	verbose("# STARTING OVER!");
+    }
 }
 
 sub verbose { say @_ if $o{verbose} }
@@ -94,9 +94,10 @@
 
 sub purge_unused {
     my ($dir, %block) = @_;
+    my ($total, $done, $t0);
+    state $subpass = -1;
 
-    my ($total, $done, $t0);
-    verbose("# pass 1 - checking for unused blocks");
+    verbose("# pass 1.@{[++$subpass]} - checking for unused blocks");
     verbose("#          estimating file count");
 
     # calculate the number of files we expect
@@ -106,7 +107,8 @@
             opendir(my $dh => $_);
             map { $total++ if not $_ ~~ [qw<. ..>] and length > 8 } readdir $dh;
             closedir($dh);
-	    $File::Find::prune = $_ =~ /^[\d[a-f]{3}$/;	# FIXME should be configurable
+            $File::Find::prune =
+              $_ =~ /^[\d[a-f]{3}$/;    # FIXME should be configurable
         },
         "$dir/data"
     );
@@ -117,10 +119,10 @@
     local $SIG{ALRM} = sub {
         return alarm 1 if not $done;
         my $speed = $done / (time - $t0 + 1);
-        verbose sprintf "# pass 1 done %5.1f%% | %25s (%*d of %d blocks)",
+        verbose sprintf
+          "# pass 1.$subpass done %5.1f%% | %25s (%*d of %d blocks)",
           100 * ($done / $total),
-          scalar(localtime $t0 + $total/$speed),
-          length($total) => $done,
+          scalar(localtime $t0 + $total / $speed), length($total) => $done,
           $total;
         alarm 5;
     };
@@ -183,19 +185,20 @@
     my ($dir, %block) = @_;
 
     my $total = grep { $_ ne "" } keys(%block);
-    my $done = 0;
-    my $t0 = time;
+    my $done  = 0;
+    my $t0    = time;
 
-    verbose("# pass 2 - checking image completeness");
+    state $subpass = -1;
+    verbose("# pass 2.@{[++$subpass]} - checking image completeness");
 
     # progress
     local $SIG{ALRM} = sub {
         return alarm 1 if not $done;
         my $speed = $done / (time - $t0 + 1);
-        verbose sprintf "# pass 2 done %5.1f%% | %25s (%*d of %d blocks)",
+        verbose sprintf
+          "# pass 2.$subpass done %5.1f%% | %25s (%*d of %d blocks)",
           100 * $done / $total,
-          scalar(localtime $t0 + $total/$speed),
-          length($total) => $done,
+          scalar(localtime $t0 + $total / $speed), length($total) => $done,
           $total;
         alarm 5;
     };
@@ -223,7 +226,7 @@
     # invalid
     my @invalid = sort @{ $block{""} }[keys %invalid];
 
-    return if not @invalid;
+    return 1 if not @invalid;
 
     say sprintf "found %d (%.1f%%) invalid images:",
       0 + @invalid,
@@ -231,17 +234,19 @@
 
     if ($o{yes}) {
         unlink @invalid;
-        return;
+        return undef;
     }
 
     while (-t) {
         print "delete? [y/N/v] ";
         given (<STDIN>) {
-            when (/^y(?:es)?$/i) { unlink @invalid; last }
+            when (/^y(?:es)?$/i) { unlink @invalid; return undef }
             when (/^v/i) { say join "\n" => @invalid; next }
             default { last }
         }
     }
+
+    return 1;
 }
 __END__