releasing/cbrtools/perl/Net/FTP/I.pm
changeset 602 3145852acc89
equal deleted inserted replaced
600:6d08f4a05d93 602:3145852acc89
       
     1 # COPYRIGHT 1996-2000 Graham Barr. All rights reserved.
       
     2 #
       
     3 # This library is free software; you can redistribute it and/or modify
       
     4 # it under the same terms as Perl itself.
       
     5 
       
     6 ## $Id: //depot/libnet/Net/FTP/I.pm#13 $
       
     7 ## Package to read/write on BINARY data connections
       
     8 ##
       
     9 
       
    10 package Net::FTP::I;
       
    11 
       
    12 use vars qw(@ISA $buf $VERSION);
       
    13 use Carp;
       
    14 
       
    15 require Net::FTP::dataconn;
       
    16 
       
    17 @ISA = qw(Net::FTP::dataconn);
       
    18 $VERSION = "1.12"; 
       
    19 
       
    20 sub read {
       
    21   my    $data 	 = shift;
       
    22   local *buf 	 = \$_[0]; shift;
       
    23   my    $size    = shift || croak 'read($buf,$size,[$timeout])';
       
    24   my    $timeout = @_ ? shift : $data->timeout;
       
    25 
       
    26   my $n;
       
    27 
       
    28   if ($size > length ${*$data} and !${*$data}{'net_ftp_eof'}) {
       
    29     $data->can_read($timeout) or
       
    30 	   croak "Timeout";
       
    31 
       
    32     my $blksize = ${*$data}{'net_ftp_blksize'};
       
    33     $blksize = $size if $size > $blksize;
       
    34 
       
    35     unless ($n = sysread($data, ${*$data}, $blksize, length ${*$data})) {
       
    36       return undef unless defined $n;
       
    37       ${*$data}{'net_ftp_eof'} = 1;
       
    38     }
       
    39   }
       
    40 
       
    41   $buf = substr(${*$data},0,$size);
       
    42 
       
    43   $n = length($buf);
       
    44 
       
    45   substr(${*$data},0,$n) = '';
       
    46 
       
    47   ${*$data}{'net_ftp_bytesread'} += $n;
       
    48 
       
    49   $n;
       
    50 }
       
    51 
       
    52 sub write {
       
    53   my    $data    = shift;
       
    54   local *buf     = \$_[0]; shift;
       
    55   my    $size    = shift || croak 'write($buf,$size,[$timeout])';
       
    56   my    $timeout = @_ ? shift : $data->timeout;
       
    57 
       
    58   # If the remote server has closed the connection we will be signal'd
       
    59   # when we write. This can happen if the disk on the remote server fills up
       
    60 
       
    61   local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
       
    62   my $sent = $size;
       
    63   my $off = 0;
       
    64 
       
    65   my $blksize = ${*$data}{'net_ftp_blksize'};
       
    66   while($sent > 0) {
       
    67     $data->can_write($timeout) or
       
    68 	 croak "Timeout";
       
    69 
       
    70     my $n = syswrite($data, $buf, $sent > $blksize ? $blksize : $sent ,$off);
       
    71     return undef unless defined($n);
       
    72     $sent -= $n;
       
    73     $off += $n;
       
    74   }
       
    75 
       
    76   $size;
       
    77 }
       
    78 
       
    79 1;
       
    80 __END__
       
    81 
       
    82 =head1 COPYRIGHT
       
    83 
       
    84 COPYRIGHT
       
    85 
       
    86   © 1996-2000 Graham Barr. All rights reserved.
       
    87 
       
    88 This library is free software; you can redistribute it and/or modify
       
    89 it under the same terms as Perl itself.
       
    90 
       
    91 =cut