Upfiles/FTPlazy.pm

filter: Statementstmt 21.3% 29 / 136 Branchbran 5.3% 3 / 56 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 27.5% 8 / 29 Podpod 0.0% 0 / 23 totaltotal 12.2% 40 / 327 SCAR 72.7
CC
46
Cov
13%
ensure_host
182.0
ensure_binary
56.0
ensure_login
42.0
CRAP 1431.3
« Upfiles.pm ↑ Summary Upfiles/SFTPlazy.pm »
1 # Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017, 2020 Kevin Ryde
2  
3 # This file is part of Upfiles.
4 #
5 # Upfiles is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Upfiles is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Upfiles. If not, see <http://www.gnu.org/licenses/>.
17  
18 package App::Upfiles::FTPlazy;
19 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Locale::TextDomain ('App-Upfiles');
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # uncomment this to run the ### lines
26 # use Smart::Comments;
27  
28 1 our $VERSION = 15;
29  
30 # Have croaks from Net::FTP reported against the caller (App::Upfiles)
31 # rather than here.
32 1 our @CARP_NOT = ('Net::FTP');
33  
34 sub new {
35 2 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
36 2 return bless { verbose => 0,
37 want_dir => '/',
38 have_site_utime => 'unknown',
39 message => '',
40 code => 0,
41 @_ }, $class;
42 }
43  
44 sub message {
45 0 ▶ my ($self) = @_;
Subroutine message: not called CC=2 SCAR=17.9
Pod undocumented
46 0 ▶ return (defined $self->{'ftp'} ? $self->{'ftp'}->message : $self->{'message'});
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'ftp'} ? : 0 0 0
47 }
48 sub ok {
49 0 ▶ my $self = shift;
Subroutine ok: not called CC=2 SCAR=17.9
Pod undocumented
50 0 my $code = $self->code;
51 0 ▶ return $code >= 0 && $code < 400;
Condition0%0/3
$code >= 0 && $code < 400
!ll&&!rl&&r
000
MC/DC0%0/2
$code >= 0 && $code < 400
$code >= 0 $code < 400
Truth table0%0/3
A && B
A= $code >= 0B= $code < 400
ABresult
0X0
100
111
52 }
53 sub code {
54 0 ▶ my ($self) = @_;
Subroutine code: not called CC=2 SCAR=17.9
Pod undocumented
55 0 ▶ return (defined $self->{'ftp'} ? $self->{'ftp'}->code : $self->{'code'});
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'ftp'} ? : 0 0 0
56 }
57  
58 sub host {
59 0 ▶ my ($self, $host) = @_;
Subroutine host: not called CC=2 SCAR=17.9
Pod undocumented
60 0 ▶ if (@_ < 2) { return $self->{'want_host'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 0 0 0
61  
62 0 $self->{'want_host'} = $host;
63 0 return 1;
64 }
65 sub ensure_host {
66 0 ▶ my ($self) = @_;
Subroutine ensure_host: not called CC=13 SCAR=52.0
Pod undocumented
67 0 ▶ if (! defined $self->{'want_host'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"want_host"}) 0 0 0
68 0 $self->{'message'} = 'No host() machine given';
69 0 return 0;
70 }
71 0 ▶ if (defined $self->{'got_host'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"got_host"} and $self->{"got_host"} eq $self->{"want_host"}) 0 0 0
Condition0%0/3
defined $self->{"got_host"} and $self->{"got_host"} eq $self->{"want_host"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"got_host"} and $self->{"got_host"} eq $self->{"want_host"}
defined $self->{"got_host"} $self->{"got_host"} eq $self->{"want_host"}
Truth table0%0/3
A and B
A= defined $self->{"got_host"}B= $self->{"got_host"} eq $self->{"want_host"}
ABresult
0X0
100
111
72 && $self->{'got_host'} eq $self->{'want_host'}) {
73 0 return 1;
74 }
75  
76 # Net::FTP without SSL support quietly ignores SSL=>1, don't let that happen
77 0 require Net::FTP;
78 0 ▶ if ($self->{'use_SSL'} && ! Net::FTP->can('can_ssl')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_SSL"} and not "Net::FTP"->can("can_ssl")) 0 0 0
Condition0%0/3
$self->{"use_SSL"} and not "Net::FTP"->can("can_ssl")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"use_SSL"} and not "Net::FTP"->can("can_ssl")
$self->{"use_SSL"} not "Net::FTP"->can("can_ssl")
Truth table0%0/3
A and not(B)
A= $self->{"use_SSL"}B= not "Net::FTP"->can("can_ssl")
ABresult
0X0
100
111
79 0 croak "ftps use SSL but Net::FTP (version ",Net::FTP->VERSION,
80 ") does not have can_ssl()";
81 }
82  
83 0 ▶ if ($self->{'verbose'} >= 2) { print "OPEN $self->{'want_host'}\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} >= 2) 0 0 0
84  
85 ### use_SSL: $self->{'use_SSL'}
86 ### use_TLS: $self->{'use_TLS'}
87 0 ▶ if ($self->{'use_SSL'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_SSL"}) 0 0 0
88 0 require IO::Socket::SSL;
89 0 ▶ if (! $IO::Socket::SSL::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IO::Socket::SSL::DEBUG) 0 0 0
90 0 $IO::Socket::SSL::DEBUG = $self->{'verbose'};
91 }
92 }
93  
94 ### open Net FTP ...
95 0 require Net::FTP;
96 my $ftp = Net::FTP->new ($self->{'want_host'},
97 Debug => ($self->{'verbose'} >= 2),
98 0 SSL => $self->{'use_SSL'},
99 );
100 0 ▶ if (! $ftp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp) 0 0 0
101 0 $self->{'message'} = $@;
102 0 $self->{'code'} = 500;
103 0 return 0;
104 }
105  
106 0 ▶ if ($self->{'verbose'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
107 # IO::Socket::IP method
108 0 my ($host, $port) = $ftp->peerhost_service;
109 0 print "Connected $host $port\n";
110 }
111  
112 0 ▶ if ($self->{'use_TLS'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_TLS"}) 0 0 0
113 0 ▶ if ($self->{'verbose'}) { print __("TLS\n"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
114 0 $ftp->starttls;
115 }
116  
117 0 undef $self->{'got_username'};
118 0 $self->{'ftp'} = $ftp;
119 0 $self->{'got_host'} = $self->{'want_host'};
120 0 return 1;
121 }
122  
123 sub login {
124 0 ▶ my ($self, $username) = @_;
Subroutine login: not called CC=1 SCAR=6.9
Pod undocumented
125 0 $self->{'want_username'} = $username;
126 0 return 1;
127 }
128 sub ensure_login {
129 0 ▶ my ($self) = @_;
Subroutine ensure_login: not called CC=6 SCAR=37.4
Pod undocumented
130 0 ▶ if (! defined $self->{'want_username'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"want_username"}) 0 0 0
131 0 $self->{'message'} = 'No login() username given';
132 0 $self->{'code'} = 500;
133 0 return 0;
134 }
135 0 ▶ if (defined $self->{'got_username'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"got_username"} and $self->{"got_username"} eq $self->{"want_username"}) 0 0 0
Condition0%0/3
defined $self->{"got_username"} and $self->{"got_username"} eq $self->{"want_username"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"got_username"} and $self->{"got_username"} eq $self->{"want_username"}
defined $self->{"got_username"} $self->{"got_username"} eq $self->{"want_username"}
Truth table0%0/3
A and B
A= defined $self->{"got_username"}B= $self->{"got_username"} eq $self->{"want_username"}
ABresult
0X0
100
111
136 && $self->{'got_username'} eq $self->{'want_username'}) {
137 0 return 1;
138 }
139 0 ▶ if ($self->{'verbose'} >= 2) { print "LOGIN $self->{'want_username'}\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} >= 2) 0 0 0
140 0 ▶ $self->{'ftp'}->login ($self->{'want_username'})
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ftp"}->login($self->{"want_username"}) or return 0 0 0 0
141 or return 0;
142 0 undef $self->{'got_binary'};
143 0 undef $self->{'got_cwd'};
144 0 $self->{'got_username'} = $self->{'want_username'};
145 0 return 1;
146 }
147  
148 sub binary {
149 0 ▶ my ($self) = @_;
Subroutine binary: not called CC=1 SCAR=6.9
Pod undocumented
150 0 $self->{'want_binary'} = 1;
151 0 return 1;
152 }
153 sub ensure_binary {
154 0 ▶ my ($self) = @_;
Subroutine ensure_binary: not called CC=7 SCAR=40.3
Pod undocumented
155 0 ▶ if (! defined $self->{'want_binary'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"want_binary"}) 0 0 0
156 0 return 1;
157 }
158 0 ▶ if (defined $self->{'got_binary'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"got_binary"} and $self->{"got_binary"} eq $self->{"want_binary"}) 0 0 0
Condition0%0/3
defined $self->{"got_binary"} and $self->{"got_binary"} eq $self->{"want_binary"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"got_binary"} and $self->{"got_binary"} eq $self->{"want_binary"}
defined $self->{"got_binary"} $self->{"got_binary"} eq $self->{"want_binary"}
Truth table0%0/3
A and B
A= defined $self->{"got_binary"}B= $self->{"got_binary"} eq $self->{"want_binary"}
ABresult
0X0
100
111
159 && $self->{'got_binary'} eq $self->{'want_binary'}) {
160 0 return 1;
161 }
162 0 ▶ my $method = ($self->{'want_binary'} ? 'binary' : 'ascii');
Branch: 1 branch
BranchTrueFalseTotal
$self->{'want_binary'} ? : 0 0 0
163 0 ▶ if ($self->{'verbose'} >= 2) { print "\U$method\E\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} >= 2) 0 0 0
164 0 ▶ $self->{'ftp'}->$method
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ftp"}->$method or return 0 0 0 0
165 or return 0;
166 0 $self->{'got_binary'} = $self->{'want_binary'};
167 0 return 1;
168 }
169  
170 sub cwd {
171 4 ▶ my ($self, $dir) = @_;
Subroutine cwd: called CC=2 SCAR=7.1
Pod undocumented
172  
173 # default root dir same as Net::FTP
174 4 ▶ if (! defined $dir) { $dir = '/'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 0 4 4
175  
176 # relative to current want_dir
177 4 require File::Spec::Unix;
178 4 $dir = File::Spec::Unix->rel2abs ($dir, $self->{'want_dir'});
179 4 $dir = _collapse_dotdot_parent ($dir);
180  
181 4 $self->{'want_dir'} = $dir;
182 4 return 1;
183 }
184 sub ensure_cwd {
185 0 ▶ my ($self) = @_;
Subroutine ensure_cwd: not called CC=5 SCAR=34.0
Pod undocumented
186 0 ▶ if (defined $self->{'got_dir'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"got_dir"} and $self->{"got_dir"} eq $self->{"want_dir"}) 0 0 0
Condition0%0/3
defined $self->{"got_dir"} and $self->{"got_dir"} eq $self->{"want_dir"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"got_dir"} and $self->{"got_dir"} eq $self->{"want_dir"}
defined $self->{"got_dir"} $self->{"got_dir"} eq $self->{"want_dir"}
Truth table0%0/3
A and B
A= defined $self->{"got_dir"}B= $self->{"got_dir"} eq $self->{"want_dir"}
ABresult
0X0
100
111
187 && $self->{'got_dir'} eq $self->{'want_dir'}) {
188 0 return 1;
189 }
190 0 ▶ if ($self->{'verbose'} >= 2) { print "CWD $self->{'want_dir'}\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"} >= 2) 0 0 0
191  
192 0 ▶ $self->{'ftp'}->cwd ($self->{'want_dir'})
Branch: 1 branch
BranchTrueFalseTotal
$self->{"ftp"}->cwd($self->{"want_dir"}) or return 0 0 0 0
193 or return 0;
194 0 $self->{'got_dir'} = $self->{'want_dir'};
195 0 return 1;
196 }
197 # this is wrong if the removed parent is a symlink, but prevents relative
198 # cwd()s accumulating an endlessly longer $self->{'want_path'}
199 sub _collapse_dotdot_parent {
200 4 ▶ my ($path) = @_;
Subroutine _collapse_dotdot_parent: called CC=2 SCAR=6.9
201 4 ▶ while ($path =~ s{[^/]+/\.\.(/|$)}{}) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[[^/]+/\.\.(/|$)][]) 1 4 5
202 4 return File::Spec::Unix->canonpath($path);
203 }
204  
205 sub pwd {
206 0 ▶ my ($self) = @_;
Subroutine pwd: not called CC=1 SCAR=6.9
Pod undocumented
207 0 return $self->{'want_dir'};
208 }
209  
210 sub ensure_all {
211 0 ▶ my ($self) = @_;
Subroutine ensure_all: not called CC=4 SCAR=30.0
Pod undocumented
212 0 ▶ return $self->ensure_host
Condition0%0/9
$self->ensure_host && $self->ensure_login && $self->ensure_binary && $self->ensure_cwd
!ll&&!rl&&r
000
$self->ensure_host and $self->ensure_login and $self->ensure_binary
!ll&&!rl&&r
000
$self->ensure_host and $self->ensure_login
!ll&&!rl&&r
000
MC/DC0%0/4
$self->ensure_host && $self->ensure_login && $self->ensure_binary && $self->ensure_cwd
$self->ensure_host $self->ensure_login $self->ensure_binary $self->ensure_cwd
Truth table0%0/5
A and B and C && D
A= $self->ensure_hostB= $self->ensure_loginC= $self->ensure_binaryD= $self->ensure_cwd
ABCDresult
0XXX0
10XX0
110X0
11100
11111
213 && $self->ensure_login
214 && $self->ensure_binary
215 && $self->ensure_cwd;
216 }
217  
218 sub put { # ($self, $local, $remote)
219 0 ▶ my $self = shift;
Subroutine put: not called CC=2 SCAR=17.9
Pod undocumented
220 ### FTPlazy put(): @_
221 0 ▶ return $self->ensure_all && $self->{'ftp'}->put (@_);
Condition0%0/3
$self->ensure_all && $self->{'ftp'}->put(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ensure_all && $self->{'ftp'}->put(@_)
$self->ensure_all $self->{'ftp'}->put(@_)
Truth table0%0/3
A && B
A= $self->ensure_allB= $self->{'ftp'}->put(@_)
ABresult
0X0
100
111
222 }
223 sub delete {
224 0 ▶ my $self = shift; # ($self, $remote)
Subroutine delete: not called CC=2 SCAR=17.9
Pod undocumented
225 0 ▶ return $self->ensure_all && $self->{'ftp'}->delete (@_);
Condition0%0/3
$self->ensure_all && $self->{'ftp'}->delete(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ensure_all && $self->{'ftp'}->delete(@_)
$self->ensure_all $self->{'ftp'}->delete(@_)
Truth table0%0/3
A && B
A= $self->ensure_allB= $self->{'ftp'}->delete(@_)
ABresult
0X0
100
111
226 }
227 sub mkdir {
228 0 ▶ my $self = shift;
Subroutine mkdir: not called CC=2 SCAR=17.9
Pod undocumented
229 0 ▶ return $self->ensure_all && $self->{'ftp'}->mkdir (@_);
Condition0%0/3
$self->ensure_all && $self->{'ftp'}->mkdir(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ensure_all && $self->{'ftp'}->mkdir(@_)
$self->ensure_all $self->{'ftp'}->mkdir(@_)
Truth table0%0/3
A && B
A= $self->ensure_allB= $self->{'ftp'}->mkdir(@_)
ABresult
0X0
100
111
230 }
231 sub rmdir { # ($self, $remote)
232 0 ▶ my $self = shift;
Subroutine rmdir: not called CC=2 SCAR=17.9
Pod undocumented
233 0 ▶ return $self->ensure_all && $self->{'ftp'}->rmdir (@_);
Condition0%0/3
$self->ensure_all && $self->{'ftp'}->rmdir(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ensure_all && $self->{'ftp'}->rmdir(@_)
$self->ensure_all $self->{'ftp'}->rmdir(@_)
Truth table0%0/3
A && B
A= $self->ensure_allB= $self->{'ftp'}->rmdir(@_)
ABresult
0X0
100
111
234 }
235 sub rename { # ($self, $remote_oldname, $remote_newname)
236 0 ▶ my $self = shift;
Subroutine rename: not called CC=2 SCAR=17.9
Pod undocumented
237 0 ▶ return $self->ensure_all && $self->{'ftp'}->rename (@_);
Condition0%0/3
$self->ensure_all && $self->{'ftp'}->rename(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ensure_all && $self->{'ftp'}->rename(@_)
$self->ensure_all $self->{'ftp'}->rename(@_)
Truth table0%0/3
A && B
A= $self->ensure_allB= $self->{'ftp'}->rename(@_)
ABresult
0X0
100
111
238 }
239 sub site {
240 0 ▶ my $self = shift;
Subroutine site: not called CC=2 SCAR=17.9
Pod undocumented
241 0 ▶ return $self->ensure_all && $self->{'ftp'}->site (@_);
Condition0%0/3
$self->ensure_all && $self->{'ftp'}->site(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ensure_all && $self->{'ftp'}->site(@_)
$self->ensure_all $self->{'ftp'}->site(@_)
Truth table0%0/3
A && B
A= $self->ensure_allB= $self->{'ftp'}->site(@_)
ABresult
0X0
100
111
242 }
243 sub quot {
244 0 ▶ my $self = shift;
Subroutine quot: not called CC=2 SCAR=17.9
Pod undocumented
245 0 ▶ return $self->ensure_all && $self->{'ftp'}->quot (@_);
Condition0%0/3
$self->ensure_all && $self->{'ftp'}->quot(@_)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->ensure_all && $self->{'ftp'}->quot(@_)
$self->ensure_all $self->{'ftp'}->quot(@_)
Truth table0%0/3
A && B
A= $self->ensure_allB= $self->{'ftp'}->quot(@_)
ABresult
0X0
100
111
246 }
247  
248 sub all_ok {
249 0 ▶ my ($self) = @_;
Subroutine all_ok: not called CC=2 SCAR=17.9
Pod undocumented
250 0 ▶ return (! $self->{'ftp'} || $self->{'ftp'}->pwd);
Condition0%0/3
!$self->{'ftp'} || $self->{'ftp'}->pwd
l!l&&r!l&&!r
000
MC/DC0%0/2
!$self->{'ftp'} || $self->{'ftp'}->pwd
!$self->{'ftp'} $self->{'ftp'}->pwd
Truth table0%0/3
not(A) || B
A= !$self->{'ftp'}B= $self->{'ftp'}->pwd
ABresult
1X1
011
000
251 }
252  
253 sub mlsd {
254 0 ▶ my $self = shift;
Subroutine mlsd: not called CC=2 SCAR=17.9
Pod undocumented
255 0 ▶ $self->ensure_all || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->ensure_all or return 0 0 0
256 0 return $self->{'ftp'}->_list_cmd("MLSD", @_);
257 }
258  
259 # sub mlsd {
260 # my ($self, $remote_dirname, $local_filename) = @_;
261 # $self->ensure_all;
262 # # ### MLSD: $remote_dirname
263 # # my $data = $self->{'ftp'}->_data_cmd("MLSD $remote_dirname")
264 # # or return undef;
265 # #
266 # # require File::Copy;
267 # # File::Copy::copy($data, $local_filename);
268 # # unless ($data->close) {
269 # # croak "Error closing data stream";
270 # # }
271 # #
272 # # ### MLSD message: $self->message
273 # # return undef;
274 # }
275  
276 1 1;
277 __END__
« Upfiles.pm ↑ Summary Upfiles/SFTPlazy.pm »