xFTP_OpenSSH.pm untested

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