xFTP_FTPSSL.pm untested

filter: Statementstmt 0.0% 0 / 247 Branchbran 0.0% 0 / 140 Conditioncond 0.0% 0 / 60 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 529 CC 71 SCAR 41.0
CC
71
Cov
0%
mkdir
132.0
new_ftpssl
110.0
copy
90.0
CRAP 60.3
1 package Net::xFTP::FTPSSL;
2
3 sub new_ftpssl
4 {
5 my $subclass = shift;
6 my $pkg = shift;
7 my $host = shift;
8 my %args = @_;
9 my %xftp_args;
10 my $xftp = bless { }, $subclass;
11 $xftp->{BlockSize} = 10240;
12 if (defined $args{BlockSize})
13 {
14 $xftp->{BlockSize} = $args{BlockSize} || 10240;
15 }
16 $xftp->{xftp} = Net::FTPSSL->new($host, %args);
17 unless (defined $xftp->{xftp})
18 {
19 $xftp->{xftp_lastmsg} = $@;
20 return undef;
21 }
22 if (defined $args{user})
23 {
24 $args{user} ||= 'anonymous';
25 $args{password} ||= 'anonymous@' if ($args{user} eq 'anonymous');
26 my @loginargs = ($args{user});
27 push (@loginargs, $args{password}) if (defined $args{password});
28 push (@loginargs, $args{account}) if (defined $args{account});
29 if ($xftp->{xftp}->login(@loginargs))
30 {
31 my $cwd = $xftp->{xftp}->pwd();
32 $xftp->{cwd} = $cwd if ($cwd);
33 $xftp->{protocol} = 'Net::FTPSSL';
34 return $xftp;
35 }
36 }
37 else
38 {
39 return $xftp if ($xftp->{xftp}->login());
40 }
41 $@ ||= 'Invalid Password?';
42 return undef;
43 }
44
45 sub protocol
46 {
47 return shift->{protocol};
48 }
49
50 {
51 no warnings 'redefine';
52 sub cwd #SET THE "CURRENT" DIRECTORY.
53 {
54 my $self = shift;
55 my $cwd = shift || '/';
56
57 my $ok;
58 $ok = $self->{xftp}->cwd($cwd);
59 $self->{cwd} = $cwd if ($ok);
60 return $ok ? 1 : undef;
61 }
62
63 sub copy
64 {
65 my $self = shift;
66
67 return undef unless (@_ >= 2);
68 my @args = @_;
69 if ($self->isadir($args[1]))
70 {
71 my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o);
72 $args[1] .= '/' unless ($args[1] =~ m#\/$#o);
73 $args[1] .= $filename;
74 }
75
76 my $ok;
77 my ($tmp, $t);
78 my $fromHandle;
79 eval { $fromHandle = $self->{xftp}->retr($args[0]) };
80 unless ($fromHandle)
81 {
82 $self->{xftp_lastmsg} = "Copy failed (". ($@||'retr failed - Unknown reason')
83 . ')!';
84 return undef;
85 }
86 while ($fromHandle->read($tmp, $self->{BlockSize}))
87 {
88 $t .= $tmp;
89 }
90 $fromHandle->close();
91 my $toHandle;
92 eval { $toHandle = $self->{xftp}->stor($args[1]) };
93 unless ($toHandle)
94 {
95 $self->{xftp_lastmsg} = "Copy failed (". ($@||'stor failed - Unknown reason')
96 . ')!';
97 return undef;
98 }
99 eval { $toHandle->write($t, length($t)) };
100 if ($@)
101 {
102 $self->{xftp_lastmsg} = "Copy failed (". ($@||'write failed - Unknown reason')
103 . ')!';
104 return undef;
105 }
106 $toHandle->close();
107 $ok = 1;
108 return $ok ? 1 : undef;
109 }
110
111 sub move
112 {
113 my $self = shift;
114
115 return undef unless (@_ >= 2);
116 return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef;
117 }
118 }
119
120 sub ascii
121 {
122 my $self = shift;
123
124 $self->{xftp}->ascii();
125 return undef;
126 }
127
128 sub binary
129 {
130 my $self = shift;
131
132 $self->{xftp}->binary();
133 return undef;
134 }
135
136 sub quit
137 {
138 my $self = shift;
139 $self->{xftp}->quit();
140 return;
141 }
142
143 sub ls
144 {
145 my $self = shift;
146 my $path = shift || '';
147 my $showall = shift || 0;
148 my @dirlist;
149 @dirlist = $self->{xftp}->nlst($path||'.');
150 return unless (defined $dirlist[0]); #ADDED 20070613 TO PREVENT WARNING.
151 shift (@dirlist) if ($dirlist[0] =~ /^total \d/o); #REMOVE TOTAL LINE!
152 my $i = 0;
153 while ($i<=$#dirlist)
154 {
155 #$dirlist[$i] =~ s#\/\/#\/#;
156 $dirlist[$i] = $1 if ($dirlist[$i] =~ m#([^\/\\]+)$#o);
157 $dirlist[$i] = $1 if ($dirlist[$i] =~ /\/(\.\.?)$/o);
158 if ($dirlist[$i] eq '..' && $path eq '/')
159 {
160 splice(@dirlist, $i, 1);
161 }
162 elsif (!$showall && $dirlist[$i] =~ /^\.[^\.]/o)
163 {
164 splice(@dirlist, $i, 1);
165 }
166 else
167 {
168 ++$i;
169 }
170 }
171 @dirlist = sort @dirlist;
172
173 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
174 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..');
175 #unshift (@dirlist, '.') unless ($dirlist[0] eq '.');
176
177 return wantarray ? @dirlist : \@dirlist;
178 }
179
180 sub dir
181 {
182 my $self = shift;
183 my $path = shift || '';
184 my $showall = shift || 0;
185 my @dirlist;
186 @dirlist = $self->{xftp}->list($path||'.');
187 return unless (defined $dirlist[0]); #ADDED 20070613 TO PREVENT WARNING.
188 shift (@dirlist) if ($dirlist[0] =~ /^total \d/o); #REMOVE TOTAL LINE!
189 my $i = 0;
190 while ($i<=$#dirlist)
191 {
192 #$dirlist[$i] =~ s#\/\/#\/#;
193 $dirlist[$i] = $1 if ($dirlist[$i] =~ m#([^\/\\]+)$#o);
194 $dirlist[$i] = $1 if ($dirlist[$i] =~ /\/(\.\.?)$/o);
195 if ($dirlist[$i] =~ /\d \.\.$/o && $path eq '/')
196 {
197 splice(@dirlist, $i, 1);
198 }
199 elsif (!$showall && $dirlist[$i] =~ /\d \.[^\.]\S*$/o)
200 {
201 splice(@dirlist, $i, 1);
202 }
203 else
204 {
205 ++$i;
206 }
207 }
208
209 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
210 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/);
211 #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/);
212
213 return wantarray ? @dirlist : \@dirlist;
214 }
215
216 sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY.
217 {
218 my $self = shift;
219
220 # my $cwd = $self->{xftp}->pwd();
221 # $self->{cwd} = $cwd if ($cwd);
222
223 return $self->{cwd};
224 }
225
226 sub get #(Remote, => Local)
227 {
228 my $self = shift;
229
230 return undef unless (@_ >= 1);
231 my @args = @_;
232 unless (@args >= 2)
233 {
234 if (ref(\$args[0]) =~ /GLOB/io)
235 {
236 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
237 return undef;
238 }
239 $args[1] = $args[0];
240 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
241 }
242 my $ok;
243 eval { $ok = $self->{xftp}->get(@args) };
244 $self->{xftp_lastmsg} = $@ if ($@);
245 return $ok ? 1 : undef;
246 }
247
248 sub put #(LOCAL => REMOTE) SFTP returns OK=1 on SUCCESS.
249 {
250 my $self = shift;
251
252 return undef unless (@_ >= 1);
253 my @args = @_;
254 unless (@args >= 2)
255 {
256 if (ref(\$args[0]) =~ /GLOB/io)
257 {
258 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
259 return undef;
260 }
261 $args[1] = $args[0];
262 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
263 }
264
265 my $ok;
266 eval { $ok = $self->{xftp}->put(@args) };
267 #print STDERR "-ftp.put(".join('|',@args)."= ok=$ok= at=$at=\n";
268 $self->{xftp_lastmsg} = $@ if ($@);
269 return $ok ? 1 : undef;
270 }
271
272 sub delete #RETURNED OK=2 WHEN LAST FAILED.
273 {
274 my $self = shift;
275 my $path = shift;
276
277 my $ok;
278 eval { $ok = $self->{xftp}->delete($path) };
279 $self->{xftp_lastmsg} = $@ if ($@);
280 return $ok ? 1 : undef;
281 }
282
283 sub rename
284 {
285 my $self = shift;
286 return undef unless (@_ == 2);
287
288 my ($oldfile, $newfile) = @_;
289
290 my $ok;
291 eval { $ok = $self->{xftp}->rename($oldfile, $newfile) };
292 $self->{xftp_lastmsg} = $@ if ($@);
293 return $ok ? 1 : undef;
294 }
295
296 sub mkdir
297 {
298 my $self = shift;
299 my $path = shift;
300 my $tryRecursion = shift||0;
301 $path =~ s#[\/\\]$##o unless ($path eq '/');
302
303 my @pathStack;
304 my $ok = '';
305 my $orgPath = $path;
306 my $didRecursion = 0;
307 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
308 while ($path)
309 {
310 $path =~ s#[^\/\\]+$##o;
311 $path =~ s#[\/\\]$##o;
312 $path = '/' unless ($path);
313 last if ($self->isadir($path));
314 if ($tryRecursion)
315 {
316 push (@pathStack, $path);
317 $didRecursion = 1;
318 last if ($path eq '/');
319 }
320 else
321 {
322 $self->{xftp_lastmsg} = "mkdir:Could not create path($orgPath) since parent not directory!";
323 return undef;
324 }
325 }
326 if ($didRecursion)
327 {
328 while (@pathStack)
329 {
330 $path = pop @pathStack;
331 eval { $ok = $self->{xftp}->mkdir($path) };
332 if ($@ || !$ok)
333 {
334 $self->{xftp_lastmsg} = $@ || $!;
335 return undef;
336 }
337 next;
338 }
339 }
340 eval { $ok = $self->{xftp}->mkdir($orgPath) };
341 if ($@ || !$ok)
342 {
343 $self->{xftp_lastmsg} = $@ || $!;
344 return undef;
345 }
346 return $ok ? 1 : undef;
347 }
348
349 sub rmdir
350 {
351 my $self = shift;
352 my $path = shift;
353 $path =~ s#[\/\\]$##o unless ($path eq '/');
354
355 my $ok;
356 eval { $ok = $self->{xftp}->rmdir($path) };
357 $self->{xftp_lastmsg} = $@ if ($@);
358 return $ok ? 1 : undef;
359 }
360
361 sub message
362 {
363 my $self = shift;
364
365 chomp $self->{xftp_lastmsg};
366 return $self->{xftp}->message;
367 }
368
369 sub mdtm
370 {
371 my $self = shift;
372 my $path = shift;
373
374 my $ok;
375 return $self->{xftp}->mdtm($path);
376 }
377
378 sub size
379 {
380 my $self = shift;
381 my $path = shift;
382
383 my $ok;
384 return $self->{xftp}->size($path);
385 }
386
387 sub isadir
388 {
389 my $self = shift;
390 my $path = shift;
391
392 my $ok;
393 my $curdir = $self->{xftp}->pwd();
394 eval { $ok = $self->{xftp}->cwd($path); };
395 if ($ok)
396 {
397 $self->{xftp}->cwd($curdir);
398 return 1;
399 }
400 return 0;
401 }
402
403 sub chmod
404 {
405 my $self = shift;
406 my $permissions = shift;
407 my $path = shift;
408
409 my ($ok, $attrs);
410 unless ($self->{xftp}->supported('SITE CHMOD'))
411 {
412 $@ = 'Server does not support chmod!';
413 $self->{xftp_lastmsg} = $@;
414 }
415 $ok = $self->{xftp}->site('CHMOD', $permissions, $path);
416 return ($ok == 2) ? 1 : undef;
417 }
418
419 sub method
420 {
421 my $self = shift;
422 my $method = shift;
423
424 for (my $i=0;$i<scalar(@_);$i++)
425 {
426 $_[$i] = "'" . $_[$i] . "'" unless ($_[$i] =~ /^['"]/o || $_[$i] =~ /^[\d\.\+\-]+$/o);
427 }
428 my $res;
429 my $xeq = " \$res = \$self->{xftp}->$method(".join(',', @_).")";
430 eval $xeq;
431 if ($@)
432 {
433 $self->{xftp_lastmsg} = "method Net::FTPSSL::$method(".join(',',@_).") returned $@";
434 return undef;
435 }
436 return $res;
437 }
438
439 1