1chmod(3) User Contributed Perl Documentation chmod(3)
2
3
4
6 File::chmod - Implements symbolic and ls chmod modes
7
9 This is File::chmod v0.32.
10
12 use File::chmod;
13
14 # chmod takes all three types
15 # these all do the same thing
16 chmod(0666,@files);
17 chmod("=rw",@files);
18 chmod("-rw-rw-rw-",@files);
19
20 # or
21
22 use File::chmod qw( symchmod lschmod );
23
24 chmod(0666,@files); # this is the normal chmod
25 symchmod("=rw",@files); # takes symbolic modes only
26 lschmod("-rw-rw-rw-",@files); # takes "ls" modes only
27
28 # more functions, read on to understand
29
31 File::chmod is a utility that allows you to bypass system calls or bit
32 processing of a file's permissions. It overloads the chmod() function
33 with its own that gets an octal mode, a symbolic mode (see below), or
34 an "ls" mode (see below). If you wish not to overload chmod(), you can
35 export symchmod() and lschmod(), which take, respectively, a symbolic
36 mode and an "ls" mode.
37
38 Symbolic modes are thoroughly described in your chmod(1) man page, but
39 here are a few examples.
40
41 # NEW: if $UMASK is true, symchmod() applies a bit-mask found in $MASK
42
43 chmod("+x","file1","file2"); # overloaded chmod(), that is...
44 # turns on the execute bit for all users on those two files
45
46 chmod("o=,g-w","file1","file2");
47 # removes 'other' permissions, and the write bit for 'group'
48
49 chmod("=u","file1","file2");
50 # sets all bits to those in 'user'
51
52 "ls" modes are the type produced on the left-hand side of an "ls -l" on
53 a directory. Examples are:
54
55 chmod("-rwxr-xr-x","file1","file2");
56 # the 0755 setting; user has read-write-execute, group and others
57 # have read-execute priveleges
58
59 chmod("-rwsrws---","file1","file2");
60 # sets read-write-execute for user and group, none for others
61 # also sets set-uid and set-gid bits
62
63 The regular chmod() and lschmod() are absolute; that is, they are not
64 appending to or subtracting from the current file mode. They set it,
65 regardless of what it had been before. symchmod() is useful for
66 allowing the modifying of a file's permissions without having to run a
67 system call or determining the file's permissions, and then combining
68 that with whatever bits are appropriate. It also operates separately
69 on each file.
70
71 An added feature to version 0.30 is the $UMASK variable, explained
72 below; if symchmod() is called and this variable is true, then the
73 function uses the (also new) $MASK variable (which defaults to umask())
74 as a mask against the new mode. This is documented below more clearly.
75
76 Functions
77 Exported by default:
78
79 chmod(MODE,FILES)
80 Takes an octal, symbolic, or "ls" mode, and then chmods each file
81 appropriately.
82
83 getchmod(MODE,FILES)
84 Returns a list of modified permissions, without chmodding files.
85 Accepts any of the three kinds of modes.
86
87 @newmodes = getchmod("+x","file1","file2");
88 # @newmodes holds the octal permissons of the files'
89 # modes, if they were to be sent through chmod("+x"...)
90
91 Exported by request:
92
93 symchmod(MODE,FILES)
94 Takes a symbolic permissions mode, and chmods each file.
95
96 lschmod(MODE,FILES)
97 Takes an "ls" permissions mode, and chmods each file.
98
99 getsymchmod(MODE,FILES)
100 Returns a list of modified permissions, without chmodding files.
101 Accepts only symbolic permisson modes.
102
103 getlschmod(MODE,FILES)
104 Returns a list of modified permissions, without chmodding files.
105 Accepts only "ls" permisson modes.
106
107 getmod(FILES)
108 Returns a list of the current mode of each file.
109
110 Variables
111 $File::chmod::DEBUG
112 If set to a true value, it will report warnings, similar to those
113 produced by chmod() on your system. Otherwise, the functions will
114 not report errors. Example: a file can not have file-locking and
115 the set-gid bits on at the same time. If $File::chmod::DEBUG is
116 true, the function will report an error. If not, you are not
117 warned of the conflict. It is set to 1 as default.
118
119 $File::chmod::MASK
120 Contains the umask to apply to new file modes when using
121 getsymchmod(). This defaults to the return value of umask() at
122 compile time. Is only applied if $UMASK is true.
123
124 $File::chmod::UMASK
125 This is a boolean which tells getsymchmod() whether or not to apply
126 the umask found in $MASK. It defaults to true.
127
129 Note: this section was started with version 0.30.
130
131 This is an in-depth look at the changes being made from version to
132 version.
133
134 0.31 to 0.32
135 license added
136 I added a license to this module so that it can be used places
137 without asking my permission. Sorry, Adam.
138
139 0.30 to 0.31
140 fixed getsymchmod() bug
141 Whoa. getsymchmod() was doing some crazy ish. That's about all I
142 can say. I did a great deal of debugging, and fixed it up. It ALL
143 had to do with two things:
144
145 $or = (/+=/ ? 1 : 0); # should have been /[+=]/
146
147 /u/ && $ok ? u_or() : u_not(); # should have been /u/ and $ok
148
149 fixed getmod() bug
150 I was using map() incorrectly in getmod(). Fixed that.
151
152 condensed lschmod()
153 I shorted it up, getting rid a variable.
154
155 0.21 to 0.30
156 added umask() honoring for symchmod()
157 The symchmod() function now honors the $UMASK and $MASK variables.
158 $UMASK is a boolean which indicates whether or not to honor the
159 $MASK variable. $MASK holds a umask, and it defaults to umask().
160 $UMASK defaults to true. These variables are NOT exported. They
161 must explictly set (i.e. $File::chmod::UMASK = 0).
162
163 function name changes
164 Renamed internal function determine_mode() to mode(). However, if
165 you happen to be using determine_mode() somewhere, mode() will be
166 called, but you'll also get a warning about deprecation.
167
168 Renamed internal functions {or,not}_{l,s,t} to {l,s,t}_{or,not}.
169 This is to keep in standard with the OTHER 6 pairs of bitwise
170 functions, such as r_or() and g_not(). I don't know WHY the others
171 had 'not' or 'or' in the front.
172
173 fixed debugging bugs
174 Certain calls to warn() were not guarded by the $DEBUG variable,
175 and now they are. Also, for some reason, I left a debugging check
176 (that didn't check to see if $DEBUG was true) in getsymchmod(),
177 line 118. It printed "ENTERING /g/". It's gone now.
178
179 fixed set-uid and set-gid bug
180 Heh, it seems that in the previous version of File::chmod, the
181 following code went along broken:
182
183 # or_s sub, File/chmod.pm, v0.21, line 330
184 ($VAL & 00100) && do {
185 $DEBUG && warn("execute bit must be on for set-uid"); 1;
186 } && next;
187
188 Aside from me using '&&' more than enough (changed in the new
189 code), this is broken. This is now fixed.
190
191 fixed file lock/set-gid bug
192 The not_l() function (now renamed to l_not()) used to take the file
193 mode and bit-wise NOT it with ~02000. However, it did not check if
194 the file was locked vs. set-gid. Now, the function is "$VAL &=
195 ~02000 if not $VAL & 00010;".
196
197 removed useless data structures
198 I do not know why I had the $S variable, or %r, %w, and %x hashes.
199 In fact, $S was declared in "use vars qw( ... );", but never given
200 a value, and the %r, %w, and %x hashes had a 'full' key which never
201 got used. And the hashes themselves weren't really needed anyway.
202 Here is a list of the variables no longer in use, and what they
203 have been replaced with (if any):
204
205 $S nothing
206 $U, $G, $O $W
207 %r, %w, %x octal numbers
208 @files @_ (I had @files = @_; in nearly EVERY sub)
209 $c $_
210
211 compacted code
212 The first version of File::chmod that was published was 0.13, and
213 it was written in approximately 10 days, being given the off-and-on
214 treatment I end up having to give several projects, due to more
215 pressing matters. Well, since then, most of the code has stayed
216 the same, although bugs were worked out. Well, I got rid of a lot
217 of slow, clunky, and redundant sections of code in this version.
218 Sections include the processing of each character of the mode in
219 getsymchmod(), the getmod() subroutine, um, nearly ALL of the
220 getsymchmod() function, now that I look at it.
221
222 Here's part of the getsymchmod() rewrite:
223
224 for ($c){
225 if (/u/){
226 u_or() if $MODE eq "+" or $MODE eq "=";
227 u_not() if $MODE eq "-";
228 }
229 ...
230 }
231
232 # changed to
233
234 /u/ && $or ? u_or() : u_and();
235 # note: operating on $_, $c isn't used anymore
236 # note: $or holds 1 if the $MODE was + or =, 0 if $MODE was -
237 # note: previous was redundant. didn't need $MODE eq "-" check
238 # because u_or() and u_not() both go to the next character
239
241 This is only good on Unix-like boxes. I would like people to help me
242 work on File::chmod for any OS that deserves it. If you would like to
243 help, please email me (address below) with the OS and any information
244 you might have on how chmod() should work on it; if you don't have any
245 specific information, but would still like to help, hey, that's good
246 too. I have the following information (from "perlport"):
247
248 Win32
249 Only good for changing "owner" read-write access, "group", and
250 "other" bits are meaningless. NOTE: Win32::File and
251 Win32::FileSecurity already do this. I do not currently see a need
252 to port File::chmod.
253
254 MacOS
255 Only limited meaning. Disabling/enabling write permission is mapped
256 to locking/unlocking the file.
257
258 RISC OS
259 Only good for changing "owner" and "other" read-write access.
260
262 Jeff "japhy" Pinyan, japhy.734+CPAN@gmail.com, CPAN ID: PINYAN
263
265 Stat::lsMode (by Mark-James Dominus, CPAN ID: MJD)
266 chmod(1) manpage
267 perldoc -f chmod
268 perldoc -f stat
269
271 Copyright (C) 2007 by Jeff Pinyan
272
273 This library is free software; you can redistribute it and/or modify it
274 under the same terms as Perl itself, either Perl version 5.8.8 or, at
275 your option, any later version of Perl 5 you may have available.
276
277
278
279perl v5.12.0 2007-07-28 chmod(3)