using sysread when possible
authorHeiko Schlittermann (JUMPER) <hs@schlittermann.de>
Fri, 19 Aug 2011 15:30:00 +0200
changeset 78 603b47708d37
parent 77 e65d90b1682d
child 79 4362b3ac6567
using sysread when possible
bin/imager.save
lib/Imager.pm
--- a/bin/imager.save	Fri Aug 19 15:10:12 2011 +0200
+++ b/bin/imager.save	Fri Aug 19 15:30:00 2011 +0200
@@ -121,7 +121,6 @@
 
     open(my $in => $src);
     binmode($in);
-    local $/ = \$o{blocksize};
     local $| = 1;
 
     my %stats = (
@@ -145,14 +144,15 @@
     };
     $SIG{ALRM}->();
 
-    while (my $buffer = <$in>) {
+    for (my $blknr = 0; sysread($in => my $buffer, $o{blocksize}) > 0; ++$blknr) {
+	
         my ($file, $ext, $cs);
         $file = $cs = md5_hex($buffer);
         $file =~ s/(?<fn>(?<prefix>...).*)/$+{prefix}\/$+{fn}/g;
         $ext .= $o{pass}     ? ".x"  : "";
 
         # the extension we do not put into the index
-        push @{ $index{BLOCKS} }, sprintf "%12d %s %s" => ($. - 1),
+        push @{ $index{BLOCKS} }, sprintf "%12d %s %s" => $blknr,
           $cs,                    $file;
 
         if (not Imager::get_file("$data/$file")) {
@@ -184,7 +184,7 @@
             syswrite $out => $buffer;
             close($out);
             rename($out => "$data/$file$ext");
-            $index{BLOCKS}[$. - 1] .= " *";
+            $index{BLOCKS}[$blknr] .= " *";
             $stats{written}++;
         }
         else {
--- a/lib/Imager.pm	Fri Aug 19 15:10:12 2011 +0200
+++ b/lib/Imager.pm	Fri Aug 19 15:30:00 2011 +0200
@@ -24,16 +24,14 @@
     given ($file) {
         when (/\.gz\.x$/) {
             open(my $fh => "openssl @{[CIPHER]} -d -pass $::o{pass} -in $file|");
-            local $/ = undef;
             gunzip($fh => $buffer) or die $GunzipError;
         }
         when (/\.gz$/) { gunzip($file => $buffer) or die $GunzipError }
         when (/\.x$/) {
             open(my $fh => "openssl @{[CIPHER]} -d -pass $::o{pass} -in $file|");
-            local $/ = undef;
             $$buffer = <$fh>;
         }
-        default { open(my $fh => $file); local $/ = undef; $$buffer = <$fh> }
+        default { open(my $fh => $file); sysread $fh => $$buffer, -s $fh }
     }
 }