Net/FTP/Common.pm

filter: Statementstmt 63.1% 118 / 187 Branchbran 32.8% 21 / 64 Conditioncond 23.0% 3 / 13 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 56.6% 17 / 30 Podpod 75.0% 18 / 24 totaltotal 54.8% 177 / 323 CC 38 SCAR 22.7
CC
38
Cov
54%
send
42.0
login
19.1
connected
12.0
CRAP 9.7
↑ Summary TestConfig.pm »
1 package Net::FTP::Common;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Carp qw(cluck confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Net::FTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 4 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 @ISA = qw(Net::FTP);
13  
14 4 $VERSION = '7.0.d';
15  
16 # Preloaded methods go here.
17  
18 4 ▶sub new {
Subroutine new: called CC=4 SCAR=14.7
Pod documented
19 4 my $pkg = shift;
20 4 my $common_cfg_in = shift;
21 4 my %netftp_cfg_in = @_;
22  
23 4 my %common_cfg_default =
24 (
25 Host => 'ftp.microsoft.com',
26 RemoteDir => '/pub',
27 # LocalDir => '.', # setup something for $ez->get
28 Type => 'I'
29 );
30  
31 4 my %netftp_cfg_default = ( Debug => 1, Timeout => 240, Passive => 1 );
32  
33 # overwrite defaults with values supplied by constructor input
34 4 @common_cfg_default{keys %$common_cfg_in} = values %$common_cfg_in;
35 4 @netftp_cfg_default{keys %netftp_cfg_in} = values %netftp_cfg_in;
36
37 4 my $self = {};
38  
39 4 @{$self->{Common}}{keys %common_cfg_default} = values %common_cfg_default;
40 4 @{$self }{keys %netftp_cfg_default} = values %netftp_cfg_default;
41  
42 4 my $new_self = { %$self, Common => $self->{Common} } ;
43  
44 4 ▶ if (my $file = $self->{Common}{STDERR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->{"Common"}{"STDERR"}) 0 4 4
45 0 ▶ open DUP, ">$file" or die "cannot dup STDERR to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open DUP, ">$file" or die "cannot dup STDERR to ${file}: $!" 0 0 0
46 0 lstat DUP; # kill used only once error
47 0 open STDERR, ">&DUP";
48 }
49  
50 warn "Net::FTP::Common::VERSION = ", $Net::FTP::Common::VERSION
51 4 ▶ if $self->{Debug} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 4 0 4
52  
53  
54 4 bless $new_self, $pkg;
55 }
56  
57 1 ▶sub config_dump {
Subroutine config_dump: called CC=1 SCAR=0.0
Pod undocumented
58 1 my $self = shift;
59
60 1 sprintf '
61 Here are the configuration parameters:
62 -------------------------------------
63 %s
64 ', Dumper($self);
65  
66 }
67  
68  
69 11 ▶sub Common {
Subroutine Common: called CC=2 SCAR=7.0
Pod documented
70 11 my $self = shift;
71  
72 11 ▶ not (@_ % 2) or die
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and die "\nOdd number of elements in assignment hash in call to Common().\nCommon() is a 'setter' subroutine. You cannot call it with an\nodd number of arguments (e.g. $self->Common('Type') ) and \nexpect it to get a value. use GetCommon() for that.\n\nHere is what you passed in.\n", Dumper(\@_) 0 11 11
73 "
74 Odd number of elements in assignment hash in call to Common().
75 Common() is a 'setter' subroutine. You cannot call it with an
76 odd number of arguments (e.g. $self->Common('Type') ) and
77 expect it to get a value. use GetCommon() for that.
78  
79 Here is what you passed in.
80 ", Dumper(\@_);
81  
82 11 my %tmp = @_;
83  
84 # warn "HA: ", Dumper(\%tmp,\@_);
85  
86 11 @{$self->{Common}}{keys %tmp} = values %tmp;
87 }
88  
89 44 ▶sub GetCommon {
Subroutine GetCommon: called CC=3 SCAR=11.2
Pod documented
90 44 my ($self,$key) = @_;
91  
92 44 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) { } 44 0 44
93 44 ▶ if (defined($self->{Common}{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Common'}{$key}) { } 38 6 44
94 38 return ($self->{Common}{$key});
95 } else {
96 6 return undef;
97 }
98 } else {
99 0 $self->{Common};
100 }
101 }
102  
103 6 ▶sub Host {
Subroutine Host: called CC=2 SCAR=7.6
Pod undocumented
104 $_[0]->{Common}->{Host}
105  
106 6 ▶ or die "Host must be defined when creating a __PACKAGE__ object"
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"Common"}{"Host"} or die "Host must be defined when creating a __PACKAGE__ object" 6 0 6
107 }
108  
109 0 ▶sub NetFTP {
Subroutine NetFTP: not called CC=1 SCAR=6.9
Pod documented
110  
111 0 my ($self, %config) = @_;
112  
113 0 @{$self}{keys %config} = values %config;
114  
115 }
116  
117 5 ▶sub login {
Subroutine login: called CC=9 SCAR=29.5
Pod documented
118 5 my ($self, %config) = @_;
119  
120 5 shift;
121  
122 5 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 5 5
123 0 die sprintf "Do not confuse Net::FTP::Common's login() with Net::FTP's login()
124 Net::FTP::Common's login() expects to be supplied a hash.
125 E.g. \$ez->login(Host => \$Host)
126  
127 It was called incorrectly (%s). Program terminating
128 %s
129 ", (join ':', @_), $self->config_dump;
130 }
131  
132 # my $ftp_session = Net::FTP->new($self->Host, %{$self->{NetFTP}});
133 5 my $ftp_session = Net::FTP->new($self->Host, %$self);
134  
135 # $ftp_session or return undef;
136 5 ▶ $ftp_session or
Branch: 1 branch
BranchTrueFalseTotal
$ftp_session or die sprintf("FATAL: attempt to create Net::FTP session on host %s failed.\nIf you cannot figure out why, supply the configuration parameters when\nemailing the support email list.\n %s", $self->Host, $self->config_dump) 4 1 5
137 die sprintf 'FATAL: attempt to create Net::FTP session on host %s failed.
138 If you cannot figure out why, supply the configuration parameters when
139 emailing the support email list.
140 %s', $self->Host, $self->config_dump;
141  
142  
143 4 my $session;
144 4 my $account = $self->GetCommon('Account');
145 4 ▶ if ($self->GetCommon('User') and $self->GetCommon('Pass')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->GetCommon('User') and $self->GetCommon('Pass')) { } 4 0 4
Condition33%1/3
$self->GetCommon("User") and $self->GetCommon("Pass")
!ll&&!rl&&r
004
MC/DC0%0/2
$self->GetCommon("User") and $self->GetCommon("Pass")
$self->GetCommon("User") $self->GetCommon("Pass")
Truth table33%1/3
A and B
A= $self->GetCommon("User")B= $self->GetCommon("Pass")
ABresult
0X0
100
111
146 4 $session =
147 $ftp_session->login($self->GetCommon('User') ,
148 $self->GetCommon('Pass'),
149 $account);
150 } else {
151 0 warn "either User or Pass was not defined. Attempting .netrc for login";
152 0 $session =
153 $ftp_session->login;
154 }
155  
156 4 ▶ $session and ($self->Common('FTPSession', $ftp_session))
Branch: 1 branch
BranchTrueFalseTotal
$session and $self->Common("FTPSession", $ftp_session) and return $ftp_session or warn "error logging in: $!" and return undef 0 0 0
Condition20%2/10
$session && $self->Common('FTPSession', $ftp_session) && (return $ftp_session) || warn("error logging in: $!") && (return undef)
l!l&&r!l&&!r
000
$session and $self->Common("FTPSession", $ftp_session) and return $ftp_session
!ll
04
$session and $self->Common("FTPSession", $ftp_session)
!ll&&!rl&&r
004
warn "error logging in: $!" and return undef
!ll
00
MC/DC0%0/3
$session && $self->Common('FTPSession', $ftp_session) && (return $ftp_session) || warn("error logging in: $!") && (return undef)
$session $self->Common("FTPSession", $ftp_session) warn "error logging in: $!"
Truth table0%0/5
A and B || C
A= $sessionB= $self->Common("FTPSession", $ftp_session)C= warn "error logging in: $!"
ABCresult
11X1
0X11
1011
0X00
1000
157 and return $ftp_session
158 or
159 warn "error logging in: $!" and return undef;
160  
161 }
162  
163 2 ▶sub ls {
Subroutine ls: called CC=2 SCAR=7.1
Pod documented
164 2 my ($self, @config) = @_;
165 2 my %config=@config;
166  
167 2 my $ftp = $self->prep(%config);
168  
169 2 my $ls = $ftp->ls;
170 2 ▶ if (!defined($ls)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ls) { } 0 2 2
171 0 return ();
172 } else {
173 2 return @{$ls};
174 }
175 }
176  
177 # contributed by kevin evans
178 # this returns a hash of hashes keyed by filename with attributes for each
179 1 ▶sub dir {
Subroutine dir: called CC=7 SCAR=19.7
Pod documented
180 1 my ($self, @config) = @_;
181 1 my %config=@config;
182  
183  
184 1 my $ftp = $self->prep(%config);
185  
186 1 my $dir = $ftp->dir;
187 1 ▶ if (!defined($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $dir) { } 0 1 1
188 0 return ();
189 } else
190 {
191 1 my %HoH;
192  
193 # Comments were made on this code in this thread:
194 # http://perlmonks.org/index.pl?node_id=287552
195  
196 1 foreach (@{$dir})
197 {
198 # $_ =~ m#([a-z-]*)\s*([0-9]*)\s*([0-9a-zA-Z]*)\s*([0-9a-zA-Z]*)\s*([0-9]*)\s*([A-Za-z]*)\s*([0-9]*)\s*([0-9A-Za-z:]*)\s*([A-Za-z0-9.-]*)#;
199 #$_ = m#([a-z-]*)\s*([0-9]*)\s*([0-9a-zA-Z]*)\s*([0-9a-zA-Z]*)\s*([0-9]*)\s*([A-Za-z]*)\s*([0-9]*)\s*([0-9A-Za-z:]*)\s*([\w*\W*\s*\S*]*)#;
200  
201 =for comment
202  
203 drwxr-xr-x 8 0 0 4096 Sep 27 2003 .
204 drwxr-xr-x 8 0 0 4096 Sep 27 2003 ..
205 drwxr-xr-x 3 0 0 4096 Sep 11 2003 .afs
206 -rw-r--r-- 1 0 0 809 Sep 26 2003 .banner
207 ----r-xr-x 1 0 0 0 Mar 4 2002 .notar
208 -rw-r--r-- 1 0 0 796 Sep 27 2003 README
209  
210 =cut
211  
212 15 ▶ warn "input-line: $_" if $self->{Debug} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 15 0 15
213  
214 15 $_ =~ m!^
215 ([\-FlrwxsStTdD]{10}) # directory and permissions
216 \s+
217 (\d+) # inode
218 \s+
219 (\w+) # 2nd number
220 \s+
221 (\w+) # 3rd number
222 \s+
223 (\d+) # file/dir size
224 \s+
225 (\w{3,4}) # month
226 \s+
227 (\d{1,2}) # day
228 \s+
229 (\d{1,2}:\d{2}|\d{4}) # year
230 \s+
231 (.+) # filename
232 $!x;
233  
234  
235 15 my $perm = $1;
236 15 my $inode = $2;
237 15 my $owner = $3;
238 15 my $group = $4;
239 15 my $size = $5;
240 15 my $month = $6;
241 15 my $day = $7;
242 15 my $yearOrTime = $8;
243 15 my $name = $9;
244 15 my $linkTarget;
245  
246 warn "
247 my $perm = $1;
248 my $inode = $2;
249 my $owner = $3;
250 my $group = $4;
251 my $size = $5;
252 my $month = $6;
253 my $day = $7;
254 my $yearOrTime = $8;
255 my $name = $9;
256 my $linkTarget;
257 15 ▶" if $self->{Debug} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 15 0 15
258  
259 15 ▶ if ( $' =~ m#\s*->\s*([A-Za-z0-9.-/]*)# ) # it's a symlink
Branch: 1 branch
BranchTrueFalseTotal
if ($' =~ m[\s*->\s*([A-Za-z0-9.-/]*)]) 0 15 15
260 0 { $linkTarget = $1; }
261  
262 15 $HoH{$name}{perm} = $perm;
263 15 $HoH{$name}{inode} = $inode;
264 15 $HoH{$name}{owner} = $owner;
265 15 $HoH{$name}{group} = $group;
266 15 $HoH{$name}{size} = $size;
267 15 $HoH{$name}{month} = $month;
268 15 $HoH{$name}{day} = $day;
269 15 $HoH{$name}{yearOrTime} = $yearOrTime;
270 15 $HoH{$name}{linkTarget} = $linkTarget;
271  
272 15 ▶ warn "regexp-matches for ($name): ", Dumper(\$HoH{$name}) if $self->{Debug} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 15 0 15
273  
274 }
275 1 return(%HoH);
276 }
277 }
278  
279  
280  
281 0 ▶sub mkdir {
Subroutine mkdir: not called CC=1 SCAR=6.9
Pod documented
282 0 my ($self,%config) = @_;
283  
284 0 my $ftp = $self->prep(%config);
285 0 my $rd = $self->GetCommon('RemoteDir');
286 0 my $r = $self->GetCommon('Recurse');
287 0 $ftp->mkdir($rd, $r);
288 }
289  
290  
291 1 ▶sub exists {
Subroutine exists: called CC=2 SCAR=7.0
Pod documented
292 1 my ($self,%cfg) = @_;
293  
294 1 my @listing = $self->ls(%cfg);
295  
296 1 my $rf = $self->GetCommon('RemoteFile');
297  
298 1 ▶ warn sprintf "[checking @listing for [%s]]", $rf if $self->{Debug} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 1 0 1
299  
300 1 scalar grep { $_ eq $self->GetCommon('RemoteFile') } @listing;
301 }
302  
303 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
304 0 my ($self,%cfg) = @_;
305  
306 0 my $ftp = $self->prep(%cfg);
307 0 my $rf = $self->GetCommon('RemoteFile');
308  
309
310 0 ▶ warn Dumper \%cfg if $self->{Debug} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
311  
312 0 $ftp->delete($rf);
313  
314 }
315  
316 1 ▶sub grep {
Subroutine grep: called CC=1 SCAR=0.0
Pod documented
317  
318 1 my ($self,%cfg) = @_;
319  
320 # warn sprintf "self: %s host: %s cfg: %s", $self, $host, Data::Dumper::Dumper(\%cfg);
321  
322 1 my @listing = $self->ls(%cfg);
323  
324 1 grep { $_ =~ /$cfg{Grep}/ } @listing;
325 }
326  
327 0 ▶sub connected {
Subroutine connected: not called CC=3 SCAR=24.8
Pod documented
328 0 my $self = shift;
329  
330 # warn "CONNECTED SELF ", Dumper($self);
331  
332 0 ▶ my $session = $self->GetCommon('FTPSession') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $session = $self->GetCommon("FTPSession") or return 0 0 0 0
333  
334 0 local $@;
335 0 my $pwd;
336 0 ▶ my $connected = $session->pwd ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$session->pwd ? : 0 0 0
337 # warn "connected: $connected RESP: $connected";
338 0 $connected;
339 }
340  
341 0 ▶sub quit {
Subroutine quit: not called CC=2 SCAR=17.9
Pod documented
342 0 my $self = shift;
343  
344 0 ▶ $self->connected and $self->GetCommon('FTPSession')->quit;
Branch: 1 branch
BranchTrueFalseTotal
$self->connected and $self->GetCommon("FTPSession")->quit 0 0 0
345  
346 }
347  
348  
349 0 ▶sub prepped {
Subroutine prepped: not called CC=2 SCAR=17.9
Pod undocumented
350 0 my $self = shift;
351 0 ▶ my $prepped = $self->GetCommon('FTPSession') and $self->connected;
Branch: 1 branch
BranchTrueFalseTotal
my $prepped = $self->GetCommon("FTPSession") and $self->connected 0 0 0
352 # warn "prepped: $prepped";
353 0 $prepped;
354 }
355  
356 4 ▶sub prep {
Subroutine prep: called CC=3 SCAR=11.1
Pod undocumented
357 4 my $self = shift;
358 4 my %cfg = @_;
359  
360 4 $self->Common(%cfg);
361  
362 # This will not work if the Host changes and you are still connected
363 # to the prior host. It might be wise to simply drop connection
364 # if the Host key changes, but I don't think I will go there right now.
365 # my $ftp = $self->connected
366 # ? $self->GetCommon('FTPSession')
367 # : $self->login ;
368 # So instead:
369 4 my $ftp = $self->login ;
370  
371
372 3 ▶ $self->Common(LocalDir => '.') unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->GetCommon("LocalDir") 2 1 3
373 $self->GetCommon('LocalDir') ;
374  
375 3 ▶ if ($self->{Common}->{RemoteDir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Common'}{'RemoteDir'}) { } 3 0 3
376 3 $ftp->cwd($self->GetCommon('RemoteDir'))
377 } else {
378 0 warn "RemoteDir not configured. ftp->cwd will not work. certain Net::FTP usages will failed.";
379 }
380 3 $ftp->type($self->GetCommon('Type'));
381  
382 3 $ftp;
383 }
384  
385 0 ▶sub binary {
Subroutine binary: not called CC=1 SCAR=6.9
Pod documented
386 0 my $self = shift;
387  
388 0 $self->{Common}{Type} = 'I';
389 }
390  
391 0 ▶sub ascii {
Subroutine ascii: not called CC=1 SCAR=6.9
Pod documented
392 0 my $self = shift;
393  
394 0 $self->{Common}{Type} = 'A';
395 }
396  
397 1 ▶sub get {
Subroutine get: called CC=3 SCAR=22.0
Pod documented
398  
399 1 my ($self,%cfg) = @_;
400  
401 1 my $ftp = $self->prep(%cfg);
402  
403 0 my $r;
404  
405 my $file;
406  
407 0 ▶ if ($self->GetCommon('LocalFile')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->GetCommon('LocalFile')) { } 0 0 0
408 0 $file= $self->GetCommon('LocalFile');
409 } else {
410 0 $file=$self->GetCommon('RemoteFile');
411 }
412
413 0 my $local_file = join '/', ($self->GetCommon('LocalDir'), $file);
414
415 # warn "LF: $local_file ", "D: ", Dumper($self);
416  
417  
418 0 ▶ if ($r = $ftp->get($self->GetCommon('RemoteFile'), $local_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r = $ftp->get($self->GetCommon('RemoteFile'), $local_file)) { } 0 0 0
419 0 return $r;
420 } else {
421 0 warn sprintf 'download of %s to %s failed',
422 $self->GetCommon('RemoteFile'), $self->GetCommon('LocalFile');
423 0 warn
424 'here are the settings in your Net::FTP::Common object: %s',
425 Dumper($self);
426 0 return undef;
427 }
428
429  
430 }
431  
432 0 ▶sub file_attr {
Subroutine file_attr: not called CC=1 SCAR=6.9
Pod undocumented
433 0 my $self = shift;
434 0 my %hash;
435 0 my @key = qw(LocalFile LocalDir RemoteFile RemoteDir);
436 0 @hash{@key} = @{$self->{Common}}{@key};
437 0 %hash;
438 }
439  
440 0 ▶sub bad_filename {
Subroutine bad_filename: not called CC=1 SCAR=6.9
Pod undocumented
441 0 shift =~ /[\r\n]/s;
442 }
443  
444 0 ▶sub send {
Subroutine send: not called CC=6 SCAR=37.4
Pod documented
445 0 my ($self,%cfg) = @_;
446  
447 0 my $ftp = $self->prep(%cfg);
448  
449 # warn "send_self", Dumper($self);
450  
451 0 my %fa = $self->file_attr;
452  
453 0 ▶ if (bad_filename($fa{LocalFile})) {
Branch: 1 branch
BranchTrueFalseTotal
if (bad_filename($fa{"LocalFile"})) 0 0 0
454 0 warn "filenames may not have CRLF in them. skipping $fa{LocalFile}";
455 0 return;
456 }
457  
458 0 ▶ warn "send_fa: ", Dumper(\%fa) if $self->{Debug} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
459  
460  
461 0 my $lf = sprintf "%s/%s", $fa{LocalDir}, $fa{LocalFile};
462 0 ▶ my $RF = $fa{RemoteFile} ? $fa{RemoteFile} : $fa{LocalFile};
Branch: 1 branch
BranchTrueFalseTotal
$fa{'RemoteFile'} ? : 0 0 0
463 0 my $rf = sprintf "%s/%s", $fa{RemoteDir}, $RF;
464  
465 0 ▶ warn "[upload $lf as $rf]" if $self->{Debug} ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
466  
467 0 ▶ $ftp->put($lf, $RF) or
Branch: 1 branch
BranchTrueFalseTotal
$ftp->put($lf, $RF) or confess(sprintf('upload of %s to %s failed', $lf, $rf)) 0 0 0
468 confess sprintf "upload of %s to %s failed", $lf, $rf;
469 }
470  
471 0 ▶sub put { goto &send }
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
472  
473 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
474  
475  
476 }
477  
478  
479 4 1;
480 __END__
↑ Summary TestConfig.pm »