blib/lib/File/chmod/Recursive.pm

filter: Statementstmt 28.7% 29 / 101 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 66.6% 8 / 12 Podpod 100% 3 / 3 totaltotal 19.1% 40 / 209 CC 43 SCAR 64.9
CC
43
Cov
16%
__ANON__
1056.0
chmod_recursive
147.7
rchmod
2.0
CRAP 658.8
↑ Summary
1 package File::chmod::Recursive;
2  
3 #######################
4 # LOAD MODULES
5 #######################
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::chmod qw(chmod);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 #######################
15 # VERSION
16 #######################
17 1 our $VERSION = '1.0.3';
18  
19 #######################
20 # EXPORT
21 #######################
22 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 our ( @EXPORT, @EXPORT_OK );
24  
25 1 @EXPORT = qw(chmod_recursive);
26 1 @EXPORT_OK = qw(chmod_recursive rchmod chmodr);
27  
28 #######################
29 # CHMOD RECURSIVE
30 #######################
31 0 ▶sub chmod_recursive {
Subroutine chmod_recursive: not called CC=12 SCAR=50.0
Pod documented
32  
33 # Read Input
34 0 my @in = @_;
35  
36 # Default mode
37 0 my $mode = {
38 files => q(),
39 dirs => q(),
40 match_dirs => {},
41 match_files => {},
42 match => {},
43 };
44  
45 # Default _find_ settings
46 0 my %find_settings = (
47 follow => 0, # Do not Follow symlinks
48 no_chdir => 1, # Do not chdir
49 );
50  
51 # Verbose mode
52 0 my $verbose = 0;
53  
54 # Check Input
55 0 my $dir;
56 0 ▶ if ( ref $in[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in[0] eq 'HASH') { } 0 0 0
57  
58 # Usage chmod_recursive({}, $dir);
59  
60 # Get modes
61 0 ▶ $mode->{files} = $in[0]->{files} || q();
Condition0%0/2
$in[0]{'files'} || ''
l!l
00
MC/DC0%0/1
$in[0]{'files'} || ''
$in[0]{'files'}
Truth table0%0/2
A
A= $in[0]{'files'}
Aresult
11
00
62 0 ▶ $mode->{dirs} = $in[0]->{dirs} || q();
Condition0%0/2
$in[0]{'dirs'} || ''
l!l
00
MC/DC0%0/1
$in[0]{'dirs'} || ''
$in[0]{'dirs'}
Truth table0%0/2
A
A= $in[0]{'dirs'}
Aresult
11
00
63 0 ▶ $mode->{match_files} = $in[0]->{match_files} || {};
Condition0%0/2
$in[0]{'match_files'} || {}
l!l
00
MC/DC0%0/1
$in[0]{'match_files'} || {}
$in[0]{'match_files'}
Truth table0%0/2
A
A= $in[0]{'match_files'}
Aresult
11
00
64 0 ▶ $mode->{match_dirs} = $in[0]->{match_dirs} || {};
Condition0%0/2
$in[0]{'match_dirs'} || {}
l!l
00
MC/DC0%0/1
$in[0]{'match_dirs'} || {}
$in[0]{'match_dirs'}
Truth table0%0/2
A
A= $in[0]{'match_dirs'}
Aresult
11
00
65 0 ▶ $mode->{match} = $in[0]->{match} || {};
Condition0%0/2
$in[0]{'match'} || {}
l!l
00
MC/DC0%0/1
$in[0]{'match'} || {}
$in[0]{'match'}
Truth table0%0/2
A
A= $in[0]{'match'}
Aresult
11
00
66  
67 # Check for _find_ settings
68 0 ▶ if ( $in[0]->{follow_symlinks} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in[0]{"follow_symlinks"}) 0 0 0
69 0 $find_settings{follow} = 1; # Follow Symlinks
70 0 $find_settings{follow_skip} = 2; # Skip duplicates
71 } ## end if ( $in[0]->{follow_symlinks...})
72 0 ▶ if ( $in[0]->{depth_first} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in[0]{"depth_first"}) 0 0 0
73 0 $find_settings{bydepth} = 1;
74 }
75  
76 # Verbose on/off
77 0 ▶ $verbose = $in[0]->{verbose} || 0;
Condition0%0/2
$in[0]{'verbose'} || 0
l!l
00
MC/DC0%0/1
$in[0]{'verbose'} || 0
$in[0]{'verbose'}
Truth table0%0/2
A
A= $in[0]{'verbose'}
Aresult
11
00
78  
79 } ## end if ( ref $in[0] eq 'HASH')
80  
81 else {
82  
83 # Usage chmod_recursive($mode, $dir);
84  
85 # Set modes
86 0 $mode->{files} = $in[0];
87 0 $mode->{dirs} = $in[0];
88 } ## end else [ if ( ref $in[0] eq 'HASH')]
89  
90 # Get directory
91 0 ▶ $dir = $in[1] || croak "Directory not provided";
Condition0%0/2
$in[1] || croak('Directory not provided')
l!l
00
MC/DC0%0/1
$in[1] || croak('Directory not provided')
$in[1]
Truth table0%0/2
A
A= $in[1]
Aresult
11
00
92 0 $dir = abs_path($dir);
93 0 ▶ croak "$dir is not a directory" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
94  
95 # Run chmod
96 0 my @updated;
97 {
98  
99 # Turn off warnings for file find
100 0 ▶ no warnings 'File::Find';
Subroutine BEGIN: called CC=1 SCAR=0.2
101  
102 # Turn off UMASK for File::chmod
103 # See: https://github.com/xenoterracide/File-chmod/issues/5
104 0 local $File::chmod::UMASK = 0;
105  
106 find(
107 {
108 %find_settings,
109 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=32 SCAR=69.6
110  
111 # The main stuff
112  
113 # Get full path
114 0 my $path = $File::Find::name;
115  
116 0 ▶ if ( not -l $path ) { # Do not set permissions on symlinks
Branch: 1 branch
BranchTrueFalseTotal
if (not -l $path) 0 0 0
117  
118 # Process files
119 0 ▶ if ( -f $path ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $path) { } 0 0 0
elsif (-d $path) { } 0 0 0
120  
121 0 my $file_isa_match = 0;
122  
123 # Process file Matches
124 0 foreach
125 0 my $match_re ( keys %{ $mode->{match_files} } )
126 {
127 0 ▶ next if $file_isa_match;
Branch: 1 branch
BranchTrueFalseTotal
if $file_isa_match 0 0 0
128 0 ▶ next unless ( $path =~ m{$match_re} );
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /$match_re/ 0 0 0
129 0 $file_isa_match = 1; # Done matching
130 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&File::chmod::Recursive::chmod($mode->{"match_files"}{$match_re}, $path)) 0 0 0
131 chmod(
132 $mode->{match_files}->{$match_re},
133 $path
134 )
135 )
136 {
137 0 push @updated, $path;
138 warn "chmod_recursive: $path -> "
139 . (
140 sprintf "%#o",
141 0 ▶ $mode->{match_files}->{$match_re}
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
142 )
143 . "\n"
144 if $verbose;
145 } ## end if ( chmod( $mode->{match_files...}))
146 } ## end foreach my $match_re ( keys...)
147  
148 # Process generic matches
149 0 foreach my $match_re ( keys %{ $mode->{match} } )
150 {
151 0 ▶ next if $file_isa_match;
Branch: 1 branch
BranchTrueFalseTotal
if $file_isa_match 0 0 0
152 0 ▶ next unless ( $path =~ m{$match_re} );
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /$match_re/ 0 0 0
153 0 $file_isa_match = 1;
154 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&File::chmod::Recursive::chmod($mode->{"match"}{$match_re}, $path)) 0 0 0
155 chmod(
156 $mode->{match}->{$match_re},
157 $path
158 )
159 )
160 {
161 0 push @updated, $path;
162 warn "chmod_recursive: $path -> "
163 . (
164 sprintf "%#o",
165 0 ▶ $mode->{match}->{$match_re}
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
166 )
167 . "\n"
168 if $verbose;
169 } ## end if ( chmod( $mode->{match...}))
170 } ## end foreach my $match_re ( keys...)
171  
172 # Process non-matches
173 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $file_isa_match and $mode->{"files"} and &File::chmod::Recursive::chmod($mode->{"files"}, $path)) 0 0 0
Condition0%0/6
not $file_isa_match and $mode->{"files"} and &File::chmod::Recursive::chmod($mode->{"files"}, $path)
!ll&&!rl&&r
000
not $file_isa_match and $mode->{"files"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $file_isa_match and $mode->{"files"} and &File::chmod::Recursive::chmod($mode->{"files"}, $path)
not $file_isa_match $mode->{"files"} &File::chmod::Recursive::chmod($mode->{"files"}, $path)
Truth table0%0/4
not(A) and B and C
A= not $file_isa_matchB= $mode->{"files"}C= &File::chmod::Recursive::chmod($mode->{"files"}, $path)
ABCresult
0XX0
10X0
1100
1111
174  
175 # Skip processed
176 ( not $file_isa_match )
177  
178 # And we're updating files
179 and ( $mode->{files} )
180  
181 # And succesfully updated
182 and ( chmod( $mode->{files}, $path ) )
183 )
184 {
185 0 push @updated, $path;
186 warn "chmod_recursive: $path -> "
187 0 ▶ . ( sprintf "%#o", $mode->{files} ) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
188 if $verbose;
189 } ## end if ( ( not $file_isa_match...))
190 } ## end if ( -f $path )
191  
192 # Process Dirs
193 elsif ( -d $path ) {
194  
195 0 my $dir_isa_match = 0;
196  
197 # Process Matches
198 0 foreach
199 0 my $match_re ( keys %{ $mode->{match_dirs} } )
200 {
201 0 ▶ next if $dir_isa_match;
Branch: 1 branch
BranchTrueFalseTotal
if $dir_isa_match 0 0 0
202 0 ▶ next unless ( $path =~ m{$match_re} );
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /$match_re/ 0 0 0
203 0 $dir_isa_match = 1; # Done matching
204 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&File::chmod::Recursive::chmod($mode->{"match_dirs"}{$match_re}, $path)) 0 0 0
205 chmod(
206 $mode->{match_dirs}->{$match_re},
207 $path
208 )
209 )
210 {
211 0 push @updated, $path;
212 warn "chmod_recursive: $path -> "
213 . (
214 sprintf "%#o",
215 0 ▶ $mode->{match_dirs}->{$match_re}
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
216 )
217 . "\n"
218 if $verbose;
219 } ## end if ( chmod( $mode->{match_dirs...}))
220 } ## end foreach my $match_re ( keys...)
221  
222 # Process generic matches
223 0 foreach my $match_re ( keys %{ $mode->{match} } )
224 {
225 0 ▶ next if $dir_isa_match;
Branch: 1 branch
BranchTrueFalseTotal
if $dir_isa_match 0 0 0
226 0 ▶ next unless ( $path =~ m{$match_re} );
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /$match_re/ 0 0 0
227 0 $dir_isa_match = 1; # Done matching
228 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&File::chmod::Recursive::chmod($mode->{"match"}{$match_re}, $path)) 0 0 0
229 chmod(
230 $mode->{match}->{$match_re},
231 $path
232 )
233 )
234 {
235 0 push @updated, $path;
236 warn "chmod_recursive: $path -> "
237 . (
238 sprintf "%#o",
239 0 ▶ $mode->{match}->{$match_re}
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
240 )
241 . "\n"
242 if $verbose;
243 } ## end if ( chmod( $mode->{match...}))
244 } ## end foreach my $match_re ( keys...)
245  
246 # Process non-matches
247 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $dir_isa_match and $mode->{"dirs"} and &File::chmod::Recursive::chmod($mode->{"dirs"}, $path)) 0 0 0
Condition0%0/6
not $dir_isa_match and $mode->{"dirs"} and &File::chmod::Recursive::chmod($mode->{"dirs"}, $path)
!ll&&!rl&&r
000
not $dir_isa_match and $mode->{"dirs"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $dir_isa_match and $mode->{"dirs"} and &File::chmod::Recursive::chmod($mode->{"dirs"}, $path)
not $dir_isa_match $mode->{"dirs"} &File::chmod::Recursive::chmod($mode->{"dirs"}, $path)
Truth table0%0/4
not(A) and B and C
A= not $dir_isa_matchB= $mode->{"dirs"}C= &File::chmod::Recursive::chmod($mode->{"dirs"}, $path)
ABCresult
0XX0
10X0
1100
1111
248  
249 # Skip processed
250 ( not $dir_isa_match )
251  
252 # And we're updating files
253 and ( $mode->{dirs} )
254  
255 # And succesfully updated
256 and ( chmod( $mode->{dirs}, $path ) )
257 )
258 {
259 0 push @updated, $path;
260 warn "chmod_recursive: $path -> "
261 0 ▶ . ( sprintf "%#o", $mode->{dirs} ) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
262 if $verbose;
263 } ## end if ( ( not $dir_isa_match...))
264 } ## end elsif ( -d $path )
265  
266 } ## end if ( not -l $path )
267  
268 },
269 },
270 0 $dir
271 );
272 }
273  
274 # Done
275 0 return scalar @updated;
276 } ## end sub chmod_recursive
277  
278 #######################
279 # ALIASES
280 #######################
281 0 ▶sub rchmod { return chmod_recursive(@_); }
Subroutine rchmod: not called CC=1 SCAR=6.9
Pod documented
282 0 ▶sub chmodr { return chmod_recursive(@_); }
Subroutine chmodr: not called CC=1 SCAR=6.9
Pod documented
283  
284 #######################
285 1 1;
286  
287 __END__
↑ Summary