xFTP_FSP.pm untested

filter: Statementstmt 0.0% 0 / 260 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 64 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 485 CC 59 SCAR 37.9
CC
59
Cov
0%
mkdir
110.0
copy
90.0
get
56.0
CRAP 44.1
1 package Net::xFTP::FSP;
2
3 sub new_fsp
4 {
5 my $subclass = shift;
6 my $pkg = shift;
7 my $host = shift;
8 my %args = @_;
9 my %xftp_args;
10 my $xftp = bless { }, $subclass;
11 $xftp->{BlockSize} = 10240;
12 if (defined $args{BlockSize})
13 {
14 $xftp->{BlockSize} = $args{BlockSize} || 10240;
15 delete($args{BlockSize});
16 }
17 my $saveEnvHome = $ENV{HOME};
18 $ENV{HOME} = $xftp_args{home} if ($xftp_args{home});
19
20 $xftp->{xftp} = Net::FSP->new($host, \%args);
21 $ENV{HOME} = $saveEnvHome || '';
22 if ($xftp->{xftp})
23 {
24 my $cwd = $xftp->{xftp}->current_dir();
25 $xftp->{cwd} = $cwd if ($cwd);
26 return $xftp;
27 }
28 return undef;
29 }
30
31 {
32 no warnings 'redefine';
33 sub cwd #SET THE "CURRENT" DIRECTORY.
34 {
35 my $self = shift;
36 my $cwd = shift || '/';
37
38 my $ok;
39 my $fullwd;
40 if ($cwd !~ m#^\/# && $self->{cwd} && $self->{cwd} !~ /^\./o)
41 {
42 if ($self->{cwd} =~ m#\/$#o)
43 {
44 $cwd = $self->{cwd} . $cwd;
45 }
46 else
47 {
48 $cwd = $self->{cwd} . '/' . $cwd;
49 }
50 }
51 $fullwd = $self->{xftp}->change_dir($cwd);
52 $ok = $self->{xftp}->current_dir();
53 if ($ok eq $cwd) # fullwd is set to the PREVIOUS directory!
54 {
55 $self->{cwd} = $cwd;
56 $ok = 1;
57 }
58 return undef;
59 }
60
61 sub copy
62 {
63 my $self = shift;
64
65 return undef unless (@_ >= 2);
66 my @args = @_;
67 for (my $i=0;$i<=1;$i++)
68 {
69 $args[$i] = $self->{cwd} . '/' . $args[$i] unless ($args[$i] =~ m#^(?:[a-zA-Z]\:|\/)#o);
70 }
71 if ($self->isadir($args[1]))
72 {
73 my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o);
74 $args[1] .= '/' unless ($args[1] =~ m#\/$#o);
75 $args[1] .= $filename;
76 }
77
78 my $ok;
79 my ($buff);
80 my $fromHandle = $self->{xftp}->open_file($args[0], '<');
81 unless (defined($fromHandle) && $fromHandle)
82 {
83 $self->{xftp_lastmsg} = "Copy failed (". ($!||'open1 failed - Unknown reason')
84 . ')!';
85 return undef;
86 }
87 my $toHandle;
88 $toHandle = $self->{xftp}->open_file($args[1], '>');
89 unless (defined($toHandle) && $toHandle)
90 {
91 $self->{xftp_lastmsg} = "Copy failed (". ($!||'do_open2 failed - Unknown reason')
92 . ')!';
93 return undef;
94 }
95 my $offset = 0;
96 my $err;
97 while ($buff = <$fromHandle>)
98 {
99 print $toHandle $buff;
100 $offset += length($buff);
101 }
102 close $toHandle;
103 close $fromHandle;
104 $ok = 1;
105 return $ok ? 1 : undef;
106 }
107
108 sub move
109 {
110 my $self = shift;
111
112 return undef unless (@_ >= 2);
113 # return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef;
114 $self->{xftp}->move_file($_[0], $_[1]);
115 }
116 }
117
118 sub ascii
119 {
120 my $self = shift;
121
122 return undef;
123 }
124
125 sub binary
126 {
127 my $self = shift;
128
129 return undef;
130 }
131
132 sub quit
133 {
134 my $self = shift;
135 $self->{xftp}->say_bye();
136 $self->{xftp} = undef;
137 delete($self->{xftp});
138
139 return;
140 }
141
142 sub ls
143 {
144 my $self = shift;
145 my $path = shift || '';
146 my $showall = shift || 0;
147 my @dirHash = $self->{xftp}->list_dir($path|'.');
148 return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY.
149 my $t;
150 my @dirlist = ();
151 for (my $i=0;$i<=$#dirHash;$i++)
152 {
153 $t = $dirHash[$i]->{name};
154 next if ($t eq '..' && $path eq '/');
155 next if (!$showall && $t =~ /^\.[^\.]/o);
156 push (@dirlist, $t);
157 }
158 @dirlist = sort @dirlist;
159
160 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
161 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..');
162 #unshift (@dirlist, '.') unless ($dirlist[0] eq '.');
163
164 return wantarray ? @dirlist : \@dirlist;
165 }
166
167 sub dir
168 {
169 my $self = shift;
170 my $path = shift || '';
171 my $showall = shift || 0;
172 my @dirlist;
173 # $realpath = $self->{cwd} . '/' . $realpath unless ($realpath =~ m#^(?:[a-zA-Z]\:|\/)#o);
174 my @dirHash = $self->{xftp}->list_dir($path||'.');
175 return unless (defined $dirHash[0]); #ADDED 20071024 FOR CONSISTENCY.
176 my ($t, @tm, $tp);
177 @dirlist = ();
178 foreach my $i (@dirHash)
179 {
180 #$t = $dirHash[$i]->{longname};
181 $t = $i->short_name();
182 next if ($t =~ /\d \.\.$/o && $path eq '/');
183 next if (!$showall && $t =~ /\d \.[^\.]\S*$/o);
184 $tp = substr($i->type(),0,1);
185 $tp = '-' if ($tp =~ /f/io);
186 @tm = localtime($i->time());
187 $_ = sprintf "%1s----------unknown- -unknown- %8s %4d-%2.2d-%2.2d %2.2d:%2.2d %s\n",
188 $tp, $i->{size}, $tm[5]+1900, $tm[4]+1, $tm[3], $tm[2], $tm[1], $t;
189 push (@dirlist, $_);
190 }
191
192 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
193 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/);
194 #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/);
195
196 return wantarray ? @dirlist : \@dirlist;
197 }
198
199 sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY.
200 {
201 my $self = shift;
202
203 return $self->{cwd};
204 }
205
206 sub get #(Remote, => Local)
207 {
208 my $self = shift;
209
210 return undef unless (@_ >= 1);
211 my @args = @_;
212 if (@args >= 2)
213 {
214 $args[1] = \$_[1] if (ref(\$args[1]) =~ /GLOB/io);
215 }
216 else
217 {
218 if (ref(\$args[0]) =~ /GLOB/io)
219 {
220 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
221 return undef;
222 }
223 $args[1] = $args[0];
224 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
225 }
226 eval { $self->{xftp}->download_file(@args); };
227 if ($@)
228 {
229 $self->{xftp_lastmsg} = $@;
230 return undef;
231 }
232 return 1;
233 }
234
235 sub put #(LOCAL => REMOTE) SFTP returns OK=1 on SUCCESS.
236 {
237 my $self = shift;
238 return undef unless (@_ >= 1);
239 my @args = @_;
240 if (@args >= 2)
241 {
242 $args[0] = \$_[0] if (ref(\$args[0]) =~ /GLOB/io);
243 }
244 else
245 {
246 if (ref(\$args[0]) =~ /GLOB/io)
247 {
248 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
249 return undef;
250 }
251 $args[1] = $args[0];
252 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
253 }
254 eval { $self->{xftp}->upload_file($args[1], $args[0]); }; #THEY REVERSED THE ARGS ON THIS (READS FROM 2ND ARG - ARGS[0])?!?!?!
255 if ($@)
256 {
257 $self->{xftp_lastmsg} = $@;
258 return undef;
259 }
260 return 1;
261 }
262
263 sub delete #RETURNED OK=2 WHEN LAST FAILED.
264 {
265 my $self = shift;
266 my $path = shift;
267
268 eval { $self->{xftp}->remove_file($path) };
269 if ($@)
270 {
271 $self->{xftp_lastmsg} = $@;
272 return undef;
273 }
274 return 1;
275 }
276
277 sub rename
278 {
279 my $self = shift;
280 return undef unless (@_ == 2);
281
282 my ($oldfile, $newfile) = @_;
283
284 # $oldfile = $self->{cwd} . '/' . $oldfile unless ($oldfile =~ m#^(?:[a-zA-Z]\:|\/)#o);
285 # $newfile = $self->{cwd} . '/' . $newfile unless ($newfile =~ m#^(?:[a-zA-Z]\:|\/)#o);
286 eval { $self->{xftp}->move_file($oldfile, $newfile) };
287 if ($@)
288 {
289 $self->{xftp_lastmsg} = $@;
290 return undef;
291 }
292 return 1;
293 }
294
295 sub mkdir
296 {
297 my $self = shift;
298 my $path = shift;
299 my $tryRecursion = shift||0;
300 $path =~ s#[\/\\]$##o unless ($path eq '/');
301
302 my @pathStack;
303 my $orgPath = $path;
304 my $didRecursion = 0;
305 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
306 while ($path)
307 {
308 $path =~ s#[^\/\\]+$##o;
309 $path =~ s#[\/\\]$##o;
310 $path = '/' unless ($path);
311 last if ($self->isadir($path));
312 if ($tryRecursion)
313 {
314 push (@pathStack, $path);
315 $didRecursion = 1;
316 last if ($path eq '/');
317 }
318 else
319 {
320 $self->{xftp_lastmsg} = "mkdir:Could not create path($orgPath) since parent not directory!";
321 return undef;
322 }
323 }
324 if ($didRecursion)
325 {
326 while (@pathStack)
327 {
328 $path = pop @pathStack;
329 eval { $self->{xftp}->make_dir($path) };
330 if ($@)
331 {
332 $self->{xftp_lastmsg} = $@;
333 return undef;
334 }
335 next;
336 }
337 }
338 eval { $self->{xftp}->make_dir($orgPath) };
339 if ($@)
340 {
341 $self->{xftp_lastmsg} = $@;
342 return undef;
343 }
344 return 1;
345 }
346
347 sub rmdir
348 {
349 my $self = shift;
350 my $path = shift;
351
352 $path =~ s#[\/\\]$##o unless ($path eq '/');
353
354 $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
355 eval { $self->{xftp}->remove_dir($path) };
356 if ($@)
357 {
358 $self->{xftp_lastmsg} = $@;
359 return undef;
360 }
361 return 1;
362 }
363
364 sub message
365 {
366 my $self = shift;
367
368 chomp $self->{xftp_lastmsg};
369 return $self->{xftp_lastmsg};
370 }
371
372 sub mdtm
373 {
374 my $self = shift;
375 my $path = shift;
376
377 my $ok;
378 # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
379 eval { $ok = $self->{xftp}->stat_file($path) };
380 if ($@ || !defined($ok))
381 {
382 $self->{xftp_lastmsg} = $@ || "xFTP: size() failed for unknown reason!";
383 return undef;
384 }
385 return $ok->time();
386 }
387
388 sub size
389 {
390 my $self = shift;
391 my $path = shift;
392
393 my $ok;
394 # $path = $self->{cwd} . '/' . $path unless ($path =~ m#^(?:[a-zA-Z]\:|\/)#o);
395 eval { $ok = $self->{xftp}->stat_file($path) };
396 if ($@ || !defined($ok))
397 {
398 $self->{xftp_lastmsg} = $@ || "xFTP: size() failed for unknown reason!";
399 return undef;
400 }
401 return $ok->size();
402 }
403
404 sub isadir
405 {
406 my $self = shift;
407 my $path = shift;
408
409 my $ok;
410 eval { $ok = $self->{xftp}->stat_file($path) };
411 if ($@ || !defined($ok))
412 {
413 $self->{xftp_lastmsg} = $@ || "xFTP: isadir() failed for unknown reason!";
414 return undef;
415 }
416 return ($ok->type() =~ /d/io) ? 1 : 0;
417 }
418
419 sub chmod
420 {
421 my $self = shift;
422 my $permissions = shift;
423 my $path = shift;
424
425 $self->{xftp_lastmsg} = 'Net::FSP: chmod function not supported!';
426 return undef;
427 }
428
429 sub method
430 {
431 my $self = shift;
432 my $method = shift;
433
434 for (my $i=0;$i<scalar(@_);$i++)
435 {
436 $_[$i] = "'" . $_[$i] . "'" unless ($_[$i] =~ /^['"]/o || $_[$i] =~ /^[\d\.\+\-]+$/o);
437 }
438 my $res;
439 my $xeq = " \$res = \$self->{xftp}->$method(".join(',', @_).")";
440 eval $xeq;
441 if ($@)
442 {
443 $self->{xftp_lastmsg} = "method Net::FSP::$method(".join(',',@_).") returned $@";
444 return undef;
445 }
446 return $res;
447 }
448
449 1