Upfiles/SFTPlazy.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 34 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 238 SCAR 58.3
CC
18
Cov
0%
ensure_host_and_login
132.0
ensure_cwd
12.0
message
6.0
CRAP 342.0
1 # Copyright 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::SFTPlazy;
19 use 5.010;
20 use strict;
21 use warnings;
22 use Carp;
23 use Net::SFTP::Foreign;
24  
25 # uncomment this to run the ### lines
26 # use Smart::Comments;
27  
28 our $VERSION = 15;
29  
30 # Have croaks from Net::SFTP reported against the caller (App::Upfiles)
31 # rather than here.
32 # our @CARP_NOT = ('Net::SFTP::Foreign');
33  
34 sub new {
35 my $class = shift;
36 return bless { verbose => 0,
37 want_dir => '/',
38 message => '',
39 code => 0,
40 @_ }, $class;
41 }
42  
43 sub message {
44 my ($self) = @_;
45 return (defined $self->{'sftp'} ? $self->{'sftp'}->error : $self->{'message'});
46 }
47 sub ok {
48 my $self = shift;
49 my $code = $self->code;
50 return $code >= 0 && $code < 400;
51 }
52 sub code {
53 my ($self) = @_;
54 return (defined $self->{'sftp'} ? $self->{'sftp'}->code : $self->{'code'});
55 }
56  
57 sub host {
58 my ($self, $host) = @_;
59 if (@_ < 2) { return $self->{'want_host'}; }
60  
61 $self->{'want_host'} = $host;
62 return 1;
63 }
64 sub login {
65 my ($self, $username) = @_;
66 ### SFTPlazy login(): $username
67 $self->{'want_username'} = $username;
68 return 1;
69 }
70 sub ensure_host_and_login {
71 my ($self) = @_;
72 if (! defined $self->{'want_host'}) {
73 $self->{'message'} = 'No host() machine given';
74 return 0;
75 }
76 if (defined $self->{'got_host'}
77 && $self->{'got_host'} eq $self->{'want_host'}) {
78 return 1;
79 }
80  
81 ### SFTPlazy ensure_host_and_login() change ...
82 ### got: $self->{'got_host'}
83 ### want: $self->{'want_host'}
84 my $username = $self->{'want_username'};
85 my $password;
86  
87 if (! defined $username) {
88 ### try netrc for username of host: $self->{'want_host'}
89 require Net::Netrc;
90 if (my $netrc = Net::Netrc->lookup($self->{'want_host'})) {
91 ### $netrc
92 $username = $netrc->login;
93 $password = $netrc->password;
94 }
95 }
96 if (! defined $self->{'want_username'}) {
97 $self->{'message'} = 'No login() username given';
98 $self->{'code'} = 500;
99 return 0;
100 }
101  
102 if (! defined $password) {
103 ### try netrc for password of username: $username
104 require Net::Netrc;
105 if (my $netrc = Net::Netrc->lookup($self->{'want_host'}, $username)) {
106 ### $netrc
107 $username = $netrc->login;
108 $password = $netrc->password;
109 }
110 }
111  
112 ### $username
113 ### $password
114 ### host: $self->{'want_host'}
115 if ($self->{'verbose'} >= 2) { print "SFTP $self->{'want_host'}\n"; }
116 my $sftp = Net::SFTP::Foreign->new
117 ($self->{'want_host'},
118 user => $self->{'want_username'},
119 more => ($self->{'verbose'} >= 2
120 ? [ '-' . ('v' x ($self->{'verbose'}-1)) ] # -v verbosity
121 : []),
122 password => $password,
123 );
124 if (my $err = $sftp->error) {
125 ### SFTP error: $err
126 $self->{'message'} = $err;
127 $self->{'code'} = 500;
128 return 0;
129 }
130  
131 $self->{'sftp'} = $sftp;
132 $self->{'got_host'} = $self->{'want_host'};
133 undef $self->{'got_cwd'};
134 return 1;
135 }
136  
137 sub binary {
138 my ($self) = @_;
139 return 1; # binary is the default in SFTP
140 }
141  
142 sub cwd {
143 my ($self, $dir) = @_;
144  
145 # default root dir, like Net::FTP
146 if (! defined $dir) { $dir = '/'; }
147  
148 # relative to current want_dir
149 require File::Spec::Unix;
150 $dir = File::Spec::Unix->rel2abs ($dir, $self->{'want_dir'});
151 $dir = _collapse_dotdot_parent ($dir);
152  
153 $self->{'want_dir'} = $dir;
154 return 1;
155 }
156 sub ensure_cwd {
157 my ($self) = @_;
158 if (defined $self->{'got_dir'}
159 && $self->{'got_dir'} eq $self->{'want_dir'}) {
160 return 1;
161 }
162 if ($self->{'verbose'} >= 2) { print "CWD $self->{'want_dir'}\n"; }
163  
164 ### SFTPlazy setcwd(): $self->{'want_dir'}
165 $self->{'sftp'}->setcwd ($self->{'want_dir'})
166 or return 0;
167 $self->{'got_dir'} = $self->{'want_dir'};
168 ### SFTPlazy setcwd() ok ...
169 return 1;
170 }
171 # this is wrong if the removed parent is a symlink, but prevents relative
172 # cwd()s accumulating an endlessly longer $self->{'want_path'}
173 sub _collapse_dotdot_parent {
174 my ($path) = @_;
175 while ($path =~ s{[^/]+/\.\.(/|$)}{}) {}
176 return File::Spec::Unix->canonpath($path);
177 }
178  
179 sub pwd {
180 my ($self) = @_;
181 return $self->{'want_dir'};
182 }
183  
184 sub ensure_all {
185 my ($self) = @_;
186 return $self->ensure_host_and_login
187 && $self->ensure_cwd;
188 }
189  
190 sub put {
191 my ($self, $local, $remote) = @_;
192 ### SFTPlazy put(): @_
193 return $self->ensure_all
194 && $self->{'sftp'}->put ($local, $remote,
195 atomic => 0,
196 copy_time => $self->{'copy_time'});
197 }
198 sub delete {
199 my ($self, $remote) = @_;
200 return $self->ensure_all
201 && $self->{'sftp'}->remove ($remote);
202 }
203 sub mkdir {
204 my ($self, $remote, $parents) = @_;
205 my $method = $parents ? 'mkpath' : 'mkdir';
206 return $self->ensure_all
207 && $self->{'sftp'}->$method ($remote);
208 }
209 sub rmdir {
210 my ($self, $remote) = @_;
211 return $self->ensure_all && $self->{'sftp'}->rmdir ($remote);
212 }
213 sub rename {
214 my ($self, $oldname, $newname) = @_;
215 return $self->ensure_all
216 && $self->{'sftp'}->rename ($oldname, $newname, overwrite=>1);
217 }
218 sub site {
219 }
220 sub quot {
221 }
222  
223 sub all_ok {
224 my ($self) = @_;
225 return (! $self->{'sftp'} # either sftp never used
226 || ! $self->{'sftp'}->error); # or no error from it
227 }
228  
229 sub mlsd {
230 my $self = shift;
231 $self->ensure_all || return;
232 return $self->{'sftp'}->_list_cmd("MLSD", @_);
233 }
234  
235 # sub mlsd {
236 # my ($self, $remote_dirname, $local_filename) = @_;
237 # $self->ensure_all;
238 # # ### MLSD: $remote_dirname
239 # # my $data = $self->{'sftp'}->_data_cmd("MLSD $remote_dirname")
240 # # or return undef;
241 # #
242 # # require File::Copy;
243 # # File::Copy::copy($data, $local_filename);
244 # # unless ($data->close) {
245 # # croak "Error closing data stream";
246 # # }
247 # #
248 # # ### MLSD message: $self->message
249 # # return undef;
250 # }
251  
252 1;
253 __END__