summary refs log tree commit diff
path: root/perl/lib
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-07-10 21:14:34 +0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-07-10 21:17:21 +0200
commit42d91b079c5d0b468663511e7b2a8e2f4048c475 (patch)
treec3459bb77cbe4d98c30f2659792223a99fdd8e60 /perl/lib
parent7bb632b02464febd8806ef4bd3fa0ac107f52650 (diff)
downloadguix-42d91b079c5d0b468663511e7b2a8e2f4048c475.tar.gz
Fix use of sysread
Diffstat (limited to 'perl/lib')
-rw-r--r--perl/lib/Nix/CopyClosure.pm24
1 files changed, 18 insertions, 6 deletions
diff --git a/perl/lib/Nix/CopyClosure.pm b/perl/lib/Nix/CopyClosure.pm
index 779d7439f6..8be4ead763 100644
--- a/perl/lib/Nix/CopyClosure.pm
+++ b/perl/lib/Nix/CopyClosure.pm
@@ -7,11 +7,24 @@ use List::Util qw(sum);
 use IPC::Open2;
 
 
+sub readN {
+    my ($bytes, $from) = @_;
+    my $res = "";
+    while ($bytes > 0) {
+        my $s;
+        my $n = sysread($from, $s, $bytes);
+        die "I/O error reading from remote side\n" if !defined $n;
+        die "got EOF while expecting $bytes bytes from remote side\n" if !$n;
+        $bytes -= $n;
+        $res .= $s;
+    }
+    return $res;
+}
+
+
 sub readInt {
     my ($from) = @_;
-    my $resp;
-    sysread($from, $resp, 8) == 8 or die "did not receive valid reply from remote host\n";
-    return unpack("L<x4", $resp);
+    return unpack("L<x4", readN(8, $from));
 }
 
 
@@ -66,10 +79,9 @@ sub copyTo {
     my $n = readInt($from);
     while ($n--) {
         my $len = readInt($from);
-        my $s;
-        sysread($from, $s, $len) == $len or die;
+        my $s = readN($len, $from);
         $present{$s} = 1;
-        sysread($from, $s, 8 - $len % 8) if $len % 8; # skip padding
+        readN(8 - $len % 8, $from) if $len % 8; # skip padding
     }
 
     my @missing = grep { !$present{$_} } @closure;