blib/lib/File/Stat/ModeString.pm

filter: Statementstmt 100% 75 / 75 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 4 totaltotal 94.9% 94 / 99 CC 5 SCAR 6.3
CC
5
Cov
99%
mode_to_string
4.0
mode_to_typechar
2.0
BEGIN
1.0
CRAP 1.9
↑ Summary
1 package File::Stat::ModeString;
2  
3 =head1 NAME
4  
5 File::Stat::ModeString - conversion file stat(2) mode to/from string representation.
6  
7 =head1 SYNOPSIS
8  
9 use File::Stat::ModeString;
10  
11 $string = mode_to_string ( $st_mode );
12 $st_mode = string_to_mode ( $string );
13 $type = mode_to_typechar( $st_mode );
14  
15 $record = <IN>; chomp $record;
16 $record =~ m/^some_prefix\s+$MODE_STRING_RE\s+some_suffix$/o
17 or die "invalid record format";
18  
19 die "Invalid mode in $string"
20 if is_mode_string_valid( $string );
21  
22  
23 =head1 DESCRIPTION
24  
25 This module provides a few functions for conversion between
26 binary and literal representations of file mode bits,
27 including file type.
28  
29 All of them use only symbolic constants for mode bits
30 from B<File::Stat::Bits>.
31  
32  
33 =cut
34  
35 1 require 5.005;
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 local $^W=1; # use warnings only since 5.006
38 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use File::Stat::Bits;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43  
44 BEGIN
45 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶ use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶ use vars qw($VERSION @ISA @EXPORT $MODE_STRING_RE
Subroutine BEGIN: called CC=1 SCAR=0.0
48 @type_to_char %char_to_typemode %ugorw_to_mode %ugox_to_mode
49 1 @perms_clnid @perms_setid @perms_stick);
50  
51 1 $VERSION = do { my @r = (q$Revision: 0.28 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
52  
53 1 @ISA = ('Exporter');
54  
55 1 @EXPORT = qw( &is_mode_string_valid $MODE_STRING_RE
56 &mode_to_typechar &mode_to_string &string_to_mode
57 );
58  
59 1 @type_to_char = ();
60 1 $type_to_char[S_IFDIR >> 9] = 'd';
61 1 $type_to_char[S_IFCHR >> 9] = 'c';
62 1 $type_to_char[S_IFBLK >> 9] = 'b';
63 1 $type_to_char[S_IFREG >> 9] = '-';
64 1 $type_to_char[S_IFIFO >> 9] = 'p';
65 1 $type_to_char[S_IFLNK >> 9] = 'l';
66 1 $type_to_char[S_IFSOCK >> 9] = 's';
67  
68 1 @perms_clnid = qw(--- --x -w- -wx r-- r-x rw- rwx);
69 1 @perms_setid = qw(--S --s -wS -ws r-S r-s rwS rws);
70 1 @perms_stick = qw(--T --t -wT -wt r-T r-t rwT rwt);
71  
72 1 %char_to_typemode =
73 (
74 'd' => S_IFDIR ,
75 'c' => S_IFCHR ,
76 'b' => S_IFBLK ,
77 '-' => S_IFREG ,
78 'p' => S_IFIFO ,
79 'l' => S_IFLNK ,
80 's' => S_IFSOCK
81 );
82  
83 1 %ugorw_to_mode =
84 (
85 'u--' => 0,
86 'ur-' => S_IRUSR,
87 'u-w' => S_IWUSR,
88 'urw' => S_IRUSR|S_IWUSR,
89  
90 'g--' => 0,
91 'gr-' => S_IRGRP,
92 'g-w' => S_IWGRP,
93 'grw' => S_IRGRP|S_IWGRP,
94  
95 'o--' => 0,
96 'or-' => S_IROTH,
97 'o-w' => S_IWOTH,
98 'orw' => S_IROTH|S_IWOTH
99 );
100  
101 1 %ugox_to_mode =
102 (
103 'u-' => 0,
104 'ux' => S_IXUSR,
105 'us' => S_IXUSR|S_ISUID,
106 'uS' => S_ISUID,
107  
108 'g-' => 0,
109 'gx' => S_IXGRP,
110 'gs' => S_IXGRP|S_ISGID,
111 'gS' => S_ISGID,
112  
113 'o-' => 0,
114 'ox' => S_IXOTH,
115 'ot' => S_IXOTH|S_ISVTX,
116 'oT' => S_ISVTX,
117 );
118 }
119  
120  
121 =head1 CONSTANTS
122  
123 =head2 $MODE_STRING_RE
124  
125 Regular expression to match mode string (without ^$).
126  
127 =cut
128  
129 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
130 1 $MODE_STRING_RE = '[-dcbpls]([r-][w-][xsS-]){2}?[r-][w-][xtT-]';
131 }
132  
133  
134  
135 =head1 FUNCTIONS
136  
137 =head2
138  
139 is_mode_string_valid( $string )
140  
141 Returns true if argument matches mode string pattern.
142  
143 =cut
144 sub is_mode_string_valid
145 28672 ▶{
Subroutine is_mode_string_valid: called CC=1 SCAR=0.0
Pod undocumented
146 28672 my $string = shift;
147  
148 28672 return $string =~ m/^$MODE_STRING_RE$/o;
149 }
150  
151  
152 =head2
153  
154 $type = mode_to_typechar( $mode )
155  
156 Returns file type character of binary mode, '?' on unknown file type.
157  
158 =cut
159 sub mode_to_typechar
160 57344 ▶{
Subroutine mode_to_typechar: called CC=2 SCAR=7.1
Pod undocumented
161 57344 my $mode = shift;
162 57344 my $type = $type_to_char[ ($mode & S_IFMT) >> 9 ];
163 57344 ▶ return defined $type ? $type : '?';
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 57344 0 57344
164 }
165  
166  
167 =head2
168  
169 $string = mode_to_string( $mode )
170  
171 Converts binary mode value to string representation.
172 '?' in file type field on unknown file type.
173  
174 =cut
175 sub mode_to_string
176 28672 ▶{
Subroutine mode_to_string: called CC=4 SCAR=13.9
Pod undocumented
177 28672 my $mode = shift;
178 28672 my $string;
179 my $perms;
180  
181 28672 $string = mode_to_typechar($mode);
182  
183 # user
184 28672 ▶ $perms = ( $mode & S_ISUID ) ? \@perms_setid : \@perms_clnid;
Branch: 1 branch
BranchTrueFalseTotal
$mode & &S_ISUID() ? : 14336 14336 28672
185 28672 $string .= $perms->[($mode & S_IRWXU) >> 6];
186  
187 # group
188 28672 ▶ $perms = ( $mode & S_ISGID ) ? \@perms_setid : \@perms_clnid;
Branch: 1 branch
BranchTrueFalseTotal
$mode & &S_ISGID() ? : 14336 14336 28672
189 28672 $string .= $perms->[($mode & S_IRWXG) >> 3];
190  
191 # other
192 28672 ▶ $perms = ( $mode & S_ISVTX ) ? \@perms_stick : \@perms_clnid;
Branch: 1 branch
BranchTrueFalseTotal
$mode & &S_ISVTX() ? : 14336 14336 28672
193 28672 $string .= $perms->[($mode & S_IRWXO)];
194  
195 28672 return $string;
196 }
197  
198  
199 =head2
200  
201 $mode = string_to_mode( $string )
202  
203 Converts string representation of file mode to binary one.
204  
205 =cut
206 sub string_to_mode
207 28672 ▶{
Subroutine string_to_mode: called CC=1 SCAR=0.0
Pod undocumented
208 28672 my $string = shift;
209 28672 my @list = split //, $string;
210 28672 my $mode = 0;
211 28672 my $char;
212  
213 # type
214 28672 $char = shift @list;
215 28672 $mode |= $char_to_typemode{$char};
216  
217 # user read | write
218 28672 $char = 'u' . shift(@list) . shift(@list);
219 28672 $mode |= $ugorw_to_mode{$char};
220  
221 # user execute
222 28672 $char = 'u' . shift @list;
223 28672 $mode |= $ugox_to_mode{$char};
224  
225 # group read | write
226 28672 $char = 'g' . shift(@list) . shift(@list);
227 28672 $mode |= $ugorw_to_mode{$char};
228  
229 # group execute
230 28672 $char = 'g' . shift @list;
231 28672 $mode |= $ugox_to_mode{$char};
232  
233 # others read | write
234 28672 $char = 'o' . shift(@list) . shift(@list);
235 28672 $mode |= $ugorw_to_mode{$char};
236  
237 # others execute
238 28672 $char = 'o' . shift @list;
239 28672 $mode |= $ugox_to_mode{$char};
240  
241  
242 28672 return $mode;
243 }
244  
245  
246 =head1 SEE ALSO
247  
248 L<stat(2)>;
249  
250 L<File::Stat::Bits(3)>;
251  
252 L<Stat::lsMode(3)>;
253  
254 =head1 AUTHOR
255  
256 Dmitry Fedorov <dm.fedorov@gmail.com>
257  
258 =head1 COPYRIGHT
259  
260 Copyright (C) 2003 Dmitry Fedorov <dm.fedorov@gmail.com>
261  
262 =head1 LICENSE
263  
264 This program is free software; you can redistribute it and/or modify
265 it under the terms of the GNU General Public License as published by
266 the Free Software Foundation; either version 2 of the License,
267 or (at your option) any later version.
268  
269 =head1 DISCLAIMER
270  
271 The author disclaims any responsibility for any mangling of your system
272 etc, that this script may cause.
273  
274 =cut
275  
276  
277 1 1;
278  
↑ Summary