xFTP_LOCAL.pm untested

filter: Statementstmt 0.0% 0 / 319 Branchbran 0.0% 0 / 200 Conditioncond 0.0% 0 / 83 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 647 CC 101 SCAR 52.6
CC
101
Cov
0%
dir
650.0
ls
182.0
get
132.0
CRAP 192.5
1 package Net::xFTP::LOCAL;
2
3 #THIS "PLACEHOLDER" MODULE IMPLEMENTS THE Net::*FTP OBJECT AND FUNCTIONS ON THE
4 #"LOCAL" FILESYSTEM AS "Net::LOCAL".
5
6 my @permvec = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
7
8 sub new_local
9 {
10 my $subclass = shift;
11 my $pkg = shift;
12 my $host = shift;
13 my %args = @_;
14 my %xftp_args;
15 my $xftp = bless { }, $subclass;
16
17 $@ = '';
18 $xftp->{cwd} = Cwd::cwd(); #JWT:NOTE 20150123: DOES NOT RETURN A TRAILING "/" UNLESS "/"!
19 $xftp->{xftp} = 1;
20 return $xftp;
21 }
22
23 {
24 no warnings 'redefine';
25 sub cwd #SET THE "CURRENT" DIRECTORY.
26 {
27 my $self = shift;
28 my $cwd = shift || '/';
29 $cwd = Cwd::cwd() . '/' . $cwd unless ($cwd =~ m#^(?:[a-zA-Z]\:|\/)#o); #JWT:ADDED 20150123 TO HANDLE CASE WHERE USER SPECIFIES A RELATIVE PATH:
30 $cwd =~ s#\/\/#\/#o;
31 unless ($cwd =~ m#^(?:[A-Za-z]\:)?\/$#) { #JWT:ADDED 20150123 TO ENSURE NO TRAILING SLASH (UNLESS "/" OR "C:/")
32 chop $cwd if ($cwd =~ m#\/$#);
33 }
34 my $ok;
35 $self->{cwd} = $cwd;
36 $ok = 1;
37 return $ok;
38 }
39
40 sub copy
41 {
42 my $self = shift;
43
44 return undef unless (@_ >= 2);
45 my @args = @_;
46 for (my $i=0;$i<=1;$i++)
47 {
48 $args[$i] = $self->{cwd} . '/' . $args[$i] unless ($args[$i] =~ m#^(?:[a-zA-Z]\:|\/)#o);
49 $args[$i] =~ s#\/\/#\/#o;
50
51 }
52 if ($self->isadir($args[1]))
53 {
54 my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o);
55 $args[1] .= '/' unless ($args[1] =~ m#[\:\/]$#o);
56 $args[1] .= $filename;
57 }
58
59 my $ok;
60 $ok = File::Copy::copy($args[0], $args[1]);
61 unless ($ok)
62 {
63 $self->{xftp_lastmsg} = $! || 'xFTP:Copy() failed - Local copy failed for unknown reason!';
64 }
65 return $ok ? 1 : undef;
66 }
67
68 sub move
69 {
70 my $self = shift;
71
72 return undef unless (@_ >= 2);
73 return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef;
74 }
75 }
76
77 sub ascii
78 {
79 my $self = shift;
80
81 return undef;
82 }
83
84 sub binary
85 {
86 my $self = shift;
87
88 return undef;
89 }
90
91 sub quit
92 {
93 my $self = shift;
94 $self->{xftp} = undef;
95 delete($self->{xftp});
96
97 return;
98 }
99
100 sub ls
101 {
102 my $self = shift;
103 my $path = shift || '';
104 my $showall = shift || 0;
105 my @dirlist;
106 my $realpath = $path || $self->{cwd} || Cwd::cwd();
107 if ($realpath =~ /^\./o)
108 {
109 my $rp = $realpath;
110 $realpath = $self->{cwd} || Cwd::cwd();
111 chop $realpath if ($realpath =~ m#[^\:]\/$#o);
112 $realpath =~ s#\/[^\/]+$#\/#o if ($rp eq '..');
113 }
114 unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o) {
115 $realpath = $self->{cwd} . (($self->{cwd} =~ m#[\:\/]$#) ? '' : '/') . $realpath;
116 }
117 if ($self->{cwd} =~ m#^[a-zA-Z]\:# && $realpath !~ /\:/) {
118 $realpath = $self->{cwd} . (($self->{cwd} =~ m#\/$# || $realpath =~ m#^\/#) ? '' : '/') . $realpath;
119 $realpath =~ s#\/\/#\/#;
120 }
121 my $t;
122 @dirlist = ();
123 if (opendir D, $realpath)
124 {
125 while ($t = readdir(D))
126 {
127 next if ($t =~ /^total \d/o);
128 next if ($t eq '..' && $path eq '/');
129 next if (!$showall && $t =~ /^\.[^\.]/o);
130 push (@dirlist, $t);
131 }
132 closedir D;
133 }
134 else
135 {
136 $self->{xftp_lastmsg} = $! || 'xFTP:ls() failed - Local ls failed for unknown reason!';
137 return;
138 }
139 @dirlist = sort @dirlist;
140
141 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
142 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..');
143 #unshift (@dirlist, '.') unless ($dirlist[0] eq '.');
144
145 return wantarray ? @dirlist : \@dirlist;
146 }
147
148 sub dir
149 {
150 my $self = shift;
151 my $path = shift || '';
152 my $showall = shift || 0;
153 my @dirlist;
154 my $realpath = $path || $self->{cwd} || Cwd::cwd();
155 if ($realpath =~ /^\./o)
156 {
157 my $rp = $realpath;
158 $realpath = $self->{cwd} || Cwd::cwd();
159 chop $realpath if ($realpath =~ m#[^\:]\/$#o);
160 $realpath =~ s#\/[^\/]+$#\/#o if ($rp eq '..');
161 }
162 unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o) {
163 $realpath = $self->{cwd} . (($self->{cwd} =~ m#[\:\/]$#) ? '' : '/') . $realpath;
164 }
165 if ($self->{cwd} =~ m#^[a-zA-Z]\:# && $realpath !~ /\:/) {
166 $realpath = $self->{cwd} . (($self->{cwd} =~ m#\/$# || $realpath =~ m#^\/#) ? '' : '/') . $realpath;
167 $realpath =~ s#\/\/#\/#;
168 }
169 my $t;
170 if ($self->{bummer})
171 {
172 if (opendir D, $realpath)
173 {
174 $realpath .= '/' unless ($realpath =~ m#[\:\/]$#o);
175 my (@sb, @tm, $permval, @permdigits, $permstr);
176 while ($t = readdir(D))
177 {
178 next if ($t =~ /^total \d/o);
179 next if ($t eq '..' && $path eq '/');
180 next if (!$showall && $t =~ /^\.[^\.]/o);
181 @sb = stat("$realpath$t");
182 @tm = localtime($sb[9]);
183 $permval = sprintf('%04o', $sb[2]&07777);
184 @permdigits = split(//, $permval);
185
186 $permstr = (-d "$realpath$t") ? 'd' : '-';
187 $_ = $permvec[$permdigits[1]];
188 if ($permdigits[0] >= 4)
189 {
190 s/\-/S/o;
191 s/x/s/o;
192 }
193 $permstr .= $_;
194 $_ = $permvec[$permdigits[2]];
195 if ($permvec[$permdigits[0]] =~ /w/o)
196 {
197 s/\-/S/o;
198 s/x/s/o;
199 }
200 $permstr .= $_;
201 $_ = $permvec[$permdigits[3]];
202 if ($permvec[$permdigits[0]] =~ /x/o)
203 {
204 s/\-/S/o;
205 s/x/s/o;
206 }
207 $permstr .= $_;
208
209 push (@dirlist, sprintf "%s\x02%10s %2d %8s %8s %10d %4d-%2.2d-%2.2d %2.2d:%2.2d %s\n",
210 $t, $permstr, $sb[3], $sb[4], $sb[5], $sb[7], $tm[5]+1900, $tm[4]+1, $tm[3], $tm[2], $tm[1], $t);
211 }
212 @dirlist = sort(@dirlist);
213 for (my $i=0;$i<=$#dirlist;$i++)
214 {
215 $dirlist[$i] =~ s/^[^\x02]*\x02//so;
216 }
217 }
218 }
219 else
220 {
221 my @d = $showall ? `ls -la "$realpath"` : `ls -l "$realpath"`;
222 if (@d)
223 {
224 shift @d if ($d[0] =~ /^total \d/o); #REMOVE "TOTAL" LINE.
225 foreach my $t (@d)
226 {
227 chomp $t;
228 next if ($t =~ /\d \.\.$/o && $path eq '/');
229 next if (!$showall && $t =~ /\d \.[^\.]\S*$/o);
230 push (@dirlist, $t);
231 }
232
233 }
234 elsif ($@)
235 {
236 $self->{xftp_lastmsg} = $@;
237 return;
238 }
239 }
240
241 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
242 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/);
243 #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/);
244
245 return wantarray ? @dirlist : \@dirlist;
246 }
247
248 sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY.
249 {
250 my $self = shift;
251 # my $cwd;
252 # $self->{cwd} ||= Cwd::cwd() || $ENV{PWD};
253
254 return $self->{cwd};
255 }
256
257 sub get #(Remote, => Local)
258 {
259 my $self = shift;
260
261 return undef unless (@_ >= 1);
262 my @args = @_;
263 $args[0] = $self->{cwd} . '/' . $args[0] unless ($args[0] =~ m#^(?:[a-zA-Z]\:|\/)#o);
264 $args[0] =~ s#\/\/#\/#o;
265
266 unless (@args >= 2)
267 {
268 if (ref(\$args[0]) =~ /GLOB/io)
269 {
270 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
271 return undef;
272 }
273 $args[1] = $args[0];
274 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
275 }
276 my $ok;
277 if (ref(\$args[1]) =~ /GLOB/io)
278 {
279 my $buff;
280 my $unsubscriptedFH = $args[1];
281
282 flush $unsubscriptedFH; #DOESN'T SEEM TO HELP - NEEDED IN CALLING ROUTINE TOO?!?!?!
283
284 local *TF;
285 unless (open(TF, $args[0]))
286 {
287 $self->{xftp_lastmsg} = "Could not open remote file ($args[0]) ("
288 . ($! ? $! : 'unknown reasons') .')!';
289 }
290 while ($buff = <TF>)
291 {
292 print $unsubscriptedFH $buff;
293 }
294 close TF;
295 flush $unsubscriptedFH;
296 return 1;
297 }
298 else
299 {
300 $ok = File::Copy::copy($args[0], $args[1]);
301 }
302 unless ($ok)
303 {
304 $self->{xftp_lastmsg} = $! || 'xFTP:get() failed - Local copy failed for unknown reason!';
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 unless (@args >= 2 || ref(\$args[0]) =~ /GLOB/io)
316 {
317 if (ref(\$args[0]) =~ /GLOB/io)
318 {
319 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
320 return undef;
321 }
322 $args[1] = $args[0];
323 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
324 }
325
326 my $ok;
327 $args[1] = $self->{cwd} . '/' . $args[1] unless ($args[1] =~ m#^(?:[a-zA-Z]\:|\/)#o);
328 $args[1] =~ s#\/\/#\/#o;
329
330 if (ref(\$args[0]) =~ /GLOB/io)
331 {
332 my $buff;
333 my $unsubscriptedFH = $args[0];
334
335 local *TF;
336 unless (open(TF, ">$args[1]"))
337 {
338 $self->{xftp_lastmsg} = "Could not open remote file ($args[1]) ("
339 . ($! ? $! : 'unknown reasons') .')!';
340 }
341 my $t;
342 while ($buff = <$unsubscriptedFH>)
343 {
344 $t .= $buff;
345 }
346 print TF $t;
347 close TF;
348 return 1;
349 }
350 else
351 {
352 $ok = File::Copy::copy($args[0], $args[1]);
353 }
354 unless ($ok)
355 {
356 $self->{xftp_lastmsg} = $! || 'xFTP:put() failed - Local copy failed for unknown reason!';
357 }
358 return $ok ? 1 : undef;
359 }
360
361 sub delete #RETURNED OK=2 WHEN LAST FAILED.
362 {
363 my $self = shift;
364 my $path = shift;
365
366 my $ok;
367 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
368 $path =~ s#\/\/#\/#o;
369
370 return unless ($path);
371 #!!!!return ($_ == 1) ? 1 : undef;
372 return unlink($path) ? 1 : undef;
373 }
374
375 sub rename
376 {
377 my $self = shift;
378 return undef unless (@_ == 2);
379
380 my ($oldfile, $newfile) = @_;
381
382 my $ok;
383 $oldfile = $self->{cwd} . '/' . $oldfile unless ($oldfile =~ m#^(?:[a-zA-Z]\:|\/)#o);
384 $oldfile =~ s#\/\/#\/#o;
385 $newfile = $self->{cwd} . '/' . $newfile unless ($newfile =~ m#^(?:[a-zA-Z]\:|\/)#o);
386 $newfile =~ s#\/\/#\/#o;
387
388 $ok = rename($oldfile, $newfile);
389 unless ($ok)
390 {
391 $self->{xftp_lastmsg} = $! || 'xFTP:rename() failed - Local rename failed for unknown reason!';
392 }
393 return $ok ? 1 : undef;
394 }
395
396 sub mkdir
397 {
398 my $self = shift;
399 my $path = shift;
400 my $tryRecursion = shift||0;
401 $path =~ s#[\/\\]$##o unless ($path eq '/');
402
403 my @pathStack;
404 my $ok = '';
405 my $orgPath = $path;
406 my $didRecursion = 0;
407 my $errored = 0;
408 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
409 $path =~ s#\/\/#\/#o;
410 while ($path)
411 {
412 $ok = mkdir $path;
413 last if ($ok);
414 if ($tryRecursion)
415 {
416 push (@pathStack, $path);
417 $path =~ s#[^\/\\]+$##o;
418 $path =~ s#[\/\\]$##o;
419 $didRecursion = 1;
420 }
421 else
422 {
423 $self->{xftp_lastmsg} = "mkdir:mkdir($path):failed($!)!";
424 $errored = 1;
425 last;
426 }
427 }
428 if ($didRecursion)
429 {
430 while (@pathStack)
431 {
432 $path = pop @pathStack;
433 next if (mkdir $path);
434 $self->{xftp_lastmsg} = "mkdir:mkdir($path):Could not recursively create subdirectory($!)!";
435 return undef;
436
437 }
438 return 1;
439 }
440 return $errored ? undef : 1;
441 }
442
443 sub rmdir
444 {
445 my $self = shift;
446 my $path = shift;
447 $path =~ s#[\/\\]$##o unless ($path eq '/');
448
449 my $ok;
450 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
451 $path =~ s#\/\/#\/#o;
452
453 return undef unless ($path);
454 $ok = rmdir $path;
455 unless ($ok)
456 {
457 $self->{xftp_lastmsg} = $! || 'xFTP:rmdir() failed - Local rename failed for unknown reason!';
458 }
459 return $ok ? 1 : undef;
460 }
461
462 sub message
463 {
464 my $self = shift;
465
466 chomp $self->{xftp_lastmsg};
467 return $self->{xftp_lastmsg};
468 }
469
470 sub mdtm
471 {
472 my $self = shift;
473 my $path = shift;
474
475 my $ok;
476 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
477 $path =~ s#\/\/#\/#o;
478
479 eval { (undef, undef, undef, undef, undef, undef, undef, undef, undef,
480 $ok) = stat($path) };
481 return $@ ? undef : $ok;
482 }
483
484 sub size
485 {
486 my $self = shift;
487 my $path = shift;
488
489 my $ok;
490 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
491 $path =~ s#\/\/#\/#o;
492
493 eval { (undef, undef, undef, undef, undef, undef, undef, $ok) = stat($path) };
494 return $@ ? undef : $ok;
495 }
496
497 sub isadir
498 {
499 my $self = shift;
500 my $path = shift;
501
502 my $ok;
503 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
504 $path =~ s#\/\/#\/#o;
505 return (-d $path) ? 1 : 0;
506 }
507
508 sub chmod
509 {
510 my $self = shift;
511 my $permissions = shift;
512 my $path = shift;
513
514 my ($ok, $attrs);
515 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
516 $path =~ s#\/\/#\/#o;
517 eval "\$permissions = 0$permissions";
518 if ($@)
519 {
520 $self->{xftp_lastmsg} = "Invalid permissions (0-777) - $@";
521 return undef;
522 }
523 $ok = chmod $permissions, $path;
524 unless ($ok)
525 {
526 $self->{xftp_lastmsg} = $! || 'xFTP:chmod() failed - Local chmod failed for unknown reason!';
527 }
528 return $ok ? 1 : undef;
529 }
530
531 sub method
532 {
533 my $self = shift;
534 my $method = shift;
535
536 for (my $i=0;$i<scalar(@_);$i++)
537 {
538 $_[$i] = "'" . $_[$i] . "'" unless ($_[$i] =~ /^['"]/o || $_[$i] =~ /^[\d\.\+\-]+$/o);
539 }
540 my $res;
541 my $xeq = " \$res = \$self->{xftp}->$method(".join(',', @_).")";
542 eval $xeq;
543 if ($@)
544 {
545 $self->{xftp_lastmsg} = "method Net::Local::$method(".join(',',@_).") returned $@";
546 return undef;
547 }
548 return $res;
549 }
550
551 1