AnyEvent/FTP/Server/Context/FSRW.pm

filter: Statementstmt 78.0% 185 / 237 Branchbran 51.6% 32 / 62 Conditioncond 40.0% 6 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 39 / 39 Podpod 0.0% 0 / 12 totaltotal 70.5% 264 / 374 SCAR 42.9
CC
38
Cov
71%
cmd_retr
13.4
cmd_stou
9.2
cmd_list
7.0
CRAP 73.1
« Test/AnyEventFTPServer.pm ↑ Summary AnyEvent/FTP/Server/Context/Memory.pm »
1 package AnyEvent::FTP::Server::Context::FSRW;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use File::ShareDir::Dist qw( dist_share );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use File::Which qw( which );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Capture::Tiny qw( capture );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 extends 'AnyEvent::FTP::Server::Context::FS';
14  
15 # ABSTRACT: FTP Server client context class with full read/write access
16 12 our $VERSION = '0.19'; # VERSION
17  
18  
19 12 with 'AnyEvent::FTP::Server::Role::TransferPrep';
20  
21  
22 sub _layer
23 {
24 46 ▶ $_[0]->type eq 'A' ? $_[0]->ascii_layer : ':raw';
Subroutine _layer: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->type eq 'A' ? : 6 40 46
25 }
26  
27 2 ▶sub help_retr { 'RETR <sp> pathname' }
Subroutine help_retr: called CC=1 SCAR=0.0
Pod undocumented
28  
29 sub cmd_retr
30 {
31 21 ▶ my($self, $con, $req) = @_;
Subroutine cmd_retr: called CC=9 SCAR=26.0
Pod undocumented
32  
33 21 my $fn = $req->args;
34  
35 21 ▶ unless(defined $self->data)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 21 21
36 {
37 0 $con->send_response(425 => 'Unable to build data connection');
38 0 return;
39 }
40  
41 21 eval {
42 12 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 21 local $CWD = $self->cwd;
44  
45 21 ▶ if(-f $fn)
Branch: 2 branches
BranchTrueFalseTotal
if (-f $fn) { } 21 0 21
elsif (-e $fn and not -d $fn) { } 0 0 0
46 {
47 # TODO: re-write so that this does not blocks
48 21 ▶ my $type = $self->type eq 'A' ? 'ASCII' : 'Binary';
Branch: 1 branch
BranchTrueFalseTotal
$self->type eq 'A' ? : 3 18 21
49 21 my $size = -s $fn;
50 21 $con->send_response(150 => "Opening $type mode data connection for $fn ($size bytes)");
51 21 open my $fh, '<', $fn;
52 21 binmode $fh, $self->_layer;
53 21 ▶ seek $fh, $self->restart_offset, 0 if $self->restart_offset;
Branch: 1 branch
BranchTrueFalseTotal
if $self->restart_offset 4 17 21
54 21 $self->data->push_write(do { local $/; <$fh> });
55 21 close $fh;
56 21 $self->data->push_shutdown;
57 21 $con->send_response(226 => 'Transfer complete');
58 }
59 ▶ elsif(-e $fn && !-d $fn)
Condition0%0/3
-e $fn and not -d $fn
!ll&&!rl&&r
000
MC/DC0%0/2
-e $fn and not -d $fn
-e $fn not -d $fn
Truth table0%0/3
A and not(B)
A= -e $fnB= not -d $fn
ABresult
0X0
100
111
60 {
61 0 $con->send_response(550 => 'Permission denied');
62 }
63 else
64 {
65 0 $con->send_response(550 => 'No such file');
66 }
67 };
68 21 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 21 21
69 {
70 0 warn $error;
71 0 ▶ if(eval { $error->can('errno') })
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $error->can('errno') } }) { } 0 0 0
72 0 { $con->send_response(550 => $error->errno) }
73 else
74 0 { $con->send_response(550 => 'Internal error') }
75 };
76 21 $self->clear_data;
77 21 $self->done;
78 }
79  
80  
81 2 ▶sub help_nlst { 'NLST [<sp> (pathname)]' }
Subroutine help_nlst: called CC=1 SCAR=0.0
Pod undocumented
82  
83 sub cmd_nlst
84 {
85 11 ▶ my($self, $con, $req) = @_;
Subroutine cmd_nlst: called CC=6 SCAR=19.3
Pod undocumented
86  
87 11 ▶ my $dir = $req->args || '.';
Condition100%2/2
$req->args || '.'
l!l
29
MC/DC100%1/1
$req->args || '.'
$req->args
Truth table100%2/2
A
A= $req->args
Aresult
11
00
88  
89 11 ▶ unless(defined $self->data)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 11 11
90 {
91 0 $con->send_response(425 => 'Unable to build data connection');
92 0 return;
93 }
94  
95 11 eval {
96 12 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 11 local $CWD = $self->cwd;
98  
99 11 $con->send_response(150 => "Opening ASCII mode data connection for file list");
100 11 my $dh;
101 11 opendir $dh, $dir;
102 my @list =
103 11 ▶ map { $req->args ? join('/', $dir, $_) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$req->args ? : 6 15 21
104 sort
105 grep !/^\.\.?$/,
106 readdir $dh;
107 11 closedir $dh;
108 11 $self->data->push_write(join '', map { $_ . "\015\012" } @list);
109 11 $self->data->push_shutdown;
110 11 $con->send_response(226 => 'Transfer complete');
111 };
112 11 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 11 11
113 {
114 0 warn $error;
115 0 ▶ if(eval { $error->can('errno') })
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $error->can('errno') } }) { } 0 0 0
116 0 { $con->send_response(550 => $error->errno) }
117 else
118 0 { $con->send_response(550 => 'Internal error') }
119 };
120 11 $self->clear_data;
121 11 $self->done;
122 }
123  
124  
125 2 ▶sub help_list { 'LIST [<sp> pathname]' }
Subroutine help_list: called CC=1 SCAR=0.0
Pod undocumented
126  
127 sub cmd_list
128 {
129 8 ▶ my($self, $con, $req) = @_;
Subroutine cmd_list: called CC=6 SCAR=19.5
Pod undocumented
130  
131 8 ▶ my $dir = $req->args || '.';
Condition100%2/2
$req->args || '.'
l!l
26
MC/DC100%1/1
$req->args || '.'
$req->args
Truth table100%2/2
A
A= $req->args
Aresult
11
00
132 8 ▶ $dir = '.' if $dir eq '-l';
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "-l" 0 8 8
133  
134 8 ▶ unless(defined $self->data)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 8 8
135 {
136 0 $con->send_response(425 => 'Unable to build data connection');
137 0 return;
138 }
139  
140 8 eval {
141 12 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 8 my @cmd = _shared_cmd('ls', '-l', $dir);
144  
145 8 local $CWD = $self->cwd;
146  
147 8 $con->send_response(150 => "Opening ASCII mode data connection for file list");
148 8 my $dh;
149 8 opendir $dh, $dir;
150  
151 8 ▶ $self->data->push_write(join "\015\012", split /\n/, scalar capture { system @cmd });
Subroutine __ANON__: called CC=1 SCAR=0.0
152 8 closedir $dh;
153 8 $self->data->push_write("\015\012");
154 8 $self->data->push_shutdown;
155 8 $con->send_response(226 => 'Transfer complete');
156 };
157 8 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 8 8
158 {
159 0 warn $error;
160 0 ▶ if(eval { $error->can('errno') })
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $error->can('errno') } }) { } 0 0 0
161 0 { $con->send_response(550 => $error->errno) }
162 else
163 0 { $con->send_response(550 => 'Internal error') }
164 };
165 8 $self->clear_data;
166 8 $self->done;
167 }
168  
169  
170 2 ▶sub help_stor { 'STOR <sp> pathname' }
Subroutine help_stor: called CC=1 SCAR=0.0
Pod undocumented
171  
172 sub cmd_stor
173 {
174 15 ▶ my($self, $con, $req) = @_;
Subroutine cmd_stor: called CC=5 SCAR=17.7
Pod undocumented
175  
176 15 my $fn = $req->args;
177  
178 15 ▶ unless(defined $self->data)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 15 15
179 {
180 0 $con->send_response(425 => 'Unable to build data connection');
181 0 return;
182 }
183  
184 15 eval {
185 12 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 15 local $CWD = $self->cwd;
187  
188 15 ▶ my $type = $self->type eq 'A' ? 'ASCII' : 'Binary';
Branch: 1 branch
BranchTrueFalseTotal
$self->type eq 'A' ? : 1 14 15
189 15 $con->send_response(150 => "Opening $type mode data connection for $fn");
190  
191 15 open my $fh, '>', $fn;
192 15 binmode $fh, $self->_layer;
193 $self->data->on_read(sub {
194 $self->data->push_read(sub {
195 30 ▶ print $fh $_[0]{rbuf};
Subroutine __ANON__: called CC=1 SCAR=0.0
196 30 $_[0]{rbuf} = '';
197 15 ▶ });
Subroutine __ANON__: called CC=1 SCAR=0.0
198 15 });
199 $self->data->on_error(sub {
200 15 ▶ close $fh;
Subroutine __ANON__: called CC=1 SCAR=0.0
201 15 $self->data->push_shutdown;
202 15 $con->send_response(226 => 'Transfer complete');
203 15 $self->clear_data;
204 15 $self->done;
205 15 });
206 };
207 15 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 15 15
208 {
209 0 warn $error;
210 0 ▶ if(eval { $error->can('errno') })
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $error->can('errno') } }) { } 0 0 0
211 0 { $con->send_response(550 => $error->errno) }
212 else
213 0 { $con->send_response(550 => 'Internal error') }
214 0 $self->clear_data;
215 0 $self->done;
216 };
217 }
218  
219  
220 2 ▶sub help_appe { 'APPE <sp> pathname' }
Subroutine help_appe: called CC=1 SCAR=0.0
Pod undocumented
221  
222 sub cmd_appe
223 {
224 7 ▶ my($self, $con, $req) = @_;
Subroutine cmd_appe: called CC=5 SCAR=17.7
Pod undocumented
225  
226 7 my $fn = $req->args;
227  
228 7 ▶ unless(defined $self->data)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 7 7
229 {
230 0 $con->send_response(425 => 'Unable to build data connection');
231 0 return;
232 }
233  
234 7 eval {
235 12 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 7 local $CWD = $self->cwd;
237  
238 7 ▶ my $type = $self->type eq 'A' ? 'ASCII' : 'Binary';
Branch: 1 branch
BranchTrueFalseTotal
$self->type eq 'A' ? : 1 6 7
239 7 $con->send_response(150 => "Opening $type mode data connection for $fn");
240  
241 7 open my $fh, '>>', $fn;
242 7 binmode $fh, $self->_layer;
243 $self->data->on_read(sub {
244 $self->data->push_read(sub {
245 16 ▶ print $fh $_[0]{rbuf};
Subroutine __ANON__: called CC=1 SCAR=0.0
246 16 $_[0]{rbuf} = '';
247 7 ▶ });
Subroutine __ANON__: called CC=1 SCAR=0.0
248 7 });
249 $self->data->on_error(sub {
250 7 ▶ close $fh;
Subroutine __ANON__: called CC=1 SCAR=0.0
251 7 $self->data->push_shutdown;
252 7 $con->send_response(226 => 'Transfer complete');
253 7 $self->clear_data;
254 7 $self->done;
255 7 });
256 };
257 7 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 7 7
258 {
259 0 warn $error;
260 0 ▶ if(eval { $error->can('errno') })
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $error->can('errno') } }) { } 0 0 0
261 0 { $con->send_response(550 => $error->errno) }
262 else
263 0 { $con->send_response(550 => 'Internal error') }
264 0 $self->clear_data;
265 0 $self->done;
266 };
267 }
268  
269  
270 2 ▶sub help_stou { 'STOU (store unique filename)' }
Subroutine help_stou: called CC=1 SCAR=0.0
Pod undocumented
271  
272 sub cmd_stou
273 {
274 3 ▶ my($self, $con, $req) = @_;
Subroutine cmd_stou: called CC=7 SCAR=22.2
Pod undocumented
275  
276 3 my $fn = $req->args;
277  
278 3 ▶ unless(defined $self->data)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 3 3
279 {
280 0 $con->send_response(425 => 'Unable to build data connection');
281 0 return;
282 }
283  
284 3 eval {
285 12 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 3 local $CWD = $self->cwd;
287  
288 3 my $fh;
289  
290 3 ▶ if($fn && ! -e $fn)
Branch: 1 branch
BranchTrueFalseTotal
if ($fn and not -e $fn) { } 0 3 3
Condition33%1/3
$fn and not -e $fn
!ll&&!rl&&r
300
MC/DC0%0/2
$fn and not -e $fn
$fn not -e $fn
Truth table33%1/3
A and not(B)
A= $fnB= not -e $fn
ABresult
0X0
100
111
291 {
292 0 open $fh, '>', $fn;
293 }
294 else
295 {
296 3 ($fh,$fn) = tempfile( "aefXXXXXX", TMPDIR => 0 )
297 }
298  
299 3 ▶ my $type = $self->type eq 'A' ? 'ASCII' : 'Binary';
Branch: 1 branch
BranchTrueFalseTotal
$self->type eq 'A' ? : 1 2 3
300 3 $con->send_response(150 => "FILE: $fn");
301  
302 3 binmode $fh, $self->_layer;
303 $self->data->on_read(sub {
304 $self->data->push_read(sub {
305 6 ▶ print $fh $_[0]{rbuf};
Subroutine __ANON__: called CC=1 SCAR=0.0
306 6 $_[0]{rbuf} = '';
307 3 ▶ });
Subroutine __ANON__: called CC=1 SCAR=0.0
308 3 });
309 $self->data->on_error(sub {
310 3 ▶ close $fh;
Subroutine __ANON__: called CC=1 SCAR=0.0
311 3 $self->data->push_shutdown;
312 3 $con->send_response(226 => 'Transfer complete');
313 3 $self->clear_data;
314 3 $self->done;
315 3 });
316 };
317 3 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 3 3
318 {
319 0 warn $error;
320 0 ▶ if(eval { $error->can('errno') })
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $error->can('errno') } }) { } 0 0 0
321 0 { $con->send_response(550 => $error->errno) }
322 else
323 0 { $con->send_response(550 => 'Internal error') }
324 0 $self->clear_data;
325 0 $self->done;
326 };
327 }
328  
329 {
330 12 state $always_use_bundled_cmd = $ENV{ANYEVENT_FTP_BUNDLED_CMD};
331 12 my %shared;
332 sub _shared_cmd
333 {
334 9 ▶ my ($cmd, @args) = @_;
Subroutine _shared_cmd: called CC=5 SCAR=18.1
335  
336 9 ▶ unless (defined $shared{$cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shared{$cmd}) 2 7 9
337 2 my $which = which $cmd;
338 2 ▶ if ($which && !$always_use_bundled_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which and not $always_use_bundled_cmd) { } 2 0 2
Condition33%1/3
$which and not $always_use_bundled_cmd
!ll&&!rl&&r
002
MC/DC0%0/2
$which and not $always_use_bundled_cmd
$which not $always_use_bundled_cmd
Truth table33%1/3
A and not(B)
A= $whichB= not $always_use_bundled_cmd
ABresult
0X0
100
111
339 2 $shared{$cmd} = [ $which ];
340 }
341 else {
342 0 ▶ $shared{$cmd} = [
Condition0%0/2
&dist_share('AnyEvent-FTP') || die('unable to find share directory')
l!l
00
MC/DC0%0/1
&dist_share('AnyEvent-FTP') || die('unable to find share directory')
&dist_share('AnyEvent-FTP')
Truth table0%0/2
A
A= &dist_share('AnyEvent-FTP')
Aresult
11
00
343 $^X, # use the same Perl
344 File::Spec->catfile((dist_share('AnyEvent-FTP') or die "unable to find share directory") , 'ppt', "$cmd.pl"),
345 ];
346 }
347 }
348  
349 9 return @{ $shared{$cmd} }, @args;
350 }
351 }
352  
353 12 1;
354  
355 __END__
« Test/AnyEventFTPServer.pm ↑ Summary AnyEvent/FTP/Server/Context/Memory.pm »