POSIX/Userland/Find.pm

filter: Statementstmt 77.9% 53 / 68 Branchbran 47.7% 21 / 44 Conditioncond 33.3% 13 / 39 MC/DCmcdc 18.1% 4 / 22 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 54.7% 99 / 181 CC 36 SCAR 51.0
CC
36
Cov
58%
find
190.6
BEGIN
1.0
BEGIN
1.0
CRAP 163.5
↑ Summary POSIX/Bits/System.pm »
1 # Copyright (c) 2014, cPanel, Inc.
2 # All rights reserved.
3 # http://cpanel.net/
4 #
5 # This is free software; you can redistribute it and/or modify it under the same
6 # terms as Perl itself. See the LICENSE file for further details.
7  
8 package Filesys::POSIX::Userland::Find;
9  
10 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use Filesys::POSIX::Bits;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Filesys::POSIX::Module ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Filesys::POSIX::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 my @METHODS = qw(find);
20  
21 8 Filesys::POSIX::Module->export_methods( __PACKAGE__, @METHODS );
22  
23 =head1 NAME
24  
25 Filesys::POSIX::Userland::Find - Crawl directories in a filesystem
26  
27 =head1 SYNOPSIS
28  
29 use Filesys::POSIX;
30 use Filesys::POSIX::Real;
31 use Filesys::POSIX::Userland::Find;
32  
33 my $fs = Filesys::POSIX->new(Filesys::POSIX::Real->new,
34 'special' => 'real:/home/foo',
35 'noatime' => 1
36 );
37  
38 $fs->find(sub {
39 my ($path, $inode) = @_;
40 printf("0%o %s\n", $inode->{'mode'}, $path->full);
41 }, '/');
42  
43 =head1 DESCRIPTION
44  
45 This module provides an extension module to L<Filesys::POSIX> that operates
46 very similarly in principle to the Perl Core module L<File::Find>, albeit with
47 some minor differences and fewer options. For the sake of efficiency, tail
48 recursion, rather than pure call recursion, is used to handle very deep
49 hierarchies.
50  
51 =head1 USAGE
52  
53 =over
54  
55 =item C<$fs-E<gt>find($callback, @paths)>
56  
57 =item C<$fs-E<gt>find($callback, $options, @paths)>
58  
59 C<$fs-E<gt>find> will perform recursive descent into each path passed, printing
60 the full pathname of each item found relative to each item found in the
61 C<@paths> list. For each item found, both a Filesys::POSIX::Path object, and an
62 inode, respectively, are passed as the sole arguments to the callback. With
63 this mechanism, it is possible to retrieve path data from each item in every way
64 currently provided by L<File::Find>, without retaining global state to do so.
65 As a reference to the corresponding item's inode object is passed, there is no
66 need to perform a C<$fs-E<gt>stat> call to further inspect the item.
67  
68 When called with an C<$options> argument, specified in the form of an anonymous
69 HASH, the following flags (whose values are set nonzero) are honored:
70  
71 =over
72  
73 =item C<follow>
74  
75 Any symlinks found along the way are resolved; if the paths they resolve to are
76 those of directories, then further descent will be made into said directories.
77  
78 =item C<recursion_mode>
79  
80 Specifies the strategy to use when recursing through directories. Available
81 options are:
82  
83 =over
84  
85 =item breadth
86  
87 Traverse in a breadth-first manner. This is the default mode.
88  
89 =item depth
90  
91 Traverse in a depth-first manner.
92  
93 =item device
94  
95 Traverse inodes that are on the same device first. This mode also tracks the
96 current filesystem it is processing and invokes the C<$fs-E<gt>enter_filesystem>
97 and C<$fs-E<gt>exit_filesystem> methods when the current filesystem changes to
98 optimize these calls.
99  
100 =back
101  
102 =item C<ignore_missing>
103  
104 When set, ignore if a file or directory becomes missing during recursion. If the
105 value is a coderef, calls that function with the name of the missing file.
106  
107 =item C<ignore_inaccessible>
108  
109 When set, ignore if a file or directory becomes unreadable during recursion. If
110 the value is a coderef, calls that function with the name of the inaccessible file.
111  
112 =cut
113  
114 13 ▶sub find {
Subroutine find: called CC=36 SCAR=52.5
Pod documented
115 13 my $self = shift;
116 13 my $callback = shift;
117 13 ▶ my %opts = ref $_[0] eq 'HASH' ? %{ (shift) } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 12 1 13
118 13 my @args = @_;
119  
120 13 my @paths = map { Filesys::POSIX::Path->new($_) } @args;
121 13 ▶ my @inodes = map { $opts{'follow'} ? $self->stat($_) : $self->lstat($_) } @args;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'follow'} ? : 1 12 13
122  
123 13 ▶ my $recursion_mode = defined $opts{'recursion_mode'} ? $opts{'recursion_mode'} : 'breadth';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'recursion_mode'} ? : 0 13 13
124 13 ▶ if ( $recursion_mode ne 'breadth' && $recursion_mode ne 'depth' && $recursion_mode ne 'device' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursion_mode ne "breadth" and $recursion_mode ne "depth" and $recursion_mode ne "device") 0 13 13
Condition33%2/6
$recursion_mode ne "breadth" and $recursion_mode ne "depth" and $recursion_mode ne "device"
!ll&&!rl&&r
1300
$recursion_mode ne "breadth" and $recursion_mode ne "depth"
!ll&&!rl&&r
1300
MC/DC0%0/3
$recursion_mode ne "breadth" and $recursion_mode ne "depth" and $recursion_mode ne "device"
$recursion_mode ne "breadth" $recursion_mode ne "depth" $recursion_mode ne "device"
Truth table25%1/4
A and B and C
A= $recursion_mode ne "breadth"B= $recursion_mode ne "depth"C= $recursion_mode ne "device"
ABCresult
0XX0
10X0
1100
1111
125 0 die "Invalid recursion mode $recursion_mode specified";
126 }
127  
128 13 my $current_dev;
129  
130 13 ▶ while ( my $inode = pop @inodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $inode = pop @inodes) 115 12 127
131 115 my $path = pop @paths;
132  
133 115 ▶ if ( $recursion_mode eq 'device' && ( !defined $current_dev || $current_dev != $inode->{'dev'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursion_mode eq "device" and !defined($current_dev) || $current_dev != $inode->{"dev"}) 0 115 115
Condition16%1/6
$recursion_mode eq "device" and !defined($current_dev) || $current_dev != $inode->{"dev"}
!ll&&!rl&&r
11500
not defined $current_dev or $current_dev != $inode->{"dev"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$recursion_mode eq "device" and !defined($current_dev) || $current_dev != $inode->{"dev"}
$recursion_mode eq "device" not defined $current_dev $current_dev != $inode->{"dev"}
Truth table25%1/4
A and not(B) or C
A= $recursion_mode eq "device"B= not defined $current_devC= $current_dev != $inode->{"dev"}
ABCresult
0XX0
1000
11X1
1011
134 0 ▶ if ( defined $current_dev && $current_dev->can('exit_filesystem') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_dev and $current_dev->can("exit_filesystem")) 0 0 0
Condition0%0/3
defined $current_dev and $current_dev->can("exit_filesystem")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current_dev and $current_dev->can("exit_filesystem")
defined $current_dev $current_dev->can("exit_filesystem")
Truth table0%0/3
A and B
A= defined $current_devB= $current_dev->can("exit_filesystem")
ABresult
0X0
100
111
135 0 $current_dev->exit_filesystem();
136 }
137 0 ▶ if ( defined $inode->{'dev'} && $inode->{'dev'}->can('enter_filesystem') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inode->{"dev"} and $inode->{"dev"}->can("enter_filesystem")) 0 0 0
Condition0%0/3
defined $inode->{"dev"} and $inode->{"dev"}->can("enter_filesystem")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $inode->{"dev"} and $inode->{"dev"}->can("enter_filesystem")
defined $inode->{"dev"} $inode->{"dev"}->can("enter_filesystem")
Truth table0%0/3
A and B
A= defined $inode->{"dev"}B= $inode->{"dev"}->can("enter_filesystem")
ABresult
0X0
100
111
138 0 $inode->{'dev'}->enter_filesystem();
139 }
140 0 $current_dev = $inode->{'dev'};
141 }
142  
143 115 $callback->( $path, $inode );
144  
145 114 ▶ if ( $inode->dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inode->dir) 67 47 114
146 67 my $directory;
147 67 eval { $directory = $inode->directory->open; };
148 67 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 67 67
149 0 ▶ if ( $! == &Errno::ENOENT && $opts{'ignore_missing'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! == &Errno::ENOENT and $opts{'ignore_missing'}) { } 0 0 0
elsif ($! == &Errno::EACCES and $opts{'ignore_inaccessible'}) { } 0 0 0
Condition0%0/3
$! == &Errno::ENOENT and $opts{"ignore_missing"}
!ll&&!rl&&r
000
MC/DC0%0/2
$! == &Errno::ENOENT and $opts{"ignore_missing"}
$! == &Errno::ENOENT $opts{"ignore_missing"}
Truth table0%0/3
A and B
A= $! == &Errno::ENOENTB= $opts{"ignore_missing"}
ABresult
0X0
100
111
150 $opts{'ignore_missing'}->( $path->full() )
151 0 ▶ if ref $opts{'ignore_missing'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts{"ignore_missing"} eq "CODE" 0 0 0
152 }
153 ▶ elsif ( $! == &Errno::EACCES && $opts{'ignore_inaccessible'} ) {
Condition0%0/3
$! == &Errno::EACCES and $opts{"ignore_inaccessible"}
!ll&&!rl&&r
000
MC/DC0%0/2
$! == &Errno::EACCES and $opts{"ignore_inaccessible"}
$! == &Errno::EACCES $opts{"ignore_inaccessible"}
Truth table0%0/3
A and B
A= $! == &Errno::EACCESB= $opts{"ignore_inaccessible"}
ABresult
0X0
100
111
154 $opts{'ignore_inaccessible'}->( $path->full() )
155 0 ▶ if ref $opts{'ignore_inaccessible'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts{"ignore_inaccessible"} eq "CODE" 0 0 0
156 }
157 else {
158 0 die $@;
159 }
160 }
161  
162 67 ▶ if ( defined $directory ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $directory) 67 0 67
163 67 ▶ while ( defined( my $item = $directory->read ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = $directory->read)) 236 67 303
164 236 ▶ next if $item eq '.' || $item eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $item eq "." or $item eq ".." 134 102 236
Condition100%3/3
$item eq "." or $item eq ".."
l!l&&r!l&&!r
6767102
MC/DC100%2/2
$item eq "." or $item eq ".."
$item eq "." $item eq ".."
Truth table100%3/3
A or B
A= $item eq "."B= $item eq ".."
ABresult
1X1
011
000
165 102 my $subpath = Filesys::POSIX::Path->new( $path->full . "/$item" );
166 102 my $subnode = $self->{'vfs'}->vnode( $directory->get($item) );
167  
168 102 ▶ if ( $opts{'follow'} && defined $subnode && $subnode->link ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"follow"} and defined $subnode and $subnode->link) 1 101 102
Condition83%5/6
$opts{"follow"} and defined $subnode and $subnode->link
!ll&&!rl&&r
83181
$opts{"follow"} and defined $subnode
!ll&&!rl&&r
83019
MC/DC66%2/3
$opts{"follow"} and defined $subnode and $subnode->link
$opts{"follow"} defined $subnode $subnode->link
Truth table75%3/4
A and B and C
A= $opts{"follow"}B= defined $subnodeC= $subnode->link
ABCresult
0XX0
10X0
1100
1111
169 1 $subnode = $self->stat( $subnode->readlink );
170 }
171  
172 102 ▶ if ( !defined $subnode ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $subnode) { } 0 102 102
elsif ($recursion_mode eq 'depth' or $recursion_mode eq 'device' and $current_dev == $subnode->{'dev'}) { } 0 102 102
173 0 ▶ if ( $opts{'ignore_inaccessible'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'ignore_inaccessible'}) { } 0 0 0
174 $opts{'ignore_inaccessible'}->( $path->full() . "/$item" )
175 0 ▶ if ref $opts{'ignore_inaccessible'} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts{"ignore_inaccessible"} eq "CODE" 0 0 0
176 }
177 else {
178 0 die "Failed to read " . $path->full() . "/$item";
179 }
180 }
181 ▶ elsif ( $recursion_mode eq 'depth' || ( $recursion_mode eq 'device' && $current_dev == $subnode->{'dev'} ) ) {
Condition33%2/6
$recursion_mode eq "depth" or $recursion_mode eq "device" and $current_dev == $subnode->{"dev"}
l!l&&r!l&&!r
00102
$recursion_mode eq "device" and $current_dev == $subnode->{"dev"}
!ll&&!rl&&r
10200
MC/DC0%0/3
$recursion_mode eq "depth" or $recursion_mode eq "device" and $current_dev == $subnode->{"dev"}
$recursion_mode eq "depth" $recursion_mode eq "device" $current_dev == $subnode->{"dev"}
Truth table25%1/4
A or B and C
A= $recursion_mode eq "depth"B= $recursion_mode eq "device"C= $current_dev == $subnode->{"dev"}
ABCresult
1XX1
0111
00X0
0100
182 0 push @paths, $subpath;
183 0 push @inodes, $subnode;
184 }
185 else {
186 102 unshift @paths, $subpath;
187 102 unshift @inodes, $subnode;
188 }
189 }
190 67 $directory->close;
191 }
192 }
193 }
194 }
195  
196 8 1;
197  
198 __END__
↑ Summary POSIX/Bits/System.pm »