/usr/local/lib/perl5/5.44.0/File/Path.pm

filter: Statementstmt 0.0% 0 / 255 Branchbran 0.0% 0 / 158 Conditioncond 0.0% 0 / 86 MC/DCmcdc 0.0% 0 / 51 Subroutinesub 0.0% 0 / 12 Podpod 100% 4 / 4 totaltotal 0.7% 4 / 566 CC 115 SCAR 69.9
CC
115
Cov
0%
_rmtree
2256.0
mkpath
756.0
rmtree
342.0
CRAP 1089.0
« /usr/local/lib/perl5/5.44.0/Exporter/Heavy.pm ↑ Summary /usr/local/lib/perl5/site_perl/5.44.0/Method/Generate/Accessor.pm »
1 package File::Path;
2  
3 use 5.005_04;
4 use strict;
5  
6 use Cwd 'getcwd';
7 use File::Basename ();
8 use File::Spec ();
9  
10 BEGIN {
11 if ( $] < 5.006 ) {
12  
13 # can't say 'opendir my $dh, $dirname'
14 # need to initialise $dh
15 eval 'use Symbol';
16 }
17 }
18  
19 use Exporter ();
20 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
21 $VERSION = '2.18';
22 $VERSION = eval $VERSION;
23 @ISA = qw(Exporter);
24 @EXPORT = qw(mkpath rmtree);
25 @EXPORT_OK = qw(make_path remove_tree);
26  
27 BEGIN {
28 for (qw(VMS MacOS MSWin32 os2)) {
29 no strict 'refs';
30 *{"_IS_\U$_"} = $^O eq $_ ? sub () { 1 } : sub () { 0 };
31 }
32  
33 # These OSes complain if you want to remove a file that you have no
34 # write permission to:
35 *_FORCE_WRITABLE = (
36 grep { $^O eq $_ } qw(amigaos dos epoc MSWin32 MacOS os2)
37 ) ? sub () { 1 } : sub () { 0 };
38  
39 # Unix-like systems need to stat each directory in order to detect
40 # race condition. MS-Windows is immune to this particular attack.
41 *_NEED_STAT_CHECK = !(_IS_MSWIN32()) ? sub () { 1 } : sub () { 0 };
42 }
43  
44 sub _carp {
45 0 ▶ require Carp;
Subroutine _carp: not called CC=1 SCAR=6.9
46 0 goto &Carp::carp;
47 }
48  
49 sub _croak {
50 0 ▶ require Carp;
Subroutine _croak: not called CC=1 SCAR=6.9
51 0 goto &Carp::croak;
52 }
53  
54 sub _error {
55 0 ▶ my $arg = shift;
Subroutine _error: not called CC=5 SCAR=34.0
56 0 my $message = shift;
57 0 my $object = shift;
58  
59 0 ▶ if ( $arg->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{'error'}) { } 0 0 0
60 0 ▶ $object = '' unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 0 0 0
61 0 ▶ $message .= ": $!" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
62 0 push @{ ${ $arg->{error} } }, { $object => $message };
63 }
64 else {
65 0 ▶ _carp( defined($object) ? "$message for $object: $!" : "$message: $!" );
Branch: 1 branch
BranchTrueFalseTotal
defined $object ? : 0 0 0
66 }
67 }
68  
69 sub __is_arg {
70 0 ▶ my ($arg) = @_;
Subroutine __is_arg: not called CC=1 SCAR=6.9
71  
72 # If client code blessed an array ref to HASH, this will not work
73 # properly. We could have done $arg->isa() wrapped in eval, but
74 # that would be expensive. This implementation should suffice.
75 # We could have also used Scalar::Util:blessed, but we choose not
76 # to add this dependency
77 0 return ( ref $arg eq 'HASH' );
78 }
79  
80 sub make_path {
81 0 ▶ push @_, {} unless @_ and __is_arg( $_[-1] );
Subroutine make_path: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and __is_arg($_[-1]) 0 0 0
Condition0%0/3
@_ and __is_arg($_[-1])
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and __is_arg($_[-1])
@_ __is_arg($_[-1])
Truth table0%0/3
A and B
A= @_B= __is_arg($_[-1])
ABresult
0X0
100
111
82 0 goto &mkpath;
83 }
84  
85 sub mkpath {
86 0 ▶ my $old_style = !( @_ and __is_arg( $_[-1] ) );
Subroutine mkpath: not called CC=27 SCAR=66.3
Pod documented
Condition0%0/3
@_ && __is_arg($_[-1])
!ll&&!rl&&r
000
MC/DC0%0/2
@_ && __is_arg($_[-1])
@_ __is_arg($_[-1])
Truth table0%0/3
A && B
A= @_B= __is_arg($_[-1])
ABresult
0X0
100
111
87  
88 0 my $data;
89 my $paths;
90  
91 0 ▶ if ($old_style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_style) { } 0 0 0
92 0 my ( $verbose, $mode );
93 0 ( $paths, $verbose, $mode ) = @_;
94 0 ▶ $paths = [$paths] unless UNIVERSAL::isa( $paths, 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($paths, "ARRAY") 0 0 0
95 0 $data->{verbose} = $verbose;
96 0 ▶ $data->{mode} = defined $mode ? $mode : oct '777';
Branch: 1 branch
BranchTrueFalseTotal
defined $mode ? : 0 0 0
97 }
98 else {
99 0 my %args_permitted = map { $_ => 1 } ( qw|
100 chmod
101 error
102 group
103 mask
104 mode
105 owner
106 uid
107 user
108 verbose
109 | );
110 0 my %not_on_win32_args = map { $_ => 1 } ( qw|
111 group
112 owner
113 uid
114 user
115 | );
116 0 my @bad_args = ();
117 0 my @win32_implausible_args = ();
118 0 my $arg = pop @_;
119 0 for my $k (sort keys %{$arg}) {
120 0 ▶ if (! $args_permitted{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $args_permitted{$k}) { } 0 0 0
elsif ($not_on_win32_args{$k} and 0) { } 0 0 0
121 0 push @bad_args, $k;
122 }
123 ▶ elsif ($not_on_win32_args{$k} and _IS_MSWIN32) {
Condition0%0/2
$not_on_win32_args{$k} and 0
!ll
00
MC/DC0%0/1
$not_on_win32_args{$k} and 0
$not_on_win32_args{$k}
Truth table0%0/2
A
A= $not_on_win32_args{$k}
Aresult
00
11
124 0 push @win32_implausible_args, $k;
125 }
126 else {
127 0 $data->{$k} = $arg->{$k};
128 }
129 }
130 0 ▶ _carp("Unrecognized option(s) passed to mkpath() or make_path(): @bad_args")
Branch: 1 branch
BranchTrueFalseTotal
if @bad_args 0 0 0
131 if @bad_args;
132 0 ▶ _carp("Option(s) implausible on Win32 passed to mkpath() or make_path(): @win32_implausible_args")
Branch: 1 branch
BranchTrueFalseTotal
if @win32_implausible_args 0 0 0
133 if @win32_implausible_args;
134 0 ▶ $data->{mode} = delete $data->{mask} if exists $data->{mask};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"mask"} 0 0 0
135 0 ▶ $data->{mode} = oct '777' unless exists $data->{mode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{"mode"} 0 0 0
136 0 ▶ ${ $data->{error} } = [] if exists $data->{error};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"error"} 0 0 0
137 0 ▶ unless (@win32_implausible_args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@win32_implausible_args) 0 0 0
138 0 ▶ $data->{owner} = delete $data->{user} if exists $data->{user};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"user"} 0 0 0
139 0 ▶ $data->{owner} = delete $data->{uid} if exists $data->{uid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"uid"} 0 0 0
140 0 ▶ if ( exists $data->{owner} and $data->{owner} =~ /\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"owner"} and $data->{"owner"} =~ /\D/) 0 0 0
Condition0%0/3
exists $data->{"owner"} and $data->{"owner"} =~ /\D/
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"owner"} and $data->{"owner"} =~ /\D/
exists $data->{"owner"} $data->{"owner"} =~ /\D/
Truth table0%0/3
A and B
A= exists $data->{"owner"}B= $data->{"owner"} =~ /\D/
ABresult
0X0
100
111
141 0 my $uid = ( getpwnam $data->{owner} )[2];
142 0 ▶ if ( defined $uid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uid) { } 0 0 0
143 0 $data->{owner} = $uid;
144 }
145 else {
146 0 _error( $data,
147 "unable to map $data->{owner} to a uid, ownership not changed"
148 );
149 0 delete $data->{owner};
150 }
151 }
152 0 ▶ if ( exists $data->{group} and $data->{group} =~ /\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"group"} and $data->{"group"} =~ /\D/) 0 0 0
Condition0%0/3
exists $data->{"group"} and $data->{"group"} =~ /\D/
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"group"} and $data->{"group"} =~ /\D/
exists $data->{"group"} $data->{"group"} =~ /\D/
Truth table0%0/3
A and B
A= exists $data->{"group"}B= $data->{"group"} =~ /\D/
ABresult
0X0
100
111
153 0 my $gid = ( getgrnam $data->{group} )[2];
154 0 ▶ if ( defined $gid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gid) { } 0 0 0
155 0 $data->{group} = $gid;
156 }
157 else {
158 0 _error( $data,
159 "unable to map $data->{group} to a gid, group ownership not changed"
160 );
161 0 delete $data->{group};
162 }
163 }
164 0 ▶ if ( exists $data->{owner} and not exists $data->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"owner"} and not exists $data->{"group"}) 0 0 0
Condition0%0/3
exists $data->{"owner"} and not exists $data->{"group"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"owner"} and not exists $data->{"group"}
exists $data->{"owner"} not exists $data->{"group"}
Truth table0%0/3
A and not(B)
A= exists $data->{"owner"}B= not exists $data->{"group"}
ABresult
0X0
100
111
165 0 $data->{group} = -1; # chown will leave group unchanged
166 }
167 0 ▶ if ( exists $data->{group} and not exists $data->{owner} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"group"} and not exists $data->{"owner"}) 0 0 0
Condition0%0/3
exists $data->{"group"} and not exists $data->{"owner"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"group"} and not exists $data->{"owner"}
exists $data->{"group"} not exists $data->{"owner"}
Truth table0%0/3
A and not(B)
A= exists $data->{"group"}B= not exists $data->{"owner"}
ABresult
0X0
100
111
168 0 $data->{owner} = -1; # chown will leave owner unchanged
169 }
170 }
171 0 $paths = [@_];
172 }
173 0 return _mkpath( $data, $paths );
174 }
175  
176 sub _mkpath {
177 0 ▶ my $data = shift;
Subroutine _mkpath: not called CC=16 SCAR=56.1
178 0 my $paths = shift;
179  
180 0 my ( @created );
181 0 foreach my $path ( @{$paths} ) {
182 0 ▶ next unless defined($path) and length($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
183 0 $path .= '/' if _IS_OS2 and $path =~ /^\w:\z/s; # feature of CRT
184  
185 # Logic wants Unix paths, so go with the flow.
186 0 if (_IS_VMS) {
187 next if $path eq '/';
188 $path = VMS::Filespec::unixify($path);
189 }
190 0 ▶ next if -d $path;
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 0 0 0
191 0 my $parent = File::Basename::dirname($path);
192 # Coverage note: It's not clear how we would test the condition:
193 # '-d $parent or $path eq $parent'
194 0 ▶ unless ( -d $parent or $path eq $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $parent or $path eq $parent) 0 0 0
Condition0%0/3
-d $parent or $path eq $parent
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $parent or $path eq $parent
-d $parent $path eq $parent
Truth table0%0/3
A or B
A= -d $parentB= $path eq $parent
ABresult
1X1
011
000
195 0 push( @created, _mkpath( $data, [$parent] ) );
196 }
197 0 ▶ print "mkdir $path\n" if $data->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"verbose"} 0 0 0
198 0 ▶ if ( mkdir( $path, $data->{mode} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir $path, $data->{'mode'}) { } 0 0 0
199 0 push( @created, $path );
200 0 ▶ if ( exists $data->{owner} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"owner"}) 0 0 0
201  
202 # NB: $data->{group} guaranteed to be set during initialisation
203 0 ▶ if ( !chown $data->{owner}, $data->{group}, $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chown $data->{"owner"}, $data->{"group"}, $path) 0 0 0
204 0 _error( $data,
205 "Cannot change ownership of $path to $data->{owner}:$data->{group}"
206 );
207 }
208 }
209 0 ▶ if ( exists $data->{chmod} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"chmod"}) 0 0 0
210 # Coverage note: It's not clear how we would trigger the next
211 # 'if' block. Failure of 'chmod' might first result in a
212 # system error: "Permission denied".
213 0 ▶ if ( !chmod $data->{chmod}, $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod $data->{"chmod"}, $path) 0 0 0
214 0 _error( $data,
215 "Cannot change permissions of $path to $data->{chmod}" );
216 }
217 }
218 }
219 else {
220 0 my $save_bang = $!;
221  
222 # From 'perldoc perlvar': $EXTENDED_OS_ERROR ($^E) is documented
223 # as:
224 # Error information specific to the current operating system. At the
225 # moment, this differs from "$!" under only VMS, OS/2, and Win32
226 # (and for MacPerl). On all other platforms, $^E is always just the
227 # same as $!.
228  
229 0 my ( $e, $e1 ) = ( $save_bang, $^E );
230 0 ▶ $e .= "; $e1" if $e ne $e1;
Branch: 1 branch
BranchTrueFalseTotal
if $e ne $e1 0 0 0
231  
232 # allow for another process to have created it meanwhile
233 0 ▶ if ( ! -d $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $path) 0 0 0
234 0 $! = $save_bang;
235 0 ▶ if ( $data->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'error'}) { } 0 0 0
236 0 push @{ ${ $data->{error} } }, { $path => $e };
237 }
238 else {
239 0 _croak("mkdir $path: $e");
240 }
241 }
242 }
243 }
244 0 return @created;
245 }
246  
247 sub remove_tree {
248 0 ▶ push @_, {} unless @_ and __is_arg( $_[-1] );
Subroutine remove_tree: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and __is_arg($_[-1]) 0 0 0
Condition0%0/3
@_ and __is_arg($_[-1])
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and __is_arg($_[-1])
@_ __is_arg($_[-1])
Truth table0%0/3
A and B
A= @_B= __is_arg($_[-1])
ABresult
0X0
100
111
249 0 goto &rmtree;
250 }
251  
252 sub _is_subdir {
253 0 ▶ my ( $dir, $test ) = @_;
Subroutine _is_subdir: not called CC=3 SCAR=24.8
254  
255 0 my ( $dv, $dd ) = File::Spec->splitpath( $dir, 1 );
256 0 my ( $tv, $td ) = File::Spec->splitpath( $test, 1 );
257  
258 # not on same volume
259 0 ▶ return 0 if $dv ne $tv;
Branch: 1 branch
BranchTrueFalseTotal
if $dv ne $tv 0 0 0
260  
261 0 my @d = File::Spec->splitdir($dd);
262 0 my @t = File::Spec->splitdir($td);
263  
264 # @t can't be a subdir if it's shorter than @d
265 0 ▶ return 0 if @t < @d;
Branch: 1 branch
BranchTrueFalseTotal
if @t < @d 0 0 0
266  
267 0 return join( '/', @d ) eq join( '/', splice @t, 0, +@d );
268 }
269  
270 sub rmtree {
271 0 ▶ my $old_style = !( @_ and __is_arg( $_[-1] ) );
Subroutine rmtree: not called CC=18 SCAR=58.3
Pod documented
Condition0%0/3
@_ && __is_arg($_[-1])
!ll&&!rl&&r
000
MC/DC0%0/2
@_ && __is_arg($_[-1])
@_ __is_arg($_[-1])
Truth table0%0/3
A && B
A= @_B= __is_arg($_[-1])
ABresult
0X0
100
111
272  
273 0 my ($arg, $data, $paths);
274  
275 0 ▶ if ($old_style) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_style) { } 0 0 0
276 0 my ( $verbose, $safe );
277 0 ( $paths, $verbose, $safe ) = @_;
278 0 $data->{verbose} = $verbose;
279 0 ▶ $data->{safe} = defined $safe ? $safe : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $safe ? : 0 0 0
280  
281 0 ▶ if ( defined($paths) and length($paths) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $paths and length $paths) { } 0 0 0
Condition0%0/3
defined $paths and length $paths
!ll&&!rl&&r
000
MC/DC0%0/2
defined $paths and length $paths
defined $paths length $paths
Truth table0%0/3
A and B
A= defined $pathsB= length $paths
ABresult
0X0
100
111
282 0 ▶ $paths = [$paths] unless UNIVERSAL::isa( $paths, 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($paths, "ARRAY") 0 0 0
283 }
284 else {
285 0 _carp("No root path(s) specified\n");
286 0 return 0;
287 }
288 }
289 else {
290 0 my %args_permitted = map { $_ => 1 } ( qw|
291 error
292 keep_root
293 result
294 safe
295 verbose
296 | );
297 0 my @bad_args = ();
298 0 my $arg = pop @_;
299 0 for my $k (sort keys %{$arg}) {
300 0 ▶ if (! $args_permitted{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args_permitted{$k}) { } 0 0 0
301 0 push @bad_args, $k;
302 }
303 else {
304 0 $data->{$k} = $arg->{$k};
305 }
306 }
307 0 ▶ _carp("Unrecognized option(s) passed to remove_tree(): @bad_args")
Branch: 1 branch
BranchTrueFalseTotal
if @bad_args 0 0 0
308 if @bad_args;
309 0 ▶ ${ $data->{error} } = [] if exists $data->{error};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"error"} 0 0 0
310 0 ▶ ${ $data->{result} } = [] if exists $data->{result};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"result"} 0 0 0
311  
312 # Wouldn't it make sense to do some validation on @_ before assigning
313 # to $paths here?
314 # In the $old_style case we guarantee that each path is both defined
315 # and non-empty. We don't check that here, which means we have to
316 # check it later in the first condition in this line:
317 # if ( $ortho_root_length && _is_subdir( $ortho_root, $ortho_cwd ) ) {
318 # Granted, that would be a change in behavior for the two
319 # non-old-style interfaces.
320  
321 0 $paths = [@_];
322 }
323  
324 0 $data->{prefix} = '';
325 0 $data->{depth} = 0;
326  
327 0 my @clean_path;
328 0 ▶ $data->{cwd} = getcwd() or do {
Branch: 1 branch
BranchTrueFalseTotal
$data->{"cwd"} = getcwd() or do { _error($data, "cannot fetch initial working directory"); return 0 } 0 0 0
329 0 _error( $data, "cannot fetch initial working directory" );
330 0 return 0;
331 };
332 0 for ( $data->{cwd} ) { /\A(.*)\Z/s; $_ = $1 } # untaint
333  
334 0 for my $p (@$paths) {
335  
336 # need to fixup case and map \ to / on Windows
337 0 my $ortho_root = _IS_MSWIN32 ? _slash_lc($p) : $p;
338 my $ortho_cwd =
339 0 _IS_MSWIN32 ? _slash_lc( $data->{cwd} ) : $data->{cwd};
340 0 my $ortho_root_length = length($ortho_root);
341 0 $ortho_root_length-- if _IS_VMS; # don't compare '.' with ']'
342 0 ▶ if ( $ortho_root_length && _is_subdir( $ortho_root, $ortho_cwd ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ortho_root_length and _is_subdir($ortho_root, $ortho_cwd)) 0 0 0
Condition0%0/3
$ortho_root_length and _is_subdir($ortho_root, $ortho_cwd)
!ll&&!rl&&r
000
MC/DC0%0/2
$ortho_root_length and _is_subdir($ortho_root, $ortho_cwd)
$ortho_root_length _is_subdir($ortho_root, $ortho_cwd)
Truth table0%0/3
A and B
A= $ortho_root_lengthB= _is_subdir($ortho_root, $ortho_cwd)
ABresult
0X0
100
111
343 0 local $! = 0;
344 0 _error( $data, "cannot remove path when cwd is $data->{cwd}", $p );
345 0 next;
346 }
347  
348 0 if (_IS_MACOS) {
349 $p = ":$p" unless $p =~ /:/;
350 $p .= ":" unless $p =~ /:\z/;
351 }
352 elsif ( _IS_MSWIN32 ) {
353 $p =~ s{[/\\]\z}{};
354 }
355 else {
356 0 $p =~ s{/\z}{};
357 }
358 0 push @clean_path, $p;
359 }
360  
361 0 ▶ @{$data}{qw(device inode)} = ( lstat $data->{cwd} )[ 0, 1 ] or do {
Branch: 1 branch
BranchTrueFalseTotal
@{$data;}{"device", "inode"} = (lstat $data->{"cwd"})[0, 1] or do { _error($data, "cannot stat initial working directory", $data->{"cwd"}); return 0 } 0 0 0
362 0 _error( $data, "cannot stat initial working directory", $data->{cwd} );
363 0 return 0;
364 };
365  
366 0 return _rmtree( $data, \@clean_path );
367 }
368  
369 sub _rmtree {
370 0 ▶ my $data = shift;
Subroutine _rmtree: not called CC=47 SCAR=77.2
371 0 my $paths = shift;
372  
373 0 my $count = 0;
374 0 my $curdir = File::Spec->curdir();
375 0 my $updir = File::Spec->updir();
376  
377 0 my ( @files, $root );
378 ROOT_DIR:
379 0 foreach my $root (@$paths) {
380  
381 # since we chdir into each directory, it may not be obvious
382 # to figure out where we are if we generate a message about
383 # a file name. We therefore construct a semi-canonical
384 # filename, anchored from the directory being unlinked (as
385 # opposed to being truly canonical, anchored from the root (/).
386  
387 my $canon =
388 $data->{prefix}
389 0 ▶ ? File::Spec->catfile( $data->{prefix}, $root )
Branch: 1 branch
BranchTrueFalseTotal
$data->{'prefix'} ? : 0 0 0
390 : $root;
391  
392 0 ▶ my ( $ldev, $lino, $perm ) = ( lstat $root )[ 0, 1, 2 ]
Branch: 1 branch
BranchTrueFalseTotal
() = (lstat $root)[0, 1, 2] or next ROOT_DIR 0 0 0
393 or next ROOT_DIR;
394  
395 0 ▶ if ( -d _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d _) { } 0 0 0
396 0 $root = VMS::Filespec::vmspath( VMS::Filespec::pathify($root) )
397 if _IS_VMS;
398  
399 0 ▶ if ( !chdir($root) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $root) 0 0 0
400  
401 # see if we can escalate privileges to get in
402 # (e.g. funny protection mask such as -w- instead of rwx)
403 # This uses fchmod to avoid traversing outside of the proper
404 # location (CVE-2017-6512)
405 0 my $root_fh;
406 0 ▶ if (open($root_fh, '<', $root)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $root_fh, "<", $root) 0 0 0
407 0 my ($fh_dev, $fh_inode) = (stat $root_fh )[0,1];
408 0 $perm &= oct '7777';
409 0 my $nperm = $perm | oct '700';
410 0 local $@;
411 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"safe"} or $nperm == $perm or not -d _ or $fh_dev ne $ldev or $fh_inode ne $lino or eval { do { chmod $nperm, $root_fh } }) 0 0 0
Condition0%0/15
$data->{"safe"} or $nperm == $perm or not -d _ or $fh_dev ne $ldev or $fh_inode ne $lino or eval { do { chmod $nperm, $root_fh } }
l!l&&r!l&&!r
000
$data->{"safe"} or $nperm == $perm or not -d _ or $fh_dev ne $ldev or $fh_inode ne $lino
l!l&&r!l&&!r
000
$data->{"safe"} or $nperm == $perm or not -d _ or $fh_dev ne $ldev
l!l&&r!l&&!r
000
$data->{"safe"} or $nperm == $perm or not -d _
l!l&&r!l&&!r
000
$data->{"safe"} or $nperm == $perm
l!l&&r!l&&!r
000
MC/DC0%0/6
$data->{"safe"} or $nperm == $perm or not -d _ or $fh_dev ne $ldev or $fh_inode ne $lino or eval { do { chmod $nperm, $root_fh } }
$data->{"safe"} $nperm == $perm not -d _ $fh_dev ne $ldev $fh_inode ne $lino eval { do { chmod $nperm, $root_fh } }
Truth table0%0/7
A or B or not(C) or D or E or F
A= $data->{"safe"}B= $nperm == $permC= not -d _D= $fh_dev ne $ldevE= $fh_inode ne $linoF= eval { do { chmod $nperm, $root_fh } }
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
412 !(
413 $data->{safe}
414 or $nperm == $perm
415 or !-d _
416 or $fh_dev ne $ldev
417 or $fh_inode ne $lino
418 0 or eval { chmod( $nperm, $root_fh ) }
419 )
420 )
421 {
422 0 _error( $data,
423 "cannot make child directory read-write-exec", $canon );
424 0 next ROOT_DIR;
425 }
426 0 close $root_fh;
427 }
428 0 ▶ if ( !chdir($root) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $root) 0 0 0
429 0 _error( $data, "cannot chdir to child", $canon );
430 0 next ROOT_DIR;
431 }
432 }
433  
434 my ( $cur_dev, $cur_inode, $perm ) = ( stat $curdir )[ 0, 1, 2 ]
435 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
() = (stat $curdir)[0, 1, 2] or do { _error($data, "cannot stat current working directory", $canon); next ROOT_DIR } 0 0 0
436 0 _error( $data, "cannot stat current working directory", $canon );
437 0 next ROOT_DIR;
438 };
439  
440 0 if (_NEED_STAT_CHECK) {
441 0 ▶ ( $ldev eq $cur_dev and $lino eq $cur_inode )
Branch: 1 branch
BranchTrueFalseTotal
$ldev eq $cur_dev and $lino eq $cur_inode or _croak("directory $canon changed before chdir, expected dev=$ldev ino=$lino, actual dev=$cur_dev ino=$cur_inode, aborting.") 0 0 0
Condition0%0/3
$ldev eq $cur_dev and $lino eq $cur_inode
!ll&&!rl&&r
000
MC/DC0%0/2
$ldev eq $cur_dev and $lino eq $cur_inode
$ldev eq $cur_dev $lino eq $cur_inode
Truth table0%0/3
A and B
A= $ldev eq $cur_devB= $lino eq $cur_inode
ABresult
0X0
100
111
442 or _croak(
443 "directory $canon changed before chdir, expected dev=$ldev ino=$lino, actual dev=$cur_dev ino=$cur_inode, aborting."
444 );
445 }
446  
447 0 $perm &= oct '7777'; # don't forget setuid, setgid, sticky bits
448 0 my $nperm = $perm | oct '700';
449  
450 # notabene: 0700 is for making readable in the first place,
451 # it's also intended to change it to writable in case we have
452 # to recurse in which case we are better than rm -rf for
453 # subtrees with strange permissions
454  
455 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"safe"} or $nperm == $perm or chmod $nperm, $curdir) 0 0 0
Condition0%0/6
$data->{"safe"} or $nperm == $perm or chmod $nperm, $curdir
l!l&&r!l&&!r
000
$data->{"safe"} or $nperm == $perm
l!l&&r!l&&!r
000
MC/DC0%0/3
$data->{"safe"} or $nperm == $perm or chmod $nperm, $curdir
$data->{"safe"} $nperm == $perm chmod $nperm, $curdir
Truth table0%0/4
A or B or C
A= $data->{"safe"}B= $nperm == $permC= chmod $nperm, $curdir
ABCresult
1XX1
01X1
0011
0000
456 !(
457 $data->{safe}
458 or $nperm == $perm
459 or chmod( $nperm, $curdir )
460 )
461 )
462 {
463 0 _error( $data, "cannot make directory read+writeable", $canon );
464 0 $nperm = $perm;
465 }
466  
467 0 my $d;
468 0 ▶ $d = gensym() if $] < 5.006;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.006" 0 0 0
469 0 ▶ if ( !opendir $d, $curdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not opendir($d, $curdir)) { } 0 0 0
470 0 _error( $data, "cannot opendir", $canon );
471 0 @files = ();
472 }
473 else {
474 0 ▶ if ( !defined ${^TAINT} or ${^TAINT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined ${^TAINT} or ${^TAINT}) { } 0 0 0
Condition0%0/3
not defined ${^TAINT} or ${^TAINT}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined ${^TAINT} or ${^TAINT}
not defined ${^TAINT} ${^TAINT}
Truth table0%0/3
not(A) or B
A= not defined ${^TAINT}B= ${^TAINT}
ABresult
1X1
011
000
475 # Blindly untaint dir names if taint mode is active
476 0 @files = map { /\A(.*)\z/s; $1 } readdir $d;
477 }
478 else {
479 0 @files = readdir $d;
480 }
481 0 closedir $d;
482 }
483  
484 0 if (_IS_VMS) {
485  
486 # Deleting large numbers of files from VMS Files-11
487 # filesystems is faster if done in reverse ASCIIbetical order.
488 # include '.' to '.;' from blead patch #31775
489 @files = map { $_ eq '.' ? '.;' : $_ } reverse @files;
490 }
491  
492 0 ▶ @files = grep { $_ ne $updir and $_ ne $curdir } @files;
Condition0%0/3
$_ ne $updir && $_ ne $curdir
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne $updir && $_ ne $curdir
$_ ne $updir $_ ne $curdir
Truth table0%0/3
A && B
A= $_ ne $updirB= $_ ne $curdir
ABresult
0X0
100
111
493  
494 0 ▶ if (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 0 0 0
495  
496 # remove the contained files before the directory itself
497 0 my $narg = {%$data};
498 0 @{$narg}{qw(device inode cwd prefix depth)} =
499 0 ( $cur_dev, $cur_inode, $updir, $canon, $data->{depth} + 1 );
500 0 $count += _rmtree( $narg, \@files );
501 }
502  
503 # restore directory permissions of required now (in case the rmdir
504 # below fails), while we are still in the directory and may do so
505 # without a race via '.'
506 0 ▶ if ( $nperm != $perm and not chmod( $perm, $curdir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nperm != $perm and not chmod($perm, $curdir)) 0 0 0
Condition0%0/3
$nperm != $perm and not chmod($perm, $curdir)
!ll&&!rl&&r
000
MC/DC0%0/2
$nperm != $perm and not chmod($perm, $curdir)
$nperm != $perm not chmod($perm, $curdir)
Truth table0%0/3
A and not(B)
A= $nperm != $permB= not chmod($perm, $curdir)
ABresult
0X0
100
111
507 0 _error( $data, "cannot reset chmod", $canon );
508 }
509  
510 # don't leave the client code in an unexpected directory
511 chdir( $data->{cwd} )
512 0 ▶ or
Branch: 1 branch
BranchTrueFalseTotal
chdir $data->{"cwd"} or _croak(qq[cannot chdir to $data->{"cwd"} from ${canon}: $!, aborting.]) 0 0 0
513 _croak("cannot chdir to $data->{cwd} from $canon: $!, aborting.");
514  
515 # ensure that a chdir upwards didn't take us somewhere other
516 # than we expected (see CVE-2002-0435)
517 0 ▶ ( $cur_dev, $cur_inode ) = ( stat $curdir )[ 0, 1 ]
Branch: 1 branch
BranchTrueFalseTotal
() = (stat $curdir)[0, 1] or _croak(qq[cannot stat prior working directory $data->{"cwd"}: $!, aborting.]) 0 0 0
518 or _croak(
519 "cannot stat prior working directory $data->{cwd}: $!, aborting."
520 );
521  
522 0 if (_NEED_STAT_CHECK) {
523 0 ▶ ( $data->{device} eq $cur_dev and $data->{inode} eq $cur_inode )
Branch: 1 branch
BranchTrueFalseTotal
$data->{"device"} eq $cur_dev and $data->{"inode"} eq $cur_inode or _croak(qq[previous directory $data->{"cwd"} ] . "changed before entering $canon, " . "expected dev=$ldev ino=$lino, " . "actual dev=$cur_dev ino=$cur_inode, aborting.") 0 0 0
Condition0%0/3
$data->{"device"} eq $cur_dev and $data->{"inode"} eq $cur_inode
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"device"} eq $cur_dev and $data->{"inode"} eq $cur_inode
$data->{"device"} eq $cur_dev $data->{"inode"} eq $cur_inode
Truth table0%0/3
A and B
A= $data->{"device"} eq $cur_devB= $data->{"inode"} eq $cur_inode
ABresult
0X0
100
111
524 or _croak( "previous directory $data->{cwd} "
525 . "changed before entering $canon, "
526 . "expected dev=$ldev ino=$lino, "
527 . "actual dev=$cur_dev ino=$cur_inode, aborting."
528 );
529 }
530  
531 0 ▶ if ( $data->{depth} or !$data->{keep_root} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"depth"} or not $data->{"keep_root"}) 0 0 0
Condition0%0/3
$data->{"depth"} or not $data->{"keep_root"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->{"depth"} or not $data->{"keep_root"}
$data->{"depth"} not $data->{"keep_root"}
Truth table0%0/3
A or not(B)
A= $data->{"depth"}B= not $data->{"keep_root"}
ABresult
1X1
011
000
532 0 ▶ if ( $data->{safe}
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"safe"} and not -w $root) 0 0 0
Condition0%0/3
$data->{"safe"} and not -w $root
!ll&&!rl&&r
000
MC/DC0%0/2
$data->{"safe"} and not -w $root
$data->{"safe"} not -w $root
Truth table0%0/3
A and not(B)
A= $data->{"safe"}B= not -w $root
ABresult
0X0
100
111
533 && ( _IS_VMS
534 ? !&VMS::Filespec::candelete($root)
535 : !-w $root ) )
536 {
537 0 ▶ print "skipped $root\n" if $data->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"verbose"} 0 0 0
538 0 next ROOT_DIR;
539 }
540 0 if ( _FORCE_WRITABLE and !chmod $perm | oct '700', $root ) {
541 _error( $data, "cannot make directory writeable", $canon );
542 }
543 0 ▶ print "rmdir $root\n" if $data->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"verbose"} 0 0 0
544 0 ▶ if ( rmdir $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rmdir $root) { } 0 0 0
545 0 ▶ push @{ ${ $data->{result} } }, $root if $data->{result};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"result"} 0 0 0
546 0 ++$count;
547 }
548 else {
549 0 _error( $data, "cannot remove directory", $canon );
550 0 if (
551 _FORCE_WRITABLE
552 && !chmod( $perm,
553 ( _IS_VMS ? VMS::Filespec::fileify($root) : $root )
554 )
555 )
556 {
557 _error(
558 $data,
559 sprintf( "cannot restore permissions to 0%o",
560 $perm ),
561 $canon
562 );
563 }
564 }
565 }
566 }
567 else {
568 # not a directory
569 0 $root = VMS::Filespec::vmsify("./$root")
570 if _IS_VMS
571 && !File::Spec->file_name_is_absolute($root)
572 && ( $root !~ m/(?<!\^)[\]>]+/ ); # not already in VMS syntax
573  
574 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"safe"} and not -l $root || -w $root) 0 0 0
Condition0%0/6
$data->{"safe"} and not -l $root || -w $root
!ll&&!rl&&r
000
-l $root || -w $root
l!l&&r!l&&!r
000
MC/DC0%0/3
$data->{"safe"} and not -l $root || -w $root
$data->{"safe"} -l $root -w $root
Truth table0%0/4
A and not(B || C)
A= $data->{"safe"}B= -l $rootC= -w $root
ABCresult
0XX0
11X0
1010
1001
575 $data->{safe}
576 && (
577 _IS_VMS
578 ? !&VMS::Filespec::candelete($root)
579 : !( -l $root || -w $root )
580 )
581 )
582 {
583 0 ▶ print "skipped $root\n" if $data->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"verbose"} 0 0 0
584 0 next ROOT_DIR;
585 }
586  
587 0 my $nperm = $perm & oct '7777' | oct '600';
588 0 if ( _FORCE_WRITABLE
589 and $nperm != $perm
590 and not chmod $nperm, $root )
591 {
592 _error( $data, "cannot make file writeable", $canon );
593 }
594 0 ▶ print "unlink $canon\n" if $data->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"verbose"} 0 0 0
595  
596 # delete all versions under VMS
597 0 for ( ; ; ) {
598 0 ▶ if ( unlink $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $root) { } 0 0 0
599 0 ▶ push @{ ${ $data->{result} } }, $root if $data->{result};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"result"} 0 0 0
600 }
601 else {
602 0 _error( $data, "cannot unlink file", $canon );
603 0 _FORCE_WRITABLE and chmod( $perm, $root )
604 or _error( $data,
605 sprintf( "cannot restore permissions to 0%o", $perm ),
606 $canon );
607 0 last;
608 }
609 0 ++$count;
610 0 last unless _IS_VMS && lstat $root;
611 }
612 }
613 }
614 0 return $count;
615 }
616  
617 sub _slash_lc {
618  
619 # fix up slashes and case on MSWin32 so that we can determine that
620 # c:\path\to\dir is underneath C:/Path/To
621 0 ▶ my $path = shift;
Subroutine _slash_lc: not called CC=1 SCAR=6.9
622 0 $path =~ tr{\\}{/};
623 0 return lc($path);
624 }
625  
626 1;
627  
628 __END__
« /usr/local/lib/perl5/5.44.0/Exporter/Heavy.pm ↑ Summary /usr/local/lib/perl5/site_perl/5.44.0/Method/Generate/Accessor.pm »