xFTP_FTP.pm untested

filter: Statementstmt 0.0% 0 / 208 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 445 CC 62 SCAR 39.3
CC
62
Cov
0%
new_ftp
110.0
copy
90.0
ls
72.0
CRAP 50.7
1 package Net::xFTP::FTP;
2
3 sub new_ftp
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 }
16 $xftp->{xftp} = Net::FTP->new($host, %args);
17 unless (defined $xftp->{xftp})
18 {
19 $xftp->{xftp_lastmsg} = $@;
20 return undef;
21 }
22 if (defined $args{user})
23 {
24 $args{user} ||= 'anonymous';
25 $args{password} ||= 'anonymous@' if ($args{user} eq 'anonymous');
26 my @loginargs = ($args{user});
27 push (@loginargs, $args{password}) if (defined $args{password});
28 push (@loginargs, $args{account}) if (defined $args{account});
29 if ($xftp->{xftp}->login(@loginargs))
30 {
31 my $cwd = $xftp->{xftp}->pwd();
32 $xftp->{cwd} = $cwd if ($cwd);
33 $xftp->{protocol} = 'Net::FTP';
34 return $xftp;
35 }
36 }
37 else
38 {
39 return $xftp if ($xftp->{xftp}->login());
40 }
41 $@ ||= 'Invalid Password?';
42 return undef;
43 }
44
45 sub protocol
46 {
47 return shift->{protocol};
48 }
49
50 {
51 no warnings 'redefine';
52 sub cwd #SET THE "CURRENT" DIRECTORY.
53 {
54 my $self = shift;
55 my $cwd = shift || '/';
56
57 my $ok;
58 $ok = $self->{xftp}->cwd($cwd);
59 $self->{cwd} = $cwd if ($ok);
60 return $ok ? 1 : undef;
61 }
62
63 sub copy
64 {
65 my $self = shift;
66
67 return undef unless (@_ >= 2);
68 my @args = @_;
69 if ($self->isadir($args[1]))
70 {
71 my $filename = $1 if ($args[0] =~ m#([^\/]+)$#o);
72 $args[1] .= '/' unless ($args[1] =~ m#\/$#o);
73 $args[1] .= $filename;
74 }
75
76 my $ok;
77 my ($tmp, $t);
78 my $fromHandle;
79 eval { $fromHandle = $self->{xftp}->retr($args[0]) };
80 unless ($fromHandle)
81 {
82 $self->{xftp_lastmsg} = "Copy failed (". ($@||'retr failed - Unknown reason')
83 . ')!';
84 return undef;
85 }
86 while ($fromHandle->read($tmp, $self->{BlockSize}))
87 {
88 $t .= $tmp;
89 }
90 $fromHandle->close();
91 my $toHandle;
92 eval { $toHandle = $self->{xftp}->stor($args[1]) };
93 unless ($toHandle)
94 {
95 $self->{xftp_lastmsg} = "Copy failed (". ($@||'stor failed - Unknown reason')
96 . ')!';
97 return undef;
98 }
99 eval { $toHandle->write($t, length($t)) };
100 if ($@)
101 {
102 $self->{xftp_lastmsg} = "Copy failed (". ($@||'write failed - Unknown reason')
103 . ')!';
104 return undef;
105 }
106 $toHandle->close();
107 $ok = 1;
108 return $ok ? 1 : undef;
109 }
110
111 sub move
112 {
113 my $self = shift;
114
115 return undef unless (@_ >= 2);
116 return ($self->copy(@_) && $self->delete($_[0])) ? 1 : undef;
117 }
118 }
119
120 sub ascii
121 {
122 my $self = shift;
123
124 $self->{xftp}->ascii();
125 return undef;
126 }
127
128 sub binary
129 {
130 my $self = shift;
131
132 $self->{xftp}->binary();
133 return undef;
134 }
135
136 sub quit
137 {
138 my $self = shift;
139 $self->{xftp}->quit();
140 return;
141 }
142
143 sub ls
144 {
145 my $self = shift;
146 my $path = shift || '';
147 my $showall = shift || 0;
148 my @dirlist;
149 @dirlist = $self->{xftp}->ls($path||'.');
150 return unless (defined $dirlist[0]); #ADDED 20070613 TO PREVENT WARNING.
151 shift (@dirlist) if ($dirlist[0] =~ /^total \d/o); #REMOVE TOTAL LINE!
152 my $i = 0;
153 while ($i<=$#dirlist)
154 {
155 #$dirlist[$i] =~ s#\/\/#\/#;
156 $dirlist[$i] = $1 if ($dirlist[$i] =~ m#([^\/\\]+)$#o);
157 $dirlist[$i] = $1 if ($dirlist[$i] =~ /\/(\.\.?)$/o);
158 if ($dirlist[$i] eq '..' && $path eq '/')
159 {
160 splice(@dirlist, $i, 1);
161 }
162 elsif (!$showall && $dirlist[$i] =~ /^\.[^\.]/o)
163 {
164 splice(@dirlist, $i, 1);
165 }
166 else
167 {
168 ++$i;
169 }
170 }
171 @dirlist = sort @dirlist;
172
173 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
174 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] eq '..');
175 #unshift (@dirlist, '.') unless ($dirlist[0] eq '.');
176
177 return wantarray ? @dirlist : \@dirlist;
178 }
179
180 sub dir
181 {
182 my $self = shift;
183 my $path = shift || '';
184 my $showall = shift || 0;
185 my @dirlist;
186 @dirlist = $self->{xftp}->dir($path||'.');
187 return unless (defined $dirlist[0]); #ADDED 20070613 TO PREVENT WARNING.
188 shift (@dirlist) if ($dirlist[0] =~ /^total \d/o); #REMOVE TOTAL LINE!
189 my $i = 0;
190 while ($i<=$#dirlist)
191 {
192 #$dirlist[$i] =~ s#\/\/#\/#;
193 $dirlist[$i] = $1 if ($dirlist[$i] =~ m#([^\/\\]+)$#o);
194 $dirlist[$i] = $1 if ($dirlist[$i] =~ /\/(\.\.?)$/o);
195 if ($dirlist[$i] =~ /\d \.\.$/o && $path eq '/')
196 {
197 splice(@dirlist, $i, 1);
198 }
199 elsif (!$showall && $dirlist[$i] =~ /\d \.[^\.]\S*$/o)
200 {
201 splice(@dirlist, $i, 1);
202 }
203 else
204 {
205 ++$i;
206 }
207 }
208
209 ##ON SOME SERVERS, THESE DON'T GET ADDED ON, SO ADD THEM HERE!
210 #unshift (@dirlist, '..') unless ($path eq '/' || $dirlist[1] =~ /\d \.\.$/);
211 #unshift (@dirlist, '.') unless ($dirlist[0] =~ /\d \.$/);
212
213 return wantarray ? @dirlist : \@dirlist;
214 }
215
216 sub pwd #GET AND RETURN THE "CURRENT" DIRECTORY.
217 {
218 my $self = shift;
219
220 # my $cwd = $self->{xftp}->pwd();
221 # $self->{cwd} = $cwd if ($cwd);
222
223 return $self->{cwd};
224 }
225
226 sub get #(Remote, => Local)
227 {
228 my $self = shift;
229
230 return undef unless (@_ >= 1);
231 my @args = @_;
232 unless (@args >= 2)
233 {
234 if (ref(\$args[0]) =~ /GLOB/io)
235 {
236 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
237 return undef;
238 }
239 $args[1] = $args[0];
240 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
241 }
242 my $ok;
243 eval { $ok = $self->{xftp}->get(@args) };
244 $self->{xftp_lastmsg} = $@ if ($@);
245 return $ok ? 1 : undef;
246 }
247
248 sub put #(LOCAL => REMOTE) SFTP returns OK=1 on SUCCESS.
249 {
250 my $self = shift;
251
252 return undef unless (@_ >= 1);
253 my @args = @_;
254 unless (@args >= 2)
255 {
256 if (ref(\$args[0]) =~ /GLOB/io)
257 {
258 $self->{xftp_lastmsg} = 'Must specify a remote filename (2 arguments) since 1st arg. is a filehandle!';
259 return undef;
260 }
261 $args[1] = $args[0];
262 $args[1] = $1 if ($args[1] =~ m#([^\/\\]+)$#o);
263 }
264
265 my $ok;
266 eval { $ok = $self->{xftp}->put(@args) };
267 #print STDERR "-ftp.put(".join('|',@args)."= ok=$ok= at=$at=\n";
268 $self->{xftp_lastmsg} = $@ if ($@);
269 return $ok ? 1 : undef;
270 }
271
272 sub delete #RETURNED OK=2 WHEN LAST FAILED.
273 {
274 my $self = shift;
275 my $path = shift;
276
277 my $ok;
278 eval { $ok = $self->{xftp}->delete($path) };
279 $self->{xftp_lastmsg} = $@ if ($@);
280 return $ok ? 1 : undef;
281 }
282
283 sub rename
284 {
285 my $self = shift;
286 return undef unless (@_ == 2);
287
288 my ($oldfile, $newfile) = @_;
289
290 my $ok;
291 eval { $ok = $self->{xftp}->rename($oldfile, $newfile) };
292 $self->{xftp_lastmsg} = $@ if ($@);
293 return $ok ? 1 : undef;
294 }
295
296 sub mkdir
297 {
298 my $self = shift;
299 my $path = shift;
300 my $tryRecursion = shift||0;
301 $path =~ s#[\/\\]$##o unless ($path eq '/');
302
303 my @pathStack;
304 my $ok = '';
305 eval { $ok = $self->{xftp}->mkdir($path, $tryRecursion) };
306 $self->{xftp_lastmsg} = $@ if ($@);
307 return $ok ? 1 : undef;
308 }
309
310 sub rmdir
311 {
312 my $self = shift;
313 my $path = shift;
314 $path =~ s#[\/\\]$##o unless ($path eq '/');
315
316 my $ok;
317 eval { $ok = $self->{xftp}->rmdir($path) };
318 $self->{xftp_lastmsg} = $@ if ($@);
319 return $ok ? 1 : undef;
320 }
321
322 sub message
323 {
324 my $self = shift;
325
326 chomp $self->{xftp_lastmsg};
327 return $self->{xftp}->message;
328 }
329
330 sub mdtm
331 {
332 my $self = shift;
333 my $path = shift;
334
335 my $ok;
336 return $self->{xftp}->mdtm($path);
337 }
338
339 sub size
340 {
341 my $self = shift;
342 my $path = shift;
343
344 my $ok;
345 return $self->{xftp}->size($path);
346 }
347
348 sub isadir
349 {
350 my $self = shift;
351 my $path = shift;
352
353 my $ok;
354 my $curdir = $self->{xftp}->pwd();
355 eval { $ok = $self->{xftp}->cwd($path); };
356 if ($ok)
357 {
358 $self->{xftp}->cwd($curdir);
359 return 1;
360 }
361 return 0;
362 }
363
364 sub chmod
365 {
366 my $self = shift;
367 my $permissions = shift;
368 my $path = shift;
369
370 my ($ok, $attrs);
371 unless ($self->{xftp}->supported('SITE CHMOD'))
372 {
373 $@ = 'Server does not support chmod!';
374 $self->{xftp_lastmsg} = $@;
375 # $self->{xftp}->set_status(1, $@);
376 }
377 $ok = $self->{xftp}->site('CHMOD', $permissions, $path);
378 return ($ok == 2) ? 1 : undef;
379 }
380
381 1