602
|
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/A.pm#17 $
|
|
7 |
## Package to read/write on ASCII data connections
|
|
8 |
##
|
|
9 |
|
|
10 |
package Net::FTP::A;
|
|
11 |
use strict;
|
|
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.16";
|
|
19 |
|
|
20 |
sub read {
|
|
21 |
my $data = shift;
|
|
22 |
local *buf = \$_[0]; shift;
|
|
23 |
my $size = shift || croak 'read($buf,$size,[$offset])';
|
|
24 |
my $timeout = @_ ? shift : $data->timeout;
|
|
25 |
|
|
26 |
if (length(${*$data}) < $size && !${*$data}{'net_ftp_eof'}) {
|
|
27 |
my $blksize = ${*$data}{'net_ftp_blksize'};
|
|
28 |
$blksize = $size if $size > $blksize;
|
|
29 |
|
|
30 |
my $l = 0;
|
|
31 |
my $n;
|
|
32 |
|
|
33 |
READ:
|
|
34 |
{
|
|
35 |
my $readbuf = defined(${*$data}{'net_ftp_cr'}) ? "\015" : '';
|
|
36 |
|
|
37 |
$data->can_read($timeout) or
|
|
38 |
croak "Timeout";
|
|
39 |
|
|
40 |
if ($n = sysread($data, $readbuf, $blksize, length $readbuf)) {
|
|
41 |
${*$data}{'net_ftp_bytesread'} += $n;
|
|
42 |
${*$data}{'net_ftp_cr'} = substr($readbuf,-1) eq "\015"
|
|
43 |
? chop($readbuf)
|
|
44 |
: undef;
|
|
45 |
}
|
|
46 |
else {
|
|
47 |
return undef
|
|
48 |
unless defined $n;
|
|
49 |
|
|
50 |
${*$data}{'net_ftp_eof'} = 1;
|
|
51 |
}
|
|
52 |
|
|
53 |
$readbuf =~ s/\015\012/\n/sgo;
|
|
54 |
${*$data} .= $readbuf;
|
|
55 |
|
|
56 |
unless (length(${*$data})) {
|
|
57 |
|
|
58 |
redo READ
|
|
59 |
if($n > 0);
|
|
60 |
|
|
61 |
$size = length(${*$data})
|
|
62 |
if($n == 0);
|
|
63 |
}
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
$buf = substr(${*$data},0,$size);
|
|
68 |
substr(${*$data},0,$size) = '';
|
|
69 |
|
|
70 |
length $buf;
|
|
71 |
}
|
|
72 |
|
|
73 |
sub write {
|
|
74 |
my $data = shift;
|
|
75 |
local *buf = \$_[0]; shift;
|
|
76 |
my $size = shift || croak 'write($buf,$size,[$timeout])';
|
|
77 |
my $timeout = @_ ? shift : $data->timeout;
|
|
78 |
|
|
79 |
(my $tmp = substr($buf,0,$size)) =~ s/\r?\n/\015\012/sg;
|
|
80 |
|
|
81 |
# If the remote server has closed the connection we will be signal'd
|
|
82 |
# when we write. This can happen if the disk on the remote server fills up
|
|
83 |
|
|
84 |
local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
|
|
85 |
|
|
86 |
my $len = length($tmp);
|
|
87 |
my $off = 0;
|
|
88 |
my $wrote = 0;
|
|
89 |
|
|
90 |
my $blksize = ${*$data}{'net_ftp_blksize'};
|
|
91 |
|
|
92 |
while($len) {
|
|
93 |
$data->can_write($timeout) or
|
|
94 |
croak "Timeout";
|
|
95 |
|
|
96 |
$off += $wrote;
|
|
97 |
$wrote = syswrite($data, substr($tmp,$off), $len > $blksize ? $blksize : $len);
|
|
98 |
return undef
|
|
99 |
unless defined($wrote);
|
|
100 |
$len -= $wrote;
|
|
101 |
}
|
|
102 |
|
|
103 |
$size;
|
|
104 |
}
|
|
105 |
|
|
106 |
1;
|
|
107 |
__END__
|
|
108 |
|
|
109 |
=head1 COPYRIGHT
|
|
110 |
|
|
111 |
COPYRIGHT
|
|
112 |
|
|
113 |
© 1996-2000 Graham Barr. All rights reserved.
|
|
114 |
|
|
115 |
This library is free software; you can redistribute it and/or modify
|
|
116 |
it under the same terms as Perl itself.
|
|
117 |
|
|
118 |
=cut
|