bin/imager.save
changeset 125 41872269ca6f
parent 124 87081e9cf3ba
parent 118 8c2cc26d45fc
equal deleted inserted replaced
124:87081e9cf3ba 125:41872269ca6f
    15 use Getopt::Long;
    15 use Getopt::Long;
    16 use Pod::Usage;
    16 use Pod::Usage;
    17 use Imager 0.1;
    17 use Imager 0.1;
    18 use if $^V >= v5.18 => (experimental => qw'lexical_topic smartmatch');
    18 use if $^V >= v5.18 => (experimental => qw'lexical_topic smartmatch');
    19 
    19 
    20 use constant KiB      => 1024;
    20 use constant KiB     => 1024;
    21 use constant MiB      => 1024 * KiB;
    21 use constant MiB     => 1024 * KiB;
    22 use constant GiB      => 1024 * MiB;
    22 use constant GiB     => 1024 * MiB;
    23 use constant BS       => 4 * MiB;
    23 use constant BS      => 4 * MiB;
    24 use constant DATEFMT  => "%Y-%m-%dT%H:%M:%SZ";
    24 use constant DATEFMT => "%Y-%m-%dT%H:%M:%SZ";
    25 use constant CIPHER   => "aes-128-cbc";
    25 use constant CIPHER  => "aes-128-cbc";
    26 
    26 
    27 sub get_devsize;
    27 sub get_devsize;
    28 sub get_devname;
    28 sub get_devname;
    29 sub save;
    29 sub save;
    30 
       
    31 
    30 
    32 $SIG{INT} = sub { die "Got INT\n" };
    31 $SIG{INT} = sub { die "Got INT\n" };
    33 
    32 
    34 my %o = (
    33 my %o = (
    35     compress  => undef,
    34     compress  => undef,
    52             );
    51             );
    53         },
    52         },
    54         "c|comment=s"   => \$o{comment},
    53         "c|comment=s"   => \$o{comment},
    55         "z|compress:i"  => sub { $o{compress} = $_[1] ? $_[1] : Z_BEST_SPEED },
    54         "z|compress:i"  => sub { $o{compress} = $_[1] ? $_[1] : Z_BEST_SPEED },
    56         "p|pass=s"      => \$o{pass},
    55         "p|pass=s"      => \$o{pass},
    57 	"now=i"		=> \$o{now},
    56         "now=i"         => \$o{now},
    58         "b|blocksize=s" => sub {
    57         "b|blocksize=s" => sub {
    59             given ($_[1]) {
    58             given ($_[1]) {
    60                 when (/(\d+)G/i) { $o{blocksize} = $1 * GiB };
    59                 when (/(\d+)G/i) { $o{blocksize} = $1 * GiB };
    61                 when (/(\d+)M/i) { $o{blocksize} = $1 * MiB };
    60                 when (/(\d+)M/i) { $o{blocksize} = $1 * MiB };
    62                 when (/(\d+)K/i) { $o{blocksize} = $1 * KiB };
    61                 when (/(\d+)K/i) { $o{blocksize} = $1 * KiB };
    82         exit;
    81         exit;
    83     }
    82     }
    84 
    83 
    85     my $rc = 0;
    84     my $rc = 0;
    86     while (wait != -1) {
    85     while (wait != -1) {
    87 	$rc = ($? >> 8) if ($? >> 8) > $rc;
    86         $rc = ($? >> 8) if ($? >> 8) > $rc;
    88     }
    87     }
    89     exit $rc;
    88     exit $rc;
    90 
    89 
    91 }
    90 }
    92 
    91 
   177             if ($o{pass}) {
   176             if ($o{pass}) {
   178                 open($out, "|openssl @{[CIPHER]} -pass $o{pass} -out $out");
   177                 open($out, "|openssl @{[CIPHER]} -pass $o{pass} -out $out");
   179             }
   178             }
   180             binmode($out);
   179             binmode($out);
   181 
   180 
   182            
   181             my $bufref = \$buffer;
   183                 my $bufref = \$buffer;
   182             if ($o{compress}) {
   184                 if ($o{compress}) {
   183                 my $zbuffer;
   185                     my $zbuffer;
   184                 gzip(
   186                     gzip(
   185                     \$buffer  => \$zbuffer,
   187                         \$buffer  => \$zbuffer,
   186                     -Minimal  => 1,
   188                         -Minimal  => 1,
   187                     -Level    => Z_BEST_SPEED,
   189                         -Level    => Z_BEST_SPEED,
   188                     -Strategy => Z_FILTERED
   190                         -Strategy => Z_FILTERED
   189                 ) or die $GzipError;
   191                     ) or die $GzipError;
   190                 if (length($zbuffer) / length($buffer) < 0.9) {
   192                     if (length($zbuffer) / length($buffer) < 0.9) {
   191                     $bufref = \$zbuffer;
   193                         $bufref = \$zbuffer;
   192                     $ext    = ".gz$ext";
   194                         $ext    = ".gz$ext";
       
   195                     }
       
   196                 }
   193                 }
   197 
   194             }
   198 		#for(my $todo = length $$bufref;
   195 
   199 		#    $todo -= syswrite $out => $$bufref, $todo, -$todo; 1)
   196             #for(my $todo = length $$bufref;
   200 		#{
   197             #    $todo -= syswrite $out => $$bufref, $todo, -$todo; 1)
   201 		#}
   198             #{
   202 		syswrite $out => $$bufref;
   199             #}
   203             
   200             syswrite $out => $$bufref or die "$0: write: $!\n";
   204             close($out) or die $!;
   201             close($out) or die "$0: close output file: $!";
       
   202 
   205             rename($out => "$data/$file$ext");
   203             rename($out => "$data/$file$ext");
   206             $index{BLOCKS}[$blknr] .= " *";
   204             $index{BLOCKS}[$blknr] .= " *";
   207             $stats{written}++;
   205             $stats{written}++;
   208         }
   206         }
   209         else {
   207         else {