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 SCAR 67.9
CC
37
Cov
15%
__open
552.0
new
132.0
rename_to
6.0
CRAP 889.6
↑ 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.05;
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 sub new {
35 0 ▶ my ($src, $mode, $uri_string, %args) = @_;
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
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 sub __open {
86 0 ▶ my ($ftp, $mode, $uri, %args) = @_;
Subroutine __open: not called CC=23 SCAR=63.1
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 sub __find_file {
168 0 ▶ my ($ftp,$pattern) = @_;
Subroutine __find_file: not called CC=1 SCAR=6.9
169  
170 0 my @files = $ftp->ls($pattern);
171 0 return $files[0];
172 }
173  
174  
175 sub filename {
176 0 ▶ my $self = shift;
Subroutine filename: not called CC=1 SCAR=6.9
Pod undocumented
177 0 return ${*$self}{'io_ftp_file'};
178 }
179  
180 sub path {
181 0 ▶ my $self = shift;
Subroutine path: not called CC=1 SCAR=6.9
Pod undocumented
182 0 return ${*$self}{'io_ftp_path'};
183 }
184  
185 sub uri {
186 0 ▶ my $self = shift;
Subroutine uri: not called CC=1 SCAR=6.9
Pod undocumented
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 sub rename_to {
193 0 ▶ my ($self, $new_name) = @_;
Subroutine rename_to: not called CC=2 SCAR=17.9
Pod documented
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 sub delete {
202 0 ▶ my ($self) = @_;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
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 sub mdtm {
212 0 ▶ my ($self) = @_;
Subroutine mdtm: not called CC=2 SCAR=17.9
Pod documented
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 sub size {
219 0 ▶ my ($self) = @_;
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
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
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 =head2 EXPORTS
263  
264 None by default.
265  
266 =head2 REQUIRES
267  
268 L<Net::FTP>
269 L<File::Basename>
270 L<URI>
271 L<Symbol>
272  
273  
274 =head1 CONSTRUCTOR
275  
276 =over 4
277  
278 =item new ( MODE, URI [,OPTIONS] )
279  
280 C<MODE> indicates the FTP command to use, and is one of
281  
282 =over 4
283  
284 =item < get
285  
286 =item > put
287  
288 =item >> append
289  
290 =item << get with wildcard match. This allows fetching a file when the name is not known,
291 or is partially known. Wildcarding is as performed by Net::FTP::ls. If more than one file matches,
292 the same one will always be returned. To process a number of files, they must be deleted
293 or renamed to not match the wildcard.
294  
295 =back
296  
297 C<URI> is an FTP format URI without the leading 'ftp:'.
298 C<OPTIONS> are passed in hash format, and can be one or more of
299  
300 =over 4
301  
302 =item TYPE force ASCII (a) or binary (i) mode for the transfer.
303  
304 =item DEBUG Enables debug messages. Also enabled Net::FTP's Debug flag.
305  
306 =item Timeout Passed to Net::FTP::new
307  
308 =item BlockSize Passed to Net::FTP::new
309  
310 =item Passive Passed to Net::FTP::new
311  
312 =back
313  
314 =back
315  
316 =head1 METHODS
317  
318 =over 4
319  
320 =item rename_to (NEW_NAME)
321 Renames the file.
322  
323 =item delete
324 Deletes the file.
325  
326 =item size
327 Returns the size of the file.
328  
329 =item mdtm
330 Returns the modification time of the fiile.
331  
332 =back
333 Note: These methods cannot be performed while the connection is open.
334 rename_to and delete will fail and return undef if used before the socket is closed.
335  
336 size and mdtm cache their values before the socket is opened.
337 After the socket is closed, they call the Net::FTP methods of the same name.
338  
339 =head1 CREDITS
340  
341 Graham Barr for his Net::FTP module, which does all the 'real work'.
342  
343 tye at PerlMonks
344  
345 =head1 COPYRIGHT
346  
347 (c) 2003 Mike Blackwell. All rights reserved.
348 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
349  
350  
351 =head1 AUTHOR
352  
353 Mike Blackwell <mikeb@cpan.org>
354  
355 =head1 SEE ALSO
356  
357 Net::FTP
358 perl(1).
359  
360 =cut
↑ Summary