joined from testing (no tmp file for index)
authorHeiko Schlittermann (JUMPER) <hs@schlittermann.de>
Mon, 01 Aug 2011 00:02:32 +0200
changeset 47 9a717ec183a2
parent 44 f1fea4381014 (current diff)
parent 46 8b655cc9bd27 (diff)
child 48 f43084e0ef0a
child 49 37e863785ccc
joined from testing (no tmp file for index)
bin/imager.save
--- a/bin/imager.save	Sun Jul 31 23:22:59 2011 +0200
+++ b/bin/imager.save	Mon Aug 01 00:02:32 2011 +0200
@@ -50,8 +50,8 @@
                 -noperldoc => system("perldoc -V >/dev/null 2>&1")
             );
         },
-        "c|comment=s"   => \$o{comment},
-        "z|compress:i"  => sub { $o{compress} = $_[1] ? $_[1] : Z_BEST_SPEED },
+        "c|comment=s"  => \$o{comment},
+        "z|compress:i" => sub { $o{compress} = $_[1] ? $_[1] : Z_BEST_SPEED },
         "p|pass=s"      => \$o{pass},
         "b|blocksize=s" => sub {
             given ($_[1]) {
@@ -101,20 +101,18 @@
     -d $dst or die "$0: $dst: $!\n";
     mkpath([$data, $idx, $info]);
 
-    my $index = File::Temp->new(DIR => $idx);
-    print {$index} <<__EOT;
-# imager
-format: 1
-host: @{[hostname]}
-filesystem: $src
-blocksize: $o{blocksize}
-devsize: $size
-timestamp: @{[NOW]}
-datetime: @{[DATETIME]}
-comment: @{[$o{comment}//"none"]}
-encryption: @{[$o{pass} ? CIPHER : "none"]}
-
-__EOT
+    my %index;
+    $index{META} = {
+        format     => 1,
+        host       => hostname,
+        filesystem => $src,
+        blocksize  => $o{blocksize},
+        devsize    => $size,
+        timestamp  => NOW,
+        datetime   => DATETIME,
+        (defined $o{comment} ? (comment => $o{comment}) : ()),
+        encryption => $o{pass} ? CIPHER : "none",
+    };
 
     open(my $in => $src);
     binmode($in);
@@ -151,7 +149,8 @@
         $ext .= $o{pass}     ? ".x"  : "";
 
         # the extension we do not put into the index
-        my $log = sprintf "%12d %s %s" => ($. - 1), $cs, $file;
+        push @{ $index{BLOCKS} }, sprintf "%12d %s %s" => ($. - 1),
+          $cs,                    $file;
 
         if (
             not(   -e "$data/$file"
@@ -182,20 +181,26 @@
             else { print {$out} $buffer }
             close($out);
             rename($out => "$data/$file$ext");
-            $log .= " *";
+            $index{BLOCKS}[$. - 1] .= " *";
             $stats{written}++;
         }
         else {
-            $log .= "  ";
             $stats{skipped}++;
         }
-
-        say {$index} $log;
     }
     $SIG{ALRM}->();
     alarm 0;
 
-    say {$index} "# DONE (runtime " . (time() - $^T) . "s)";
+    $index{META}{blocks}  = @{ $index{BLOCKS} };
+    $index{META}{runtime} = time() - $^T . "s";
+
+    my $index = File::Temp->new(DIR => $idx);
+    say $index join "\n" => "# imager",
+      (map { "$_: $index{META}{$_}" } sort(keys %{ $index{META} })),
+      "",
+      @{ $index{BLOCKS} };
+    close($index);
+    rename $index->filename => "$idx/" . DATETIME;
 
     say "# $src DONE (runtime " . (time() - $^T) . "s)";
     say "# $src WRITTEN $stats{written}, SKIPPED $stats{skipped} blocks";
@@ -203,9 +208,6 @@
       . sprintf "%3d%%" => 100 *
       ($stats{skipped} / ($stats{written} + $stats{skipped}));
 
-    rename $index->filename => "$idx/" . DATETIME;
-    close $index;
-
 }
 
 sub get_devsize {