blib/lib/IO/Ftp.pm

filter: Statementstmt 21.5% 33 / 153 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 44.4% 8 / 18 Podpod 62.5% 5 / 8 totaltotal 17.6% 46 / 261 CC 37 SCAR 55.7
CC
37
Cov
15%
__open
552.0
new
132.0
rename_to
6.0
CRAP 263.2
↑ Summary
1 package IO::Ftp;
2 1 require 5.005_62;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8  
9 1 our @ISA = qw(Exporter);
10  
11 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 $VERSION = 0.06;
14 1 our %EXPORT_TAGS = ( 'all' => [ qw(
15 new
16 delete
17 rename_to
18 mdtm
19 size
20 filename
21 ) ] );
22  
23 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24 1 our @EXPORT = qw();
25  
26  
27 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Net::FTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
35 0 my ($src, $mode, $uri_string, %args) = @_;
36 0 ▶ my $class = ref $src || 'IO::Ftp';
Condition0%0/2
ref $src || 'IO::Ftp'
l!l
00
MC/DC0%0/1
ref $src || 'IO::Ftp'
ref $src
Truth table0%0/2
A
A= ref $src
Aresult
11
00
37 0 ▶ if (ref $src and not $src->isa('IO::Ftp')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src and not $src->isa("IO::Ftp")) 0 0 0
Condition0%0/3
ref $src and not $src->isa("IO::Ftp")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $src and not $src->isa("IO::Ftp")
ref $src not $src->isa("IO::Ftp")
Truth table0%0/3
A and not(B)
A= ref $srcB= not $src->isa("IO::Ftp")
ABresult
0X0
100
111
38 0 carp "Can't make an IO::FTP from a ", ref $src;
39 0 return;
40 }
41  
42 0 my $uri;
43 0 ▶ if (ref $uri_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri_string) { } 0 0 0
44 0 ▶ unless ($uri_string->isa('URI')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri_string->isa("URI")) 0 0 0
45 0 carp "can' t make a URI from a ", ref $uri_string;
46 0 return;
47 }
48 0 $uri = $uri_string;
49 } else {
50 0 $uri = URI->new('ftp:' . $uri_string);
51 }
52  
53 0 my $ftp;
54 0 ▶ if (ref $src and not $uri->host) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src and not $uri->host) { } 0 0 0
Condition0%0/3
ref $src and not $uri->host
!ll&&!rl&&r
000
MC/DC0%0/2
ref $src and not $uri->host
ref $src not $uri->host
Truth table0%0/3
A and not(B)
A= ref $srcB= not $uri->host
ABresult
0X0
100
111
55 0 ▶ if ($src->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src->connected) 0 0 0
56 0 warn "Can't reuse host with open connection";
57 0 return;
58 }
59 0 $ftp = ${*$src}{'io_ftp_ftp'};
60 } else {
61 $ftp = Net::FTP->new(
62 $uri->host,
63 Port => $uri->port,
64 Debug => $args{DEBUG},
65 Timeout => $args{Timeout},
66 BlockSize => $args{BlockSize},
67 Passive => $args{Passive},
68 0 );
69 }
70
71 0 ▶ unless ($ftp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp) 0 0 0
72 0 carp "Can't connect to host ", $uri->host;
73 0 return;
74 }
75
76 0 my $self = __open($ftp, $mode, $uri, %args);
77 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
78
79 0 ${*$self}{'io_ftp_ftp'} = $ftp;
80 0 ${*$self}{'io_ftp_uri'} = $uri;
81  
82 0 return bless $self, $class;
83 }
84  
85 0 ▶sub __open {
Subroutine __open: not called CC=23 SCAR=63.1
86 0 my ($ftp, $mode, $uri, %args) = @_;
87  
88 0 ▶ my $id = $uri->user || 'anonymous';
Condition0%0/2
$uri->user || 'anonymous'
l!l
00
MC/DC0%0/1
$uri->user || 'anonymous'
$uri->user
Truth table0%0/2
A
A= $uri->user
Aresult
11
00
89 0 ▶ my $pwd = $uri->password || 'anon@anon.org';
Condition0%0/2
$uri->password || 'anon@anon.org'
l!l
00
MC/DC0%0/1
$uri->password || 'anon@anon.org'
$uri->password
Truth table0%0/2
A
A= $uri->password
Aresult
11
00
90
91 0 ▶ unless ($ftp->login($id, $pwd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp->login($id, $pwd)) 0 0 0
92 0 warn "Can't login: ", $ftp->message;
93 0 return;
94 }
95
96 0 ▶ fileparse_set_fstype($args{OS}) if $args{OS};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"OS"} 0 0 0
97
98 0 my ($file, $path) = fileparse($uri->path);
99 0 ▶ warn "File: $file, Path: $path" if $args{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"DEBUG"} 0 0 0
100  
101 0 ▶ if ($path =~ m{^//(.*)}) { # initial single / is relative path, // is absolute
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^//(.*)]) 0 0 0
102 0 $path = $1;
103 0 ▶ warn "cwd /" if $args{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"DEBUG"} 0 0 0
104 0 ▶ unless ($ftp->cwd('/')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp->cwd("/")) 0 0 0
105 0 warn "Can't cwd to /";
106 0 return;
107 }
108 }
109
110 0 foreach (split '/', $path) {
111 0 ▶ next unless $_; #ignore embedded back-to-back /. else will cwd with no parm, which will default to 'cwd /'
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 0 0
112 0 ▶ warn "cwd $_" if $args{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"DEBUG"} 0 0 0
113 0 ▶ unless ($ftp->cwd($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp->cwd($_)) 0 0 0
114 0 warn "Can't cwd to $_";
115 0 return;
116 }
117 }
118 0 ▶ if ($args{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"type"}) 0 0 0
119 0 $args{type} = uc $args{type};
120 0 ▶ unless ($args{type} =~ /^[AI]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"type"} =~ /^[AI]$/) 0 0 0
121 0 carp "Invalid type: $args{type}";
122 0 return;
123 }
124 0 ▶ unless ($ftp->type($args{type}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp->type($args{"type"})) 0 0 0
125 0 carp "Can't set type $args{type}: ", $ftp->message;
126 }
127 }
128  
129 0 ▶ if ($mode eq '<<') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "<<") 0 0 0
130 0 $file = __find_file($ftp, $file);
131 0 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
132 }
133  
134 # cache these in case user wants initial values. Can't get them once the data connection is open.
135 0 my $size = $ftp->size($file);
136 0 my $mdtm = $ftp->mdtm($file);
137
138
139 0 my $dataconn;
140 0 ▶ if ($mode eq '<' or $mode eq '<<') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq '<' or $mode eq '<<') { } 0 0 0
elsif ($mode eq '>') { } 0 0 0
elsif ($mode eq '>>') { } 0 0 0
Condition0%0/3
$mode eq "<" or $mode eq "<<"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mode eq "<" or $mode eq "<<"
$mode eq "<" $mode eq "<<"
Truth table0%0/3
A or B
A= $mode eq "<"B= $mode eq "<<"
ABresult
1X1
011
000
141 0 $dataconn = $ftp->retr($file);
142 } elsif ($mode eq '>') {
143 0 $dataconn = $ftp->stor($file);
144 } elsif ($mode eq '>>') {
145 0 $dataconn = $ftp->appe($file);
146 } else {
147 0 carp "Invalid mode $mode";
148 0 return;
149 }
150  
151 0 ▶ unless ($dataconn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dataconn) 0 0 0
152 0 carp "Can't open $file: ", $ftp->message ;
153 0 return;
154 }
155  
156 # we want to be a subclass of the dataconn, but its class is dynamic.
157 0 push @ISA, ref $dataconn;
158
159 0 ${*$dataconn}{'io_ftp_file'} = $file;
160 0 ${*$dataconn}{'io_ftp_path'} = $path;
161 0 ${*$dataconn}{'io_ftp_size'} = $size;
162 0 ${*$dataconn}{'io_ftp_mdtm'} = $mdtm;
163
164 0 return $dataconn;
165 }
166  
167 0 ▶sub __find_file {
Subroutine __find_file: not called CC=1 SCAR=6.9
168 0 my ($ftp,$pattern) = @_;
169  
170 0 my @files = $ftp->ls($pattern);
171 0 return $files[0];
172 }
173  
174  
175 0 ▶sub filename {
Subroutine filename: not called CC=1 SCAR=6.9
Pod undocumented
176 0 my $self = shift;
177 0 return ${*$self}{'io_ftp_file'};
178 }
179  
180 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my $self = shift;
182 0 return ${*$self}{'io_ftp_path'};
183 }
184  
185 0 ▶sub uri {
Subroutine uri: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my $self = shift;
187 0 return ${*$self}{'io_ftp_uri'};
188 }
189  
190 ### allow shortcuts to Net::FTP's rename and delete, but only if data connection not open. OTW we'll hang.
191  
192 0 ▶sub rename_to {
Subroutine rename_to: not called CC=2 SCAR=17.9
Pod documented
193 0 my ($self, $new_name) = @_;
194 0 ▶ return if $self->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->connected 0 0 0
195
196 0 my $ret = ${*$self}{'io_ftp_ftp'}->rename(${*$self}{'io_ftp_file'}, $new_name);
197 0 ${*$self}{'io_ftp_file'} = $new_name;
198 0 return $ret;
199 }
200  
201 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
202 0 my ($self) = @_;
203 0 ▶ return if $self->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->connected 0 0 0
204
205 0 return ${*$self}{'io_ftp_ftp'}->delete(${*$self}{'io_ftp_file'});
206 }
207  
208  
209 ### return cached stats if connected, or real ones if connection closed.
210  
211 0 ▶sub mdtm {
Subroutine mdtm: not called CC=2 SCAR=17.9
Pod documented
212 0 my ($self) = @_;
213 0 ▶ return ${*$self}{'io_ftp_mdtm'} if $self->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->connected 0 0 0
214
215 0 return ${*$self}{'io_ftp_ftp'}->mdtm(${*$self}{'io_ftp_file'});
216 }
217  
218 0 ▶sub size {
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
219 0 my ($self) = @_;
220 0 ▶ return ${*$self}{'io_ftp_size'} if $self->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->connected 0 0 0
221
222 0 return ${*$self}{'io_ftp_ftp'}->size(${*$self}{'io_ftp_file'});
223 }
224  
225  
226 1 1;
227  
228  
229 =head1 NAME
230  
231 IO::Ftp - A simple interface to Net::FTP's socket level get/put (DEPRECATED)
232  
233 =head1 SYNOPSIS
234  
235  
236 use IO::Ftp;
237
238 my $out = IO::Ftp->new('>','//user:pwd@foo.bar.com/foo/bar/fu.bar', TYPE=>'a');
239 my $in = IO::Ftp->new('<','//foo.bar.com/foo/bar/fu.bar', TYPE=>'a'); #anon access example
240
241 while (<$in>) {
242 s/foo/bar/g;
243 print $out;
244 }
245
246 close $in;
247 close $out;
248  
249  
250 ### for something along the lines of 'mget':
251
252 while (my $in = IO::Ftp->new('<<','//foo.bar.com/foo/bar/*.txt', TYPE=>'a') {
253 print "processing ",$in->filename, "\n";
254 #...
255 $in->close;
256 $in->delete;
257 }
258  
259  
260 =head1 DESCRIPTION
261  
262 Deprecated. Other better options exist. See, for example, IO::All::FTP
263  
264 =head2 EXPORTS
265  
266 None by default.
267  
268 =head2 REQUIRES
269  
270 L<Net::FTP>
271 L<File::Basename>
272 L<URI>
273 L<Symbol>
274  
275  
276 =head1 CONSTRUCTOR
277  
278 =over 4
279  
280 =item new ( MODE, URI [,OPTIONS] )
281  
282 C<MODE> indicates the FTP command to use, and is one of
283  
284 =over 4
285  
286 =item < get
287  
288 =item > put
289  
290 =item >> append
291  
292 =item << get with wildcard match. This allows fetching a file when the name is not known,
293 or is partially known. Wildcarding is as performed by Net::FTP::ls. If more than one file matches,
294 the same one will always be returned. To process a number of files, they must be deleted
295 or renamed to not match the wildcard.
296  
297 =back
298  
299 C<URI> is an FTP format URI without the leading 'ftp:'.
300 C<OPTIONS> are passed in hash format, and can be one or more of
301  
302 =over 4
303  
304 =item TYPE force ASCII (a) or binary (i) mode for the transfer.
305  
306 =item DEBUG Enables debug messages. Also enabled Net::FTP's Debug flag.
307  
308 =item Timeout Passed to Net::FTP::new
309  
310 =item BlockSize Passed to Net::FTP::new
311  
312 =item Passive Passed to Net::FTP::new
313  
314 =back
315  
316 =back
317  
318 =head1 METHODS
319  
320 =over 4
321  
322 =item rename_to (NEW_NAME)
323 Renames the file.
324  
325 =item delete
326 Deletes the file.
327  
328 =item size
329 Returns the size of the file.
330  
331 =item mdtm
332 Returns the modification time of the fiile.
333  
334 =back
335  
336 Note: These methods cannot be performed while the connection is open.
337 rename_to and delete will fail and return undef if used before the socket is closed.
338  
339 size and mdtm cache their values before the socket is opened.
340 After the socket is closed, they call the Net::FTP methods of the same name.
341  
342 =head1 CREDITS
343  
344 Graham Barr for his Net::FTP module, which does all the 'real work'.
345  
346 tye at PerlMonks
347  
348 =head1 COPYRIGHT
349  
350 (c) 2003 Mike Blackwell. All rights reserved.
351 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
352  
353  
354 =head1 AUTHOR
355  
356 Mike Blackwell <mikeb@cpan.org>
357  
358 =head1 SEE ALSO
359  
360 Net::FTP
361 perl(1).
362  
363 =cut
↑ Summary