Paranoid/Filesystem.pm

filter: Statementstmt 80.8% 334 / 413 Branchbran 64.8% 157 / 242 Conditioncond 51.6% 31 / 60 MC/DCmcdc 30.5% 11 / 36 Subroutinesub 93.5% 29 / 31 Podpod 100% 16 / 16 totaltotal 72.4% 578 / 798 CC 150 SCAR 36.3
CC
150
Cov
73%
pchown
272.0
pchownR
56.0
pchmod
22.2
CRAP 37.9
« Paranoid/Process.pm ↑ Summary Paranoid/Log.pm »
1 # Paranoid::Filesystem -- Filesystem support for paranoid programs
2 #
3 # $Id: lib/Paranoid/Filesystem.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the "Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>",
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::Filesystem;
33  
34 21 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 21 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 21 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 21 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 21 ▶use Errno qw(:POSIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 21 ▶use Fcntl qw(:DEFAULT :seek :flock :mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 21 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 21 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 21 ▶use Paranoid::Process qw(ptranslateUser ptranslateGroup);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 21 ▶use Paranoid::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 21 ▶use Paranoid::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 21 ▶use Paranoid::Glob;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 21 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
51  
52 21 @EXPORT = qw(
53 preadDir psubdirs pfiles
54 pmkdir prm prmR ptouch
55 ptouchR pchmod pchmodR pchown
56 pchownR pwhich
57 );
58 21 @EXPORT_OK = (
59 @EXPORT, qw(
60 ptranslateLink
61 pcleanPath
62 ptranslatePerms
63 ) );
64 21 %EXPORT_TAGS = ( all => [@EXPORT_OK], );
65  
66 21 ▶use constant PERMMASK => 0777;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 #####################################################################
69 #
70 # Module code follows
71 #
72 #####################################################################
73  
74 8 ▶sub pmkdir ($;$\%) {
Subroutine pmkdir: called CC=17 SCAR=28.4
Pod documented
75  
76 # Purpose: Simulates a 'mkdir -p' command in pure Perl
77 # Returns: True (1) if all targets were successfully created,
78 # False (0) if there are any errors
79 # Usage: $rv = pmkdir("/foo/{a1,b2}");
80 # Usage: $rv = pmkdir("/foo", 0750);
81 # Usage: $rv = pmkdir("/foo", 0750, %errors);
82  
83 8 my $path = shift;
84 8 my $mode = shift;
85 8 ▶ my $eref = shift || {};
Condition50%1/2
shift() || {}
l!l
08
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
86 8 my ( $dirs, $directory, $subdir, @parts, $i );
87 8 my $rv = 1;
88  
89 8 subPreamble( PDLEVEL1, '$;$\%', $path, $mode, $eref );
90  
91 # Create a glob object if we weren't handed one.
92 8 ▶ if ( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 7 1 8
93 7 ▶ $dirs =
Branch: 1 branch
BranchTrueFalseTotal
ref $path eq 'Paranoid::Glob' ? : 2 5 7
94 ref $path eq 'Paranoid::Glob'
95 ? $path
96 : Paranoid::Glob->new( globs => [$path] );
97 }
98  
99 # Leave Paranoid::Glob's errors in place if there was a problem
100 8 ▶ $rv = 0 unless defined $dirs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dirs 1 7 8
101  
102 # Set and detaint mode
103 8 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 7 1 8
104 7 ▶ $mode = ptranslatePerms( defined $mode ? $mode : umask ^ PERMMASK );
Branch: 1 branch
BranchTrueFalseTotal
defined $mode ? : 3 4 7
105 7 ▶ unless ( detaint( $mode, 'int' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&detaint(\$mode, "int")) 1 6 7
106 1 Paranoid::ERROR =
107 pdebug( 'invalid mode argument passed', PDLEVEL1 );
108 1 $rv = 0;
109 }
110 }
111  
112 # Start creating directories
113 8 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 6 2 8
114  
115 # Iterate over each directory in the glob
116 6 foreach $directory (@$dirs) {
117 15 pdebug( 'processing %s', PDLEVEL2, $directory );
118  
119 # Skip directories already present
120 15 ▶ next if -d $directory;
Branch: 1 branch
BranchTrueFalseTotal
if -d $directory 0 15 15
121  
122 # Otherwise, split so we can backtrack to the first available
123 # subdirectory and start creating subdirectories from there
124 15 @parts = split m#/+#s, $directory;
125 15 ▶ $i = $parts[0] eq '' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$parts[0] eq '' ? : 0 15 15
126 15 ▶ $i++ while $i < $#parts and -d join '/', @parts[ 0 .. $i ];
Branch: 1 branch
BranchTrueFalseTotal
$i < $#parts and -d join("/", @parts[0 .. $i]) and do { ++$i } 28 15 43
Condition100%3/3
$i < $#parts and -d join("/", @parts[0 .. $i])
!ll&&!rl&&r
10528
MC/DC100%2/2
$i < $#parts and -d join("/", @parts[0 .. $i])
$i < $#parts -d join("/", @parts[0 .. $i])
Truth table100%3/3
A and B
A= $i < $#partsB= -d join("/", @parts[0 .. $i])
ABresult
0X0
100
111
127 15 ▶ while ( $i <= $#parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#parts) 22 11 33
128 22 $subdir = join '/', @parts[ 0 .. $i ];
129 22 ▶ unless ( -d $subdir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $subdir) 22 0 22
130 22 ▶ if ( mkdir $subdir, $mode ) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir $subdir, $mode) { } 18 4 22
131  
132 # Make sure perms are applied
133 18 chmod $mode, $subdir;
134  
135 } else {
136  
137 # Error out and halt all work
138 4 Paranoid::ERROR = pdebug( 'failed to create %s: %s',
139 PDLEVEL1, $subdir, $! );
140 4 $rv = 0;
141 4 last;
142 }
143 }
144 18 $i++;
145 }
146 }
147 }
148  
149 8 subPostamble( PDLEVEL1, '$', $rv );
150  
151 8 return $rv;
152 }
153  
154 7 ▶sub prm ($;\%) {
Subroutine prm: called CC=12 SCAR=26.6
Pod documented
155  
156 # Purpose: Simulates a "rm -f" command in pure Perl
157 # Returns: True (1) if all targets were successfully removed,
158 # False (0) if there are any errors
159 # Usage: $rv = prm("/foo");
160 # Usage: $rv = prm("/foo", %errors);
161  
162 7 my $target = shift;
163 7 my $errRef = shift;
164 7 my $rv = 1;
165 7 my ( $glob, $tglob, @fstat );
166  
167 7 subPreamble( PDLEVEL1, '$;\%', $target, $errRef );
168  
169 # Prep error hash
170 7 ▶ $errRef = {} unless defined $errRef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errRef 0 7 7
171 7 %$errRef = ();
172  
173 # Create a glob object if we weren't handed one.
174 7 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 7 0 7
175 7 ▶ $glob =
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'Paranoid::Glob' ? : 6 1 7
176 ref $target eq 'Paranoid::Glob'
177 ? $target
178 : Paranoid::Glob->new( globs => [$target] );
179 }
180 7 ▶ $rv = 0 unless defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 7 7
181  
182 # Start removing files
183 7 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 7 0 7
184  
185 # Consolidate the entries
186 7 $glob->consolidate;
187  
188 # Iterate over entries
189 7 foreach ( reverse @$glob ) {
190 21 pdebug( 'processing %s', PDLEVEL2, $_ );
191  
192 # Stat the file
193 21 @fstat = lstat $_;
194  
195 21 ▶ unless (@fstat) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fstat) 2 19 21
196  
197 # If the file is missing, consider the removal successful and
198 # move on.
199 2 ▶ next if $! == ENOENT;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 2 2 0 2
200  
201 # Report remaining errors (permission denied, etc.)
202 0 $rv = 0;
203 0 $$errRef{$_} = $!;
204 0 Paranoid::ERROR =
205 pdebug( 'failed to remove %s: %s', PDLEVEL1, $_, $! );
206 0 next;
207 }
208  
209 19 ▶ if ( S_ISDIR( $fstat[2] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_ISDIR($fstat[2])) { } 12 7 19
210  
211 # Remove directories
212 12 ▶ unless ( rmdir $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rmdir $_) 1 11 12
213  
214 # Record errors
215 1 $rv = 0;
216 1 $$errRef{$_} = $!;
217 1 Paranoid::ERROR =
218 pdebug( 'failed to remove %s: %s', PDLEVEL1, $_, $! );
219 }
220  
221 } else {
222  
223 # Remove all non-directories
224 7 ▶ unless ( unlink $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $_) 0 7 7
225  
226 # Record errors
227 0 $rv = 0;
228 0 $$errRef{$_} = $!;
229 0 Paranoid::ERROR =
230 pdebug( 'failed to remove %s: %s', PDLEVEL1, $_, $! );
231 }
232 }
233 }
234 }
235  
236 7 subPostamble( PDLEVEL1, '$', $rv );
237  
238 7 return $rv;
239 }
240  
241 4 ▶sub prmR ($;$\%) {
Subroutine prmR: called CC=7 SCAR=20.5
Pod documented
242  
243 # Purpose: Recursively calls prm to simulate "rm -rf"
244 # Returns: True (1) if all targets were successfully removed,
245 # False (0) if there are any errors
246 # Usage: $rv = prmR("/foo");
247 # Usage: $rv = prmR("/foo", 1);
248 # Usage: $rv = prmR("/foo", 1, %errors);
249  
250 4 my $target = shift;
251 4 my $follow = shift;
252 4 my $errRef = shift;
253 4 my $rv = 1;
254 4 my ( $glob, $tglob );
255  
256 4 subPreamble( PDLEVEL1, '$;$\%', $target, $follow, $errRef );
257  
258 # Prep error hash
259 4 ▶ $errRef = {} unless defined $errRef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errRef 0 4 4
260 4 %$errRef = ();
261  
262 # Create a glob object if we weren't handed one.
263 4 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 4 0 4
264 4 ▶ $glob =
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'Paranoid::Glob' ? : 0 4 4
265 ref $target eq 'Paranoid::Glob'
266 ? $target
267 : Paranoid::Glob->new( globs => [$target] );
268 }
269 4 ▶ $rv = 0 unless defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 4 4
270  
271 4 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 4 0 4
272  
273 # Load the directory tree and execute prm
274 4 ▶ $rv = $glob->recurse( $follow, 1 ) && prm( $glob, %$errRef );
Condition33%1/3
$glob->recurse($follow, 1) && &prm($glob, \%$errRef)
!ll&&!rl&&r
004
MC/DC0%0/2
$glob->recurse($follow, 1) && &prm($glob, \%$errRef)
$glob->recurse($follow, 1) &prm($glob, \%$errRef)
Truth table33%1/3
A && B
A= $glob->recurse($follow, 1)B= &prm($glob, \%$errRef)
ABresult
0X0
100
111
275 }
276  
277 4 subPostamble( PDLEVEL1, '$', $rv );
278  
279 4 return $rv;
280 }
281  
282 9 ▶sub preadDir ($\@;$) {
Subroutine preadDir: called CC=13 SCAR=26.4
Pod documented
283  
284 # Purpose: Populates the passed array ref with a list of all the
285 # directory entries (minus the '.' & '..') in the passed
286 # directory
287 # Returns: True (1) if the read was successful,
288 # False (0) if there are any errors
289 # Usage: $rv = preadDir("/tmp", @entries);
290 # Usage: $rv = preadDir("/tmp", @entries, 1);
291  
292 9 my ( $dir, $aref, $noLinks ) = @_;
293 9 my $rv = 1;
294 9 my $fh;
295  
296 9 subPreamble( PDLEVEL1, '$\@;$', $dir, $aref, $noLinks );
297 9 @$aref = ();
298  
299 # Validate directory and exit early, if need be
300 9 ▶ unless ( defined $dir and -e $dir and -d _ and -r _ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir and -e $dir and -d _ and -r _) 4 5 9
Condition77%7/9
defined $dir and -e $dir and -d _ and -r _
!ll&&!rl&&r
405
defined $dir and -e $dir and -d _
!ll&&!rl&&r
315
defined $dir and -e $dir
!ll&&!rl&&r
036
MC/DC50%2/4
defined $dir and -e $dir and -d _ and -r _
defined $dir -e $dir -d _ -r _
Truth table60%3/5
A and B and C and D
A= defined $dirB= -e $dirC= -d _D= -r _
ABCDresult
0XXX0
10XX0
110X0
11100
11111
301 4 $rv = 0;
302 4 ▶ Paranoid::ERROR = pdebug( (
Branch: 3 branches
BranchTrueFalseTotal
!defined($dir) ? : 0 4 4
!-e(_) ? : 3 1 4
!-d(_) ? : 1 0 1
303 !defined $dir ? 'undefined value passed as directory name'
304 : !-e _ ? 'directory (%s) does not exist'
305 : !-d _ ? '%s is not a directory'
306 : 'directory (%s) is not readable by the effective user'
307 ),
308 PDLEVEL1, $dir
309 );
310 }
311  
312 9 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 5 4 9
313  
314 # Read the directory's contents
315 5 $rv = opendir $fh, $dir;
316  
317 5 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 5 0 5
318  
319 # Get the list, filtering out '.' & '..'
320 5 foreach ( readdir $fh ) {
321 23 ▶ push @$aref, "$dir/$_" unless m/^\.\.?$/s;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\.\.?$/s 13 10 23
322 }
323 5 closedir $fh;
324  
325 # Filter out symlinks, if necessary
326 0 ▶ @$aref = grep { !-l $_ } @$aref if $noLinks;
Branch: 1 branch
BranchTrueFalseTotal
if $noLinks 0 5 5
327  
328 } else {
329 0 Paranoid::ERROR = pdebug( 'error opening directory (%s): %s',
330 PDLEVEL1, $dir, $! );
331 }
332 }
333  
334 9 pdebug( 'returning %d entries', PDLEVEL2, scalar @$aref );
335  
336 9 subPostamble( PDLEVEL1, '$', $rv );
337  
338 9 return $rv;
339 }
340  
341 3 ▶sub psubdirs ($\@;$) {
Subroutine psubdirs: called CC=3 SCAR=11.0
Pod documented
342  
343 # Purpose: Performs a preadDir but filters out all non-directory entries
344 # so that only subdirectory entries are returned. Can
345 # optionally filter out symlinks to directories as well.
346 # Returns: True (1) if the directory read was successful,
347 # False (0) if there are any errors
348 # Usage: $rv = psubdirs($dir, @entries);
349 # Usage: $rv = psubdirs($dir, @entries, 1);
350  
351 3 my ( $dir, $aref, $noLinks ) = @_;
352 3 my $rv = 0;
353  
354 3 subPreamble( PDLEVEL1, '$\@;$', $dir, $aref, $noLinks );
355  
356 # Validate arguments
357 3 ▶ $noLinks = 0 unless defined $noLinks;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $noLinks 3 0 3
358  
359 # Empty target array and retrieve list
360 3 $rv = preadDir( $dir, @$aref, $noLinks );
361  
362 # Filter out all non-directories
363 3 ▶ @$aref = grep { -d $_ } @$aref if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 2 1 3
364  
365 3 pdebug( 'returning %d entries', PDLEVEL2, scalar @$aref );
366  
367 3 subPostamble( PDLEVEL1, '$', $rv );
368  
369 3 return $rv;
370 }
371  
372 3 ▶sub pfiles ($\@;$) {
Subroutine pfiles: called CC=3 SCAR=11.0
Pod documented
373  
374 # Purpose: Performs a preadDir but filters out all directory entries
375 # so that only file entries are returned. Can
376 # optionally filter out symlinks to files as well.
377 # Returns: True (1) if the directory read was successful,
378 # False (0) if there are any errors
379 # Usage: $rv = pfiles($dir, @entries);
380 # Usage: $rv = pfiles($dir, @entries, 1);
381  
382 3 my ( $dir, $aref, $noLinks ) = @_;
383 3 my $rv = 0;
384  
385 3 subPreamble( PDLEVEL1, '$\@;$', $dir, $aref, $noLinks );
386  
387 # Validate arguments
388 3 ▶ $noLinks = 0 unless defined $noLinks;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $noLinks 3 0 3
389  
390 # Empty target array and retrieve list
391 3 @$aref = ();
392 3 $rv = preadDir( $dir, @$aref, $noLinks );
393  
394 # Filter out all non-files
395 3 ▶ @$aref = grep { -f $_ } @$aref if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 2 1 3
396  
397 3 pdebug( 'returning %d entries', PDLEVEL2, scalar @$aref );
398  
399 3 subPostamble( PDLEVEL1, '$', $rv );
400  
401 3 return $rv;
402 }
403  
404 9 ▶sub pcleanPath ($) {
Subroutine pcleanPath: called CC=7 SCAR=19.5
Pod documented
405  
406 # Purpose: Removes/resolves directory artifacts like '/../', etc.
407 # Returns: Filtered string
408 # Usage: $filename = pcleanPath($filename);
409  
410 9 my $filename = shift;
411  
412 9 subPreamble( PDLEVEL1, '$', $filename );
413  
414 9 ▶ if ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 8 1 9
415  
416 # Strip all //+, /./, and /{parent}/../
417 8 ▶ while ( $filename =~ m#/\.?/+#s ) { $filename =~ s#/\.?/+#/#sg }
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[/\.?/+]s) 1 8 9
418 8 ▶ while ( $filename =~ m#/(?:(?!\.\.)[^/]{2,}|[^/])/\.\./#s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[/(?:(?!\.\.)[^/]{2,}|[^/])/\.\./]s) 6 8 14
419 6 $filename =~ s#/(?:(?!\.\.)[^/]{2,}|[^/])/\.\./#/#sg;
420 }
421  
422 # Strip trailing /. and leading /../
423 8 $filename =~ s#/\.$##s;
424 8 ▶ while ( $filename =~ m#^/\.\./#s ) { $filename =~ s#^/\.\./#/#s }
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[^/\.\./]s) 3 8 11
425  
426 # Strip any ^[^/]+/../
427 8 ▶ while ( $filename =~ m#^[^/]+/\.\./#s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[^[^/]+/\.\./]s) 1 8 9
428 1 $filename =~ s#^[^/]+/\.\./##s;
429 }
430  
431 # Strip any trailing /^[^/]+/..$
432 8 ▶ while ( $filename =~ m#/[^/]+/\.\.$#s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[/[^/]+/\.\.$]s) 1 8 9
433 1 $filename =~ s#/[^/]+/\.\.$##s;
434 }
435 }
436  
437 9 subPostamble( PDLEVEL1, '$', $filename );
438  
439 9 return $filename;
440 }
441  
442 2 ▶sub ptranslateLink ($;$) {
Subroutine ptranslateLink: called CC=9 SCAR=28.4
Pod documented
443  
444 # Purpose: Performs either a full (realpath) or a partial one (last
445 # filename element only) on the passed filename
446 # Returns: Altered filename if successful, undef if there are any
447 # failures
448 # Usage: $filename = ptranslateLink($filename);
449 # Usage: $filename = ptranslateLink($filename, 1);
450  
451 2 my $link = shift;
452 2 ▶ my $fullyTranslate = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
453 2 my $nLinks = 0;
454 2 my ( $i, $target );
455  
456 2 subPreamble( PDLEVEL1, '$;$', $link, $fullyTranslate );
457  
458 # Validate link and exit early, if need be
459 2 ▶ unless ( defined $link and scalar lstat $link ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $link and scalar lstat $link) 0 2 2
Condition50%1/2
defined $link and scalar lstat $link
!ll
02
MC/DC0%0/1
defined $link and scalar lstat $link
defined $link
Truth table50%1/2
A
A= defined $link
Aresult
00
11
460 0 Paranoid::ERROR = pdebug( 'link (%s) does not exist on filesystem',
461 PDLEVEL1, $link );
462 0 pOut();
463 0 pdebug( 'leaving w/rv: undef', PDLEVEL1 );
464 0 return undef;
465 }
466  
467 # Check every element in the path for symlinks and translate it if
468 # if a full translation was requested
469 2 ▶ if ($fullyTranslate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fullyTranslate) { } 0 2 2
470  
471 # Resolve the link
472 0 $target = realpath($link);
473  
474 # Make sure we got an answer
475 0 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) { } 0 0 0
476  
477 # Save the answer
478 0 $link = $target;
479  
480 } else {
481  
482 # Report our inability to resolve the link
483 0 Paranoid::ERROR =
484 pdebug( 'link (%s) couldn\'t be resolved fully: %s',
485 PDLEVEL1, $link, $! );
486 0 $link = undef;
487 }
488  
489 } else {
490  
491 # Is the file passed a symlink?
492 2 ▶ if ( -l $link ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-l $link) 2 0 2
493  
494 # Yes it is, let's get the target
495 2 $target = readlink $link;
496 2 pdebug( 'last element is a link to %s', PDLEVEL1, $target );
497  
498 # Is the target a relative filename?
499 2 ▶ if ( $target =~ m#^(?:\.\.?/|[^/])#s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ m[^(?:\.\.?/|[^/])]s) { } 2 0 2
500  
501 # Yupper, replace the filename with the target
502 2 $link =~ s#[^/]+$#$target#s;
503  
504 } else {
505  
506 # The target is fully qualified, so replace link entirely
507 0 $link = $target;
508 }
509 }
510 }
511  
512 2 ▶ $link = pcleanPath($link) if defined $link;
Branch: 1 branch
BranchTrueFalseTotal
if defined $link 2 0 2
513  
514 2 subPostamble( PDLEVEL1, '$', $link );
515  
516 2 return $link;
517 }
518  
519 7 ▶sub ptouch ($;$\%) {
Subroutine ptouch: called CC=15 SCAR=27.9
Pod documented
520  
521 # Purpose: Simulates a "touch" command in pure Perl
522 # Returns: True (1) if all targets were successfully touched,
523 # False (0) if there are any errors
524 # Usage: $rv = ptouch("/foo/*");
525 # Usage: $rv = ptouch("/foo/*", $tstamp);
526 # Usage: $rv = ptouch("/foo/*", $tstamp, %errors);
527  
528 7 my $target = shift;
529 7 my $stamp = shift;
530 7 my $errRef = shift;
531 7 my $rv = 1;
532 7 my $irv = 1;
533 7 my ( $glob, $tglob, $fh );
534  
535 7 subPreamble( PDLEVEL1, '$;$\%', $target, $stamp, $errRef );
536  
537 # Prep error hash
538 7 ▶ $errRef = {} unless defined $errRef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errRef 2 5 7
539 7 %$errRef = ();
540  
541 # Create a glob object if we weren't handed one.
542 7 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 7 0 7
543 7 ▶ $glob =
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'Paranoid::Glob' ? : 3 4 7
544 ref $target eq 'Paranoid::Glob'
545 ? $target
546 : Paranoid::Glob->new( globs => [$target] );
547 }
548 7 ▶ $rv = 0 unless defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 7 7
549  
550 7 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 7 0 7
551  
552 # Apply the default timestamp if omitted
553 7 ▶ $stamp = time unless defined $stamp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stamp 4 3 7
554  
555 7 ▶ unless ( detaint( $stamp, 'int' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&detaint(\$stamp, "int")) 0 7 7
556 0 Paranoid::ERROR = pdebug( 'Invalid characters in timestamp: %s',
557 PDLEVEL2, $stamp );
558 0 $rv = 0;
559 }
560 }
561  
562 # Start touching stuff
563 7 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 7 0 7
564  
565 # Consolidate the entries
566 7 $glob->consolidate;
567  
568 # Iterate over entries
569 7 foreach $target (@$glob) {
570 16 pdebug( 'processing %s', PDLEVEL2, $target );
571 16 $irv = 1;
572  
573 # Create the target if it does not exist
574 16 ▶ unless ( -e $target ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $target) 4 12 16
575 4 pdebug( 'creating empty file (%s)', PDLEVEL2, $target );
576 4 ▶ $fh = popen( $target, O_CREAT | O_EXCL | O_RDWR )
Condition66%2/3
popen($target, 194) || popen($target, 2)
l!l&&r!l&&!r
202
MC/DC50%1/2
popen($target, 194) || popen($target, 2)
popen($target, 194) popen($target, 2)
Truth table66%2/3
A || B
A= popen($target, 194)B= popen($target, 2)
ABresult
1X1
011
000
577 || popen( $target, O_RDWR );
578 4 ▶ if ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) { } 2 2 4
579 2 pclose($target);
580 } else {
581 2 $$errRef{$target} = $!;
582 2 $irv = $rv = 0;
583 }
584 }
585  
586 # Touch the file
587 16 ▶ if ($irv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irv) 14 2 16
588 14 ▶ unless ( utime $stamp, $stamp, $target ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utime $stamp, $stamp, $target) 0 14 14
589 0 $$errRef{$target} = $!;
590 0 $rv = 0;
591 }
592 }
593 }
594 }
595  
596 7 subPostamble( PDLEVEL1, '$', $rv );
597  
598 7 return $rv;
599 }
600  
601 3 ▶sub ptouchR ($;$$\%) {
Subroutine ptouchR: called CC=7 SCAR=19.6
Pod documented
602  
603 # Purpose: Calls ptouch recursively
604 # Returns: True (1) if all targets were successfully touched,
605 # False (0) if there are any errors
606 # Usage: $rv = ptouchR("/foo");
607 # Usage: $rv = ptouchR("/foo", $tstamp);
608 # Usage: $rv = ptouchR("/foo", $tstamp, $follow);
609 # Usage: $rv = ptouchR("/foo", $tstamp, $follow, %errors);
610  
611 3 my $target = shift;
612 3 my $stamp = shift;
613 3 my $follow = shift;
614 3 my $errRef = shift;
615 3 my $rv = 1;
616 3 my ( $glob, $tglob );
617  
618 3 subPreamble( PDLEVEL1, '$;$$\%', $target, $stamp, $follow, $errRef );
619  
620 # Prep error hash
621 3 ▶ $errRef = {} unless defined $errRef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errRef 1 2 3
622 3 %$errRef = ();
623  
624 # Create a glob object if we weren't handed one.
625 3 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 3 0 3
626 3 ▶ $glob =
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'Paranoid::Glob' ? : 1 2 3
627 ref $target eq 'Paranoid::Glob'
628 ? $target
629 : Paranoid::Glob->new( globs => [$target] );
630 }
631 3 ▶ $rv = 0 unless defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 3 3
632  
633 3 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 3 0 3
634  
635 # Load the directory tree and execute prm
636 3 ▶ $rv = $glob->recurse( $follow, 1 )
Condition66%2/3
$glob->recurse($follow, 1) && &ptouch($glob, $stamp, \%$errRef)
!ll&&!rl&&r
012
MC/DC50%1/2
$glob->recurse($follow, 1) && &ptouch($glob, $stamp, \%$errRef)
$glob->recurse($follow, 1) &ptouch($glob, $stamp, \%$errRef)
Truth table66%2/3
A && B
A= $glob->recurse($follow, 1)B= &ptouch($glob, $stamp, \%$errRef)
ABresult
0X0
100
111
637 && ptouch( $glob, $stamp, %$errRef );
638 }
639  
640 3 subPostamble( PDLEVEL1, '$', $rv );
641  
642 3 return $rv;
643 }
644  
645 22 ▶sub ptranslatePerms ($) {
Subroutine ptranslatePerms: called CC=19 SCAR=29.7
Pod documented
646  
647 # Purpose: Translates symbolic permissions (as supported by userland
648 # chmod, etc.) into the octal permissions.
649 # Returns: Numeric permissions if valid symbolic permissions were passed,
650 # undef otherwise
651 # Usage: $perm = ptranslatePerms('ug+srw');
652  
653 22 my $perm = shift;
654 22 my $rv = undef;
655 22 my ( @tmp, $o, $p );
656  
657 22 subPreamble( PDLEVEL1, '$', $perm );
658  
659 # Validate permissions string
660 22 ▶ if ( defined $perm and $perm =~ /^\d+$/s ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $perm and $perm =~ /^\d+$/s) { } 13 9 22
elsif (defined $perm and $perm =~ /^([ugo]+)([+\-])([rwxst]+)$/s) { } 6 3 9
Condition66%2/3
defined $perm and $perm =~ /^\d+$/s
!ll&&!rl&&r
0913
MC/DC50%1/2
defined $perm and $perm =~ /^\d+$/s
defined $perm $perm =~ /^\d+$/s
Truth table66%2/3
A and B
A= defined $permB= $perm =~ /^\d+$/s
ABresult
0X0
100
111
661  
662 13 ▶ if ( $perm =~ /^0/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perm =~ /^0/s) { } 2 11 13
663 2 ▶ if ( $perm =~ /^0[0-8]{3,4}$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perm =~ /^0[0-8]{3,4}$/s) { } 1 1 2
664  
665 # String representation of octal number
666 1 eval "\$perm = $perm;";
667 1 detaint( $perm, 'int', $p );
668  
669 } else {
670 1 pdebug( 'invalid octal presentation: %s', PDLEVEL1, $perm );
671 }
672  
673 } else {
674  
675 # Probably a converted integer already, treat it as verbatim
676 11 detaint( $perm, 'int', $p );
677 }
678  
679 ▶ } elsif ( defined $perm and $perm =~ /^([ugo]+)([+\-])([rwxst]+)$/s ) {
Condition66%2/3
defined $perm and $perm =~ /^([ugo]+)([+\-])([rwxst]+)$/s
!ll&&!rl&&r
036
MC/DC50%1/2
defined $perm and $perm =~ /^([ugo]+)([+\-])([rwxst]+)$/s
defined $perm $perm =~ /^([ugo]+)([+\-])([rwxst]+)$/s
Truth table66%2/3
A and B
A= defined $permB= $perm =~ /^([ugo]+)([+\-])([rwxst]+)$/s
ABresult
0X0
100
111
680  
681 # Translate symbolic representation
682 6 $o = $p = 00;
683 6 @tmp = ( $1, $2, $3 );
684 6 ▶ $o = S_IRWXU if $tmp[0] =~ /u/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[0] =~ /u/s 3 3 6
685 6 ▶ $o |= S_IRWXG if $tmp[0] =~ /g/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[0] =~ /g/s 2 4 6
686 6 ▶ $o |= S_IRWXO if $tmp[0] =~ /o/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[0] =~ /o/s 4 2 6
687 6 ▶ $p = ( S_IRUSR | S_IRGRP | S_IROTH ) if $tmp[2] =~ /r/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[2] =~ /r/s 6 0 6
688 6 ▶ $p |= ( S_IWUSR | S_IWGRP | S_IWOTH ) if $tmp[2] =~ /w/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[2] =~ /w/s 5 1 6
689 6 ▶ $p |= ( S_IXUSR | S_IXGRP | S_IXOTH ) if $tmp[2] =~ /x/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[2] =~ /x/s 6 0 6
690 6 $p &= $o;
691 6 ▶ $p |= S_ISVTX if $tmp[2] =~ /t/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[2] =~ /t/s 1 5 6
692 6 ▶ $p |= S_ISGID if $tmp[2] =~ /s/s && $tmp[0] =~ /g/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[2] =~ /s/s and $tmp[0] =~ /g/s 0 6 6
Condition66%2/3
$tmp[2] =~ /s/s and $tmp[0] =~ /g/s
!ll&&!rl&&r
510
MC/DC0%0/2
$tmp[2] =~ /s/s and $tmp[0] =~ /g/s
$tmp[2] =~ /s/s $tmp[0] =~ /g/s
Truth table66%2/3
A and B
A= $tmp[2] =~ /s/sB= $tmp[0] =~ /g/s
ABresult
0X0
100
111
693 6 ▶ $p |= S_ISUID if $tmp[2] =~ /s/s && $tmp[0] =~ /u/s;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp[2] =~ /s/s and $tmp[0] =~ /u/s 1 5 6
Condition66%2/3
$tmp[2] =~ /s/s and $tmp[0] =~ /u/s
!ll&&!rl&&r
501
MC/DC50%1/2
$tmp[2] =~ /s/s and $tmp[0] =~ /u/s
$tmp[2] =~ /s/s $tmp[0] =~ /u/s
Truth table66%2/3
A and B
A= $tmp[2] =~ /s/sB= $tmp[0] =~ /u/s
ABresult
0X0
100
111
694  
695 } else {
696  
697 # Report invalid characters in permission string
698 3 Paranoid::ERROR =
699 pdebug( 'invalid permissions (%s)', PDLEVEL1, $perm );
700  
701 }
702 22 $rv = $p;
703  
704 22 ▶ subPostamble( PDLEVEL1, '$', defined $rv ? sprintf( '%04o', $rv ) : $rv );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv ? : 18 4 22
705  
706 22 return $rv;
707 }
708  
709 8 ▶sub pchmod ($$;\%) {
Subroutine pchmod: called CC=18 SCAR=31.0
Pod documented
710  
711 # Purpose: Simulates a "chmod" command in pure Perl
712 # Returns: True (1) if all targets were successfully chmod'd,
713 # False (0) if there are any errors
714 # Usage: $rv = pchmod("/foo", $perms);
715 # Usage: $rv = pchmod("/foo", $perms, %errors);
716  
717 8 my $target = shift;
718 8 my $perms = shift;
719 8 my $errRef = shift;
720 8 my $rv = 1;
721 8 my ( $glob, $tglob, @fstat );
722 8 my ( $ptrans, $cperms, $addPerms, @tmp );
723  
724 8 subPreamble( PDLEVEL1, '$$;\%', $target, $perms, $errRef );
725  
726 # Prep error hash
727 8 ▶ $errRef = {} unless defined $errRef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errRef 1 7 8
728 8 %$errRef = ();
729  
730 # Create a glob object if we weren't handed one.
731 8 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 8 0 8
732 8 ▶ $glob =
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'Paranoid::Glob' ? : 6 2 8
733 ref $target eq 'Paranoid::Glob'
734 ? $target
735 : Paranoid::Glob->new( globs => [$target] );
736 }
737 8 ▶ $rv = 0 unless defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 8 8
738  
739 # Convert perms if they're symbolic
740 8 ▶ if ( defined $perms and defined( $ptrans = ptranslatePerms($perms) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $perms and defined($ptrans = &ptranslatePerms($perms))) { } 8 0 8
Condition33%1/3
defined $perms and defined($ptrans = &ptranslatePerms($perms))
!ll&&!rl&&r
008
MC/DC0%0/2
defined $perms and defined($ptrans = &ptranslatePerms($perms))
defined $perms defined($ptrans = &ptranslatePerms($perms))
Truth table33%1/3
A and B
A= defined $permsB= defined($ptrans = &ptranslatePerms($perms))
ABresult
0X0
100
111
741 8 ▶ if ( $perms =~ /[ugo]+[+-]/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perms =~ /[ugo]+[+-]/is) { } 3 5 8
742 3 ▶ $addPerms = $perms =~ /-/s ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$perms =~ /-/s ? : 0 3 3
743 } else {
744 5 $ptrans = undef;
745 }
746 } else {
747 0 pdebug( 'invalid permissions passed: %s', PDLEVEL1, $perms );
748 0 $rv = 0;
749 }
750  
751 8 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 8 0 8
752  
753 # Consolidate the entries
754 8 $glob->consolidate;
755  
756 # Iterate over entries
757 8 foreach (@$glob) {
758 20 pdebug( 'processing %s', PDLEVEL2, $_ );
759  
760 20 ▶ if ( defined $ptrans ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ptrans) { } 8 12 20
761  
762 # Get the current file mode
763 8 @fstat = stat $_;
764 8 ▶ unless (@fstat) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fstat) 1 7 8
765 1 $rv = 0;
766 1 $$errRef{$_} = $!;
767 1 Paranoid::ERROR =
768 pdebug( 'failed to adjust permissions of %s: %s',
769 PDLEVEL1, $_, $! );
770 1 next;
771 }
772  
773 # If ptrans is defined we're going to do relative
774 # application of permissions
775 pdebug(
776 7 ▶ $addPerms
Branch: 1 branch
BranchTrueFalseTotal
$addPerms ? : 7 0 7
777 ? sprintf( 'adding perms %04o', $ptrans )
778 : sprintf( 'removing perms %04o', $ptrans ),
779 PDLEVEL2
780 );
781  
782 # Get the current permissions
783 7 $cperms = $fstat[2] & PERMMASK;
784 7 pdebug(
785 sprintf( 'current permissions of %s: %04o', $_, $cperms ),
786 PDLEVEL2
787 );
788 7 ▶ $cperms =
Branch: 1 branch
BranchTrueFalseTotal
$addPerms ? : 7 0 7
789 $addPerms
790 ? ( $cperms | $ptrans )
791 : ( $cperms & ( PERMMASK ^ $ptrans ) );
792 7 pdebug( sprintf( 'new permissions of %s: %04o', $_, $cperms ),
793 PDLEVEL2 );
794 7 ▶ unless ( chmod $cperms, $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod $cperms, $_) 0 7 7
795 0 $rv = 0;
796 0 $$errRef{$_} = $!;
797 0 Paranoid::ERROR =
798 pdebug( 'failed to adjust permissions of %s: %s',
799 PDLEVEL1, $_, $! );
800 }
801  
802 } else {
803  
804 # Otherwise, the permissions are explicit
805 #
806 # Detaint number mode
807 12 ▶ if ( detaint( $perms, 'int' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&detaint(\$perms, 'int')) { } 12 0 12
808  
809 # Detainted, now apply
810 12 pdebug(
811 sprintf(
812 'assigning permissions of %04o to %s',
813 $perms, $_
814 ),
815 PDLEVEL2
816 );
817 12 ▶ unless ( chmod $perms, $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chmod $perms, $_) 2 10 12
818 2 $rv = 0;
819 2 $$errRef{$_} = $!;
820 }
821 } else {
822  
823 # Detainting failed -- report
824 0 $$errRef{$_} = $!;
825 0 Paranoid::ERROR =
826 pdebug( 'failed to detaint permissions mode',
827 PDLEVEL1 );
828 0 $rv = 0;
829 }
830 }
831 }
832 }
833  
834 8 subPostamble( PDLEVEL1, '$', $rv );
835  
836 8 return $rv;
837 }
838  
839 4 ▶sub pchmodR ($$;$\%) {
Subroutine pchmodR: called CC=7 SCAR=19.8
Pod documented
840  
841 # Purpose: Recursively calls pchmod
842 # Returns: True (1) if all targets were successfully chmod'd,
843 # False (0) if there are any errors
844 # Usage: $rv = pchmodR("/foo", $perms);
845 # Usage: $rv = pchmodR("/foo", $perms, $follow);
846 # Usage: $rv = pchmodR("/foo", $perms, $follow, %errors);
847  
848 4 my $target = shift;
849 4 my $perms = shift;
850 4 my $follow = shift;
851 4 my $errRef = shift;
852 4 my $rv = 1;
853 4 my ( $glob, $tglob );
854  
855 4 subPreamble( PDLEVEL1, '$$;$\%', $target, $perms, $follow, $errRef );
856  
857 # Prep error hash
858 4 ▶ $errRef = {} unless defined $errRef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errRef 2 2 4
859 4 %$errRef = ();
860  
861 # Create a glob object if we weren't handed one.
862 4 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 4 0 4
863 4 ▶ $glob =
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'Paranoid::Glob' ? : 0 4 4
864 ref $target eq 'Paranoid::Glob'
865 ? $target
866 : Paranoid::Glob->new( globs => [$target] );
867 }
868 4 ▶ $rv = 0 unless defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 4 4
869  
870 4 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 4 0 4
871  
872 # Load the directory tree and execute pchmod
873 4 ▶ $rv = $glob->recurse( $follow, 1 )
Condition66%2/3
$glob->recurse($follow, 1) && &pchmod($glob, $perms, \%$errRef)
!ll&&!rl&&r
013
MC/DC50%1/2
$glob->recurse($follow, 1) && &pchmod($glob, $perms, \%$errRef)
$glob->recurse($follow, 1) &pchmod($glob, $perms, \%$errRef)
Truth table66%2/3
A && B
A= $glob->recurse($follow, 1)B= &pchmod($glob, $perms, \%$errRef)
ABresult
0X0
100
111
874 && pchmod( $glob, $perms, %$errRef );
875 }
876  
877 4 subPostamble( PDLEVEL1, '$', $rv );
878  
879 4 return $rv;
880 }
881  
882 0 ▶sub pchown ($$;$\%) {
Subroutine pchown: not called CC=16 SCAR=56.1
Pod documented
883  
884 # Purpose: Simulates a "chown" command in pure Perl
885 # Returns: True (1) if all targets were successfully owned,
886 # False (0) if there are any errors
887 # Usage: $rv = pchown("/foo", $user);
888 # Usage: $rv = pchown("/foo", $user, $group);
889 # Usage: $rv = pchown("/foo", $user, $group, %errors);
890  
891 0 my $target = shift;
892 0 my $user = shift;
893 0 my $group = shift;
894 0 my $errRef = shift;
895 0 my $rv = 1;
896 0 my ( $glob, $tglob, @fstat );
897  
898 0 subPreamble( PDLEVEL1, '$$;$\%', $target, $user, $group, $errRef );
899  
900 # Translate to UID/GID
901 0 ▶ $user = -1 unless defined $user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user 0 0 0
902 0 ▶ $group = -1 unless defined $group;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group 0 0 0
903 0 ▶ $user = ptranslateUser($user) unless $user =~ /^-?\d+$/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $user =~ /^-?\d+$/s 0 0 0
904 0 ▶ $group = ptranslateGroup($group) unless $group =~ /^-?\d+$/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $group =~ /^-?\d+$/s 0 0 0
905 0 ▶ unless ( defined $user and defined $group ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user and defined $group) 0 0 0
Condition0%0/3
defined $user and defined $group
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user and defined $group
defined $user defined $group
Truth table0%0/3
A and B
A= defined $userB= defined $group
ABresult
0X0
100
111
906 0 $rv = 0;
907 0 Paranoid::ERROR =
908 pdebug( 'unsuccessful at translating uid/gid', PDLEVEL1 );
909 }
910  
911 # Prep error hash
912 0 ▶ $errRef = {} unless defined $errRef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errRef 0 0 0
913 0 %$errRef = ();
914  
915 # Create a glob object if we weren't handed one.
916 0 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 0 0 0
917 0 ▶ $glob =
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'Paranoid::Glob' ? : 0 0 0
918 ref $target eq 'Paranoid::Glob'
919 ? $target
920 : Paranoid::Glob->new( globs => [$target] );
921 }
922 0 ▶ $rv = 0 unless defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 0 0
923  
924 0 ▶ if ( $rv and ( $user != -1 or $group != -1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and $user != -1 || $group != -1) 0 0 0
Condition0%0/6
$rv and $user != -1 || $group != -1
!ll&&!rl&&r
000
$user != -1 or $group != -1
l!l&&r!l&&!r
000
MC/DC0%0/3
$rv and $user != -1 || $group != -1
$rv $user != -1 $group != -1
Truth table0%0/4
A and B or C
A= $rvB= $user != -1C= $group != -1
ABCresult
0XX0
1000
11X1
1011
925  
926 # Proceed
927 0 pdebug( 'UID: %s GID: %s', PDLEVEL2, $user, $group );
928  
929 # Consolidate the entries
930 0 $glob->consolidate;
931  
932 # Process the list
933 0 foreach (@$glob) {
934  
935 0 pdebug( 'processing %s', PDLEVEL2, $_ );
936  
937 0 ▶ unless ( chown $user, $group, $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chown $user, $group, $_) 0 0 0
938 0 $rv = 0;
939 0 $$errRef{$_} = $!;
940 0 Paranoid::ERROR =
941 pdebug( 'failed to adjust ownership of %s: %s',
942 PDLEVEL1, $_, $! );
943 }
944 }
945 }
946  
947 0 subPostamble( PDLEVEL1, '$', $rv );
948  
949 0 return $rv;
950 }
951  
952 0 ▶sub pchownR ($$;$$\%) {
Subroutine pchownR: not called CC=7 SCAR=40.3
Pod documented
953  
954 # Purpose: Calls pchown recursively
955 # Returns: True (1) if all targets were successfully owned,
956 # False (0) if there are any errors
957 # Usage: $rv = pchownR("/foo", $user);
958 # Usage: $rv = pchownR("/foo", $user, $group);
959 # Usage: $rv = pchownR("/foo", $user, $group, $follow);
960 # Usage: $rv = pchownR("/foo", $user, $group, $follow, %errors);
961  
962 0 my $target = shift;
963 0 my $user = shift;
964 0 my $group = shift;
965 0 my $follow = shift;
966 0 my $errRef = shift;
967 0 my $rv = 1;
968 0 my ( $glob, $tglob );
969  
970 0 subPreamble( PDLEVEL1, '$$;$$\%', $target, $user, $group, $follow,
971 $errRef );
972  
973 # Prep error hash
974 0 ▶ $errRef = {} unless defined $errRef;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errRef 0 0 0
975 0 %$errRef = ();
976  
977 # Create a glob object if we weren't handed one.
978 0 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 0 0 0
979 0 ▶ $glob =
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'Paranoid::Glob' ? : 0 0 0
980 ref $target eq 'Paranoid::Glob'
981 ? $target
982 : Paranoid::Glob->new( globs => [$target] );
983 }
984 0 ▶ $rv = 0 unless defined $glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $glob 0 0 0
985  
986 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 0 0
987  
988 # Load the directory tree and execute pchown
989 0 ▶ $rv = $glob->recurse( $follow, 1 )
Condition0%0/3
$glob->recurse($follow, 1) && &pchown($glob, $user, $group, \%$errRef)
!ll&&!rl&&r
000
MC/DC0%0/2
$glob->recurse($follow, 1) && &pchown($glob, $user, $group, \%$errRef)
$glob->recurse($follow, 1) &pchown($glob, $user, $group, \%$errRef)
Truth table0%0/3
A && B
A= $glob->recurse($follow, 1)B= &pchown($glob, $user, $group, \%$errRef)
ABresult
0X0
100
111
990 && pchown( $glob, $user, $group, %$errRef );
991 }
992  
993 0 subPostamble( PDLEVEL1, '$', $rv );
994  
995 0 return $rv;
996 }
997  
998 2 ▶sub pwhich ($) {
Subroutine pwhich: called CC=5 SCAR=16.2
Pod documented
999  
1000 # Purpose: Simulates a "which" command in pure Perl
1001 # Returns: The full path to the requested program if successful
1002 # undef if not found
1003 # Usage: $filename = pwhich('ls');
1004  
1005 2 my $binary = shift;
1006 2 my @directories = grep /^.+$/s, split /:/s, $ENV{PATH};
1007 2 my $match = undef;
1008  
1009 2 subPreamble( PDLEVEL1, '$', $binary );
1010  
1011 # Try to detaint filename
1012 2 ▶ if ( detaint( $binary, 'filename', $b ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&detaint(\$binary, 'filename', \$b)) { } 2 0 2
1013  
1014 # Success -- start searching directories in PATH
1015 2 foreach (@directories) {
1016 5 pdebug( 'searching %s', PDLEVEL2, $_ );
1017 5 ▶ if ( -r "$_/$b" && -x _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r "$_/$b" and -x _) 1 4 5
Condition66%2/3
-r "$_/$b" and -x _
!ll&&!rl&&r
401
MC/DC50%1/2
-r "$_/$b" and -x _
-r "$_/$b" -x _
Truth table66%2/3
A and B
A= -r "$_/$b"B= -x _
ABresult
0X0
100
111
1018 1 $match = "$_/$b";
1019 1 $match =~ s#/+#/#sg;
1020 1 last;
1021 }
1022 }
1023  
1024 } else {
1025  
1026 # Report detaint failure
1027 0 Paranoid::ERROR = pdebug( 'failed to detaint %s', PDLEVEL1, $binary );
1028 }
1029  
1030 2 subPostamble( PDLEVEL1, '$', $match );
1031  
1032 2 return $match;
1033 }
1034  
1035 21 1;
1036  
1037 __END__
« Paranoid/Process.pm ↑ Summary Paranoid/Log.pm »