xFTP_SFTP.pm untested

filter: Statementstmt 0.0% 0 / 381 Branchbran 0.0% 0 / 200 Conditioncond 0.0% 0 / 89 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 719 CC 101 SCAR 45.2
CC
101
Cov
0%
put
210.0
get
182.0
copy
156.0
CRAP 91.8
1 package Net::xFTP::SFTP;
2
3 sub new_sftp
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 delete($args{BlockSize});
16 }
17 my $saveEnvHome = $ENV{HOME};
18 $ENV{HOME} = $xftp_args{home} if ($xftp_args{home});
19 eval { $xftp->{xftp} = Net::SFTP->new($host, %args, warn => \&sftpWarnings); };
20 $xftp->{xftp_lastmsg} = $@ if ($@);
21 $ENV{HOME} = $saveEnvHome || '';
22 if ($xftp->{xftp})
23 {
24 my $cwd;
25 eval { $cwd = $xftp->{xftp}->do_realpath('.') };
26 $xftp->{cwd} = $cwd if ($cwd);
27 return $xftp;
28 }
29 return undef;
30 }
31
32 {
33 no warnings 'redefine';
34 sub cwd #SET THE "CURRENT" DIRECTORY.
35 {
36 my $self = shift;
37 my $cwd = shift || '/';
38
39 my $ok;
40 my $fullwd;
41 #NEXT 22 ADDED 20060815 TO FIX RELATIVE PATH CHANGES.
42 if ($cwd !~ m#^\/# && $self->{cwd} && $self->{cwd} !~ /^\./o)
43 {
44 if ($self->{cwd} =~ m#\/$#o)
45 {
46 $cwd = $self->{cwd} . $cwd;
47 }
48 else
49 {
50 $cwd = $self->{cwd} . '/' . $cwd;
51 }
52 }
53 elsif ($cwd eq '..')
54 {
55 $cwd = $self->{xftp}->do_realpath($self->{cwd});
56 chop($cwd) if ($cwd =~ m#\/$#o);
57 $cwd =~ s#\/[^\/]+$##o;
58 $cwd ||= '/';
59 }
60 elsif ($cwd eq '.')
61 {
62 $cwd = $self->{xftp}->do_realpath($self->{cwd});
63 }
64 eval { $fullwd = $self->{xftp}->do_realpath($cwd) };
65 if ($fullwd)
66 {
67 $self->{cwd} = $fullwd;
68 $ok = 1;
69 }
70 return $ok ? 1 : undef;
71 }
72
73 sub copy
74 {
75 my $self = shift;
76
77 return undef unless (@_ >= 2);
78 my @args = @_;
79 for (my $i=0;$i<=1;$i++)
80 {
81 $args[$i] = $self->{cwd} . '/' . $args[$i] unless ($args[$i] =~ m#^(?:[a-zA-Z]\:|\/)#o);
82 }
83 if ($self->isadir($args[1]))
84 {
85 my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o);
86 $args[1] .= '/' unless ($args[1] =~ m#\/$#o);
87 $args[1] .= $filename;
88 }
89
90 my $ok;
91 unless (defined($self->{haveSFTPConstants}) && $self->{haveSFTPConstants})
92 {
93 $self->{xftp_lastmsg} =
94 "Copy failed (You must install the Net::SFTP::Constants Perl module!)";
95 return undef;
96 }
97 my ($tmp, $t);
98 my $fromHandle;
99 eval { $fromHandle = $self->{xftp}->do_open($args[0], 0) };
100 unless (defined($fromHandle) && $fromHandle)
101 {
102 $self->{xftp_lastmsg} = "Copy failed (". ($@||'do_open1 failed - Unknown reason')
103 . ')!';
104 return undef;
105 }
106 my $offset = 0;
107 my $err;
108 while (1)
109 {
110 ($tmp, $err) = $self->{xftp}->do_read($fromHandle, $offset, $self->{BlockSize});
111 last if (defined $err);
112 $t .= $tmp;
113 $offset += $self->{BlockSize};
114 }
115 $self->{xftp}->do_close($fromHandle);
116 my $toHandle;
117 eval { no strict 'subs'; $toHandle = $self->{xftp}->do_open($args[1],
118 SSH2_FXF_WRITE | SSH2_FXF_CREAT | SSH2_FXF_TRUNC) };
119 unless (defined($toHandle) && $toHandle)
120 {
121 $self->{xftp_lastmsg} = "Copy failed (". ($@||'do_open2 failed - Unknown reason')
122 . ')!';
123 return undef;
124 }
125 eval { $self->{xftp}->do_write($toHandle, 0, $t) };
126 if ($@)
127 {
128 $self->{xftp_lastmsg} = "Copy failed (". ($@||'write failed - Unknown reason')
129 . ')!';
130 return undef;
131 }
132 $self->{xftp}->do_close($toHandle);
133 $ok = 1;
134 return $ok ? 1 : undef;
135 }
136
137 sub move
138 {
139 my $self = shift;
140
141 return undef unless (@_ >= 2);
142 return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef;
143 }
144 }
145
146 sub ascii
147 {
148 my $self = shift;
149
150 return undef;
151 }
152
153 sub binary
154 {
155 my $self = shift;
156
157 return undef;
158 }
159
160 sub quit
161 {
162 my $self = shift;
163 $self->{xftp} = undef;
164 delete($self->{xftp});
165
166 return;
167 }
168
169 sub ls
170 {
171 my $self = shift;
172 my $path = shift || '';
173 my $showall = shift || 0;
174 my @dirlist;
175 my $realpath;
176 eval { $realpath = $self->{xftp}->do_realpath($path||$self->{cwd}||'.') };
177 chomp $realpath;
178 $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o);
179 my @dirHash;
180 eval { @dirHash = $self->{xftp}->ls($realpath) };
181 if ($@)
182 {
183 $self->{xftp_lastmsg} = $@;
184 my $err = $self->{xftp}->status;
185 return if ($err);
186 }
187 return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY.
188 shift (@dirHash) if ($dirHash[0]->{longname} =~ /^total \d/o); #REMOVE TOTAL LINE!
189 my $t;
190 @dirlist = ();
191 for (my $i=0;$i<=$#dirHash;$i++)
192 {
193 $t = $dirHash[$i]->{filename};
194 next if ($t eq '..' && $path eq '/');
195 next if (!$showall && $t =~ /^\.[^\.]/o);
196 push (@dirlist, $t);
197 }
198 @dirlist = sort @dirlist;
199
200 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
201 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..');
202 #unshift (@dirlist, '.') unless ($dirlist[0] eq '.');
203
204 return wantarray ? @dirlist : \@dirlist;
205 }
206
207 sub dir
208 {
209 my $self = shift;
210 my $path = shift || '';
211 my $showall = shift || 0;
212 my @dirlist;
213 my $realpath;
214 eval { $realpath = $self->{xftp}->do_realpath($path||$self->{cwd}||'.') };
215 chomp $realpath;
216 $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o);
217 my @dirHash;
218 eval { @dirHash = $self->{xftp}->ls($realpath) };
219 #return if ($@);
220 if ($@)
221 {
222 $self->{xftp_lastmsg} = $@;
223 my $err = $self->{xftp}->status;
224 return if ($err);
225 }
226 return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY.
227 shift (@dirHash) if ($dirHash[0]->{longname} =~ /^total \d/o); #REMOVE TOTAL LINE!
228 my $t;
229 @dirlist = ();
230 #for (my $i=0;$i<=$#dirHash;$i++)
231 foreach my $i (sort { $a->{filename} cmp $b->{filename} } @dirHash)
232 {
233 #$t = $dirHash[$i]->{longname};
234 $t = $i->{longname};
235 next if ($t =~ /\d \.\.$/o && $path eq '/');
236 next if (!$showall && $t =~ /\d \.[^\.]\S*$/o);
237 push (@dirlist, $t);
238 }
239
240 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
241 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/);
242 #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/);
243
244 return wantarray ? @dirlist : \@dirlist;
245 }
246
247 sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY.
248 {
249 my $self = shift;
250
251 return $self->{cwd};
252 }
253
254 sub get #(Remote, => Local)
255 {
256 my $self = shift;
257
258 return undef unless (@_ >= 1);
259 my @args = @_;
260 $args[0] = $self->{cwd} . '/' . $args[0] unless ($args[0] =~ m#^(?:[a-zA-Z]\:|\/)#o);
261 if (@args >= 2)
262 {
263 $args[1] = \$_[1] if (ref(\$args[1]) =~ /GLOB/io);
264 }
265 else
266 {
267 if (ref(\$args[0]) eq 'GLOB')
268 {
269 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
270 return undef;
271 }
272 $args[1] = $args[0];
273 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
274 }
275 my $ok;
276 if (ref(\$_[1]) eq 'GLOB')
277 {
278 my $remoteHandle;
279 my $offset = 0;
280 my $buff;
281 my $unsubscriptedFH = $_[1];
282 eval { $remoteHandle = $self->{xftp}->do_open($args[0], 0) };
283 if ($remoteHandle)
284 {
285 my $err;
286 while (1)
287 {
288 ($buff, $err) = $self->{xftp}->do_read($remoteHandle, $offset, $self->{BlockSize});
289 last if (defined $err);
290 print $unsubscriptedFH $buff;
291 $offset += $self->{BlockSize};
292 }
293 $self->{xftp}->do_close($remoteHandle);
294 return 1;
295 }
296 else
297 {
298 $self->{xftp_lastmsg} = $@ || 'Could not open remote handle for unknown reasons!';
299 return undef;
300 }
301 }
302 else
303 {
304 eval { $self->{xftp}->get(@args) };
305 if ($@)
306 {
307 $self->{xftp_lastmsg} = $@;
308 $ok = $self->{xftp}->status;
309 return $ok ? undef : 1;
310 }
311 else
312 {
313 return 1;
314 }
315 }
316 return $ok ? 1 : undef;
317 }
318
319 sub put #(LOCAL => REMOTE) SFTP returns OK=1 on SUCCESS.
320 {
321 my $self = shift;
322
323 return undef unless (@_ >= 1);
324 my @args = @_;
325 if (@args >= 2)
326 {
327 $args[0] = \$_[0] if (ref(\$args[0]) =~ /GLOB/io);
328 }
329 else
330 {
331 if (ref(\$args[0]) =~ /GLOB/io)
332 {
333 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
334 return undef;
335 }
336 $args[1] = $args[0];
337 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
338 }
339 $args[1] = $self->{cwd} . '/' . $args[1] unless ($args[1] =~ m#^(?:[a-zA-Z]\:|\/)#o);
340
341 my $ok;
342 if (ref(\$_[0]) eq 'GLOB')
343 {
344 unless (defined($self->{haveSFTPConstants}) && $self->{haveSFTPConstants})
345 {
346 $self->{xftp_lastmsg} =
347 "Copy failed (You must install the Net::SFTP::Constants Perl module!)";
348 return undef;
349 }
350 my $remoteHandle;
351 my $offset = 0;
352 my $buff;
353 my $unsubscriptedFH = $_[0];
354 eval { no strict 'subs'; $remoteHandle = $self->{xftp}->do_open($args[1],
355 SSH2_FXF_WRITE | SSH2_FXF_CREAT | SSH2_FXF_TRUNC) };
356 if ($remoteHandle)
357 {
358 my $t;
359 while ($buff = <$unsubscriptedFH>)
360 {
361 $t .= $buff;
362 }
363 eval { $self->{xftp}->do_write($remoteHandle, 0, $t) };
364 if ($@)
365 {
366 $self->{xftp_lastmsg} = $@;
367 $ok = $self->{xftp}->status;
368 return $ok ? undef : 1;
369 }
370 else
371 {
372 return 1;
373 }
374 $self->{xftp}->do_close($remoteHandle);
375 return 1;
376 }
377 else
378 {
379 $self->{xftp_lastmsg} = $@ || 'Could not open remote handle for unknown reasons!';
380 return undef;
381 }
382 }
383 else
384 {
385 eval { $ok = $self->{xftp}->put(@args) };
386 $self->{xftp_lastmsg} = $@ if ($@);
387 }
388 return $ok ? 1 : undef;
389 }
390
391 sub delete #RETURNED OK=2 WHEN LAST FAILED.
392 {
393 my $self = shift;
394 my $path = shift;
395
396 my $ok;
397 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
398 eval { $ok = $self->{xftp}->do_remove($path) };
399 $self->{xftp_lastmsg} = $@ if ($@);
400 return $@ ? undef : 1;
401 }
402
403 sub rename
404 {
405 my $self = shift;
406 return undef unless (@_ == 2);
407
408 my ($oldfile, $newfile) = @_;
409
410 my $ok;
411 $oldfile = $self->{cwd} . '/' . $oldfile unless ($oldfile =~ m#^(?:[a-zA-Z]\:|\/)#o);
412 $newfile = $self->{cwd} . '/' . $newfile unless ($newfile =~ m#^(?:[a-zA-Z]\:|\/)#o);
413 eval { $ok = $self->{xftp}->do_rename($oldfile, $newfile) };
414 $self->{xftp_lastmsg} = $@ if ($@);
415 return $@ ? undef : 1;
416 }
417
418 sub mkdir
419 {
420 my $self = shift;
421 my $path = shift;
422 my $tryRecursion = shift||0;
423 $path =~ s#[\/\\]$##o unless ($path eq '/');
424
425 my @pathStack;
426 my $ok = '';
427 my $orgPath = $path;
428 my $didRecursion = 0;
429 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
430 while ($path)
431 {
432 $path =~ s#[^\/\\]+$##o;
433 $path =~ s#[\/\\]$##o;
434 $path = '/' unless ($path);
435 last if ($self->isadir($path));
436 if ($tryRecursion)
437 {
438 push (@pathStack, $path);
439 $didRecursion = 1;
440 last if ($path eq '/');
441 }
442 else
443 {
444 $self->{xftp_lastmsg} = "mkdir:Could not create path($orgPath) since parent not directory!";
445 return undef;
446 }
447 }
448 if ($didRecursion)
449 {
450 while (@pathStack)
451 {
452 $path = pop @pathStack;
453 eval { $ok = $self->{xftp}->do_mkdir($path, Net::SFTP::Attributes->new()) };
454 if ($@)
455 {
456 $self->{xftp_lastmsg} = $@;
457 return undef;
458 }
459 next;
460 }
461 }
462 eval { $ok = $self->{xftp}->do_mkdir($orgPath, Net::SFTP::Attributes->new()) };
463 if ($@)
464 {
465 $self->{xftp_lastmsg} = $@;
466 return undef;
467 }
468 return 1;
469 }
470
471 sub rmdir
472 {
473 my $self = shift;
474 my $path = shift;
475 $path =~ s#[\/\\]$##o unless ($path eq '/');
476
477 my $ok;
478 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
479 eval { $ok = $self->{xftp}->do_rmdir($path) };
480 $self->{xftp_lastmsg} = $@ if ($@);
481 return $@ ? undef : 1;
482 }
483
484 sub message
485 {
486 my $self = shift;
487
488 chomp $self->{xftp_lastmsg};
489 my @res = $self->{xftp}->status;
490 return ($self->{xftp_lastmsg} =~ /^\s*$res[0]/) ? $self->{xftp_lastmsg} : "$res[0]: $res[1] - $self->{xftp_lastmsg}";
491 }
492
493 sub sftpWarnings #ONLY WAY TO GET NON-FATAL WARNINGS INTO $@ INSTEAD OF STDERR IS TO USE THIS CALLBACK!
494 { #(WE ALWAYS WRAP SFTP->METHODS W/AN EVAL)!
495 my $self = shift;
496 my @res = $self->status;
497 die "$res[0]: $res[1] - ".join(' ', @_)."\n";
498 }
499
500 sub mdtm
501 {
502 my $self = shift;
503 my $path = shift;
504
505 my $ok;
506 $path = $self->{cwd} . '/' . $path unless ($path =~
507 m#^(?:[a-zA-Z]\:|\/)#o);
508 eval { $ok = $self->{xftp}->do_stat($path) };
509 unless (defined($ok) && $ok)
510 {
511 $self->{xftp_lastmsg} = $@;
512 return undef;
513 }
514 return $ok->mtime();
515 }
516
517 sub size
518 {
519 my $self = shift;
520 my $path = shift;
521
522 my $ok;
523 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
524 eval { $ok = $self->{xftp}->do_stat($path) };
525 unless (defined($ok) && $ok)
526 {
527 $self->{xftp_lastmsg} = $@;
528 return undef;
529 }
530 return $ok->size();
531 }
532
533 sub isadir
534 {
535 my $self = shift;
536 my $path = shift;
537
538 my $ok;
539 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
540 eval { $ok = $self->{xftp}->do_opendir($path) };
541 if (defined($ok) && $ok)
542 {
543 eval { $self->{xftp}->do_close($ok) };
544 return 1;
545 }
546 return 0;
547 }
548
549 sub chmod
550 {
551 my $self = shift;
552 my $permissions = shift;
553 my $path = shift;
554
555 my ($ok, $attrs);
556 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
557 eval { $attrs = $self->{xftp}->do_stat($path) };
558 unless (defined($attrs) && $attrs)
559 {
560 $self->{xftp_lastmsg} = $@;
561 return undef;
562 }
563 eval "\$permissions = 0$permissions";
564 if ($@)
565 {
566 $self->{xftp_lastmsg} = "Invalid permissions (000-777) - $@";
567 return undef;
568 }
569 $attrs->perm($permissions);
570 eval { $ok = $self->{xftp}->do_setstat($path, $attrs) };
571 if ($@ || !defined($ok))
572 {
573 $self->{xftp_lastmsg} = $@;
574 return undef;
575 }
576 return 1;
577 }
578
579 sub method
580 {
581 my $self = shift;
582 my $method = shift;
583
584 for (my $i=0;$i<scalar(@_);$i++)
585 {
586 $_[$i] = "'" . $_[$i] . "'" unless ($_[$i] =~ /^['"]/o || $_[$i] =~ /^[\d\.\+\-]+$/o);
587 }
588 my $res;
589 my $xeq = " \$res = \$self->{xftp}->$method(".join(',', @_).")";
590 eval $xeq;
591 if ($@)
592 {
593 $self->{xftp_lastmsg} = "method Net::SFTP::$method(".join(',',@_).") returned $@";
594 return undef;
595 }
596 return $res;
597 }
598
599 1