blib/lib/Stat/lsMode.pm

filter: Statementstmt 62.9% 39 / 62 Branchbran 22.5% 9 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 50.0% 2 / 4 totaltotal 49.5% 57 / 115 CC 21 SCAR 35.1
CC
21
Cov
47%
format_mode
63.6
file_mode
10.1
novice
6.0
CRAP 33.6
↑ Summary
1 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Stat::lsMode;
4 1 our $VERSION = '0.51';
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶BEGIN { require Exporter; *import = \&Exporter::import }
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our @EXPORT = qw(format_mode file_mode format_perms);
9  
10 1 our (@perms, @ftype);
11 1 @perms = qw(--- --x -w- -wx r-- r-x rw- rwx);
12 1 @ftype = qw(. p c ? d ? b ? - ? l ? s D ? ?);
13 1 $ftype[0] = '';
14  
15 1 our $NOVICE_MODE = 1; # Default on?
16 0 ▶sub novice {
Subroutine novice: not called CC=2 SCAR=17.9
Pod undocumented
17 0 my $pack = shift;
18 0 ▶ croak "novice_mode requires one boolean argument" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
19 0 my $old = $NOVICE_MODE; # Should this be localized t $pack?
20 0 $NOVICE_MODE = $_[0];
21 0 $old;
22 }
23  
24 14 ▶sub format_mode {
Subroutine format_mode: called CC=14 SCAR=41.5
Pod documented
25 14 ▶ croak "format_mode requires a mode as an argument" unless @_ >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 1 0 14 14
26 14 my $mode = shift;
27 14 my %opts = @_;
28  
29 14 ▶ unless (defined $mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mode) 0 14 14
30 0 ▶ return wantarray() ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
31 }
32  
33 14 ▶ _novice_warning($mode) if $NOVICE_MODE;
Branch: 1 branch
BranchTrueFalseTotal
if $Stat::lsMode::NOVICE_MODE 14 0 14
34  
35 14 my $setids = ($mode & 07000)>>9;
36 14 my @permstrs = @perms[($mode&0700)>>6, ($mode&0070)>>3, $mode&0007];
37 14 my $ftype = $ftype[($mode & 0170000)>>12];
38 14 ▶ my @ftype = $opts{no_ftype} ? () : ($ftype);
Branch: 1 branch
BranchTrueFalseTotal
$opts{'no_ftype'} ? : 0 14 14
39  
40 14 ▶ if ($setids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setids) 0 14 14
41 0 ▶ if ($setids & 01) { # Sticky bit
Branch: 1 branch
BranchTrueFalseTotal
if ($setids & 1) 0 0 0
42 0 ▶ $permstrs[2] =~ s/([-x])$/$1 eq 'x' ? 't' : 'T'/e;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'x' ? : 0 0 0
43 }
44 0 ▶ if ($setids & 04) { # Setuid bit
Branch: 1 branch
BranchTrueFalseTotal
if ($setids & 4) 0 0 0
45 0 ▶ $permstrs[0] =~ s/([-x])$/$1 eq 'x' ? 's' : 'S'/e;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'x' ? : 0 0 0
46 }
47 0 ▶ if ($setids & 02) { # Setgid bit
Branch: 1 branch
BranchTrueFalseTotal
if ($setids & 2) 0 0 0
48 0 ▶ $permstrs[1] =~ s/([-x])$/$1 eq 'x' ? 's' : 'S'/e;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'x' ? : 0 0 0
49 }
50 }
51  
52 14 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 14 14
53 0 (@ftype, @permstrs);
54 } else {
55 14 join '', @ftype, @permstrs;
56 }
57 }
58  
59 6 ▶sub file_mode {
Subroutine file_mode: called CC=5 SCAR=23.1
Pod documented
60 6 ▶ croak "file_mode requires one filename as an argument" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 6 6
61 6 my $file = shift;
62 6 my $mode = (lstat $file)[2];
63  
64 6 ▶ unless (defined $mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mode) 0 6 6
65 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
66 0 return ();
67 } else {
68 0 ▶ carp "Couldn't get mode for file `$file': $!" if $NOVICE_MODE;
Branch: 1 branch
BranchTrueFalseTotal
if $Stat::lsMode::NOVICE_MODE 0 0 0
69 0 return undef;
70 }
71 }
72  
73 6 format_mode($mode, @_);
74 }
75  
76 0 ▶sub format_perms {
Subroutine format_perms: not called CC=2 SCAR=17.9
Pod undocumented
77 0 ▶ croak "format_perms requires a permission mode as an argument" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
78 0 format_mode($_[0], no_ftype => 1);
79 }
80  
81 # None of these are really plausible modes.
82 # They are all almost certain to have occurred
83 # when someone used decimal instead of octal to specify a mode.
84  
85 1 my %badmode = map +($_ => 1), (
86 777, 775, 755, 770, 700, 750,
87 751,
88 666, 664, 644, 660, 600, 640,
89 444, 440,
90 400, # 400 = rw--w---- which is just barely plausible.
91 # 000 *is* OK. It means just what you think.
92 711, 771, 751, 551, 111,
93 );
94  
95 # Novices like to ask for the bits for mode `666' instead of `0666'.
96 # Try to detect and diagnose that.
97 14 ▶sub _novice_warning {
Subroutine _novice_warning: called CC=2 SCAR=8.1
98 14 my $mode = shift;
99 14 ▶ if ($badmode{$mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($badmode{$mode}) 0 14 14
100 0 carp "mode $mode is very surprising. Perhaps you meant 0$mode";
101 }
102 }
103  
104 1 1;
105  
106 __END__
↑ Summary