xFTP_Foreign.pm untested

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