blib/lib/PerlPowerTools/SymbolicMode.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 2 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 140 CC 23 SCAR 62.7
CC
23
Cov
0%
mod
552.0
BEGIN
2.0
CRAP 529.1
1 package PerlPowerTools::SymbolicMode;
2  
3 #
4 # $Id: SymbolicMode.pm,v 1.1 2004/07/23 20:10:01 cwest Exp $
5 #
6 # $Log: SymbolicMode.pm,v $
7 # Revision 1.1 2004/07/23 20:10:01 cwest
8 # initial import
9 #
10 # Revision 1.1 1999/03/07 12:03:54 abigail
11 # Initial revision
12 #
13 #
14  
15 use strict;
16  
17 sub mod ($$) {
18 my $symbolic = shift;
19 my $file = shift;
20  
21 # Initialization.
22 # The 'user', 'group' and 'other' groups.
23 my @ugo = qw /u g o/;
24 # Bit masks for '[sg]uid', 'sticky', 'read', 'write' and 'execute'.
25 # Can't use qw // cause silly Perl doesn't know '2' is a number
26 # when dealing with &= ~$bit.
27 my %bits = (s => 8, t => 8, r => 4, w => 2, x => 1);
28  
29  
30 # For parsing.
31 my $who_re = '[augo]*';
32 my $action_re = '[-+=][rstwxXugo]*';
33  
34  
35 # Find the current permissions. This is what we start with.
36 my $mode = sprintf "%04o" => (stat $file) [2] || 0;
37 my $current = substr $mode => -3; # rwx permissions for ugo.
38  
39 my %perms;
40 @perms {@ugo} = split // => $current;
41  
42 # Handle the suid, guid and sticky bits.
43 # It looks like permission are 4 groups of 3 bits, groups for user,
44 # group and others, and a group for the special flags, but they are
45 # really 3 groups of 4 bits. Or maybe not.
46 # Anyway, this function is greatly simplified by treating them as
47 # 3 4-bit groups. The highest bit will be "special" one. suid for
48 # the users group, guid for the group group, and the sticky bit
49 # for the others group.
50 my $special = substr $mode => -4, 1;
51 my $bit = 1;
52 foreach my $c (reverse @ugo) {
53 $perms {$c} |= 8 if $special & $bit;
54 $bit <<= 1;
55 }
56  
57 # Keep track of the original permissions.
58 my %orig = %perms;
59  
60 # Find the umask setting, and store the bits for each group
61 # in a hash.
62 my %umask; # umask bits.
63 @umask {@ugo} = split // => sprintf "%03o" => umask;
64  
65  
66 # Time to parse...
67 foreach my $clause (split /,/ => $symbolic) {
68  
69 # Perhaps we should die if we can't parse it?
70 return undef unless
71 my ($who, $actions) =
72 $clause =~ /^($who_re)((?:$action_re)+)$/o;
73 # We would rather split the different actions out here,
74 # but there doesn't seem to be a way to collect them.
75 # /^($who_re)($action_re)+/ only gets the last one.
76 # Now, we have to reparse in later.
77  
78 my %who;
79 if ($who) {
80 $who =~ s/a/ugo/; # Ignore multiple 'a's.
81 @who {split // => $who} = undef;
82 }
83  
84 # @who will contain who these settings applies to.
85 # if who isn't set, it might be masked with the umask,
86 # hence, this isn't the final decision.
87 # Maybe we don't need this.
88 my @who = $who ? keys %who : @ugo;
89  
90 foreach my $action (split /(?=$action_re)/o => $actions) {
91 # The first character has to be the operator.
92 my $operator = substr $action, 0, 1;
93 # And the rest are the permissions.
94 my $perms = substr $action, 1;
95  
96 # BSD documentation says 'X' is to be ignored unless
97 # the operator is '-'. GNU, HP, SunOS and Solaris handle
98 # '-' and '=', while OpenBSD ignores only '-'.
99 # Solaris, HP and OpenBSD all turn a file with permission
100 # 666 to a file with permission 000 if chmod =X is
101 # is applied on it. SunOS and GNU act as if chmod = was
102 # applied to it. I cannot find out what the reasoning
103 # behind the choices of Solaris, HP and OpenBSD is.
104 # GNU and SunOS seem to ignore the 'X', which, after
105 # careful studying of the documentation seems to be
106 # the right choice.
107 # Therefore, remove any 'X' if the operator ain't '+';
108 $perms =~ s/X+//g unless $operator eq '+';
109  
110 # If there are no permissions, things are simple.
111 unless ($perms) {
112 # Things like u+ and go- are ignored; only = makes sense.
113 next unless $operator eq '=';
114 # Clear permissions on u= and go=.
115 if ($who) {@perms {keys %who} = (0) x 3;}
116 # '=' is special. Sets permissions to the umask.
117 else {%perms = %umask;}
118 next;
119 }
120  
121 # If we arrive here, $perms is a string.
122 # We can iterate over the characters.
123 foreach (split // => $perms) {
124 if ($_ eq 'X') {
125 # We know the operator eq '+'.
126 # Permission of `X' is special. If used on a regular file,
127 # the execution bit will only be turned on if any of the
128 # execution bits of the _unmodified_ file are turned on.
129 # That is,
130 # chmod 600 file; chmod u+x,a+X file;
131 # should result in the file having permission 700, not 711.
132 # GNU and SunOS get this wrong;
133 # Solaris, HP and OpenBSD get it right.
134 next unless -d $file || grep {$orig {$_} & 1} @ugo;
135 # Now, do as if it's an x.
136 $_ = 'x';
137 }
138  
139 if (/[st]/) {
140 # BSD man page says operations on 's' and 't' are to
141 # be ignored if they operate only on the "other" group.
142 # GNU and HP happely accept 'o+t'. Sun rejects 'o+t',
143 # but also rejects 'g+t', accepting only 'u+t'.
144 # OpenBSD acceps both 'u+t' and 'g+t', ignoring 'o+t'.
145 # We do too.
146 # OpenBSD however, accepts 'o=t', clearing all the bits
147 # of the "other" group.
148 # We don't, as that doesn't make any sense, and doesn't
149 # confirm to the documentation.
150 next if $who =~ /^o+$/;
151 }
152  
153 # Determine the $bit for the mask.
154 my $bit = /[ugo]/ ? $orig {$_} & ~8 : $bits {$_};
155  
156 die "Weird permission `$_' found\n" unless defined $bit;
157 # Should not happen.
158  
159 # Determine the set on which to operate.
160 my @set = $who ? @who : grep {!($umask {$_} & $bit)} @ugo;
161  
162 # If the permission is 's', don't operate on the other group.
163 # Unless the operator was '='. But in that case, don't set
164 # the 8 bit for 'other'.
165 my $equal_s;
166 if (/s/) {
167 if ($operator eq '=') {$equal_s = 1;}
168 else {@set = grep {!/o/} @set or next;}
169 }
170 # If the permission is 't', only operate on the other group;
171 # regardless what the 'who' settings are.
172 # Note that for a directory with permissions 1777, and a
173 # umask of 002, a chmod =t on HP and Solaris turn the
174 # permissions to 1000, GNU and SunOS turn the permissiosn
175 # to 1020, while OpenBSD keeps 1777.
176 /t/ and @set = qw /o/;
177  
178 # Apply.
179 foreach my $s (@set) {
180 do {$perms {$s} |= $bit; next} if $operator eq '+';
181 do {$perms {$s} &= ~$bit; next} if $operator eq '-';
182 do {$perms {$s} = $bit; next} if $operator eq '=';
183 die "Weird operator `$operator' found\n";
184 # Should not happen.
185 }
186  
187 # Special case '=s'.
188 $perms {o} &= ~$bit if $equal_s;
189 }
190 }
191 }
192  
193 # Now, translate @perms to an *octal* number.
194  
195 # First, deal with the suid, guid, and sticky bits by collecting
196 # the high bits of the ugo permissions.
197 my $first = 0;
198 $bit = 1;
199 for my $c (reverse @ugo) {
200 if ($perms {$c} & 8) {$first |= $bit; $perms {$c} &= ~8;}
201 $bit <<= 1;
202 }
203  
204 join "" => $first, @perms {@ugo};
205 }
206  
207 1;
208  
209  
210 __END__