chiark / gitweb /
fix file exchange protocol implementation
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 22 Oct 2013 15:29:30 +0000 (16:29 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 22 Oct 2013 15:29:30 +0000 (16:29 +0100)
dgit

diff --git a/dgit b/dgit
index e12bfdc86b6b0d78a647110388a407d41b1bdba1..31e57c5b9a3e2f5e0d47332a85d28fcc288256e8 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -206,17 +206,19 @@ sub protocol_send_file ($$) {
        my $d;
        my $got = read PF, $d, 65536;
        die "$ourfn: $!" unless defined $got;
        my $d;
        my $got = read PF, $d, 65536;
        die "$ourfn: $!" unless defined $got;
-       last if $got;
+       last if !$got;
        print $fh "data-block ".length($d)."\n" or die $!;
        print $d or die $!;
     }
        print $fh "data-block ".length($d)."\n" or die $!;
        print $d or die $!;
     }
+    PF->eof or die "$ourfn $!";
+    PF->error and die "$ourfn $!";
     print $fh "data-end\n" or die $!;
     close PF;
 }
 
 sub protocol_read_bytes ($$) {
     my ($fh, $nbytes) = @_;
     print $fh "data-end\n" or die $!;
     close PF;
 }
 
 sub protocol_read_bytes ($$) {
     my ($fh, $nbytes) = @_;
-    $nbytes =~ m/^\d{1,6}$/ or badproto \*RO, "bad byte count";
+    $nbytes =~ m/^[1-9]\d{0,5}$/ or badproto \*RO, "bad byte count";
     my $d;
     my $got = read $fh, $d, $nbytes;
     $got==$nbytes or badproto $fh, "eof during data block";
     my $d;
     my $got = read $fh, $d, $nbytes;
     $got==$nbytes or badproto $fh, "eof during data block";
@@ -228,14 +230,16 @@ sub protocol_receive_file ($$) {
     open PF, ">", $ourfn or die "$ourfn: $!";
     for (;;) {
        my ($y,$l) = protocol_expect {
     open PF, ">", $ourfn or die "$ourfn: $!";
     for (;;) {
        my ($y,$l) = protocol_expect {
-           m/^data-block (.*})$|data-end$/;
-           length $1 ? (1,$1) : (0);
+           m/^data-block (.*)$/ ? (1,$1) :
+           m/^data-end$/ ? (0,) :
+           ();
        } $fh;
        last unless $y;
        } $fh;
        last unless $y;
-       my $d = protocol_read_bytes $fh, $1;
+       my $d = protocol_read_bytes $fh, $l;
        print PF $d or die $!;
     }
        print PF $d or die $!;
     }
-    printdebug "received into $ourfn\n";
+    close PF or die $!;
+    printdebug "() $ourfn\n";
 }
 
 #---------- remote protocol support, responder ----------
 }
 
 #---------- remote protocol support, responder ----------