Connector/Proxy/Net/FTP.pm

filter: Statementstmt 29.5% 42 / 142 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 58.8% 10 / 17 Podpod 100% 5 / 5 totaltotal 24.2% 57 / 235 CC 29 SCAR 37.8
CC
29
Cov
21%
set
110.0
_client
56.0
get
30.0
CRAP 43.7
« Connector/Proxy/Net/LDAP.pm ↑ Summary Connector/Builtin/Authentication/LDAP.pm »
1 package Connector::Proxy::Net::FTP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Temp qw(tempfile tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::FTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 extends 'Connector::Proxy';
15 1 with 'Connector::Role::LocalPath';
16  
17 1 has port => (
18 is => 'rw',
19 isa => 'Int',
20 default => 21,
21 );
22  
23 1 has basedir => (
24 is => 'rw',
25 isa => 'Str',
26 );
27  
28 1 has content => (
29 is => 'rw',
30 isa => 'Str',
31 );
32  
33 1 has username => (
34 is => 'rw',
35 isa => 'Str',
36 );
37  
38 1 has password => (
39 is => 'rw',
40 isa => 'Str',
41 );
42  
43 1 has timeout => (
44 is => 'rw',
45 isa => 'Int',
46 default => 30
47 );
48  
49 1 has debug => (
50 is => 'rw',
51 isa => 'Bool',
52 default => 0,
53 );
54  
55 1 has active => (
56 is => 'rw',
57 isa => 'Bool',
58 default => 0,
59 );
60  
61 1 has binary => (
62 is => 'rw',
63 isa => 'Bool',
64 default => 1,
65 );
66  
67 # return the content of the file
68 0 ▶sub get {
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
69  
70 0 my $self = shift;
71 0 my $path = shift;
72  
73 0 my $source = $self->_sanitize_path( $path );
74  
75  
76 0 my $tmpdir = tempdir( CLEANUP => 1 );
77 0 my ($fh, $target) = tempfile( DIR => $tmpdir );
78  
79 0 my $ftp = $self->_client();
80  
81 0 my ($dirname, $filename) = $self->_sanitize_path( $path );
82  
83 0 ▶ if ($dirname && $dirname ne '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirname and $dirname ne ".") 0 0 0
Condition0%0/3
$dirname and $dirname ne "."
!ll&&!rl&&r
000
MC/DC0%0/2
$dirname and $dirname ne "."
$dirname $dirname ne "."
Truth table0%0/3
A and B
A= $dirnameB= $dirname ne "."
ABresult
0X0
100
111
84 0 $self->log()->debug('Change dir to ' . $dirname );
85 0 ▶ if (!$ftp->cwd($dirname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp->cwd($dirname)) 0 0 0
86 0 $self->log()->info("Cannot change working directory $dirname");
87 0 return $self->_die_on_undef();
88 }
89 }
90  
91 0 $self->log()->debug('Send get '. $filename . ' => ' . $target );
92 0 ▶ if (!$ftp->get( $filename, $target )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp->get($filename, $target)) 0 0 0
93 0 $self->log()->info("Cannot read file $filename");
94 0 return $self->_die_on_undef();
95 }
96  
97 0 $ftp->quit;
98  
99 # read the content from temporary file
100 0 my $content = do {
101 0 local $INPUT_RECORD_SEPARATOR;
102 0 open my $fh, '<', $target;
103 0 <$fh>;
104 };
105  
106 0 unlink $target;
107  
108 0 return $content;
109 }
110  
111 0 ▶sub get_keys {
Subroutine get_keys: not called CC=5 SCAR=34.0
Pod documented
112  
113 0 my $self = shift;
114 0 my $path = shift;
115  
116 0 my $dirname = $self->_sanitize_path( $path );
117  
118 0 my $ftp = $self->_client();
119  
120 0 ▶ if ($dirname && $dirname ne '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirname and $dirname ne ".") 0 0 0
Condition0%0/3
$dirname and $dirname ne "."
!ll&&!rl&&r
000
MC/DC0%0/2
$dirname and $dirname ne "."
$dirname $dirname ne "."
Truth table0%0/3
A and B
A= $dirnameB= $dirname ne "."
ABresult
0X0
100
111
121 0 $self->log()->debug('Change dir to ' . $dirname );
122 0 ▶ if (!$ftp->cwd($dirname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ftp->cwd($dirname)) 0 0 0
123 0 $self->log()->info("Cannot change working directory $dirname");
124 0 return $self->_die_on_undef();
125 }
126 }
127  
128 0 my @files = $ftp->ls();
129 0 $self->log()->debug('List content of directory ' . (join "|", @files));
130 0 ▶ return map { $_ unless ($_ =~ /\A\.\.?\z/) } @files;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /\A\.\.?\z/ 0 0 0
131  
132 }
133  
134 0 ▶sub get_meta {
Subroutine get_meta: not called CC=1 SCAR=6.9
Pod documented
135 0 my $self = shift;
136 0 return {TYPE => "scalar" };
137 }
138  
139  
140 0 ▶sub exists {
Subroutine exists: not called CC=2 SCAR=17.9
Pod documented
141  
142 0 my $self = shift;
143  
144 # No path = connector root which always exists
145 0 my @path = $self->_build_path_with_prefix( shift );
146 0 ▶ if (scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) 0 0 0
147 0 return 1;
148 }
149  
150 0 return 1;
151  
152 }
153  
154  
155 # return the content of the file
156 0 ▶sub set {
Subroutine set: not called CC=10 SCAR=47.0
Pod documented
157  
158 0 my $self = shift;
159 0 my $file = shift;
160 0 my $data = shift;
161  
162 0 my $content;
163 0 ▶ if ($self->content()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content) { } 0 0 0
164 0 $self->log()->debug('Process template for content ' . $self->content());
165 0 my $template = Template->new({});
166  
167 0 ▶ $data = { DATA => $data } if (ref $data eq '');
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "" 0 0 0
168  
169 0 ▶ $template->process( \$self->content(), $data, \$content) || $self->_log_and_die("Error processing content template.");
Branch: 1 branch
BranchTrueFalseTotal
$template->process(\$self->content, $data, \$content) or $self->_log_and_die("Error processing content template.") 0 0 0
170 } else {
171 0 ▶ if (ref $data ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "") 0 0 0
172 0 $self->_log_and_die("You need to define a content template if data is not a scalar");
173 }
174 0 $content = $data;
175 }
176  
177 0 my $tmpdir = tempdir( CLEANUP => 1 );
178 0 my ($fh, $source) = tempfile( DIR => $tmpdir );
179  
180 0 ▶ open FILE, ">$source" || $self->_log_and_die("Unable to open file for writing");
Condition0%0/3
">$source" || $self->_log_and_die('Unable to open file for writing')
l!l&&r!l&&!r
000
MC/DC0%0/2
">$source" || $self->_log_and_die('Unable to open file for writing')
">$source" $self->_log_and_die('Unable to open file for writing')
Truth table0%0/3
A || B
A= ">$source"B= $self->_log_and_die('Unable to open file for writing')
ABresult
1X1
011
000
181 0 print FILE $content;
182 0 close FILE;
183  
184 0 my $ftp = $self->_client();
185  
186 0 my ($dirname, $filename) = $self->_sanitize_path( $file, $data );
187  
188 0 ▶ if ($dirname && $dirname ne '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirname and $dirname ne ".") 0 0 0
Condition0%0/3
$dirname and $dirname ne "."
!ll&&!rl&&r
000
MC/DC0%0/2
$dirname and $dirname ne "."
$dirname $dirname ne "."
Truth table0%0/3
A and B
A= $dirnameB= $dirname ne "."
ABresult
0X0
100
111
189 0 $self->log()->debug('Change dir to ' . $dirname );
190 0 ▶ $ftp->cwd($dirname) or
Branch: 1 branch
BranchTrueFalseTotal
$ftp->cwd($dirname) or $self->_log_and_die('Cannot change working directory: ' . $ftp->message) 0 0 0
191 $self->_log_and_die('Cannot change working directory: ' . $ftp->message);
192 }
193  
194  
195 0 $self->log()->debug('Send put '. $source . ' => ' . $filename );
196 0 ▶ $ftp->put( $source, $filename)
Branch: 1 branch
BranchTrueFalseTotal
$ftp->put($source, $filename) or $self->_log_and_die('put failed: ' . $ftp->message) 0 0 0
197 or $self->_log_and_die('put failed: ' . $ftp->message);
198  
199 0 $ftp->quit;
200  
201 0 return 1;
202 }
203  
204 0 ▶sub _sanitize_path {
Subroutine _sanitize_path: not called CC=5 SCAR=34.0
205  
206 0 my $self = shift;
207 0 my $inargs = shift;
208 0 my $data = shift;
209  
210 0 my @args = $self->_build_path_with_prefix( $inargs );
211  
212  
213 0 my $file;
214 0 my $template = Template->new({});
215  
216 0 ▶ if ($self->path() || $self->file()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->path or $self->file) { } 0 0 0
Condition0%0/3
$self->path or $self->file
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->path or $self->file
$self->path $self->file
Truth table0%0/3
A or B
A= $self->pathB= $self->file
ABresult
1X1
011
000
217 0 $file = $self->_render_local_path( \@args, $data );
218 } else {
219 0 $self->log()->debug('Neither target pattern nor file set, join arguments');
220 map {
221 0 ▶ if ($_ =~ /\.\.|\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ m[\.\.|/]) 0 0 0
222 0 $self->_log_and_die("args contains invalid characters (double dot or slash)");
223 }
224 } @args;
225 0 $file = join("/", @args);
226 0 $file =~ s/[^\s\w\.\-\\\/]//g;
227 }
228  
229 0 $self->log()->debug('Filename evaluated to ' . $file);
230  
231 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
232 0 return (dirname($file), basename($file));
233 } else {
234 0 return $file;
235 }
236  
237 }
238  
239 0 ▶sub _client {
Subroutine _client: not called CC=7 SCAR=40.3
240  
241 0 my $self = shift;
242  
243 0 ▶ my $ftp = Net::FTP->new( $self->LOCATION(),
Branch: 1 branch
BranchTrueFalseTotal
my $ftp = "Net::FTP"->new($self->LOCATION, "Passive", !$self->active, "Debug", $self->debug, "Port", $self->port) or $self->_log_and_die(sprintf("Cannot connect to %s (%s)", $self->LOCATION, $@)) 0 0 0
244 'Passive' => (not $self->active()),
245 'Debug' => $self->debug(),
246 'Port' => $self->port(),
247 ) or $self->_log_and_die(sprintf("Cannot connect to %s (%s)", $self->LOCATION(), $@));
248  
249 0 ▶ if ($self->username()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->username) 0 0 0
250 0 ▶ $ftp->login($self->username(),$self->password())
Branch: 1 branch
BranchTrueFalseTotal
$ftp->login($self->username, $self->password) or $self->_log_and_die('Cannot login ' . $ftp->message) 0 0 0
251 or $self->_log_and_die("Cannot login " . $ftp->message);
252  
253 }
254  
255 0 ▶ if ($self->basedir()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->basedir) 0 0 0
256 0 $self->log()->debug('Change basedir to ' . $self->basedir());
257 0 ▶ $ftp->cwd($self->basedir()) or $self->_log_and_die("Cannot change base directory " . $ftp->message);
Branch: 1 branch
BranchTrueFalseTotal
$ftp->cwd($self->basedir) or $self->_log_and_die('Cannot change base directory ' . $ftp->message) 0 0 0
258 }
259  
260 0 ▶ if ($self->binary()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->binary) { } 0 0 0
261 0 $ftp->binary();
262 0 $self->log()->trace('Set binary transfer mode');
263 } else {
264 0 $ftp->ascii();
265 0 $self->log()->trace('Set ascii transfer mode');
266 }
267  
268 0 return $ftp;
269  
270 }
271  
272 1 1;
273 __END__
« Connector/Proxy/Net/LDAP.pm ↑ Summary Connector/Builtin/Authentication/LDAP.pm »