blib/lib/File/Mode.pm

filter: Statementstmt 19.3% 17 / 88 Branchbran 0.0% 0 / 22 Conditioncond 3.5% 1 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 6 totaltotal 13.5% 23 / 170 CC 25 SCAR 42.5
CC
25
Cov
13%
FileList
132.0
UnixToOct
90.0
OctToUnix
20.0
CRAP 69.8
↑ Summary
1 package File::Mode;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = 0.05;
8
9 #-------------------------- File::Mode Ver 0.05 ---------------------#
10 #--------------------- ©2000 Idan Robbins aka Aqutiv ----------------#
11
12 #I was thinking... If they invented comments, why not use them?
13 #So there you go, here's a comment. And you know what? I actually like comments.
14 #Yeah, that's it. Much better, fills in some empty spaces like this.
15 #What could have been here without these comments. ah?
16 #
17 #Shit, I've nothing left to say... Ah, well. See ya in the next version. :)
18
19 #--------------------- New Object ---------------------#
20 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod undocumented
21 1 my $which = shift;
22 1 ▶ my $class = ref($which) || $which;
Condition33%1/3
ref $which || $which
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $which || $which
ref $which $which
Truth table33%1/3
A || B
A= ref $whichB= $which
ABresult
1X1
011
000
23 1 return bless {},$class;
24 }
25
26 #-------------- Unix file mode to Number Mode ---------#
27 0 ▶sub UnixToOct {
Subroutine UnixToOct: not called CC=9 SCAR=45.0
Pod undocumented
28 0 my $self = shift;
29 0 ▶ my $mod = shift || "-" x 6;
Condition0%0/2
shift() || '------'
l!l
00
MC/DC0%0/1
shift() || '------'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
30 0 ▶ my $NoType = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
31 0 my $type = undef;
32 0 my $skipchar = 0;
33 0 my $OctMod = 0;
34
35 #The common format that is used for file modes. (i.e the mode that 'ls -l' unix commands gives)
36 0 ▶ if ($mod =~ /^([d\-]?)([r\-][w\-][x\-]){3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^([d\-]?)([r\-][w\-][x\-]){3}$/) { } 0 0 0
37
38 #Determine whether it is a file or a directory. (stays undefined if none)
39 0 ▶ if (!$1) { $skipchar = 1;}
Branch: 2 branches
BranchTrueFalseTotal
if (not $1) { } 0 0 0
elsif (substr($mod, 0, 1) eq 'd') { } 0 0 0
40 0 elsif (substr($mod, 0, 1) eq "d") {$type = "DIR";}
41 0 else {$type = "FILE";}
42
43 0 ▶ $mod =~ s/^.// unless $skipchar; #get rid of the first character for easy handling.
Branch: 1 branch
BranchTrueFalseTotal
unless $skipchar 0 0 0
44
45 0 my($ownermod) = substr($mod, 0, 3);
46 0 my($groupmod) = substr($mod, 3, 3);
47 0 my($publicmod) = substr($mod, 6, 3);
48
49 0 my($dig1) = $self->GetNum($ownermod);
50 0 my($dig2) = $self->GetNum($groupmod);
51 0 my($dig3) = $self->GetNum($publicmod);
52
53 0 $OctMod = 0 . $dig1 . $dig2 . $dig3;
54
55
56 }
57
58 else {
59 #Uncomment if you want the module to be strict, otherwise will return an empty list.
60 #croak "Wrong format for Unix file mode";
61 0 return ();
62 }
63
64 0 ▶ if(!$type || $NoType) { return $OctMod}
Branch: 1 branch
BranchTrueFalseTotal
if (not $type or $NoType) { } 0 0 0
Condition0%0/3
not $type or $NoType
l!l&&r!l&&!r
000
MC/DC0%0/2
not $type or $NoType
not $type $NoType
Truth table0%0/3
not(A) or B
A= not $typeB= $NoType
ABresult
1X1
011
000
65 0 else {return $OctMod, $type}
66
67
68 }
69
70 #-------------- Numeric Mode to Unix Standart ---------#
71 0 ▶sub OctToUnix {
Subroutine OctToUnix: not called CC=4 SCAR=30.0
Pod undocumented
72 0 my $self = shift;
73 0 ▶ my $mod = shift || "000";
Condition0%0/2
shift() || '000'
l!l
00
MC/DC0%0/1
shift() || '000'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
74 0 my($UnixMod) = 0;
75
76 #Makes sure that the number is made of 3 digits (or 4, with an option 0 at start) and each one of them is 7 or smaller.
77 #For example, 755 and 0755 will both match.
78 0 ▶ if (($mod =~ /^0?\d{3}$/) && ($mod !~ /[89]/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^0?\d{3}$/ and not $mod =~ /[89]/) 0 0 0
Condition0%0/3
$mod =~ /^0?\d{3}$/ and not $mod =~ /[89]/
!ll&&!rl&&r
000
MC/DC0%0/2
$mod =~ /^0?\d{3}$/ and not $mod =~ /[89]/
$mod =~ /^0?\d{3}$/ not $mod =~ /[89]/
Truth table0%0/3
A and not(B)
A= $mod =~ /^0?\d{3}$/B= not $mod =~ /[89]/
ABresult
0X0
100
111
79
80 #A tricky method to get each one of the digits, don't you think?
81 #It also takes care of the optional 4 digits. ;)
82 0 my($ownermod) = int($mod / 100); #first
83 0 my($groupmod) = int($mod % 100 / 10); #middle
84 0 my($publicmod) = int($mod % 10); #last
85
86 0 my($str1) = $self->GetString($ownermod);
87 0 my($str2) = $self->GetString($groupmod);
88 0 my($str3) = $self->GetString($publicmod);
89
90 0 $UnixMod = $str1 . $str2 . $str3;
91
92 }
93 #Uncomment if you want the module to be strict. Otherwise, returns 0.
94 #else {
95 # croak "Illegal octal file mode";
96 #}
97
98 0 return $UnixMod;
99
100 }
101 #--------------------- String to Digit Conversion ---------------------#
102 0 ▶sub GetNum {
Subroutine GetNum: not called CC=2 SCAR=17.9
Pod undocumented
103
104 0 my $self = shift;
105 0 ▶my $ModValue = shift || "-" x 3;
Condition0%0/2
shift() || '---'
l!l
00
MC/DC0%0/1
shift() || '---'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
106
107 0 $ModValue =~ tr/\-a-z/01/; #Converts the string to a binary value.
108
109 #Convert from binary into an octal digit.
110 0 $ModValue = oct( unpack("N", pack("B32", substr("0" x 32 . $ModValue, -32))) );
111 #Took me awhile to figure this out... Ok, I admit, Grabbed it out of the perldocs...
112 #But it still took me some time to find it. ;)
113
114 0 return $ModValue;
115
116 }
117
118 #--------------------- Digit to (short) String Conversion ---------------------#
119 0 ▶sub GetString {
Subroutine GetString: not called CC=2 SCAR=17.9
Pod undocumented
120
121 0 my $self = shift;
122 0 ▶my $ModDig = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
123
124 #Convert to a binary value:
125 0 my($ModValue) = unpack('B*', $ModDig);
126 0 $ModValue =~ s/^.*(\d{3})$/$1/; #Remove useless digits.
127
128 #Binary to unix file mode convertion.
129 0 $ModValue =~ tr/0/\-/;
130 0 $ModValue =~ s/^1/r/;
131 0 $ModValue =~ s/^(.)1(.)$/$1w$2/;
132 0 $ModValue =~ s/1$/x/;
133
134 0 return $ModValue;
135
136 }
137
138 #----------------------- File plus Mode Directory Listing ---------------------#
139 0 ▶sub FileList {
Subroutine FileList: not called CC=11 SCAR=48.8
Pod undocumented
140
141 0 my $self = shift;
142 0 ▶my $HashRef = shift || croak "No hash refeference specified for method FileList";
Condition0%0/2
shift() || croak('No hash refeference specified for method FileList')
l!l
00
MC/DC0%0/1
shift() || croak('No hash refeference specified for method FileList')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
143 0 ▶my $Directory = shift || "./";
Condition0%0/2
shift() || './'
l!l
00
MC/DC0%0/1
shift() || './'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
144 0 ▶my $NoDir = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
145 0 my $mode = 000;
146 0 my $error = 0;
147 0 my $file = '';
148
149 0 ▶croak qq{Not a hash reference for method FileList} unless ref($HashRef) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $HashRef eq "HASH" 0 0 0
150
151 0 ▶opendir(DIR, $Directory) or $error = 1;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $Directory or $error = 1 0 0 0
152 0 my @files = readdir(DIR);
153 0 closedir(DIR);
154
155 0 foreach $file (@files) {
156
157 0 $mode = (stat("$Directory/$file"))[2];
158 0 $mode = sprintf "%04o", $mode & 07777;
159
160 0 $mode = $self->OctToUnix($mode);
161
162 #Determine Directories
163 0 ▶ if ((-d $file) && !$NoDir) {$mode = "d" . $mode}
Branch: 2 branches
BranchTrueFalseTotal
if (-d $file and not $NoDir) { } 0 0 0
elsif (not $NoDir) { } 0 0 0
Condition0%0/3
-d $file and not $NoDir
!ll&&!rl&&r
000
MC/DC0%0/2
-d $file and not $NoDir
-d $file not $NoDir
Truth table0%0/3
A and not(B)
A= -d $fileB= not $NoDir
ABresult
0X0
100
111
164 0 elsif (!$NoDir) {$mode = "-" . $mode}
165
166 0 $HashRef->{$file} = "$mode";
167
168 }
169
170 0 ▶if ($error) {return 0}
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 0 0
171 0 else {return 1}
172
173 }
174
175 1 1;
176 __END__
↑ Summary