4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@users.sourceforge.net>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* modechange.c -- file mode manipulation
24 * This rest of this file was taken from GNU FileUtils, with minor
25 * modifications (eg changing the memory handling to use GLib).
28 /* Written by David MacKenzie <djm@ai.mit.edu> */
30 /* The ASCII mode string is compiled into a linked list of `struct
31 modechange', which can then be applied to each file to be changed.
32 We do this instead of re-parsing the ASCII string for each file
33 because the compiled form requires less computation to use; when
34 changing the mode of many files, this probably results in a
39 #include <sys/types.h>
44 #include "modechange.h"
50 #if STAT_MACROS_BROKEN
54 #if !defined(S_ISDIR) && defined(S_IFDIR)
55 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
58 #define isodigit(c) ((c) >= '0' && (c) <= '7')
60 /* Return a positive integer containing the value of the ASCII
61 octal number S. If S is not an octal number, return -1. */
70 for (i
= 0; isodigit (*s
); ++s
)
77 /* Return a linked list of file mode change operations created from
78 MODE_STRING, an ASCII string that contains either an octal number
79 specifying an absolute mode, or symbolic mode change operations with
81 [ugoa...][[+-=][rwxXstugo...]...][,...]
82 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
83 should not affect bits set in the umask when no users are given.
84 Operators not selected in MASKED_OPS ignore the umask.
86 Return NULL if `mode_string' does not contain a valid
87 representation of file mode change operations. */
90 mode_compile (const char *mode_string
, unsigned int masked_ops
)
92 struct mode_change
*head
; /* First element of the linked list. */
93 struct mode_change
*change
; /* An element of the linked list. */
94 int i
; /* General purpose temporary. */
95 int umask_value
; /* The umask value (surprise). */
96 unsigned short affected_bits
; /* Which bits in the mode are operated on. */
97 unsigned short affected_masked
; /* `affected_bits' modified by umask. */
98 unsigned ops_to_mask
; /* Operators to actually use umask on. */
100 i
= oatoi (mode_string
);
105 head
= g_new(struct mode_change
, 1);
110 head
->affected
= 07777; /* Affect all permissions. */
114 umask_value
= umask (0);
115 umask (umask_value
); /* Restore the old value. */
121 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
126 /* Turn on all the bits in `affected_bits' for each group given. */
127 for (++mode_string
;; ++mode_string
)
128 switch (*mode_string
)
131 affected_bits
|= 04700;
134 affected_bits
|= 02070;
137 affected_bits
|= 01007;
140 affected_bits
|= 07777;
143 goto no_more_affected
;
147 /* If none specified, affect all bits, except perhaps those
149 if (affected_bits
== 0)
151 affected_bits
= 07777;
152 ops_to_mask
= masked_ops
;
155 while (*mode_string
== '=' || *mode_string
== '+' || *mode_string
== '-')
157 /* Add the element to the tail of the list, so the operations
158 are performed in the correct order. */
161 head
= g_new(struct mode_change
, 1);
166 change
->next
= g_new(struct mode_change
, 1);
167 change
= change
->next
;
171 change
->op
= *mode_string
; /* One of "=+-". */
172 affected_masked
= affected_bits
;
173 if (ops_to_mask
& (*mode_string
== '=' ? MODE_MASK_EQUALS
174 : *mode_string
== '+' ? MODE_MASK_PLUS
176 affected_masked
&= ~umask_value
;
177 change
->affected
= affected_masked
;
181 /* Set `value' according to the bits set in `affected_masked'. */
182 for (++mode_string
;; ++mode_string
)
183 switch (*mode_string
)
186 change
->value
|= 00444 & affected_masked
;
189 change
->value
|= 00222 & affected_masked
;
192 change
->flags
|= MODE_X_IF_ANY_X
;
195 change
->value
|= 00111 & affected_masked
;
198 /* Set the setuid/gid bits if `u' or `g' is selected. */
199 change
->value
|= 06000 & affected_masked
;
202 /* Set the "save text image" bit if `o' is selected. */
203 change
->value
|= 01000 & affected_masked
;
206 /* Set the affected bits to the value of the `u' bits
210 change
->value
= 00700;
211 change
->flags
|= MODE_COPY_EXISTING
;
214 /* Set the affected bits to the value of the `g' bits
218 change
->value
= 00070;
219 change
->flags
|= MODE_COPY_EXISTING
;
222 /* Set the affected bits to the value of the `o' bits
226 change
->value
= 00007;
227 change
->flags
|= MODE_COPY_EXISTING
;
234 } while (*mode_string
== ',');
235 if (*mode_string
== 0)
242 /* Return file mode OLDMODE, adjusted as indicated by the list of change
243 operations CHANGES. If OLDMODE is a directory, the type `X'
244 change affects it even if no execute bits were set in OLDMODE.
245 The returned value has the S_IFMT bits cleared. */
248 mode_adjust (unsigned int oldmode
, const struct mode_change
*changes
)
250 unsigned short newmode
; /* The adjusted mode and one operand. */
251 unsigned short value
; /* The other operand. */
253 newmode
= oldmode
& 07777;
255 for (; changes
; changes
= changes
->next
)
257 if (changes
->flags
& MODE_COPY_EXISTING
)
259 /* Isolate in `value' the bits in `newmode' to copy, given in
260 the mask `changes->value'. */
261 value
= newmode
& changes
->value
;
263 if (changes
->value
& 00700)
264 /* Copy `u' permissions onto `g' and `o'. */
265 value
|= (value
>> 3) | (value
>> 6);
266 else if (changes
->value
& 00070)
267 /* Copy `g' permissions onto `u' and `o'. */
268 value
|= (value
<< 3) | (value
>> 3);
270 /* Copy `o' permissions onto `u' and `g'. */
271 value
|= (value
<< 3) | (value
<< 6);
273 /* In order to change only `u', `g', or `o' permissions,
274 or some combination thereof, clear unselected bits.
275 This can not be done in mode_compile because the value
276 to which the `changes->affected' mask is applied depends
277 on the old mode of each file. */
278 value
&= changes
->affected
;
282 value
= changes
->value
;
283 /* If `X', do not affect the execute bits if the file is not a
284 directory and no execute bits are already set. */
285 if ((changes
->flags
& MODE_X_IF_ANY_X
)
286 && !S_ISDIR (oldmode
)
287 && (newmode
& 00111) == 0)
288 value
&= ~00111; /* Clear the execute bits. */
294 /* Preserve the previous values in `newmode' of bits that are
295 not affected by this change operation. */
296 newmode
= (newmode
& ~changes
->affected
) | value
;
309 /* Free the memory used by the list of file mode change operations
313 mode_free (register struct mode_change
*changes
)
315 register struct mode_change
*next
;
319 next
= changes
->next
;