blib/lib/File/Find/Object.pm

filter: Statementstmt 99.2% 252 / 254 Branchbran 84.6% 44 / 52 Conditioncond 88.8% 8 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 57 / 57 Podpod 100% 8 / 8 totaltotal 96.6% 372 / 385 CC 31 SCAR 6.7
CC
31
Cov
97%
_move_next
4.0
next_obj
4.0
_become_default
3.0
CRAP 2.0
« blib/lib/File/Find/Object/PathComp.pm ↑ Summary blib/lib/File/Find/Object/Result.pm »
1 package File::Find::Object;
2 6 $File::Find::Object::VERSION = '0.3.8';
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package File::Find::Object::DeepPath;
7 6 $File::Find::Object::DeepPath::VERSION = '0.3.8';
8 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 ▶use parent 'File::Find::Object::PathComp';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub new
20 {
21 53 ▶ my ( $class, $top, $from ) = @_;
Subroutine new: called CC=3 SCAR=11.0
22  
23 53 my $self = {};
24 53 bless $self, $class;
25  
26 53 $self->_stat_ret( $top->_top_stat_copy() );
27  
28 53 my $find = { %{ $from->_inodes() } };
29 53 ▶ if ( my $inode = $self->_inode )
Branch: 1 branch
BranchTrueFalseTotal
if (my $inode = $self->_inode) 53 0 53
30 {
31 $find->{ join( ",", $self->_dev(), $inode ) } =
32 53 $#{ $top->_dir_stack() };
33 }
34 53 $self->_set_inodes($find);
35  
36 53 $self->_last_dir_scanned(undef);
37  
38 53 $top->_fill_actions($self);
39  
40 53 push @{ $top->_curr_comps() }, "";
41  
42 53 ▶ return $top->_open_dir() ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$top->_open_dir ? : 53 0 53
43 }
44  
45 sub _move_next
46 {
47 112 ▶ my ( $self, $top ) = @_;
Subroutine _move_next: called CC=2 SCAR=6.9
48  
49 112 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_curr_file($top->_current_father->_next_traverse_to)) { } 62 50 112
50 defined(
51 $self->_curr_file( $top->_current_father()->_next_traverse_to() )
52 )
53 )
54 {
55 62 $top->_curr_comps()->[-1] = $self->_curr_file();
56 62 $top->_calc_curr_path();
57  
58 62 $top->_fill_actions($self);
59 62 $top->_mystat();
60  
61 62 return 1;
62 }
63 else
64 {
65 50 return 0;
66 }
67 }
68  
69 package File::Find::Object::TopPath;
70 6 $File::Find::Object::TopPath::VERSION = '0.3.8';
71 6 ▶use parent 'File::Find::Object::PathComp';
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 sub new
74 {
75 14 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
76 14 my $top = shift;
77  
78 14 my $self = {};
79 14 bless $self, $class;
80  
81 14 $top->_fill_actions($self);
82  
83 14 return $self;
84 }
85  
86 sub _move_to_next_target
87 {
88 19 ▶ my $self = shift;
Subroutine _move_to_next_target: called CC=1 SCAR=0.0
89 19 my $top = shift;
90  
91 19 my $target = $self->_curr_file( $top->_calc_next_target() );
92 19 @{ $top->_curr_comps() } = ($target);
93 19 $top->_calc_curr_path();
94  
95 19 return $target;
96 }
97  
98 sub _move_next
99 {
100 28 ▶ my $self = shift;
Subroutine _move_next: called CC=4 SCAR=13.9
101 28 my $top = shift;
102  
103 28 ▶ while ( $top->_increment_target_index() )
Branch: 1 branch
BranchTrueFalseTotal
if ($top->_increment_target_index) 19 11 30
104 {
105 19 ▶ if ( -e $self->_move_to_next_target($top) )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->_move_to_next_target($top)) 17 2 19
106 {
107 17 $top->_fill_actions($self);
108 17 $top->_mystat();
109 17 $self->_stat_ret( $top->_top_stat_copy() );
110 17 $top->_dev( $self->_dev );
111  
112 17 my $inode = $self->_inode();
113 17 ▶ $self->_set_inodes(
Branch: 1 branch
BranchTrueFalseTotal
$inode == 0 ? : 0 17 17
114 ( $inode == 0 )
115 ? {}
116 : {
117 join( ",", $self->_dev(), $inode ) => 0,
118 },
119 );
120  
121 17 return 1;
122 }
123 }
124  
125 11 return 0;
126 }
127  
128 package File::Find::Object;
129  
130 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 6 ▶use parent 'File::Find::Object::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 6 ▶use File::Find::Object::Result ();
Subroutine BEGIN: called CC=1 SCAR=0.0
136  
137 6 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
138 6 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 sub _get_options_ids
141 {
142 20 ▶ my $class = shift;
Subroutine _get_options_ids: called CC=1 SCAR=0.0
143 return [
144 20 qw(
145 callback
146 depth
147 filter
148 followlink
149 nocrossfs
150 )
151 ];
152 }
153  
154 # _curr_comps are the components (comps) of the master object's current path.
155 # _curr_path is the concatenated path itself.
156  
157 use Class::XSAccessor accessors => {
158 (
159 108 map { $_ => $_ } (
160 qw(
161 _check_subdir_h
162 _curr_comps
163 _current
164 _curr_path
165 _def_actions
166 _dev
167 _dir_stack
168 item_obj
169 _target_index
170 _targets
171 _top_is_dir
172 _top_is_link
173 _top_stat
174 ),
175 6 @{ __PACKAGE__->_get_options_ids() }
176 )
177 )
178 6 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180 6 __PACKAGE__->_make_copy_methods(
181 [
182 qw(
183 _top_stat
184 )
185 ]
186 );
187  
188 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 sub new
191 {
192 14 ▶ my ( $class, $options, @targets ) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
193  
194 # The *existence* of an _st key inside the struct
195 # indicates that the stack is full.
196 # So now it's empty.
197 14 my $tree = {
198 _dir_stack => [],
199 _curr_comps => [],
200 };
201  
202 14 bless( $tree, $class );
203  
204 14 foreach my $opt ( @{ $tree->_get_options_ids() } )
205 {
206 70 $tree->$opt( $options->{$opt} );
207 }
208  
209 14 $tree->_gen_check_subdir_helper();
210  
211 14 $tree->_targets( \@targets );
212 14 $tree->_target_index(-1);
213  
214 14 $tree->_calc_default_actions();
215  
216 14 push @{ $tree->_dir_stack() },
217 $tree->_current( File::Find::Object::TopPath->new($tree) );
218  
219 14 $tree->_last_dir_scanned(undef);
220  
221 14 return $tree;
222 }
223  
224 sub _curr_not_a_dir
225 {
226 155 ▶ return !shift->_top_is_dir();
Subroutine _curr_not_a_dir: called CC=1 SCAR=0.0
227 }
228  
229 # Calculates _curr_path from $self->_curr_comps().
230 # Must be called whenever _curr_comps is modified.
231 sub _calc_curr_path
232 {
233 81 ▶ my $self = shift;
Subroutine _calc_curr_path: called CC=1 SCAR=0.0
234  
235 81 $self->_curr_path( File::Spec->catfile( @{ $self->_curr_comps() } ) );
236  
237 81 return;
238 }
239  
240 sub _calc_current_item_obj
241 {
242 79 ▶ my $self = shift;
Subroutine _calc_current_item_obj: called CC=2 SCAR=6.9
243  
244 79 my @comps = @{ $self->_curr_comps() };
245  
246 79 my $ret = {
247 path => scalar( $self->_curr_path() ),
248 dir_components => \@comps,
249 base => shift(@comps),
250 stat_ret => scalar( $self->_top_stat_copy() ),
251 is_file => scalar( -f _ ),
252 is_dir => scalar( -d _ ),
253 is_link => $self->_top_is_link(),
254 };
255  
256 79 ▶ if ( $self->_curr_not_a_dir() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_curr_not_a_dir) 23 56 79
257 {
258 23 $ret->{basename} = pop(@comps);
259 }
260  
261 79 return bless $ret, "File::Find::Object::Result";
262 }
263  
264 sub next_obj
265 {
266 90 ▶ my $self = shift;
Subroutine next_obj: called CC=4 SCAR=13.9
Pod documented
267  
268 90 ▶ until (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_process_current or not $self->_master_move_to_next and $self->_me_die) 129 90 219
Condition100%6/6
$self->_process_current or not $self->_master_move_to_next and $self->_me_die
l!l&&r!l&&!r
7911129
not $self->_master_move_to_next and $self->_me_die
!ll&&!rl&&r
795011
MC/DC66%2/3
$self->_process_current or not $self->_master_move_to_next and $self->_me_die
$self->_process_current not $self->_master_move_to_next $self->_me_die
Truth table75%3/4
A or not(B) and C
A= $self->_process_currentB= not $self->_master_move_to_nextC= $self->_me_die
ABCresult
1XX1
0111
00X0
0100
269 $self->_process_current || ( ( !$self->_master_move_to_next() )
270 && $self->_me_die() )
271 )
272 {
273 # Do nothing
274 }
275  
276 90 return $self->item_obj();
277 }
278  
279 sub next
280 {
281 76 ▶ my $self = shift;
Subroutine next: called CC=1 SCAR=0.0
Pod documented
282  
283 76 $self->next_obj();
284  
285 76 return $self->item();
286 }
287  
288 sub item
289 {
290 76 ▶ my $self = shift;
Subroutine item: called CC=2 SCAR=6.9
Pod documented
291  
292 76 ▶ return $self->item_obj() ? $self->item_obj()->path() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->item_obj ? : 67 9 76
293 }
294  
295 sub _current_father
296 {
297 112 ▶ return shift->_dir_stack->[-2];
Subroutine _current_father: called CC=1 SCAR=0.0
298 }
299  
300 sub _increment_target_index
301 {
302 30 ▶ my $self = shift;
Subroutine _increment_target_index: called CC=1 SCAR=0.0
303 30 $self->_target_index( $self->_target_index() + 1 );
304  
305 30 return ( $self->_target_index() < scalar( @{ $self->_targets() } ) );
306 }
307  
308 sub _calc_next_target
309 {
310 19 ▶ my $self = shift;
Subroutine _calc_next_target: called CC=2 SCAR=7.1
311  
312 19 my $target = $self->_targets()->[ $self->_target_index() ];
313  
314 19 ▶ return defined($target) ? File::Spec->canonpath($target) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $target ? : 19 0 19
315 }
316  
317 sub _master_move_to_next
318 {
319 140 ▶ my $self = shift;
Subroutine _master_move_to_next: called CC=1 SCAR=0.0
320  
321 140 return $self->_current()->_move_next($self);
322 }
323  
324 sub _me_die
325 {
326 61 ▶ my $self = shift;
Subroutine _me_die: called CC=2 SCAR=6.9
327  
328 61 ▶ if ( exists( $self->{_st} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_st"}) 50 11 61
329 {
330 50 return $self->_become_default();
331 }
332  
333 11 $self->item_obj( undef() );
334 11 return 1;
335 }
336  
337 sub _become_default
338 {
339 50 ▶ my $self = shift;
Subroutine _become_default: called CC=3 SCAR=11.1
340  
341 50 my $st = $self->_dir_stack();
342  
343 50 pop(@$st);
344 50 $self->_current( $st->[-1] );
345 50 pop( @{ $self->_curr_comps() } );
346  
347 50 ▶ if ( @$st == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@$st == 1) { } 13 37 50
348 {
349 13 delete( $self->{_st} );
350 }
351 else
352 {
353 # If depth is false, then we no longer need the _curr_path
354 # of the directories above the previously-set value, because we
355 # already traversed them.
356 37 ▶ if ( $self->depth() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->depth) 0 37 37
357 {
358 0 $self->_calc_curr_path();
359 }
360 }
361  
362 50 return 0;
363 }
364  
365 sub _calc_default_actions
366 {
367 14 ▶ my $self = shift;
Subroutine _calc_default_actions: called CC=3 SCAR=11.0
368  
369 14 ▶ my @calc_obj =
Branch: 1 branch
BranchTrueFalseTotal
$self->callback ? : 2 12 14
370 $self->callback()
371 ? (qw(_run_cb))
372 : (qw(_set_obj));
373  
374 14 my @rec = qw(_recurse);
375  
376 14 ▶ $self->_def_actions(
Branch: 1 branch
BranchTrueFalseTotal
$self->depth ? : 0 14 14
377 [
378 $self->depth()
379 ? ( @rec, @calc_obj )
380 : ( @calc_obj, @rec )
381 ]
382 );
383  
384 14 return;
385 }
386  
387 sub _fill_actions
388 {
389 146 ▶ my $self = shift;
Subroutine _fill_actions: called CC=1 SCAR=0.0
390 146 my $other = shift;
391  
392 146 $other->_actions( [ @{ $self->_def_actions() } ] );
393  
394 146 return;
395 }
396  
397 sub _mystat
398 {
399 79 ▶ my $self = shift;
Subroutine _mystat: called CC=2 SCAR=6.9
400  
401 79 $self->_top_stat( [ lstat( $self->_curr_path() ) ] );
402  
403 79 $self->_top_is_dir( scalar( -d _ ) );
404  
405 79 ▶ if ( $self->_top_is_link( scalar( -l _ ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_top_is_link(scalar -l(_))) 2 77 79
406 {
407 2 stat( $self->_curr_path() );
408 2 $self->_top_is_dir( scalar( -d _ ) );
409 }
410  
411 79 return "SKIP";
412 }
413  
414 sub _next_action
415 {
416 228 ▶ my $self = shift;
Subroutine _next_action: called CC=1 SCAR=0.0
417  
418 228 return shift( @{ $self->_current->_actions() } );
419 }
420  
421 sub _check_process_current
422 {
423 219 ▶ my $self = shift;
Subroutine _check_process_current: called CC=2 SCAR=7.1
424  
425 219 ▶ return ( defined( $self->_current->_curr_file() )
Condition66%2/3
defined $self->_current->_curr_file && $self->_filter_wrapper
!ll&&!rl&&r
140205
MC/DC50%1/2
defined $self->_current->_curr_file && $self->_filter_wrapper
defined $self->_current->_curr_file $self->_filter_wrapper
Truth table66%2/3
A && B
A= defined $self->_current->_curr_fileB= $self->_filter_wrapper
ABresult
0X0
100
111
426 && $self->_filter_wrapper() );
427 }
428  
429 # Return true if there is something next
430 sub _process_current
431 {
432 219 ▶ my $self = shift;
Subroutine _process_current: called CC=2 SCAR=6.9
433  
434 219 ▶ if ( !$self->_check_process_current() )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_check_process_current) { } 14 205 219
435 {
436 14 return 0;
437 }
438 else
439 {
440 205 return $self->_process_current_actions();
441 }
442 }
443  
444 sub _set_obj
445 {
446 79 ▶ my $self = shift;
Subroutine _set_obj: called CC=1 SCAR=0.0
447  
448 79 $self->item_obj( $self->_calc_current_item_obj() );
449  
450 79 return 1;
451 }
452  
453 sub _run_cb
454 {
455 10 ▶ my $self = shift;
Subroutine _run_cb: called CC=1 SCAR=0.0
456  
457 10 $self->_set_obj();
458  
459 10 $self->callback()->( $self->_curr_path() );
460  
461 10 return 1;
462 }
463  
464 sub _process_current_actions
465 {
466 205 ▶ my $self = shift;
Subroutine _process_current_actions: called CC=3 SCAR=11.0
467  
468 205 ▶ while ( my $action = $self->_next_action() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $self->_next_action) 155 73 228
469 {
470 155 my $status = $self->$action();
471  
472 155 ▶ if ( $status ne "SKIP" )
Branch: 1 branch
BranchTrueFalseTotal
if ($status ne "SKIP") 132 23 155
473 {
474 132 return $status;
475 }
476 }
477  
478 73 return 0;
479 }
480  
481 sub _recurse
482 {
483 76 ▶ my $self = shift;
Subroutine _recurse: called CC=2 SCAR=6.9
484  
485 76 ▶ $self->_check_subdir()
Branch: 1 branch
BranchTrueFalseTotal
$self->_check_subdir or return "SKIP" 23 53 76
486 or return "SKIP";
487  
488 53 push @{ $self->_dir_stack() },
489 $self->_current(
490 File::Find::Object::DeepPath->new( $self, $self->_current() ) );
491  
492 53 $self->{_st} = 1;
493  
494 53 return 0;
495 }
496  
497 sub _filter_wrapper
498 {
499 205 ▶ my $self = shift;
Subroutine _filter_wrapper: called CC=2 SCAR=7.2
500  
501 205 ▶ return defined( $self->filter() )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->filter ? : 0 205 205
502 ? $self->filter()->( $self->_curr_path() )
503 : 1;
504 }
505  
506 sub _check_subdir
507 {
508 76 ▶ my $self = shift;
Subroutine _check_subdir: called CC=2 SCAR=6.9
509  
510 # If current is not a directory always return 0, because we may
511 # be asked to traverse single-files.
512  
513 76 ▶ if ( $self->_curr_not_a_dir() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_curr_not_a_dir) { } 22 54 76
514 {
515 22 return 0;
516 }
517 else
518 {
519 54 return $self->_check_subdir_h()->($self);
520 }
521 }
522  
523 sub _warn_about_loop
524 {
525 1 ▶ my $self = shift;
Subroutine _warn_about_loop: called CC=1 SCAR=0.0
526 1 my $component_idx = shift;
527  
528 # Don't pass strings directly to the format.
529 # Instead - use %s
530 # This was a security problem.
531 warn(
532 sprintf(
533 "Avoid loop %s => %s\n",
534 File::Spec->catdir(
535 1 @{ $self->_curr_comps() }[ 0 .. $component_idx ]
536 ),
537 $self->_curr_path(),
538 )
539 );
540  
541 1 return;
542 }
543  
544 sub _is_loop
545 {
546 40 ▶ my $self = shift;
Subroutine _is_loop: called CC=2 SCAR=6.9
547  
548 40 my $key = join( ",", @{ $self->_top_stat() }[ 0, 1 ] );
549 40 my $lookup = $self->_current->_inodes;
550  
551 40 ▶ if ( exists( $lookup->{$key} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup->{$key}) { } 1 39 40
552 {
553 1 $self->_warn_about_loop( $lookup->{$key} );
554 1 return 1;
555 }
556 else
557 {
558 39 return;
559 }
560 }
561  
562 # We eval "" the helper of check_subdir because the conditions that
563 # affect the checks are instance-wide and constant and so we can
564 # determine how the code should look like.
565  
566 sub _gen_check_subdir_helper
567 {
568 14 ▶ my $self = shift;
Subroutine _gen_check_subdir_helper: called CC=3 SCAR=11.0
569  
570 14 my @clauses;
571  
572 14 ▶ if ( !$self->followlink() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->followlink) 13 1 14
573 {
574 13 push @clauses, '$s->_top_is_link()';
575 }
576  
577 14 ▶ if ( $self->nocrossfs() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nocrossfs) 1 13 14
578 {
579 1 push @clauses, '($s->_top_stat->[0] != $s->_dev())';
580 }
581  
582 14 push @clauses, '$s->_is_loop()';
583  
584 14 $self->_check_subdir_h(
585 _context_less_eval(
586 'sub { my $s = shift; '
587 . 'return ((!exists($s->{_st})) || !('
588 . join( "||", @clauses ) . '));' . '}'
589 )
590 );
591 }
592  
593 sub _context_less_eval
594 {
595 ## no critic
596 14 ▶ my $code = shift;
Subroutine _context_less_eval: called CC=1 SCAR=0.0
597 14 return eval $code;
598 ## use critic
599 }
600  
601 sub _open_dir
602 {
603 63 ▶ my $self = shift;
Subroutine _open_dir: called CC=1 SCAR=0.0
604  
605 63 return $self->_current()->_component_open_dir( $self->_curr_path() );
606 }
607  
608 sub set_traverse_to
609 {
610 2 ▶ my ( $self, $children ) = @_;
Subroutine set_traverse_to: called CC=1 SCAR=0.0
Pod documented
611  
612 # Make sure we scan the current directory for sub-items first.
613 2 $self->get_current_node_files_list();
614  
615 2 $self->_current->_traverse_to( [@$children] );
616 }
617  
618 sub get_traverse_to
619 {
620 1 ▶ my $self = shift;
Subroutine get_traverse_to: called CC=1 SCAR=0.0
Pod documented
621  
622 1 return $self->_current->_traverse_to_copy();
623 }
624  
625 sub get_current_node_files_list
626 {
627 10 ▶ my $self = shift;
Subroutine get_current_node_files_list: called CC=2 SCAR=7.6
Pod documented
628  
629 # _open_dir can return undef if $self->_current is not a directory.
630 10 ▶ if ( $self->_open_dir() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_open_dir) { } 10 0 10
631 {
632 10 return $self->_current->_files_copy();
633 }
634 else
635 {
636 0 return [];
637 }
638 }
639  
640 sub prune
641 {
642 1 ▶ my $self = shift;
Subroutine prune: called CC=1 SCAR=0.0
Pod documented
643  
644 1 return $self->set_traverse_to( [] );
645 }
646  
647 6 1;
648  
649 __END__
« blib/lib/File/Find/Object/PathComp.pm ↑ Summary blib/lib/File/Find/Object/Result.pm »