xFTP_LOCAL.pm untested

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