AnyEvent/FTP/Server/Context/Memory.pm

filter: Statementstmt 94.4% 188 / 199 Branchbran 87.1% 61 / 70 Conditioncond 55.5% 10 / 18 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 35 / 35 Podpod 12.0% 3 / 25 totaltotal 83.8% 300 / 358 SCAR 38.2
CC
44
Cov
90%
find
10.4
cmd_nlst
8.1
cmd_mkd
5.0
CRAP 45.8
« AnyEvent/FTP/Server/Context/FSRW.pm ↑ Summary AnyEvent/FTP/Client.pm »
1 package AnyEvent::FTP::Server::Context::Memory;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Path::Class::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Path::Class::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 extends 'AnyEvent::FTP::Server::Context';
11  
12 # ABSTRACT: FTP Server client context class with full read/write access
13 16 our $VERSION = '0.19'; # VERSION
14  
15  
16 16 with 'AnyEvent::FTP::Server::Role::Auth';
17 16 with 'AnyEvent::FTP::Server::Role::Help';
18 16 with 'AnyEvent::FTP::Server::Role::Old';
19 16 with 'AnyEvent::FTP::Server::Role::Type';
20 16 with 'AnyEvent::FTP::Server::Role::TransferPrep';
21  
22  
23 sub store
24 {
25 # The store for this class is global.
26 # if you wanted each connection or user
27 # to have their own store you could subclass
28 # and redefine the store method as apropriate
29 58 ▶ state $store = {};
Subroutine store: called CC=1 SCAR=0.0
Pod documented
30 58 $store;
31 }
32  
33  
34 has cwd => (
35 is => 'rw',
36 default => sub {
37 17 ▶ Path::Class::Dir->new_foreign('Unix', '/');
Subroutine __ANON__: called CC=1 SCAR=0.0
38 },
39 16 );
40  
41  
42 sub _first_index (&@)
43 {
44 41 ▶ my $f = shift;
Subroutine _first_index: called CC=3 SCAR=11.0
45 41 foreach my $i ( 0 .. $#_ )
46 {
47 122 local *_ = \$_[$i];
48 122 ▶ return $i if $f->();
Branch: 1 branch
BranchTrueFalseTotal
if &$f() 8 114 122
49 }
50 33 return -1;
51 }
52  
53 sub find
54 {
55 49 ▶ my($self, $path) = @_;
Subroutine find: called CC=10 SCAR=23.4
Pod documented
56 49 ▶ $path = Path::Class::Dir->new_foreign('Unix', $path) unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 10 39 49
57 49 ▶ $path = Path::Class::Dir->new_foreign('Unix', $self->cwd, $path)
Branch: 1 branch
BranchTrueFalseTotal
unless $path->is_absolute 22 27 49
58 unless $path->is_absolute;
59  
60 49 my $store = $self->store;
61  
62 49 ▶ return $store if $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/" 22 27 49
63  
64 27 my @list = $path->components;
65  
66 27 while(1)
67 {
68 28 ▶ my $i = _first_index { $_ eq '..' } @list;
Subroutine __ANON__: called CC=1 SCAR=0.0
69 28 ▶ last if $i == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $i == -1 27 1 28
70 1 ▶ if($i > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) { } 0 1 1
71 {
72 0 splice @list, $i-1, 2;
73 }
74 else
75 {
76 1 splice @list, $i, 1;
77 }
78 }
79  
80 27 shift @list; # shift off the root
81 27 my $top = pop @list;
82  
83 27 foreach my $part (@list)
84 {
85 19 ▶ if(exists($store->{$part}) && ref($store->{$part}) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (exists $store->{$part} and ref $store->{$part} eq 'HASH') { } 19 0 19
Condition33%1/3
exists $store->{$part} and ref $store->{$part} eq "HASH"
!ll&&!rl&&r
0019
MC/DC0%0/2
exists $store->{$part} and ref $store->{$part} eq "HASH"
exists $store->{$part} ref $store->{$part} eq "HASH"
Truth table33%1/3
A and B
A= exists $store->{$part}B= ref $store->{$part} eq "HASH"
ABresult
0X0
100
111
86 {
87 19 $store = $store->{$part};
88 }
89 else
90 {
91 0 return;
92 }
93 }
94  
95 27 ▶ if(exists $store->{$top})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $store->{$top}) { } 18 9 27
96 18 { return $store->{$top} }
97 else
98 9 { return }
99 }
100  
101  
102 sub rename_from
103 {
104 9 ▶ my($self, $value) = @_;
Subroutine rename_from: called CC=2 SCAR=6.9
Pod documented
105 9 ▶ $self->{rename_from} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 4 5 9
106 9 $self->{rename_from};
107 }
108  
109  
110 2 ▶sub help_cwd { 'CWD <sp> pathname' }
Subroutine help_cwd: called CC=1 SCAR=0.0
Pod undocumented
111  
112 sub cmd_cwd
113 {
114 6 ▶ my($self, $con, $req) = @_;
Subroutine cmd_cwd: called CC=5 SCAR=16.1
Pod undocumented
115  
116 6 my $dir = Path::Class::Dir->new_foreign('Unix', $req->args)->cleanup;
117 6 ▶ $dir = $dir->absolute($self->cwd) unless $dir->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->is_absolute 4 2 6
118  
119 6 my @list = grep !/^\.$/, $dir->components;
120  
121 6 while(1)
122 {
123 13 ▶ my $i = _first_index { $_ eq '..' } @list;
Subroutine __ANON__: called CC=1 SCAR=0.0
124 13 ▶ last if $i == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $i == -1 6 7 13
125 7 ▶ if($i > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) { } 4 3 7
126 {
127 4 splice @list, $i-1, 2;
128 }
129 else
130 {
131 3 splice @list, $i, 1;
132 }
133 }
134  
135  
136 6 $dir = Path::Class::Dir->new_foreign('Unix', @list);
137  
138 6 ▶ if(ref($self->find($dir)) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->find($dir) eq 'HASH') { } 4 2 6
139 {
140 4 $self->cwd($dir);
141 4 $con->send_response(250 => 'CWD command successful');
142 }
143 else
144 {
145 2 $con->send_response(550 => 'CWD error');
146 }
147  
148 6 $self->done;
149 }
150  
151  
152 2 ▶sub help_cdup { 'CDUP' }
Subroutine help_cdup: called CC=1 SCAR=0.0
Pod undocumented
153  
154 sub cmd_cdup
155 {
156 3 ▶ my($self, $con, $req) = @_;
Subroutine cmd_cdup: called CC=2 SCAR=6.9
Pod undocumented
157  
158 3 my $dir = $self->cwd->parent;
159  
160 3 ▶ if(ref($self->find($dir)) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->find($dir) eq 'HASH') { } 2 1 3
161 {
162 2 $self->cwd($dir);
163 2 $con->send_response(250 => 'CDUP command successful');
164 }
165 else
166 {
167 1 $con->send_response(550 => 'CDUP error');
168 }
169  
170 3 $self->done;
171 }
172  
173  
174 2 ▶sub help_pwd { 'PWD' }
Subroutine help_pwd: called CC=1 SCAR=0.0
Pod undocumented
175  
176 sub cmd_pwd
177 {
178 2 ▶ my($self, $con, $req) = @_;
Subroutine cmd_pwd: called CC=1 SCAR=0.0
Pod undocumented
179  
180 2 my $cwd = $self->cwd;
181 2 $con->send_response(257 => "\"$cwd\" is the current directory");
182 2 $self->done;
183 }
184  
185  
186 2 ▶sub help_size { 'SIZE <sp> pathname' }
Subroutine help_size: called CC=1 SCAR=0.0
Pod undocumented
187  
188 sub cmd_size
189 {
190 3 ▶ my($self, $con, $req) = @_;
Subroutine cmd_size: called CC=4 SCAR=13.9
Pod undocumented
191  
192 3 my $file = $self->find(Path::Class::File->new_foreign('Unix', $req->args));
193  
194 3 ▶ if(defined($file) && !ref($file))
Branch: 2 branches
BranchTrueFalseTotal
if (defined $file and not ref $file) { } 1 2 3
elsif (defined $file) { } 1 1 2
Condition100%3/3
defined $file and not ref $file
!ll&&!rl&&r
111
MC/DC100%2/2
defined $file and not ref $file
defined $file not ref $file
Truth table100%3/3
A and not(B)
A= defined $fileB= not ref $file
ABresult
0X0
100
111
195 {
196 1 $con->send_response(213 => length $file);
197 }
198 elsif(defined $file)
199 {
200 1 $con->send_response(550 => $req->args . ": not a regular file");
201 }
202 else
203 {
204 1 $con->send_response(550 => $req->args . ": No such file or directory");
205 }
206  
207 3 $self->done;
208 }
209  
210  
211 2 ▶sub help_mkd { 'MKD <sp> pathname' }
Subroutine help_mkd: called CC=1 SCAR=0.0
Pod undocumented
212  
213 sub cmd_mkd
214 {
215 6 ▶ my($self, $con, $req) = @_;
Subroutine cmd_mkd: called CC=5 SCAR=16.1
Pod undocumented
216  
217 6 my $path = Path::Class::Dir->new_foreign('Unix', $req->args);
218 6 my $file = $self->find($path->parent);
219 6 ▶ if($path->basename ne '' && defined($file) && ref($file) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if ($path->basename ne '' and defined $file and ref $file eq 'HASH') { } 5 1 6
Condition66%4/6
$path->basename ne "" and defined $file and ref $file eq "HASH"
!ll&&!rl&&r
105
$path->basename ne "" and defined $file
!ll&&!rl&&r
105
MC/DC33%1/3
$path->basename ne "" and defined $file and ref $file eq "HASH"
$path->basename ne "" defined $file ref $file eq "HASH"
Truth table50%2/4
A and B and C
A= $path->basename ne ""B= defined $fileC= ref $file eq "HASH"
ABCresult
0XX0
10X0
1100
1111
220 {
221 5 ▶ if(exists $file->{$path->basename})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$file{$path->basename}) { } 3 2 5
222 {
223 3 $con->send_response(521 => "\"$path\" directory exists");
224 }
225 else
226 {
227 2 $file->{$path->basename} = {};
228 2 $con->send_response(257 => "\"$path\" new directory created");
229 }
230 }
231 else
232 {
233 1 $con->send_response(550 => "MKD error");
234 }
235 6 $self->done;
236 }
237  
238  
239 2 ▶sub help_rmd { 'RMD <sp> pathname' }
Subroutine help_rmd: called CC=1 SCAR=0.0
Pod undocumented
240  
241 sub cmd_rmd
242 {
243 4 ▶ my($self, $con, $req) = @_;
Subroutine cmd_rmd: called CC=4 SCAR=14.4
Pod undocumented
244  
245 # TODO: be more picky about rmd and file or dele a directory
246 4 my $path = Path::Class::Dir->new_foreign('Unix', $req->args);
247 4 my $file = $self->find($path->parent);
248 4 ▶ if(defined($file) && ref($file) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and ref $file eq 'HASH') { } 4 0 4
Condition33%1/3
defined $file and ref $file eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $file and ref $file eq "HASH"
defined $file ref $file eq "HASH"
Truth table33%1/3
A and B
A= defined $fileB= ref $file eq "HASH"
ABresult
0X0
100
111
249 {
250 4 ▶ if(exists $file->{$path->basename})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$file{$path->basename}) { } 2 2 4
251 {
252 2 delete $file->{$path->basename};
253 2 $con->send_response(250 => "RMD command successful");
254 }
255 else
256 {
257 2 $con->send_response(550 => "$path: No such file or directory");
258 }
259 }
260 else
261 {
262 0 $con->send_response(550 => "$path: No such file or directory");
263 }
264 4 $self->done;
265  
266 }
267  
268  
269 2 ▶sub help_dele { 'DELE <sp> pathname' }
Subroutine help_dele: called CC=1 SCAR=0.0
Pod undocumented
270  
271 sub cmd_dele
272 {
273 4 ▶ my($self, $con, $req) = @_;
Subroutine cmd_dele: called CC=4 SCAR=14.4
Pod undocumented
274  
275 4 my $path = Path::Class::File->new_foreign('Unix', $req->args);
276 4 my $file = $self->find($path->parent);
277 4 ▶ if(defined($file) && ref($file) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and ref $file eq 'HASH') { } 4 0 4
Condition33%1/3
defined $file and ref $file eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $file and ref $file eq "HASH"
defined $file ref $file eq "HASH"
Truth table33%1/3
A and B
A= defined $fileB= ref $file eq "HASH"
ABresult
0X0
100
111
278 {
279 4 ▶ if(exists $file->{$path->basename})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$file{$path->basename}) { } 2 2 4
280 {
281 2 delete $file->{$path->basename};
282 2 $con->send_response(250 => "File removed");
283 }
284 else
285 {
286 2 $con->send_response(550 => "$path: No such file or directory");
287 }
288 }
289 else
290 {
291 0 $con->send_response(550 => "$path: No such file or directory");
292 }
293 4 $self->done;
294 }
295  
296  
297 2 ▶sub help_rnfr { 'RNFR <sp> pathname' }
Subroutine help_rnfr: called CC=1 SCAR=0.0
Pod undocumented
298  
299 sub cmd_rnfr
300 {
301 8 ▶ my($self, $con, $req) = @_;
Subroutine cmd_rnfr: called CC=3 SCAR=11.0
Pod undocumented
302  
303 8 my $path = Path::Class::File->new_foreign('Unix', $req->args);
304 8 my $dir = $self->find($path->parent);
305 8 ▶ if(ref($dir) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dir eq 'HASH') { } 7 1 8
306 {
307 7 ▶ if(exists $dir->{$path->basename})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$dir{$path->basename}) { } 4 3 7
308 {
309 4 $self->rename_from([$dir,$path->basename]);
310 4 $con->send_response(350 => 'File or directory exists, ready for destination name');
311 }
312 else
313 {
314 3 $con->send_response(550 => 'No such file or directory');
315 }
316 }
317 else
318 {
319 1 $con->send_response(550 => 'No such file or directory');
320 }
321  
322 8 $self->done;
323 }
324  
325  
326 2 ▶sub help_rnto { 'RNTO <sp> pathname' }
Subroutine help_rnto: called CC=1 SCAR=0.0
Pod undocumented
327  
328 sub cmd_rnto
329 {
330 5 ▶ my($self, $con, $req) = @_;
Subroutine cmd_rnto: called CC=4 SCAR=13.9
Pod undocumented
331  
332 5 my $from = $self->rename_from;
333  
334 5 ▶ unless(defined $from)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from) 1 4 5
335 {
336 1 $con->send_response(503 => 'Bad sequence of commands');
337 1 $self->done;
338 1 return;
339 }
340  
341 4 my $path = Path::Class::File->new_foreign('Unix', $req->args);
342 4 my $dir = $self->find($path->parent);
343  
344 4 ▶ if(ref($dir) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dir eq 'HASH') { } 3 1 4
345 {
346 3 ▶ if(exists $dir->{$path->basename})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$dir{$path->basename}) { } 1 2 3
347 {
348 1 $con->send_response(550 => 'File already exists');
349 }
350 else
351 {
352 2 $dir->{$path->basename} = delete $from->[0]->{$from->[1]};
353 2 $con->send_response(250 => 'Rename successful');
354 }
355 }
356 else
357 {
358 1 $con->send_response(550 => 'Rename failed');
359 }
360 4 $self->done;
361 }
362  
363  
364 2 ▶sub help_stat { 'STAT [<sp> pathname]' }
Subroutine help_stat: called CC=1 SCAR=0.0
Pod undocumented
365  
366 sub cmd_stat
367 {
368 6 ▶ my($self, $con, $req) = @_;
Subroutine cmd_stat: called CC=3 SCAR=11.0
Pod undocumented
369  
370 6 my $file = $self->find($req->args);
371  
372 6 ▶ if(defined $file)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 4 2 6
373 {
374 4 ▶ if(ref($file) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'HASH') { } 3 1 4
375 {
376 3 $con->send_response(211 => "It's a directory");
377 }
378 else
379 {
380 1 $con->send_response(211 => "It's a file");
381 }
382 }
383 else
384 {
385 2 $con->send_response(450 => 'No such file or directory');
386 }
387 6 $self->done;
388 }
389  
390  
391 2 ▶sub help_nlst { 'NLST [<sp> (pathname)]' }
Subroutine help_nlst: called CC=1 SCAR=0.0
Pod undocumented
392  
393 sub cmd_nlst
394 {
395 5 ▶ my($self, $con, $req) = @_;
Subroutine cmd_nlst: called CC=7 SCAR=20.9
Pod undocumented
396  
397 5 my $dir = $req->args;
398  
399 5 ▶ unless(defined $self->data)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 5 5
400 {
401 0 $con->send_response(425 => 'Unable to build data connection');
402 0 return;
403 }
404  
405 5 eval {
406 5 $con->send_response(150 => "Opening ASCII mode data connection for file list");
407 5 my @list;
408 5 ▶ if($dir)
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 4 1 5
409 {
410 4 my $h = $self->find($dir);
411 4 ▶ if(ref($h) eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h eq 'HASH') { } 2 2 4
412 {
413 2 $dir = Path::Class::Dir->new_foreign('Unix', $dir);
414 2 @list = map { $dir->file($_) } sort keys %$h;
415 }
416 else
417 {
418 2 $dir = Path::Class::File->new_foreign('Unix', $dir);
419 2 @list = "$dir";
420 }
421 }
422 else
423 {
424 1 my $h = $self->find($self->cwd);
425 1 ▶ die 'unable to find cwd' unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 0 1 1
426 1 @list = sort keys %$h;
427 }
428 5 $self->data->push_write(join '', map { $_ . "\015\012" } @list);
429 5 $self->data->push_shutdown;
430 5 $con->send_response(226 => 'Transfer complete');
431 };
432 5 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 5 5
433 {
434 0 warn $error;
435 0 ▶ if(eval { $error->can('errno') })
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $error->can('errno') } }) { } 0 0 0
436 0 { $con->send_response(550 => $error->errno) }
437 else
438 0 { $con->send_response(550 => 'Internal error') }
439 };
440 5 $self->clear_data;
441 5 $self->done;
442 }
443  
444 16 1;
445  
446  
447 # TODO: cmd_retr
448 # TODO: cmd_list
449 # TODO: cmd_stor
450 # TODO: cmd_appe
451 # TODO: cmd_stou
452  
453 __END__
« AnyEvent/FTP/Server/Context/FSRW.pm ↑ Summary AnyEvent/FTP/Client.pm »