xFTP_Foreign.pm untested

filter: Statementstmt 0.0% 0 / 347 Branchbran 0.0% 0 / 158 Conditioncond 0.0% 0 / 99 MC/DCmcdc 0.0% 0 / 59 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 714 CC 80 SCAR 41.3
CC
80
Cov
0%
new_foreign
156.0
copy
132.0
mkdir
110.0
CRAP 62.3
1 package Net::xFTP::Foreign;
2 use Fcntl ':mode';
3 use Net::SFTP::Foreign::Constants qw( SSH2_FXF_CREAT SSH2_FXF_WRITE );
4
5 my @permvec = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
6
7 sub new_foreign
8 {
9 my $subclass = shift;
10 my $pkg = shift;
11 my $host = shift;
12 my %args = @_;
13 my %xftp_args;
14 my $xftp = bless { }, $subclass;
15 $xftp->{BlockSize} = 10240;
16 if (defined $args{BlockSize})
17 {
18 $xftp->{BlockSize} = $args{BlockSize} || 10240;
19 delete($args{BlockSize});
20 }
21 delete($args{'Debug'}) if (defined $args{'Debug'});
22 delete($args{'warn'}) if (defined $args{'warn'});
23 my $saveEnvHome = $ENV{HOME};
24 # $ENV{HOME} = $xftp_args{home} if ($xftp_args{home});
25 $SIG{ALRM} = sub { die "timeout" };
26 eval
27 {
28 alarm(defined($args{'login_timeout'}) ? $args{'login_timeout'} : 25);
29 $xftp->{xftp} = Net::SFTP::Foreign->new($host, %args);
30 };
31 my $at = $@;
32 $xftp->{xftp_lastmsg} = $at if ($at);
33 alarm(0);
34 return undef if ($at =~ /timeout/io);
35 # $ENV{HOME} = $saveEnvHome || '';
36 if ($xftp->{xftp})
37 {
38 my $errORwarn;
39 if ($xftp->{xftp}->error)
40 {
41 $errORwarn = $xftp->{xftp}->error;
42 # $xftp->{xftp_lastmsg} = $xftp->{xftp}->error;
43 # $@ = $xftp->{xftp_lastmsg};
44 # return undef;
45 }
46 my $cwd = $xftp->{xftp}->setcwd('.');
47 if (defined($cwd) && $cwd)
48 {
49 $xftp->{cwd} = $cwd;
50 return $xftp;
51 }
52 if ($xftp->{xftp}->error)
53 {
54 $xftp->{xftp_lastmsg} = $xftp->{xftp}->error;
55 $xftp->{xftp_lastmsg} .= ' ' . $errORwarn if ($errORwarn);
56 $@ = $xftp->{xftp_lastmsg};
57 $xftp->{protocol} = 'Net::SFTP::Foreign';
58 return undef;
59 }
60 return $xftp;
61 }
62 return undef;
63 }
64
65 sub protocol
66 {
67 return shift->{protocol};
68 }
69
70 {
71 no warnings 'redefine';
72 sub cwd #SET THE "CURRENT" DIRECTORY.
73 {
74 my $self = shift;
75 my $cwd = shift || '/';
76
77 my $ok;
78 my $fullwd;
79 #NEXT 22 ADDED 20060815 TO FIX RELATIVE PATH CHANGES.
80 if ($cwd !~ m#^\/# && $self->{cwd} && $self->{cwd} !~ /^\./o)
81 {
82 if ($self->{cwd} =~ m#\/$#o)
83 {
84 $cwd = $self->{cwd} . $cwd;
85 }
86 else
87 {
88 $cwd = $self->{cwd} . '/' . $cwd;
89 }
90 }
91 $fullwd = $self->{xftp}->setcwd($cwd);
92 if ($fullwd)
93 {
94 $self->{cwd} = $fullwd;
95 $ok = 1;
96 }
97 return $ok ? 1 : undef;
98 }
99
100 sub copy
101 {
102 my $self = shift;
103
104 return undef unless (@_ >= 2);
105 my @args = @_;
106 for (my $i=0;$i<=1;$i++)
107 {
108 $args[$i] = $self->{cwd} . '/' . $args[$i] unless ($args[$i] =~ m#^(?:[a-zA-Z]\:|\/)#o);
109 }
110 if ($self->isadir($args[1]))
111 {
112 my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o);
113 $args[1] .= '/' unless ($args[1] =~ m#\/$#o);
114 $args[1] .= $filename;
115 }
116
117 my $ok;
118 my ($tmp, $t);
119 my $fromHandle = $self->{xftp}->open($args[0]);
120 unless (defined($fromHandle) && $fromHandle)
121 {
122 $self->{xftp_lastmsg} = "Copy failed1 (". ($self->{xftp}->error||'open failed - Unknown reason')
123 . ')!';
124 return undef;
125 }
126 my $offset = 0;
127 my $err;
128 while (1)
129 {
130 $tmp = $self->{xftp}->read($fromHandle, $self->{BlockSize});
131 last if ($self->{xftp}->error);
132 $t .= $tmp;
133 $offset += $self->{BlockSize};
134 }
135 $self->{xftp}->close($fromHandle);
136 my $toHandle = $self->{xftp}->open($args[1],
137 SSH2_FXF_CREAT | SSH2_FXF_WRITE );
138 unless (defined($toHandle) && $toHandle)
139 {
140 $self->{xftp_lastmsg} = "Copy failed2 (". ($self->{xftp}->error||'open2 failed - Unknown reason')
141 . ')!';
142 return undef;
143 }
144 $ok = $self->{xftp}->write($toHandle, $t);
145 unless (defined($ok) && $ok)
146 {
147 $self->{xftp_lastmsg} = "Copy failed3 (". ($self->{xftp}->error||'write failed - Unknown reason')
148 . ')!';
149 return undef;
150 }
151 $self->{xftp}->close($toHandle);
152 $ok = 1;
153 return $ok ? 1 : undef;
154 }
155
156 sub move
157 {
158 my $self = shift;
159
160 return undef unless (@_ >= 2);
161 return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef;
162 }
163 }
164
165 sub ascii
166 {
167 my $self = shift;
168
169 return undef;
170 }
171
172 sub binary
173 {
174 my $self = shift;
175
176 return undef;
177 }
178
179 sub quit
180 {
181 my $self = shift;
182
183 $self->{xftp} = undef;
184 delete($self->{xftp});
185
186 return;
187 }
188
189 sub ls
190 {
191 my $self = shift;
192 my $path = shift || '';
193 my $showall = shift || 0;
194 my @dirlist;
195 my $realpath = $path;
196 $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o);
197 my $dirListRef = $self->{xftp}->ls($realpath);
198 unless (defined $dirListRef)
199 {
200 $self->{xftp_lastmsg} = $self->{xftp}->error;
201 return undef;
202 }
203 my ($t, @tm, @tp, $permStr);
204 @dirlist = ();
205 foreach my $i (sort { $a->{filename} cmp $b->{filename} } @{$dirListRef})
206 {
207 push (@dirlist, $i->{filename});
208 }
209
210 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
211 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\.\.$/);
212 #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\.$/);
213
214 return wantarray ? @dirlist : \@dirlist;
215 }
216
217 sub dir
218 {
219 my $self = shift;
220 my $path = shift || '';
221 my $showall = shift || 0;
222 my @dirlist;
223 my $realpath = $path;
224 $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o);
225 my $dirListRef = $self->{xftp}->ls($realpath);
226 #return if ($@);
227 unless (defined $dirListRef)
228 {
229 $self->{xftp_lastmsg} = $self->{xftp}->error;
230 return undef;
231 }
232 # shift (@dirHash) if ($dirHash[0]->{longname} =~ /^total \d/o); #REMOVE TOTAL LINE!
233 my ($t, @tm, @tp, $permStr);
234 @dirlist = ();
235 #for (my $i=0;$i<=$#dirHash;$i++)
236 foreach my $i (sort { $a->{filename} cmp $b->{filename} } @{$dirListRef})
237 {
238 #$t = $dirHash[$i]->{longname};
239 $t = $i->{filename};
240 chomp $t;
241 next if ($t =~ /\.\.$/o && $path eq '/');
242 next if (!$showall && $t =~ /\.[^\.]\S*$/o);
243 # $tp = substr($i->{a}->flags,0,1);
244 # $tp = '-' if ($tp =~ /f/io);
245 @tm = localtime($i->{a}->mtime);
246 $permStr = &getPermStr($i->{a}->{perm});
247 $_ = sprintf "%10s %s %s %8s %4d-%2.2d-%2.2d %2.2d:%2.2d %s\n",
248 $permStr, (($i->{a}->uid =~ /\S/o) ? $i->{a}->uid : '-unknown-'),
249 (($i->{a}->gid =~ /\S/o) ? $i->{a}->gid : '-unknown-'),
250 $i->{a}->{size}||'0',
251 $tm[5]+1900, $tm[4]+1, $tm[3], $tm[2], $tm[1], $t;
252 push (@dirlist, $_);
253 }
254
255 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
256 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\.\.$/);
257 #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\.$/);
258
259 return wantarray ? @dirlist : \@dirlist;
260 }
261
262 sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY.
263 {
264 my $self = shift;
265
266 return $self->{cwd};
267 }
268
269 sub get #(Remote, => Local)
270 {
271 my $self = shift;
272
273 return undef unless (@_ >= 1);
274 my @args = @_;
275 # $args[0] = $self->{cwd} . '/' . $args[0] unless ($args[0] =~ m#^(?:[a-zA-Z]\:|\/)#o);
276 if (@args >= 2)
277 {
278 $args[1] = \$_[1] if (ref(\$args[1]) =~ /GLOB/io);
279 }
280 else
281 {
282 if (ref(\$args[0]) =~ /GLOB/io)
283 {
284 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
285 return undef;
286 }
287 $args[1] = $args[0];
288 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
289 }
290 my $ok = $self->{xftp}->get(@args);
291 unless (defined($ok) && $ok)
292 {
293 $self->{xftp_lastmsg} = $self->{xftp}->error;
294 return undef;
295 }
296 return 1;
297 }
298
299 sub put #(LOCAL => REMOTE) SFTP returns OK=1 on SUCCESS.
300 {
301 my $self = shift;
302
303 return undef unless (@_ >= 1);
304 my @args = @_;
305 if (@args >= 2)
306 {
307 $args[0] = \$_[0] if (ref(\$args[0]) =~ /GLOB/io);
308 }
309 else
310 {
311 if (ref(\$args[0]) =~ /GLOB/io)
312 {
313 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
314 return undef;
315 }
316 $args[1] = $args[0];
317 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
318 }
319 # $args[1] = $self->{cwd} . '/' . $args[1] unless ($args[1] =~ m#^(?:[a-zA-Z]\:|\/)#o);
320
321 my $ok = $self->{xftp}->put(@args);
322 unless (defined($ok) && $ok)
323 {
324 $self->{xftp_lastmsg} = $self->{xftp}->error;
325 return undef;
326 }
327 return 1;
328 }
329
330 sub delete #RETURNED OK=2 WHEN LAST FAILED.
331 {
332 my $self = shift;
333 my $path = shift;
334
335 # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
336 my $ok = $self->{xftp}->remove($path);
337 unless (defined($ok) && $ok)
338 {
339 $self->{xftp_lastmsg} = $self->{xftp}->error;
340 return undef;
341 }
342 return 1;
343 }
344
345 sub rename
346 {
347 my $self = shift;
348 return undef unless (@_ == 2);
349
350 my ($oldfile, $newfile) = @_;
351
352 my $ok = $self->{xftp}->rename($oldfile, $newfile);
353 unless (defined($ok) && $ok)
354 {
355 $self->{xftp_lastmsg} = $self->{xftp}->error;
356 return undef;
357 }
358 return 1;
359 }
360
361 sub mkdir
362 {
363 my $self = shift;
364 my $path = shift;
365 my $tryRecursion = shift||0;
366 $path =~ s#[\/\\]$##o unless ($path eq '/');
367
368 my @pathStack;
369 my $ok = '';
370 my $orgPath = $path;
371 my $didRecursion = 0;
372 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
373 while ($path)
374 {
375 $path =~ s#[^\/\\]+$##o;
376 $path =~ s#[\/\\]$##o;
377 $path = '/' unless ($path);
378 last if ($self->isadir($path));
379 if ($tryRecursion)
380 {
381 push (@pathStack, $path);
382 $didRecursion = 1;
383 last if ($path eq '/');
384 }
385 else
386 {
387 $self->{xftp_lastmsg} = "mkdir:Could not create path($orgPath) since parent not directory!";
388 return undef;
389 }
390 }
391 if ($didRecursion)
392 {
393 while (@pathStack)
394 {
395 $path = pop @pathStack;
396 $ok = $self->{xftp}->mkdir($path);
397 unless (defined($ok) && $ok)
398 {
399 $self->{xftp_lastmsg} = $self->{xftp}->error;
400 return undef;
401 }
402 next;
403 }
404 }
405 $ok = $self->{xftp}->mkdir($orgPath);
406 unless (defined($ok) && $ok)
407 {
408 $self->{xftp_lastmsg} = $self->{xftp}->error;
409 return undef;
410 }
411 return 1;
412 }
413
414 sub rmdir
415 {
416 my $self = shift;
417 my $path = shift;
418 $path =~ s#[\/\\]$##o unless ($path eq '/');
419
420 my $ok = $self->{xftp}->rmdir($path);
421 unless (defined($ok) && $ok)
422 {
423 $self->{xftp_lastmsg} = $self->{xftp}->error;
424 return undef;
425 }
426 return 1;
427 }
428
429 sub message
430 {
431 my $self = shift;
432
433 $self->{xftp_lastmsg} ||= '';
434 chomp $self->{xftp_lastmsg};
435 my $res = $self->{xftp}->status;
436 return (length $res) ? ("$res - ".$self->{xftp_lastmsg}) : $self->{xftp_lastmsg};
437 }
438
439 sub mdtm
440 {
441 my $self = shift;
442 my $path = shift;
443
444 my $ok;
445 # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
446 $ok = $self->{xftp}->stat($path);
447 unless (defined($ok) && $ok && defined($ok->mtime))
448 {
449 $self->{xftp_lastmsg} = $@;
450 return undef;
451 }
452 return $ok->mtime;
453 }
454
455 sub size
456 {
457 my $self = shift;
458 my $path = shift;
459
460 my $ok;
461 # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
462 $ok = $self->{xftp}->stat($path);
463 unless (defined($ok) && $ok && defined($ok->size))
464 {
465 $self->{xftp_lastmsg} = $@;
466 return undef;
467 }
468 return $ok->size;
469 }
470
471 sub isadir
472 {
473 my $self = shift;
474 my $path = shift;
475
476 my $ok;
477 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
478 $ok = $self->{xftp}->opendir($path);
479 if (defined($ok) && $ok)
480 {
481 eval { $self->{xftp}->closedir($ok) };
482 my $stat = $self->{xftp}->stat($path); #JWT:NEXT 2 ADDED PATCH BY mavit.org.uk TO ADDRESS BUG#74082: Net::xFTP::Foreign::isadir() to GlobalSCAPE EFT
483 return 1 if (defined($stat) && S_ISDIR($stat->perm));
484 }
485 return 0;
486 }
487
488 sub chmod
489 {
490 my $self = shift;
491 my $permissions = shift;
492 my $path = shift;
493
494 my ($ok, $attrs);
495 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
496 $attrs = $self->{xftp}->stat($path);
497 unless (defined($attrs) && $attrs)
498 {
499 $self->{xftp_lastmsg} = $self->{xftp}->error;
500 return undef;
501 }
502 eval "\$permissions = 0$permissions";
503 if ($@)
504 {
505 $self->{xftp_lastmsg} = "Invalid permissions (000-777) - $@";
506 return undef;
507 }
508 $attrs->set_perm($permissions);
509 $ok = $self->{xftp}->setstat($path, $attrs);
510 unless (defined($ok) && $ok)
511 {
512 $self->{xftp_lastmsg} = $self->{xftp}->error;
513 return undef;
514 }
515 return 1;
516 }
517
518 sub getPermStr
519 {
520 my $mode = shift;
521
522 my (%ftypes);
523 $ftypes{(S_IFDIR)} = "d";
524 $ftypes{(S_IFCHR)} = "c";
525 $ftypes{(S_IFREG)} = "-" if (defined S_IFREG);
526 if ($bummer) #FOR SOME REASON WINDOWS FCNTL DOES NOT HAVE THESE, BUT WE NEED -
527 { #THEM WHEN LOOKING AT SPECIAL FILES ON OTHER UNIX SYSTEMS:
528 $ftypes{24576} = "b";
529 $ftypes{4096} = "p";
530 $ftypes{40960} = "l";
531 $ftypes{49152} = "s";
532 }
533 else
534 {
535 $ftypes{(S_IFBLK)} = "b";
536 $ftypes{(S_IFIFO)} = "p";
537 $ftypes{(S_IFLNK)} = "l";
538 $ftypes{(S_IFSOCK)} = "s";
539 }
540 my @fsperms = ('----', '---t', '--s-', '--st', '-s--', '-s-t', '-ss-', '-sst');
541
542 my $permissions = sprintf "%04o", S_IMODE($mode);
543 my @permissions = split(//o, sprintf("%04o", S_IMODE($mode)));
544 my @spermissions = split(//o, $fsperms[$permissions[0]]);
545 my $pstr = $ftypes{S_IFMT($mode)};
546 my $ps;
547 for (my $i=1;$i<=3;$i++)
548 {
549 $ps = $permvec[$permissions[$i]]; #r-x
550 if ($spermissions[$i] =~ /\w/o)
551 {
552 my $loc = substr($ps,2,2);
553 if ($loc =~ /\w/o)
554 {
555 $loc = $spermissions[$i];
556 }
557 else
558 {
559 $loc = uc($spermissions[$i]);
560 }
561 $ps =~ s/^(..).$/$1$loc/;
562 }
563 $pstr .= $ps;
564 }
565 return $pstr;
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::SFTP::Foreign::$method(".join(',',@_).") returned $@";
583 return undef;
584 }
585 return $res;
586 }
587
588 1