xFTP_OpenSSH.pm untested

filter: Statementstmt 0.0% 0 / 346 Branchbran 0.0% 0 / 202 Conditioncond 0.0% 0 / 166 MC/DCmcdc 0.0% 0 / 95 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 857 CC 102 SCAR 47.1
CC
102
Cov
0%
get
306.0
put
210.0
new_openssh
182.0
CRAP 110.8
1 package Net::xFTP::OpenSSH;
2
3 use Time::Local;
4
5 sub new_openssh
6 {
7 my $subclass = shift;
8 my $pkg = shift;
9 my $host = shift;
10 my %args = @_;
11 my %xftp_args;
12 my $xftp = bless { }, $subclass;
13 $xftp->{BlockSize} = 10240;
14 if (defined $args{BlockSize})
15 {
16 $xftp->{BlockSize} = $args{BlockSize} || 10240;
17 delete($args{BlockSize});
18 }
19 delete($args{'Debug'}) if (defined $args{'Debug'});
20 delete($args{'warn'}) if (defined $args{'warn'});
21 $args{'timeout'} = 15 unless (defined $args{'timeout'});
22 if (defined $args{'user'})
23 {
24 $host =~ s/^[^\@]*\@//o;
25 $host = $args{'user'} . '@' . $host;
26 delete($args{'user'});
27 }
28 if (defined $args{'password'})
29 {
30 if (defined($args{'passphrase'}) && $args{'passphrase'} =~ /\*/)
31 {
32 #FOR "passphrase => '*'": USE USER-ENTERED PASSWORD FIELD AS THE PASSPHRASE INSTEAD!:
33 $args{'passphrase'} = $args{'password'};
34 }
35 else
36 {
37 $host =~ s/\:[^\@]*//o;
38 $host =~ s/\@/\:$args{'password'}\@/;
39 }
40 delete($args{'password'});
41 }
42 my $saveEnvHome = $ENV{HOME};
43 $ENV{HOME} = $xftp_args{home} if ($xftp_args{home});
44
45 $xftp->{xftp} = Net::OpenSSH->new($host, %args);
46 $xftp->{xftp_lastmsg} = $@ if ($@);
47 $ENV{HOME} = $saveEnvHome || '';
48 if ($xftp->{xftp})
49 {
50 if ($xftp->{xftp}->error)
51 {
52 $xftp->{xftp_lastmsg} = $xftp->{xftp}->error;
53 $@ = $xftp->{xftp_lastmsg};
54 return undef;
55 }
56 my $cwd = $xftp->{xftp}->capture('pwd');
57 if (!$cwd || $xftp->{xftp}->error)
58 {
59 $xftp->{xftp_lastmsg} = $xftp->{xftp}->error || "xFTP:new_openssh() failed for unknown reason!"
60 . ')!';
61 $@ = $xftp->{xftp_lastmsg};
62 return undef;
63 }
64 chomp $cwd;
65 $xftp->{cwd} = $cwd;
66 $xftp->{protocol} = 'Net::OpenSSH';
67
68 return $xftp;
69 }
70 return undef;
71 }
72
73 sub protocol
74 {
75 return shift->{protocol};
76 }
77
78 {
79 no warnings 'redefine';
80 sub cwd #SET THE "CURRENT" DIRECTORY.
81 {
82 my $self = shift;
83 my $cwd = shift || '/';
84
85 my $fullwd;
86 #NEXT 22 ADDED 20060815 TO FIX RELATIVE PATH CHANGES.
87 if ($cwd !~ m#^\/# && $self->{cwd} && $self->{cwd} !~ /^\./o)
88 {
89 $cwd = ($self->{cwd} =~ m#\/$#o) ? $self->{cwd} . $cwd : $self->{cwd} . '/' . $cwd;
90 }
91 elsif ($cwd eq '..')
92 {
93 #DOES NOT CHANGE PWD TO CWD, SO WE KEEP OURSELVES?! $self->{xftp}->capture("cd \"$$self{cwd}\"");
94 #DOES NOT CHANGE PWD TO CWD, SO WE KEEP OURSELVES?! $cwd = $self->{xftp}->capture('pwd');
95 $cwd = $self->{cwd}; #DOES NOT CHANGE PWD TO CWD, SO WE KEEP OURSELVES?!
96 chomp $cwd;
97 chop $cwd if ($cwd =~ m#\/$#o);
98 $cwd =~ s#\/[^\/]+$##o;
99 $cwd ||= '/';
100 }
101 elsif ($cwd eq '.')
102 {
103 #DOES NOT CHANGE PWD TO CWD, SO WE KEEP OURSELVES?! $cwd = $self->{xftp}->capture('pwd');
104 $cwd = $self->{cwd}; #DOES NOT CHANGE PWD TO CWD, SO WE KEEP OURSELVES?!
105 }
106 $self->{xftp}->capture("cd \"$cwd\"");
107 $fullwd = $self->{xftp}->capture('pwd');
108 if (!$fullwd || $self->{xftp}->error)
109 {
110 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:cwd() failed for unknown reason!"
111 . ')!';
112 return undef;
113 }
114 #DOES NOT CHANGE PWD TO CWD, SO WE KEEP OURSELVES?! chomp $fullwd;
115 #DOES NOT CHANGE PWD TO CWD, SO WE KEEP OURSELVES?! $self->{cwd} = $fullwd;
116 $self->{cwd} = $cwd; #DOES NOT CHANGE PWD TO CWD, SO WE KEEP OURSELVES?!
117 return 1;
118 }
119
120 sub copy
121 {
122 my $self = shift;
123
124 return undef unless (@_ >= 2);
125 my @args = @_;
126 for (my $i=0;$i<=1;$i++)
127 {
128 $args[$i] = $self->{cwd} . '/' . $args[$i] unless ($args[$i] =~ m#^(?:[a-zA-Z]\:|\/)#o);
129 }
130 if ($self->isadir($args[1]))
131 {
132 my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o);
133 $args[1] .= '/' unless ($args[1] =~ m#\/$#o);
134 $args[1] .= $filename;
135 }
136
137 my $notok = $self->{xftp}->capture("cp \"$args[0]\" \"$args[1]\"");
138 if ($notok || $self->{xftp}->error)
139 {
140 $self->{xftp_lastmsg} = "Copy failed (". ($self->{xftp}->error || "xFTP:scp_put() failed for unknown reason!")
141 . ')!';
142 return undef;
143 }
144 return 1;
145 }
146
147 sub move
148 {
149 my $self = shift;
150
151 return undef unless (@_ >= 2);
152 return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef;
153 }
154 }
155
156 sub ascii
157 {
158 my $self = shift;
159
160 return undef;
161 }
162
163 sub binary
164 {
165 my $self = shift;
166
167 return undef;
168 }
169
170 sub quit
171 {
172 my $self = shift;
173 $self->{xftp} = undef;
174 delete($self->{xftp});
175
176 return;
177 }
178
179 sub ls
180 {
181 my $self = shift;
182 my $path = shift || '';
183 my $showall = shift || 0;
184
185 my @dirlist;
186 my $realpath = $path || $self->{cwd} || '.';
187
188 $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o);
189 my @dirHash = $self->{xftp}->capture("ls \"$realpath\"");
190 if (!@dirHash || $self->{xftp}->error)
191 {
192 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:ls() failed for unknown reason!";
193 my $err = $self->{xftp}->error;
194 return undef;
195 }
196 return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY.
197
198 # shift (@dirHash) if ($dirHash[0]->{longname} =~ /^total \d/o); #REMOVE TOTAL LINE!
199 my $t;
200 @dirlist = ();
201 for (my $i=0;$i<=$#dirHash;$i++)
202 {
203 $t = $dirHash[$i];
204 chomp $t;
205 next if ($t eq '..' && $path eq '/');
206 next if (!$showall && $t =~ /^\.[^\.]/o);
207 push (@dirlist, $t);
208 }
209 @dirlist = sort @dirlist;
210
211 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
212 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..');
213 #unshift (@dirlist, '.') unless ($dirlist[0] eq '.');
214
215 return wantarray ? @dirlist : \@dirlist;
216 }
217
218 sub dir
219 {
220 my $self = shift;
221 my $path = shift || '';
222 my $showall = shift || 0;
223
224 my @dirlist;
225 my $realpath = $path || $self->{cwd} || '.';
226 $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o);
227 my @dirHash = $self->{xftp}->capture("ls -l \"$realpath\"");
228 if (!@dirHash || $self->{xftp}->error)
229 {
230 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:dir() failed for unknown reason!";
231 my $err = $self->{xftp}->error;
232 return undef;
233 }
234 my $t;
235 @dirlist = ();
236 foreach my $t (sort @dirHash)
237 {
238 #$t = $dirHash[$i]->{longname};
239 next if ($t =~ /\d \.\.$/o && $path eq '/');
240 next if (!$showall && $t =~ /\d \.[^\.]\S*$/o);
241 next if ($t =~ /^total\s+\d+$/o);
242 push (@dirlist, $t);
243 }
244
245 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
246 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/);
247 #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/);
248
249 return wantarray ? @dirlist : \@dirlist;
250 }
251
252 sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY.
253 {
254 my $self = shift;
255
256 return $self->{cwd} || $self->{xftp}->capture('pwd');
257 }
258
259 sub get #(Remote, => Local [, opts])
260 {
261 my $self = shift;
262
263 return undef unless (@_ >= 1);
264 my @args = @_;
265 my $getops = undef;
266 # $args[0] = $self->{cwd} . '/' . $args[0] unless ($args[0] =~ m#^(?:[a-zA-Z]\:|\/)#o);
267 if (scalar(@args) >= 2)
268 {
269 $args[1] = \$_[1] if (ref(\$args[1]) =~ /GLOB/io);
270 $getops = pop(@args) if (scalar(@args) > 2);
271 }
272 else
273 {
274 if (ref(\$args[0]) =~ /GLOB/io)
275 {
276 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
277 return undef;
278 }
279 $args[1] = $args[0];
280 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
281 }
282 my $ok;
283 if (ref(\$_[1]) =~ /GLOB/io) #2ND ARG(LOCAL FID) IS A FILE HANDLE!
284 {
285 my ($remoteHandle, $pid);
286 my $offset = 0;
287 my $buff;
288 my $unsubscriptedFH = $_[1];
289 ($remoteHandle, $pid) = $self->{xftp}->pipe_out("cat $args[0]");
290 if (defined($remoteHandle) && $remoteHandle)
291 {
292 binary $remoteHandle;
293 while ($buff = <$remoteHandle>) #NEVER SEEMS TO READ ANYTHING, THOUGH THIS IS HOW THE DOCS SAY TO DO IT?!
294 {
295 print $unsubscriptedFH $buff;
296 $offset += length($buff);
297 }
298 close $remoteHandle;
299 return $offset; #FOR FILE-HANDLES, LET'S RETURN THE LENGTH READ (ZERO IS STILL FALSE!)
300 }
301 else
302 {
303 $self->{xftp_lastmsg} = $self->{xftp}->error || 'xFTP:get() Could not open remote source handle for unknown reason!';
304 return undef;
305 }
306 }
307 else
308 {
309 my $ok = (defined $getops) ? $self->{xftp}->scp_get($getops, @args)
310 : $self->{xftp}->scp_get(@args);
311 if ($ok)
312 {
313 return $ok if (-f $args[1]);
314 #RETURNED OK, BUT LOCAL FILE NOT CREATED?!:
315 $self->{xftp_lastmsg} = 'xFTP:get() returned "Ok" but local file not created? ('
316 . $! . ') (' . $xftp->{xftp}->error . ')!';
317 return undef;
318 }
319 my $bang = $!;
320 my $err = $self->{xftp}->error;
321 if ($err || $bang)
322 {
323 $self->{xftp_lastmsg} = $err || $bang || "xFTP:get() failed - unknown reason!";
324 if ($SIG{CHLD} eq 'IGNORE' && $err eq 'scp failed: child exited with code 1')
325 {
326 $self->{xftp_lastmsg} = $bang;
327 if (-f $args[1]) {
328 return 1;
329 } else {
330 return undef;
331 }
332 }
333 return $ok ? 1 : undef;
334 }
335 }
336 return $ok ? 1 : undef;
337 }
338
339 sub put #(LOCAL, => REMOTE [, opts]) SFTP returns OK=1 on SUCCESS.
340 {
341 my $self = shift;
342
343 return undef unless (@_ >= 1);
344 my @args = @_;
345 my $putops = undef;
346 if (scalar(@args) >= 2)
347 {
348 $args[0] = \$_[0] if (ref(\$args[0]) =~ /GLOB/io);
349 $putops = pop(@args) if (scalar(@args) > 2);
350 }
351 else
352 {
353 if (ref(\$args[0]) =~ /GLOB/io)
354 {
355 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
356 return undef;
357 }
358 $args[1] = $args[0];
359 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
360 }
361 # $args[1] = $self->{cwd} . '/' . $args[1] unless ($args[1] =~ m#^(?:[a-zA-Z]\:|\/)#o);
362
363 my $ok;
364 if (ref(\$_[0]) =~ /GLOB/io) #1ST ARG(LOCAL FID) IS A FILE HANDLE!
365 {
366 my ($remoteHandle, $pid);
367 my $offset = 0;
368 my $buff;
369 my $unsubscriptedFH = $_[0];
370 ($remoteHandle, $pid) = $self->{xftp}->pipe_out(" cat >\"$args[1]\"");
371 if (defined($remoteHandle) && $remoteHandle)
372 {
373 binary $remoteHandle;
374 while ($buff = <$unsubscriptedFH>)
375 {
376 print $remoteHandle $buff;
377 $offset += length($buff);
378 }
379 close $remoteHandle;
380 return $offset; #FOR FILE-HANDLES, LET'S RETURN THE LENGTH WRITTEN (ZERO IS STILL FALSE!)
381 }
382 else
383 {
384 $self->{xftp_lastmsg} = $self->{xftp}->error || 'xFTP:put() Could not open remote target handle for unknown reason!';
385 return undef;
386 }
387 }
388 else
389 {
390 my $ok = (defined $putops) ? $self->{xftp}->scp_put($putops, @args)
391 : $self->{xftp}->scp_put(@args);
392 my $bang = $!;
393 my $err = $self->{xftp}->error;
394 if (!$ok || $err || $bang)
395 {
396 $self->{xftp_lastmsg} = $err || $bang || "xFTP:put() failed - unknown reason!";
397 if ($SIG{CHLD} eq 'IGNORE' && $err eq 'scp failed: child exited with code 1')
398 {
399 $self->{xftp_lastmsg} = $bang;
400 return 1;
401 }
402 return $ok ? 1 : undef;
403 }
404 }
405 return $ok ? 1 : undef;
406 }
407
408 sub delete #RETURNED OK=2 WHEN LAST FAILED.
409 {
410 my $self = shift;
411 my $path = shift;
412
413 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
414 my $notok = $self->{xftp}->capture("rm -f \"$path\"");
415 if ($notok || $self->{xftp}->error)
416 {
417 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:delete() failed - unknown reason!";
418 return undef;
419 }
420 return 1;
421 }
422
423 sub rename
424 {
425 my $self = shift;
426
427 return undef unless (@_ == 2);
428
429 my ($oldfile, $newfile) = @_;
430
431 $oldfile = $self->{cwd} . '/' . $oldfile unless ($oldfile =~ m#^(?:[a-zA-Z]\:|\/)#o);
432 $newfile = $self->{cwd} . '/' . $newfile unless ($newfile =~ m#^(?:[a-zA-Z]\:|\/)#o);
433 my $notok = $self->{xftp}->capture("mv \"$oldfile\" \"$newfile\"");
434 if ($notok || $self->{xftp}->error)
435 {
436 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:rename() failed - unknown reason!";
437 return undef;
438 }
439 return 1;
440 }
441
442 sub mkdir
443 {
444 my $self = shift;
445 my $path = shift;
446 my $tryRecursion = shift||0;
447
448 $path =~ s#[\/\\]$##o unless ($path eq '/');
449 my @pathStack;
450 my $ok = '';
451 my $orgPath = $path;
452 my $didRecursion = 0;
453 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
454 while ($path)
455 {
456 $path =~ s#[^\/\\]+$##o;
457 $path =~ s#[\/\\]$##o;
458 $path = '/' unless ($path);
459 last if ($self->isadir($path));
460 if ($tryRecursion)
461 {
462 push (@pathStack, $path);
463 $didRecursion = 1;
464 last if ($path eq '/');
465 }
466 else
467 {
468 $self->{xftp_lastmsg} = "mkdir:Could not create path($orgPath) since parent not directory!";
469 return undef;
470 }
471 }
472 if ($didRecursion)
473 {
474 while (@pathStack)
475 {
476 $path = pop @pathStack;
477 $ok = $self->{xftp}->capture("mkdir \"$path\"");
478 if ($ok || $self->{xftp}->error)
479 {
480 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:mkdir() failed - unknown reason!";
481 return undef;
482 }
483 next;
484 }
485 }
486 $ok = $self->{xftp}->capture("mkdir \"$orgPath\"");
487 if ($ok || $self->{xftp}->error)
488 {
489 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:mkdir() failed - unknown reason!";
490 return undef;
491 }
492 return 1;
493 }
494
495 sub rmdir
496 {
497 my $self = shift;
498 my $path = shift;
499
500 $path =~ s#[\/\\]$##o unless ($path eq '/');
501
502 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
503 my $notok = $self->{xftp}->capture("rmdir \"$path\"");
504 if ($notok || $self->{xftp}->error)
505 {
506 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:rmdir() failed - unknown reason!";
507 return undef;
508 }
509 return $@ ? undef : 1;
510 }
511
512 sub message
513 {
514 my $self = shift;
515
516 return '' unless ($self->{xftp_lastmsg});
517 chomp $self->{xftp_lastmsg};
518
519 return $self->{xftp_lastmsg};
520 }
521
522 sub mdtm
523 {
524 my $self = shift;
525 my $path = shift;
526
527 my $ok;
528 # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
529 my @dirHash = $self->{xftp}->capture("stat \"$path\"");
530 if (!@dirHash || $self->{xftp}->error)
531 {
532 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:mdtm() failed for unknown reason!";
533 return undef;
534 }
535 while (@dirHash)
536 {
537 $ok = shift(@dirHash);
538 if ($ok =~ /^Modify\:\s*(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)/o)
539 {
540 my ($yr, $mth, $d, $h, $m, $s) = ($1, $2, $3, $4, $5, $6);
541 $ok = timelocal($s, $m, $h, $d, $mth-1, $yr);
542 return ($ok > 1) ? $ok : undef;
543 }
544 }
545 return undef;
546 }
547
548 sub size
549 {
550 my $self = shift;
551 my $path = shift;
552
553 # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
554 my @dirHash = $self->{xftp}->capture("ls -l \"$path\"");
555 if (!@dirHash || $self->{xftp}->error)
556 {
557 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:size() failed for unknown reason!";
558 return undef;
559 }
560 return undef if ($dirHash[0] =~ /^total\s+\d+$/o);
561 return $1 if ($dirHash[$#dirHash] =~ /^\S+\s+\d+\s+\S+\s+\S+\s+(\d+)/o);
562 return undef;
563 }
564
565 sub isadir
566 {
567 my $self = shift;
568 my $path = shift;
569
570 #x $path = $self->{cwd} . $path unless ($path =~ m#^\/\.#o);
571 unless ($path =~ m#^[\/\.]#o)
572 {
573 $path = ($self->{cwd} =~ m#\/$#o) ? $self->{cwd} . $path : $self->{cwd} . '/' . $path;
574 }
575 my @dirHash = $self->{xftp}->capture("ls -ld \"$path\"");
576 if (!@dirHash || $self->{xftp}->error)
577 {
578 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:isadir() failed for unknown reason!";
579 return undef;
580 }
581 return 1 if ($dirHash[0] =~ /^(?:dr|total\s+\d+)/o || $dirHash[$#dirHash] =~ /^total\s+\d+$/o);
582 return 0;
583 }
584
585 sub chmod
586 {
587 my $self = shift;
588 my $permissions = shift;
589 my $path = shift;
590
591 my ($notok, $attrs, @dirHash);
592 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
593 $notok = $self->{xftp}->capture("chmod $permissions \"$path\"");
594 if ($notok || $self->{xftp}->error)
595 {
596 $self->{xftp_lastmsg} = $self->{xftp}->error || "xFTP:chmod() failed for unknown reason!";
597 return undef;
598 }
599 return 1;
600 }
601
602 sub method
603 {
604 my $self = shift;
605 my $method = shift;
606
607 for (my $i=0;$i<scalar(@_);$i++)
608 {
609 $_[$i] = "'" . $_[$i] . "'" unless ($_[$i] =~ /^['"]/o || $_[$i] =~ /^[\d\.\+\-]+$/o);
610 }
611 my $res;
612 my $xeq = " \$res = \$self->{xftp}->$method(".join(',', @_).")";
613 eval $xeq;
614 if ($@)
615 {
616 $self->{xftp_lastmsg} = "method Net::OpenSSH::$method(".join(',',@_).") returned $@";
617 return undef;
618 }
619 return $res;
620 }
621
622 1