4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, the ROX-Filer team.
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"
46 #define isodigit(c) ((c) >= '0' && (c) <= '7')
48 /* Return a positive integer containing the value of the ASCII
49 octal number S. If S is not an octal number or is more than
50 4 digits, return -1. */
57 if (*s
== 0 || strlen (s
) > 4)
59 for (i
= 0; isodigit (*s
); ++s
)
66 /* Return a linked list of file mode change operations created from
67 MODE_STRING, an ASCII string that contains either an octal number
68 specifying an absolute mode, or symbolic mode change operations with
70 [ugoa...][[+-=][rwxXstugo...]...][,...]
71 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
72 should not affect bits set in the umask when no users are given.
73 Operators not selected in MASKED_OPS ignore the umask.
75 Return NULL if `mode_string' does not contain a valid
76 representation of file mode change operations. */
79 mode_compile (const char *mode_string
, unsigned int masked_ops
)
81 struct mode_change
*head
; /* First element of the linked list. */
82 struct mode_change
*change
; /* An element of the linked list. */
83 int i
; /* General purpose temporary. */
84 int umask_value
; /* The umask value (surprise). */
85 unsigned short affected_bits
; /* Which bits in the mode are operated on. */
86 unsigned short affected_masked
; /* `affected_bits' modified by umask. */
87 unsigned ops_to_mask
; /* Operators to actually use umask on. */
89 i
= oatoi (mode_string
);
94 head
= g_new(struct mode_change
, 1);
99 head
->affected
= 07777; /* Affect all permissions. */
103 umask_value
= umask (0);
104 umask (umask_value
); /* Restore the old value. */
110 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
115 /* Turn on all the bits in `affected_bits' for each group given. */
116 for (++mode_string
;; ++mode_string
)
117 switch (*mode_string
)
120 affected_bits
|= 04700;
123 affected_bits
|= 02070;
126 affected_bits
|= 01007;
129 affected_bits
|= 07777;
132 goto no_more_affected
;
136 /* If none specified, affect all bits, except perhaps those
138 if (affected_bits
== 0)
140 affected_bits
= 07777;
141 ops_to_mask
= masked_ops
;
144 while (*mode_string
== '=' || *mode_string
== '+' || *mode_string
== '-')
146 /* Add the element to the tail of the list, so the operations
147 are performed in the correct order. */
150 head
= g_new(struct mode_change
, 1);
155 change
->next
= g_new(struct mode_change
, 1);
156 change
= change
->next
;
160 change
->op
= *mode_string
; /* One of "=+-". */
161 affected_masked
= affected_bits
;
162 if (ops_to_mask
& (*mode_string
== '=' ? MODE_MASK_EQUALS
163 : *mode_string
== '+' ? MODE_MASK_PLUS
165 affected_masked
&= ~umask_value
;
166 change
->affected
= affected_masked
;
170 /* Set `value' according to the bits set in `affected_masked'. */
171 for (++mode_string
;; ++mode_string
)
172 switch (*mode_string
)
175 change
->value
|= 00444 & affected_masked
;
178 change
->value
|= 00222 & affected_masked
;
181 change
->flags
|= MODE_X_IF_ANY_X
;
184 change
->value
|= 00111 & affected_masked
;
187 /* Set the setuid/gid bits if `u' or `g' is selected. */
188 change
->value
|= 06000 & affected_masked
;
191 /* Set the "save text image" bit if `o' is selected. */
192 change
->value
|= 01000 & affected_masked
;
195 /* Set the affected bits to the value of the `u' bits
199 change
->value
= 00700;
200 change
->flags
|= MODE_COPY_EXISTING
;
203 /* Set the affected bits to the value of the `g' bits
207 change
->value
= 00070;
208 change
->flags
|= MODE_COPY_EXISTING
;
211 /* Set the affected bits to the value of the `o' bits
215 change
->value
= 00007;
216 change
->flags
|= MODE_COPY_EXISTING
;
223 } while (*mode_string
== ',');
224 if (*mode_string
== 0)
231 /* Return file mode OLDMODE, adjusted as indicated by the list of change
232 operations CHANGES. If OLDMODE is a directory, the type `X'
233 change affects it even if no execute bits were set in OLDMODE.
234 The returned value has the S_IFMT bits cleared. */
237 mode_adjust (unsigned int oldmode
, const struct mode_change
*changes
)
239 unsigned short newmode
; /* The adjusted mode and one operand. */
240 unsigned short value
; /* The other operand. */
242 newmode
= oldmode
& 07777;
244 for (; changes
; changes
= changes
->next
)
246 if (changes
->flags
& MODE_COPY_EXISTING
)
248 /* Isolate in `value' the bits in `newmode' to copy, given in
249 the mask `changes->value'. */
250 value
= newmode
& changes
->value
;
252 if (changes
->value
& 00700)
253 /* Copy `u' permissions onto `g' and `o'. */
254 value
|= (value
>> 3) | (value
>> 6);
255 else if (changes
->value
& 00070)
256 /* Copy `g' permissions onto `u' and `o'. */
257 value
|= (value
<< 3) | (value
>> 3);
259 /* Copy `o' permissions onto `u' and `g'. */
260 value
|= (value
<< 3) | (value
<< 6);
262 /* In order to change only `u', `g', or `o' permissions,
263 or some combination thereof, clear unselected bits.
264 This can not be done in mode_compile because the value
265 to which the `changes->affected' mask is applied depends
266 on the old mode of each file. */
267 value
&= changes
->affected
;
271 value
= changes
->value
;
272 /* If `X', do not affect the execute bits if the file is not a
273 directory and no execute bits are already set. */
274 if ((changes
->flags
& MODE_X_IF_ANY_X
)
275 && !S_ISDIR (oldmode
)
276 && (newmode
& 00111) == 0)
277 value
&= ~00111; /* Clear the execute bits. */
283 /* Preserve the previous values in `newmode' of bits that are
284 not affected by this change operation. */
285 newmode
= (newmode
& ~changes
->affected
) | value
;
298 /* Free the memory used by the list of file mode change operations
302 mode_free (register struct mode_change
*changes
)
304 register struct mode_change
*next
;
308 next
= changes
->next
;