xFTP_FTP.pm untested

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