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 |
|
|
7 |
|
|
8 |
##
|
|
9 |
## Generic data connection package
|
|
10 |
##
|
|
11 |
|
|
12 |
package Net::FTP::dataconn;
|
|
13 |
|
|
14 |
use Carp;
|
|
15 |
use vars qw(@ISA $timeout $VERSION);
|
|
16 |
use Net::Cmd;
|
|
17 |
use Errno;
|
|
18 |
|
|
19 |
$VERSION = '0.11';
|
|
20 |
@ISA = qw(IO::Socket::INET);
|
|
21 |
|
|
22 |
sub reading
|
|
23 |
{
|
|
24 |
my $data = shift;
|
|
25 |
${*$data}{'net_ftp_bytesread'} = 0;
|
|
26 |
}
|
|
27 |
|
|
28 |
sub abort
|
|
29 |
{
|
|
30 |
my $data = shift;
|
|
31 |
my $ftp = ${*$data}{'net_ftp_cmd'};
|
|
32 |
|
|
33 |
# no need to abort if we have finished the xfer
|
|
34 |
return $data->close
|
|
35 |
if ${*$data}{'net_ftp_eof'};
|
|
36 |
|
|
37 |
# for some reason if we continously open RETR connections and not
|
|
38 |
# read a single byte, then abort them after a while the server will
|
|
39 |
# close our connection, this prevents the unexpected EOF on the
|
|
40 |
# command channel -- GMB
|
|
41 |
if(exists ${*$data}{'net_ftp_bytesread'}
|
|
42 |
&& (${*$data}{'net_ftp_bytesread'} == 0)) {
|
|
43 |
my $buf="";
|
|
44 |
my $timeout = $data->timeout;
|
|
45 |
$data->can_read($timeout) && sysread($data,$buf,1);
|
|
46 |
}
|
|
47 |
|
|
48 |
${*$data}{'net_ftp_eof'} = 1; # fake
|
|
49 |
|
|
50 |
$ftp->abort; # this will close me
|
|
51 |
}
|
|
52 |
|
|
53 |
sub _close
|
|
54 |
{
|
|
55 |
my $data = shift;
|
|
56 |
my $ftp = ${*$data}{'net_ftp_cmd'};
|
|
57 |
|
|
58 |
$data->SUPER::close();
|
|
59 |
|
|
60 |
delete ${*$ftp}{'net_ftp_dataconn'}
|
|
61 |
if exists ${*$ftp}{'net_ftp_dataconn'} &&
|
|
62 |
$data == ${*$ftp}{'net_ftp_dataconn'};
|
|
63 |
}
|
|
64 |
|
|
65 |
sub close
|
|
66 |
{
|
|
67 |
my $data = shift;
|
|
68 |
my $ftp = ${*$data}{'net_ftp_cmd'};
|
|
69 |
|
|
70 |
if(exists ${*$data}{'net_ftp_bytesread'} && !${*$data}{'net_ftp_eof'}) {
|
|
71 |
my $junk;
|
|
72 |
$data->read($junk,1,0);
|
|
73 |
return $data->abort unless ${*$data}{'net_ftp_eof'};
|
|
74 |
}
|
|
75 |
|
|
76 |
$data->_close;
|
|
77 |
|
|
78 |
$ftp->response() == CMD_OK &&
|
|
79 |
$ftp->message =~ /unique file name:\s*(\S*)\s*\)/ &&
|
|
80 |
(${*$ftp}{'net_ftp_unique'} = $1);
|
|
81 |
|
|
82 |
$ftp->status == CMD_OK;
|
|
83 |
}
|
|
84 |
|
|
85 |
sub _select {
|
|
86 |
my ($data, $timeout, $do_read) = @_;
|
|
87 |
my ($rin,$rout,$win,$wout,$tout,$nfound);
|
|
88 |
|
|
89 |
vec($rin='',fileno($data),1) = 1;
|
|
90 |
|
|
91 |
($win, $rin) = ($rin, $win) unless $do_read;
|
|
92 |
|
|
93 |
while (1) {
|
|
94 |
$nfound = select($rout=$rin, $wout=$win, undef, $tout=$timeout);
|
|
95 |
|
|
96 |
last if $nfound >= 0;
|
|
97 |
|
|
98 |
croak "select: $!"
|
|
99 |
unless $!{EINTR};
|
|
100 |
}
|
|
101 |
|
|
102 |
$nfound;
|
|
103 |
}
|
|
104 |
|
|
105 |
sub can_read
|
|
106 |
{
|
|
107 |
_select(@_[0,1],1);
|
|
108 |
}
|
|
109 |
|
|
110 |
sub can_write
|
|
111 |
{
|
|
112 |
_select(@_[0,1],0);
|
|
113 |
}
|
|
114 |
|
|
115 |
sub cmd
|
|
116 |
{
|
|
117 |
my $ftp = shift;
|
|
118 |
|
|
119 |
${*$ftp}{'net_ftp_cmd'};
|
|
120 |
}
|
|
121 |
|
|
122 |
sub bytes_read {
|
|
123 |
my $ftp = shift;
|
|
124 |
|
|
125 |
${*$ftp}{'net_ftp_bytesread'} || 0;
|
|
126 |
}
|
|
127 |
|
|
128 |
1;
|
|
129 |
__END__
|
|
130 |
|
|
131 |
=head1 COPYRIGHT
|
|
132 |
|
|
133 |
COPYRIGHT
|
|
134 |
|
|
135 |
© 1996-2000 Graham Barr. All rights reserved.
|
|
136 |
|
|
137 |
This library is free software; you can redistribute it and/or modify
|
|
138 |
it under the same terms as Perl itself.
|
|
139 |
|
|
140 |
=cut
|