about summary refs log tree commit
diff options
context:
space:
mode:
authorGraham Barr <gbarr@pobox.com>2002-02-28 13:52:08 +0000
committerGraham Barr <gbarr@pobox.com>2002-02-28 13:52:08 +0000
commit6d66de25153d69fbf5546c31bdf7f6983b9a2e9b (patch)
treec74ff81785fa67fb9cd2bd31b46d6a5c7c82131e
parent03156a4b45d27ccf027ce7e165ece60f5b9c80c4 (diff)
downloadperl-libnet-6d66de25153d69fbf5546c31bdf7f6983b9a2e9b.tar.gz
Net::FTP::I
- Don't call sysread multiple times after calling select() as it
may block.

-rw-r--r--Net/FTP/I.pm31
1 files changed, 17 insertions, 14 deletions
diff --git a/Net/FTP/I.pm b/Net/FTP/I.pm
index 18005f6..69619e7 100644
--- a/Net/FTP/I.pm
+++ b/Net/FTP/I.pm
@@ -1,4 +1,4 @@
-## $Id: //depot/libnet/Net/FTP/I.pm#12 $
+## $Id: //depot/libnet/Net/FTP/I.pm#13 $
 ## Package to read/write on BINARY data connections
 ##
 
@@ -10,7 +10,7 @@ use Carp;
 require Net::FTP::dataconn;
 
 @ISA = qw(Net::FTP::dataconn);
-$VERSION = "1.11";
+$VERSION = "1.12";
 
 sub read {
   my    $data          = shift;
@@ -18,25 +18,28 @@ sub read {
   my    $size    = shift || croak 'read($buf,$size,[$timeout])';
   my    $timeout = @_ ? shift : $data->timeout;
 
-  $data->can_read($timeout) or
-         croak "Timeout";
+  my $n;
 
-  my($b,$n,$l);
-  my $blksize = ${*$data}{'net_ftp_blksize'};
-  $blksize = $size if $size > $blksize;
+  if ($size > length ${*$data} and !${*$data}{'net_ftp_eof'}) {
+    $data->can_read($timeout) or
+           croak "Timeout";
 
-  while(($l = length(${*$data})) < $size) {
-   $n += ($b = sysread($data, ${*$data}, $blksize, $l)) || 0;
-   last unless $b;
+    my $blksize = ${*$data}{'net_ftp_blksize'};
+    $blksize = $size if $size > $blksize;
+
+    unless ($n = sysread($data, ${*$data}, $blksize, length ${*$data})) {
+      return undef unless defined $n;
+      ${*$data}{'net_ftp_eof'} = 1;
+    }
   }
 
-  $n = $size < ($l = length(${*$data})) ? $size : $l;
+  $buf = substr(${*$data},0,$size);
+
+  $n = length($buf);
 
-  $buf = substr(${*$data},0,$n);
   substr(${*$data},0,$n) = '';
 
-  ${*$data}{'net_ftp_bytesread'} += $n if $n;
-  ${*$data}{'net_ftp_eof'} = 1 unless $n;
+  ${*$data}{'net_ftp_bytesread'} += $n;
 
   $n;
 }