Paranoid/Glob.pm

filter: Statementstmt 70.5% 151 / 214 Branchbran 77.0% 37 / 48 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 70.0% 21 / 30 Podpod 100% 17 / 17 totaltotal 72.3% 238 / 329 CC 34 SCAR 16.1
CC
34
Cov
72%
new
9.4
__ANON__
9.3
addGlobs
7.2
CRAP 5.0
« Paranoid/Module.pm ↑ Summary Paranoid/Log/File.pm »
1 # Paranoid::Glob -- Paranoid Glob objects
2 #
3 # $Id: lib/Paranoid/Glob.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::Glob;
33  
34 22 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 22 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 22 ▶use Errno qw(:POSIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 22 ▶use Fcntl qw(:mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 22 ▶use File::Glob qw(bsd_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 22 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 22 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 22 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/s );
48  
49 #####################################################################
50 #
51 # Module code follows
52 #
53 #####################################################################
54  
55 36 ▶sub _sanitize (\@) {
Subroutine _sanitize: called CC=3 SCAR=12.0
56  
57 # Purpose: Detaints passed strings
58 # Returns: True if successful, false on any detaint errors
59 # Usage: $rv = _sanitize(@globs);
60  
61 36 my $aref = shift;
62 36 my $rv = 1;
63  
64 # Make sure all glob entries are sane
65 36 foreach (@$aref) {
66 43 ▶ if (/^([[:print:]]+)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([[:print:]]+)$/s) { } 43 0 43
67 43 $_ = $1;
68 43 $_ =~ s#/{2,}#/#sg;
69 } else {
70 0 $Paranoid::ERROR =
71 pdebug( 'invalid glob entry: %s', PDLEVEL1, $_ );
72 0 $rv = 0;
73 0 last;
74 }
75 }
76  
77 36 return $rv;
78 }
79  
80 37 ▶sub new {
Subroutine new: called CC=9 SCAR=22.4
Pod documented
81  
82 # Purpose: Instantiates a new object of this class
83 # Returns: Object reference if successful, undef otherwise
84 # Usage: $obj = Paranoid::Glob->new(
85 # globs => [ qw(/lib/* /sbin/*) ],
86 # literals => [ qw(/lib/{sadfe-asda}) ],
87 # );
88  
89 37 my ( $class, %args ) = splice @_;
90 37 my $self = [];
91 37 my $rv = 1;
92  
93 37 subPreamble( PDLEVEL1, '%', %args );
94  
95 # Validate arguments
96 37 ▶ if ( exists $args{globs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"globs"}) 33 4 37
97 croak 'Optional key/value pair "globs" not properly defined'
98 33 ▶ unless ref $args{globs} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"globs"} eq "ARRAY" 0 33 33
99 }
100 37 ▶ if ( exists $args{literals} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"literals"}) 3 34 37
101 croak 'Optional key/value pair "literals" not properly defined'
102 3 ▶ unless ref $args{literals} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"literals"} eq "ARRAY" 0 3 3
103 }
104  
105 37 bless $self, $class;
106  
107 # Add any globs or literals if they were passed during inititation
108 3 $rv = $self->addLiterals( @{ $args{literals} } )
109 37 ▶ if exists $args{literals};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"literals"} 3 34 37
110 37 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 37 0 37
111 37 ▶ $rv = $self->addGlobs( @{ $args{globs} } ) if exists $args{globs};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"globs"} 33 4 37
112 }
113  
114 37 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 37 0 37
115 37 $rv = $self;
116 } else {
117 0 $rv = 'undef';
118 0 $self = undef;
119 }
120  
121 37 subPostamble( PDLEVEL1, '$', $self );
122  
123 37 return $self;
124 }
125  
126 33 ▶sub addGlobs {
Subroutine addGlobs: called CC=7 SCAR=19.7
Pod documented
127  
128 # Purpose: Adds more globs to the object that need to be filtered through
129 # the bsd_glob
130 # Returns: True if all globs passed muster, false if not
131 # Usage: $rv = $obj->addGlobs(qw(/etc/* /root/*));
132  
133 33 my ( $self, @globs ) = splice @_;
134 33 my $rv = 1;
135 33 my @tmp;
136  
137 33 subPreamble( PDLEVEL1, '@' );
138  
139 # Silently remove undefs and zero strings
140 33 ▶ @globs = grep { defined $_ and length $_ } @globs;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
141  
142 # Make sure all glob entries are sane
143 33 $rv = _sanitize(@globs);
144  
145 33 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 33 0 33
146  
147 # Filter them through bsd_glob unless the file exists as named in the
148 # literal string
149 33 foreach (@globs) {
150 40 ▶ push @tmp, -e $_ ? $_ : bsd_glob($_);
Branch: 1 branch
BranchTrueFalseTotal
-e $_ ? : 13 27 40
151 }
152  
153 # Final detaint
154 33 ▶ foreach (@tmp) { /^([[:print:]]+)$/s and $_ = $1 }
Branch: 1 branch
BranchTrueFalseTotal
/^([[:print:]]+)$/s and $_ = $1 167 0 167
155  
156 33 pdebug( 'added %d entries', PDLEVEL2, scalar @tmp );
157  
158 # Add to ourself
159 33 push @$self, splice @tmp;
160 }
161  
162 33 subPostamble( PDLEVEL1, '$', $rv );
163  
164 33 return $rv;
165 }
166  
167 3 ▶sub addLiterals {
Subroutine addLiterals: called CC=3 SCAR=11.3
Pod documented
168  
169 # Purpose: Adds more globs to the object as literal strings
170 # Returns: True if all globs passed muster, false if not
171 # Usage: $rv = $obj->addLiterals(qw(/etc/* /root/*));
172  
173 3 my ( $self, @globs ) = splice @_;
174 3 my $rv = 1;
175  
176 3 subPreamble( PDLEVEL1, '@' );
177  
178 # Silently remove undefs and zero strings
179 3 ▶ @globs = grep { defined $_ and length $_ } @globs;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
003
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
180  
181 # Make sure all glob entries are sane
182 3 $rv = _sanitize(@globs);
183  
184 3 ▶ push @$self, splice @globs if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 3 0 3
185  
186 3 subPostamble( PDLEVEL1, '$', $rv );
187  
188 3 return $rv;
189 }
190  
191 50 ▶sub consolidate {
Subroutine consolidate: called CC=1 SCAR=0.0
Pod documented
192  
193 # Purpose: Removes redundant entries and sorts alphabetically
194 # Returns: True
195 # Usage: $obj->consolidate;
196  
197 50 my ($self) = @_;
198 50 my (%tmp);
199  
200 50 pdebug( 'entering w/%d entries', PDLEVEL1, scalar @$self );
201 50 subPreamble(PDLEVEL1);
202  
203 50 %tmp = map { $_ => 1 } @$self;
204 50 @$self = sort keys %tmp;
205  
206 50 pdebug( 'leaving w/%d entries', PDLEVEL1, scalar @$self );
207 50 subPostamble( PDLEVEL1, '$', 1 );
208  
209 50 return 1;
210 }
211  
212 8 ▶sub exists {
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
213  
214 # Purpose: Returns a list of the entries that exist on the file system
215 # Returns: List of existing filesystem entries
216 # Usage: @entries = $obj->existing;
217  
218 8 my ($self) = @_;
219 8 my @entries = grep { scalar lstat $_ } @$self;
220  
221 8 subPreamble(PDLEVEL1);
222 8 subPostamble( PDLEVEL1, '@', @entries );
223  
224 8 return @entries;
225 }
226  
227 0 ▶sub readable {
Subroutine readable: not called CC=1 SCAR=6.9
Pod documented
228  
229 # Purpose: Returns a list of the entries that are readable by the
230 # effective user
231 # Returns: List of readable entries
232 # Usage: @entries = $obj->readable;
233  
234 0 my ($self) = @_;
235 0 my @entries = grep { -r $_ } $self->exists;
236  
237 0 subPreamble(PDLEVEL1);
238 0 subPostamble( PDLEVEL1, '@', @entries );
239  
240 0 return @entries;
241 }
242  
243 0 ▶sub writable {
Subroutine writable: not called CC=1 SCAR=6.9
Pod documented
244  
245 # Purpose: Returns a list of the entries that are writable by the
246 # effective user
247 # Returns: List of writable entries
248 # Usage: @entries = $obj->writable;
249  
250 0 my ($self) = @_;
251 0 my @entries = grep { -w $_ } $self->exists;
252  
253 0 subPreamble(PDLEVEL1);
254 0 subPostamble( PDLEVEL1, '@', @entries );
255  
256 0 return @entries;
257 }
258  
259 0 ▶sub executable {
Subroutine executable: not called CC=1 SCAR=6.9
Pod documented
260  
261 # Purpose: Returns a list of the entries that are executable/traversable
262 # by the effective user
263 # Returns: List of executable/traversable entries
264 # Usage: @entries = $obj->executable;
265  
266 0 my ($self) = @_;
267 0 my @entries = grep { -x $_ } $self->exists;
268  
269 0 subPreamble(PDLEVEL1);
270 0 subPostamble( PDLEVEL1, '@', @entries );
271  
272 0 return @entries;
273 }
274  
275 0 ▶sub owned {
Subroutine owned: not called CC=1 SCAR=6.9
Pod documented
276  
277 # Purpose: Returns a list of the entries that are owned by the
278 # effective user
279 # Returns: List of owned entries
280 # Usage: @entries = $obj->owned;
281  
282 0 my ($self) = @_;
283 0 my @entries = grep { -o $_ } $self->exists;
284  
285 0 subPreamble(PDLEVEL1);
286 0 subPostamble( PDLEVEL1, '@', @entries );
287  
288 0 return @entries;
289 }
290  
291 3 ▶sub directories {
Subroutine directories: called CC=1 SCAR=0.0
Pod documented
292  
293 # Purpose: Returns a list of existing directories
294 # Returns: List of directories
295 # Usage: @dirs = $obj->directories;
296  
297 3 my ($self) = @_;
298 3 my @dirs = grep { -d $_ } $self->exists;
299  
300 3 subPreamble(PDLEVEL1);
301 3 subPostamble( PDLEVEL1, '@', @dirs );
302  
303 3 return @dirs;
304 }
305  
306 0 ▶sub files {
Subroutine files: not called CC=1 SCAR=6.9
Pod documented
307  
308 # Purpose: Returns a list of existing files
309 # Returns: List of files
310 # Usage: @files = $obj->files;
311  
312 0 my ($self) = @_;
313 0 my @files = grep { -f $_ } $self->exists;
314  
315 0 subPreamble(PDLEVEL1);
316 0 subPostamble( PDLEVEL1, '@', @files );
317  
318 0 return @files;
319 }
320  
321 2 ▶sub symlinks {
Subroutine symlinks: called CC=1 SCAR=0.0
Pod documented
322  
323 # Purpose: Returns a list of existing symlinks
324 # Returns: List of symlinks
325 # Usage: @files = $obj->symlinks;
326  
327 2 my ($self) = @_;
328 2 my @symlinks = grep { -l $_ } $self->exists;
329  
330 2 subPreamble(PDLEVEL1);
331 2 subPostamble( PDLEVEL1, '@', @symlinks );
332  
333 2 return @symlinks;
334 }
335  
336 0 ▶sub pipes {
Subroutine pipes: not called CC=1 SCAR=6.9
Pod documented
337  
338 # Purpose: Returns a list of existing pipes
339 # Returns: List of pipes
340 # Usage: @files = $obj->pipes;
341  
342 0 my ($self) = @_;
343 0 my @pipes = grep { -p $_ } $self->exists;
344  
345 0 subPreamble(PDLEVEL1);
346 0 subPostamble( PDLEVEL1, '@', @pipes );
347  
348 0 return @pipes;
349 }
350  
351 0 ▶sub sockets {
Subroutine sockets: not called CC=1 SCAR=6.9
Pod documented
352  
353 # Purpose: Returns a list of existing sockets
354 # Returns: List of sockets
355 # Usage: @files = $obj->sockets;
356  
357 0 my ($self) = @_;
358 0 my @sockets = grep { -S $_ } $self->exists;
359  
360 0 subPreamble(PDLEVEL1);
361 0 subPostamble( PDLEVEL1, '@', @sockets );
362  
363 0 return @sockets;
364 }
365  
366 0 ▶sub blockDevs {
Subroutine blockDevs: not called CC=1 SCAR=6.9
Pod documented
367  
368 # Purpose: Returns a list of existing block nodes
369 # Returns: List of block devs
370 # Usage: @files = $obj->blockDevs;
371  
372 0 my ($self) = @_;
373 0 my @bdevs = grep { -b $_ } $self->exists;
374  
375 0 subPreamble(PDLEVEL1);
376 0 subPostamble( PDLEVEL1, '@', @bdevs );
377  
378 0 return @bdevs;
379 }
380  
381 0 ▶sub charDevs {
Subroutine charDevs: not called CC=1 SCAR=6.9
Pod documented
382  
383 # Purpose: Returns a list of existing character nodes
384 # Returns: List of character devs
385 # Usage: @files = $obj->charDevs;
386  
387 0 my ($self) = @_;
388 0 my @cdevs = grep { -c $_ } $self->exists;
389  
390 0 subPreamble(PDLEVEL1);
391 0 subPostamble( PDLEVEL1, '@', @cdevs );
392  
393 0 return @cdevs;
394 }
395  
396 14 ▶sub recurse {
Subroutine recurse: called CC=3 SCAR=11.0
Pod documented
397  
398 # Purpose: Recursively adds all subdirectories and their contents to the
399 # glob. Passing an optional boolean argument will tell it
400 # whether or not to follow symlinks. Defaults to not following
401 # symlinks (false). Another optional boolean argument instructs
402 # this method whether or not to include hidden directories. In
403 # accordance with the traditional behavior of shell globbing it
404 # defaults to false.
405 # Returns: True if successful, false on any errors (like permission
406 # denied, etc.)
407 # Usage: $rv = $obj->recurse;
408 # Usage: $rv = $obj->recurse(1);
409 # Usage: $rv = $obj->recurse(1, 1);
410  
411 14 my ( $self, $follow, $hidden ) = @_;
412 14 my $rv = 1;
413 14 my ( %seen, @crawl, $lindex, $slindex );
414  
415 14 subPreamble(PDLEVEL1);
416  
417 # Define our dirFilter sub, who's sole purpose is to extract a list of
418 # directories from the passed list of entries
419 54 ▶ my $dirFilter = sub {
Subroutine __ANON__: called CC=9 SCAR=22.3
420 54 my @entries = @_;
421 54 my ( $entry, @fstat, @dirs );
422  
423 # Extract a list of directories from our current contents
424 54 foreach $entry (@entries) {
425 66 @fstat = lstat $entry;
426 66 ▶ if (@fstat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fstat) { } 62 4 66
427  
428 # Entry exists
429 62 ▶ if ( S_ISDIR( $fstat[2] ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (S_ISDIR($fstat[2])) { } 44 18 62
elsif ($follow and S_ISLNK($fstat[2])) { } 4 24 28
430  
431 # Filter out sockets, etc.
432 44 ▶ next if $fstat[2] &
Branch: 1 branch
BranchTrueFalseTotal
if $fstat[2] & push(@dirs, $entry) 34 10 44
433  
434 # Add the directory
435 push @dirs, $entry;
436  
437 ▶ } elsif ( $follow and S_ISLNK( $fstat[2] ) ) {
Condition100%3/3
$follow and S_ISLNK($fstat[2])
!ll&&!rl&&r
1044
MC/DC100%2/2
$follow and S_ISLNK($fstat[2])
$follow S_ISLNK($fstat[2])
Truth table100%3/3
A and B
A= $followB= S_ISLNK($fstat[2])
ABresult
0X0
100
111
438  
439 # Add symlinks pointing to directories if we're set
440 # to follow
441 4 ▶ push @dirs, $entry if -d $entry;
Branch: 1 branch
BranchTrueFalseTotal
if -d $entry 4 0 4
442 }
443  
444 } else {
445  
446 # Report any errors for anything other than ENOENT
447 4 ▶ unless ( $! == ENOENT ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($! == 2) 0 4 4
448 0 Paranoid::ERROR = pdebug( 'couldn\'t access %s: %s',
449 PDLEVEL1, $entry, $! );
450 0 $rv = 0;
451 }
452 }
453 }
454  
455 54 return @dirs;
456 14 };
457  
458 # Define our addDir sub, whose purpose is to return the contents of the
459 # passed directory
460 40 ▶ my $addDir = sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
461 40 my $dir = shift;
462 40 my ( $fh, @contents );
463  
464 40 ▶ if ( opendir $fh, $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir $fh, $dir) { } 40 0 40
465  
466 # Get the list, filtering out '.' & '..'
467 40 foreach ( readdir $fh ) {
468 117 ▶ next if m/^\.\.?$/s;
Branch: 1 branch
BranchTrueFalseTotal
if /^\.\.?$/s 80 37 117
469 37 ▶ next if m/^\./s and not $hidden;
Branch: 1 branch
BranchTrueFalseTotal
if /^\./s and not $hidden 1 36 37
Condition100%3/3
/^\./s and not $hidden
!ll&&!rl&&r
3331
MC/DC100%2/2
/^\./s and not $hidden
/^\./s not $hidden
Truth table100%3/3
A and not(B)
A= /^\./sB= not $hidden
ABresult
0X0
100
111
470 36 push @contents, "$dir/$_";
471 }
472 40 closedir $fh;
473  
474 } else {
475 0 Paranoid::ERROR =
476 pdebug( 'error opening directory %s: %s', PDLEVEL1, $dir,
477 $! );
478 0 $rv = 0;
479 }
480  
481 40 return @contents;
482 14 };
483  
484 # Consolidate to reduce potential redundancies
485 14 $self->consolidate;
486  
487 # Get our initial list of directories to crawl
488 14 @crawl = &$dirFilter(@$self);
489  
490 # Start crawling
491 14 $lindex = 0;
492 14 $slindex = $#$self;
493 14 ▶ while ( $lindex <= $#crawl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lindex <= $#crawl) 48 14 62
494  
495 # Skip the directory if we've already crawled it
496 48 ▶ if ( exists $seen{ $crawl[$lindex] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$crawl[$lindex]}) 8 40 48
497 8 $lindex++;
498 8 next;
499 }
500  
501 # Add the directory's contents
502 40 push @$self, ( &$addDir( $crawl[$lindex] ) );
503 40 $seen{ $crawl[$lindex] } = 0;
504 40 $lindex++;
505 40 $slindex++;
506  
507 # Add any new directories to the crawl list
508 40 push @crawl, ( &$dirFilter( @$self[ $slindex .. $#$self ] ) );
509 40 $slindex = $#$self;
510 }
511  
512 # Final consolidation
513 14 $self->consolidate;
514  
515 14 subPostamble( PDLEVEL1, '$', $rv );
516  
517 14 return $rv;
518 }
519  
520 22 1;
521  
522 __END__
« Paranoid/Module.pm ↑ Summary Paranoid/Log/File.pm »