uFTP/FTP.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 223 CC 20 SCAR 33.8
CC
20
Cov
0%
put
90.0
get
72.0
AUTOLOAD
12.0
CRAP 29.4
1 package Net::uFTP::FTP;
2  
3 use vars qw($VERSION);
4  
5 $VERSION = 0.16;
6 #--------------
7  
8 use warnings;
9 use strict;
10 use File::Find;
11 use File::Basename qw(basename dirname);
12 use File::Path qw(mkpath);
13 use Cwd qw(getcwd);
14 use Net::FTP::AutoReconnect;
15 #======================================================================
16 use base qw(Class::Accessor::Fast::XS);
17 #----------------------------------------------------------------------
18 __PACKAGE__->mk_accessors(qw(ftp host type user password debug));
19 #======================================================================
20 sub AUTOLOAD {
21 our $AUTOLOAD;
22 my ($method) = $AUTOLOAD =~ /::([^:]+)$/o;
23  
24 return if $method eq 'DESTROY';
25
26 my $self = shift;
27
28 croak(qq/Unsupported method "$method"/) unless $self->ftp()->can($method);
29
30 return $self->ftp()->$method(@_);
31 }
32 #======================================================================
33 sub new {
34 my ($self, $host, %params) = (shift, shift, @_);
35
36 $self = bless \%params, $self;
37  
38 $self->ftp(Net::FTP::AutoReconnect->new($host, Port => $params{port}, Debug => $self->debug, ));
39 $self->ftp()->login($self->user(), $self->password()) or return;
40 return $self;
41 }
42 #======================================================================
43 sub put {
44 my ($self, $local, $remote, $recurse) = @_;
45 return if not defined $local or not -e $local;
46  
47 $remote = $self->pwd() unless defined $remote;
48
49 if(not $recurse and -d $local){
50 $self->mkdir($remote);
51 }elsif(-d $local){
52 my (@dirs, @files);
53 find(sub {
54 return if /^\./o;
55 if(-d $File::Find::name){ push @dirs, $File::Find::name; }
56 elsif(-f $File::Find::name){ push @files, $File::Find::name; }
57 }, $local);
58
59 my $base = basename($local);
60 $self->mkdir(File::Spec->catfile($remote,$_),1) for sort map { s/^$local/$base/; $_ } @dirs;
61 for(@files){
62 (my $r = $_) =~ s/^$local/$base/;
63 $self->ftp()->put($_, File::Spec->catfile($remote,$r));
64 }
65 }elsif(-f $local){
66 $self->mkdir(dirname($remote));
67 $self->ftp()->put($local, $remote);
68 }
69 }
70 #======================================================================
71 sub is_dir {
72 my ($self, $path) = @_;
73 return unless defined $path and $self->ftp()->cwd($path);
74 $self->ftp()->cdup();
75 return 1;
76 }
77 #======================================================================
78 sub is_file {
79 my ($self, $path) = @_;
80 return unless defined $path and $self->ftp()->mdtm($path);
81 return 1;
82 }
83 #======================================================================
84 sub get {
85 my ($self, $remote, $local, $recurse) = @_;
86
87 return if not defined $remote;
88
89 $local = getcwd() unless defined $local;
90 if(-d $local){ $local = File::Spec->catfile($local, basename($remote)); }
91 else{ mkpath dirname($local); }
92
93 if($self->is_dir($remote)){
94 mkpath $local;
95 foreach my $file($self->ftp()->ls($remote)){
96 next if $file =~ /^\./o;
97 my $dst = File::Spec->catfile($local, basename($file));
98 if($recurse and $self->is_dir($file)){
99 $self->get($file, $dst, 1);
100 }elsif($self->is_dir($file)){
101 mkdir $dst;
102 }else{
103 $self->ftp()->get($file, $dst);
104 }
105 }
106 }else{
107 $self->ftp()->get($remote, $local);
108 }
109 }
110 #======================================================================
111 sub ls { return shift->ftp()->ls(@_); }
112 #======================================================================
113 sub dir { return shift->ftp()->dir(@_); }
114 #======================================================================
115 sub cwd { return shift->ftp()->cwd(@_); }
116 #======================================================================
117 sub pwd { return shift->ftp()->pwd(@_); }
118 #======================================================================
119 sub rename { return shift->ftp()->rename(@_); }
120 #======================================================================
121 sub size { return shift->ftp()->size(@_); }
122 #======================================================================
123 sub mdtm { return shift->ftp()->mdtm(@_); }
124 #======================================================================
125 sub binary { return shift->ftp()->binary(@_); }
126 #======================================================================
127 sub ascii { return shift->ftp()->mdtm(@_); }
128 #======================================================================
129 sub port { return shift->ftp()->port(@_); }
130 #======================================================================
131 #sub get { return shift->ftp()->get(@_); }
132 #======================================================================
133 #sub put { return shift->ftp()->put(@_); }
134 #======================================================================
135 sub mkdir { return shift->ftp()->mkdir(@_); }
136 #======================================================================
137 sub rmdir { return shift->ftp()->rmdir(@_); }
138 #======================================================================
139 sub delete { return shift->ftp()->delete(@_); }
140 #======================================================================
141 sub message { return shift->ftp()->message(@_); }
142 #======================================================================
143 sub cdup { return shift->ftp()->cdup(); }
144 #======================================================================
145 sub pasv { return shift->ftp()->pasv(@_); }
146 #======================================================================
147 sub quit { return shift->ftp()->quit(@_); }
148 #======================================================================
149 1;