POSIX/Bits/System.pm

filter: Statementstmt 97.1% 67 / 69 Branchbran 89.4% 68 / 76 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 92.4% 147 / 159 CC 40 SCAR 27.5
CC
40
Cov
93%
convertModeToSystem
23.0
convertFlagsToSystem
13.3
convertWhenceToSystem
4.1
CRAP 15.6
« POSIX/Userland/Find.pm ↑ Summary POSIX/Userland/Tar.pm »
1 # Copyright (c) 2014, cPanel, Inc.
2 # All rights reserved.
3 # http://cpanel.net/
4 #
5 # This is free software; you can redistribute it and/or modify it under the same
6 # terms as Perl itself. See the LICENSE file for further details.
7  
8 package Filesys::POSIX::Bits::System;
9  
10 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 29 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 29 ▶use Filesys::POSIX::Bits;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 Filesys::POSIX::Bits::System - Bitfield and constant conversions for file modes
19 and system call flags to system values
20  
21 =head1 DESCRIPTION
22  
23 This file contains functions to convert the values of bitfields and constants
24 from the values defined in C<Filesys::POSIX::Bits> to the values used by the
25 system, defined in C<Fcntl>. Only exported values are supported.
26  
27 The following (unexported) functions are provided:
28  
29 =over
30  
31 =item C<convertFlagsToSystem($flags)>
32  
33 Converts the constants beginning with 'C<$O_>' to their values on the current
34 system. These constants are generally used in the C<$flags> field of
35 C<sysopen>.
36  
37 Values that are not supported by this system will throw a warning and will be
38 left out of the returned value. The flags must include an access mode (e.g.
39 C<$O_RDONLY>, C<$O_WRONLY>, xor C<$O_RDWR>) in addition to any other values
40 desired. If an access mode is not provided or its value is unknown to
41 C<Filesys::POSIX::Bits>, then the function will die.
42  
43 Note that C<$O_EVTONLY> is specific to this module and unsupported by C<Fcntl>.
44 Trying to convert it to a system value will result in a warning.
45  
46 =cut
47  
48 22 ▶sub convertFlagsToSystem {
Subroutine convertFlagsToSystem: called CC=13 SCAR=25.9
Pod documented
49 22 my $value = shift;
50 22 my $out;
51  
52 # Handle access modes first
53 22 my $access = $value & $Filesys::POSIX::Bits::O_MODE;
54 22 ▶ if ( $access == $O_RDWR ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($access == $Filesys::POSIX::Bits::System::O_RDWR) { } 1 21 22
elsif ($access == $Filesys::POSIX::Bits::System::O_WRONLY) { } 3 18 21
elsif ($access == $Filesys::POSIX::Bits::System::O_RDONLY) { } 18 0 18
55 1 $out = &Fcntl::O_RDWR;
56 }
57 elsif ( $access == $O_WRONLY ) {
58 3 $out = &Fcntl::O_WRONLY;
59 }
60 elsif ( $access == $O_RDONLY ) {
61 18 $out = &Fcntl::O_RDONLY;
62 }
63 else {
64 0 die "Unknown access mode: $access";
65 }
66  
67 22 ▶ $out |= _getOrWarn('O_APPEND') if $value & $O_APPEND;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_APPEND 4 18 22
68 22 ▶ $out |= _getOrWarn('O_CREAT') if $value & $O_CREAT;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_CREAT 4 18 22
69 22 ▶ $out |= _getOrWarn('O_EXCL') if $value & $O_EXCL;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_EXCL 1 21 22
70 22 ▶ $out |= _getOrWarn('O_EXLOCK') if $value & $O_EXLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_EXLOCK 0 22 22
71 22 ▶ $out |= _getOrWarn('O_NOFOLLOW') if $value & $O_NOFOLLOW;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_NOFOLLOW 1 21 22
72 22 ▶ $out |= _getOrWarn('O_NONBLOCK') if $value & $O_NONBLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_NONBLOCK 7 15 22
73 22 ▶ $out |= _getOrWarn('O_SHLOCK') if $value & $O_SHLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_SHLOCK 0 22 22
74 22 ▶ $out |= _getOrWarn('O_TRUNC') if $value & $O_TRUNC;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_TRUNC 2 20 22
75  
76 22 ▶ warn "O_EVTONLY is not supported by Fcntl" if $value & $O_EVTONLY;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::O_EVTONLY 0 22 22
77  
78 22 return $out;
79 }
80  
81 =item C<convertModeToSystem($mode)>
82  
83 Converts the constants beginning with 'C<$S_I>' to their values on the current
84 system. These constants are generally used in the C<$mode> field of C<sysopen>
85 and in the C<$mode> field of C<stat>.
86  
87 File types that are not supported by this system will throw a warning and will
88 be left out of the returned value. The mode may include zero or one file type
89 (values beginning with C<$S_IF>), but not more. If a file type unknown to
90 C<Filesys::POSIX::Bits> is provided, then the function will die.
91  
92 =cut
93  
94 22 ▶sub convertModeToSystem {
Subroutine convertModeToSystem: called CC=23 SCAR=31.4
Pod documented
95 22 my $value = shift;
96  
97 22 my $out = 0;
98  
99 # Convert file types (system support may vary)
100 22 my $type = $value & $S_IFMT;
101 22 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 10 12 22
102 10 my $name;
103 10 ▶ $name = 'S_IFIFO' if $type == $S_IFIFO;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $Filesys::POSIX::Bits::System::S_IFIFO 1 9 10
104 10 ▶ $name = 'S_IFCHR' if $type == $S_IFCHR;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $Filesys::POSIX::Bits::System::S_IFCHR 1 9 10
105 10 ▶ $name = 'S_IFDIR' if $type == $S_IFDIR;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $Filesys::POSIX::Bits::System::S_IFDIR 1 9 10
106 10 ▶ $name = 'S_IFBLK' if $type == $S_IFBLK;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $Filesys::POSIX::Bits::System::S_IFBLK 1 9 10
107 10 ▶ $name = 'S_IFREG' if $type == $S_IFREG;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $Filesys::POSIX::Bits::System::S_IFREG 4 6 10
108 10 ▶ $name = 'S_IFLNK' if $type == $S_IFLNK;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $Filesys::POSIX::Bits::System::S_IFLNK 1 9 10
109 10 ▶ $name = 'S_IFSOCK' if $type == $S_IFSOCK;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $Filesys::POSIX::Bits::System::S_IFSOCK 1 9 10
110 10 ▶ $name = 'S_IFWHT' if $type == $S_IFWHT;
Branch: 1 branch
BranchTrueFalseTotal
if $type == $Filesys::POSIX::Bits::System::S_IFWHT 0 10 10
111 10 ▶ die "Unknown file type: $type" if !$name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 10 10
112  
113 10 $out = _getOrWarn($name);
114 }
115  
116 # Convert permissions
117 22 ▶ $out |= &Fcntl::S_IRUSR if $value & $S_IRUSR;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IRUSR 4 18 22
118 22 ▶ $out |= &Fcntl::S_IWUSR if $value & $S_IWUSR;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IWUSR 4 18 22
119 22 ▶ $out |= &Fcntl::S_IXUSR if $value & $S_IXUSR;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IXUSR 1 21 22
120 22 ▶ $out |= &Fcntl::S_IRGRP if $value & $S_IRGRP;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IRGRP 4 18 22
121 22 ▶ $out |= &Fcntl::S_IWGRP if $value & $S_IWGRP;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IWGRP 1 21 22
122 22 ▶ $out |= &Fcntl::S_IXGRP if $value & $S_IXGRP;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IXGRP 1 21 22
123 22 ▶ $out |= &Fcntl::S_IROTH if $value & $S_IROTH;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IROTH 4 18 22
124 22 ▶ $out |= &Fcntl::S_IWOTH if $value & $S_IWOTH;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IWOTH 1 21 22
125 22 ▶ $out |= &Fcntl::S_IXOTH if $value & $S_IXOTH;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_IXOTH 1 21 22
126  
127 # Convert sticky bits
128 22 ▶ $out |= &Fcntl::S_ISUID if $value & $S_ISUID;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_ISUID 1 21 22
129 22 ▶ $out |= &Fcntl::S_ISGID if $value & $S_ISGID;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_ISGID 1 21 22
130 22 ▶ $out |= &Fcntl::S_ISVTX if $value & $S_ISVTX;
Branch: 1 branch
BranchTrueFalseTotal
if $value & $Filesys::POSIX::Bits::System::S_ISVTX 1 21 22
131  
132 22 return $out;
133 }
134  
135 =item C<convertWhenceToSystem($whence)>
136  
137 Converts the constants beginning with 'C<$SEEK_>' to their values on the
138 current system. These constants are generally used in the C<$whence> field
139 of C<sysseek>.
140  
141 If a value unknown to C<Filesys::POSIX::Bits> is provided, then the function
142 will die.
143  
144 =cut
145  
146 4 ▶sub convertWhenceToSystem {
Subroutine convertWhenceToSystem: called CC=4 SCAR=14.0
Pod documented
147 4 my $value = shift;
148  
149 4 ▶ if ( $value == $SEEK_SET ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value == $Filesys::POSIX::Bits::System::SEEK_SET) { } 2 2 4
elsif ($value == $Filesys::POSIX::Bits::System::SEEK_CUR) { } 1 1 2
elsif ($value == $Filesys::POSIX::Bits::System::SEEK_END) { } 1 0 1
150 2 return &Fcntl::SEEK_SET;
151 }
152 elsif ( $value == $SEEK_CUR ) {
153 1 return &Fcntl::SEEK_CUR;
154 }
155 elsif ( $value == $SEEK_END ) {
156 1 return &Fcntl::SEEK_END;
157 }
158 else {
159 0 die "Unknown whence value: $value";
160 }
161 }
162  
163 =back
164  
165 =cut
166  
167 # Private function that either returns the requested value from Fcntl or
168 # throws a warning. If a warning is thrown, the value 0 is returned.
169 29 ▶sub _getOrWarn {
Subroutine _getOrWarn: called CC=3 SCAR=11.4
170 29 my $var = shift;
171  
172 29 ▶ my $value = eval("\&Fcntl::$var") || 0;
Condition50%1/2
eval "&Fcntl::$var" || 0
l!l
290
MC/DC0%0/1
eval "&Fcntl::$var" || 0
eval "&Fcntl::$var"
Truth table50%1/2
A
A= eval "&Fcntl::$var"
Aresult
11
00
173 29 ▶ warn "$var is not supported by this system" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 29 29
174  
175 29 return $value;
176 }
177  
178 29 1;
179  
180 =head1 DIAGNOSTICS
181  
182 =over
183  
184 =item I<CONSTANT> is not supported by this system
185  
186 The system's Fcntl does not have a value defined for the given I<CONSTANT> and
187 thus it can't (and won't) be converted.
188  
189 =item I<CONSTANT> is not supported by Fcntl
190  
191 The Fcntl module does not define the given I<CONSTANT> and thus it can't (and
192 won't) be converted.
193  
194 =item Unknown access mode: I<flag>
195  
196 The access mode provided does not match C<$O_RDONLY>, C<$O_WRONLY>, xor
197 C<$O_RDWR>; or an access mode was not provided at all.
198  
199 =item Unknown file type: I<mode>
200  
201 The optional file type component that was provided does not match one of:
202 C<$S_IFIFO>, C<$S_IFCHR>, C<$S_IFDIR>, C<$S_IFBLK>, C<$S_IFREG>, C<$S_IFLNK>,
203 C<$S_IFSOCK>, xor C<$S_IFWHT>.
204  
205 =item Unknown whence value: I<whence>
206  
207 The whence value provided was not one of: C<$SEEK_SET>, C<$SEEK_CUR>, xor
208 C<$SEEK_END>.
209  
210 =back
211  
212 =head1 KNOWN ISSUES
213  
214 =over
215  
216 =item SEEK_END is assumed to exist
217  
218 The C<Fcntl> value C<SEEK_END> is assumed to exist when it is not specified
219 by POSIX, but is rather an almost ubiquitously supported extension.
220  
221 =back
222  
223 =head1 AUTHORS
224  
225 =over
226  
227 =item Rikus Goodell <rikus.goodell@cpanel.net>
228  
229 =item Brian Carlson <brian.carlson@cpanel.net>
230  
231 =back
232  
233 =head1 COPYRIGHT
234  
235 Copyright (c) 2014, cPanel, Inc. Distributed under the terms of the Perl
236 Artistic license.
« POSIX/Userland/Find.pm ↑ Summary POSIX/Userland/Tar.pm »