4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, 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>
45 #include "modechange.h"
47 #define isodigit(c) ((c) >= '0' && (c) <= '7')
49 /* Return a positive integer containing the value of the ASCII
50 octal number S. If S is not an octal number or is more than
51 4 digits, return -1. */
58 if (*s
== 0 || strlen (s
) > 4)
60 for (i
= 0; isodigit (*s
); ++s
)
67 /* Return a linked list of file mode change operations created from
68 MODE_STRING, an ASCII string that contains either an octal number
69 specifying an absolute mode, or symbolic mode change operations with
71 [ugoa...][[+-=][rwxXstugo...]...][,...]
72 MASKED_OPS is a bitmask indicating which symbolic mode operators (=+-)
73 should not affect bits set in the umask when no users are given.
74 Operators not selected in MASKED_OPS ignore the umask.
76 Return NULL if `mode_string' does not contain a valid
77 representation of file mode change operations. */
80 mode_compile (const char *mode_string
, unsigned int masked_ops
)
82 struct mode_change
*head
; /* First element of the linked list. */
83 struct mode_change
*change
; /* An element of the linked list. */
84 int i
; /* General purpose temporary. */
85 int umask_value
; /* The umask value (surprise). */
86 unsigned short affected_bits
; /* Which bits in the mode are operated on. */
87 unsigned short affected_masked
; /* `affected_bits' modified by umask. */
88 unsigned ops_to_mask
; /* Operators to actually use umask on. */
90 i
= oatoi (mode_string
);
95 head
= g_new(struct mode_change
, 1);
100 head
->affected
= 07777; /* Affect all permissions. */
104 umask_value
= umask (0);
105 umask (umask_value
); /* Restore the old value. */
111 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
116 /* Turn on all the bits in `affected_bits' for each group given. */
117 for (++mode_string
;; ++mode_string
)
118 switch (*mode_string
)
121 affected_bits
|= 04700;
124 affected_bits
|= 02070;
127 affected_bits
|= 01007;
130 affected_bits
|= 07777;
133 goto no_more_affected
;
137 /* If none specified, affect all bits, except perhaps those
139 if (affected_bits
== 0)
141 affected_bits
= 07777;
142 ops_to_mask
= masked_ops
;
145 while (*mode_string
== '=' || *mode_string
== '+' || *mode_string
== '-')
147 /* Add the element to the tail of the list, so the operations
148 are performed in the correct order. */
151 head
= g_new(struct mode_change
, 1);
156 change
->next
= g_new(struct mode_change
, 1);
157 change
= change
->next
;
161 change
->op
= *mode_string
; /* One of "=+-". */
162 affected_masked
= affected_bits
;
163 if (ops_to_mask
& (*mode_string
== '=' ? MODE_MASK_EQUALS
164 : *mode_string
== '+' ? MODE_MASK_PLUS
166 affected_masked
&= ~umask_value
;
167 change
->affected
= affected_masked
;
171 /* Set `value' according to the bits set in `affected_masked'. */
172 for (++mode_string
;; ++mode_string
)
173 switch (*mode_string
)
176 change
->value
|= 00444 & affected_masked
;
179 change
->value
|= 00222 & affected_masked
;
182 change
->flags
|= MODE_X_IF_ANY_X
;
185 change
->value
|= 00111 & affected_masked
;
188 /* Set the setuid/gid bits if `u' or `g' is selected. */
189 change
->value
|= 06000 & affected_masked
;
192 /* Set the "save text image" bit if `o' is selected. */
193 change
->value
|= 01000 & affected_masked
;
196 /* Set the affected bits to the value of the `u' bits
200 change
->value
= 00700;
201 change
->flags
|= MODE_COPY_EXISTING
;
204 /* Set the affected bits to the value of the `g' bits
208 change
->value
= 00070;
209 change
->flags
|= MODE_COPY_EXISTING
;
212 /* Set the affected bits to the value of the `o' bits
216 change
->value
= 00007;
217 change
->flags
|= MODE_COPY_EXISTING
;
224 } while (*mode_string
== ',');
225 if (*mode_string
== 0)
232 /* Return file mode OLDMODE, adjusted as indicated by the list of change
233 operations CHANGES. If OLDMODE is a directory, the type `X'
234 change affects it even if no execute bits were set in OLDMODE.
235 The returned value has the S_IFMT bits cleared. */
238 mode_adjust (unsigned int oldmode
, const struct mode_change
*changes
)
240 unsigned short newmode
; /* The adjusted mode and one operand. */
241 unsigned short value
; /* The other operand. */
243 newmode
= oldmode
& 07777;
245 for (; changes
; changes
= changes
->next
)
247 if (changes
->flags
& MODE_COPY_EXISTING
)
249 /* Isolate in `value' the bits in `newmode' to copy, given in
250 the mask `changes->value'. */
251 value
= newmode
& changes
->value
;
253 if (changes
->value
& 00700)
254 /* Copy `u' permissions onto `g' and `o'. */
255 value
|= (value
>> 3) | (value
>> 6);
256 else if (changes
->value
& 00070)
257 /* Copy `g' permissions onto `u' and `o'. */
258 value
|= (value
<< 3) | (value
>> 3);
260 /* Copy `o' permissions onto `u' and `g'. */
261 value
|= (value
<< 3) | (value
<< 6);
263 /* In order to change only `u', `g', or `o' permissions,
264 or some combination thereof, clear unselected bits.
265 This can not be done in mode_compile because the value
266 to which the `changes->affected' mask is applied depends
267 on the old mode of each file. */
268 value
&= changes
->affected
;
272 value
= changes
->value
;
273 /* If `X', do not affect the execute bits if the file is not a
274 directory and no execute bits are already set. */
275 if ((changes
->flags
& MODE_X_IF_ANY_X
)
276 && !S_ISDIR (oldmode
)
277 && (newmode
& 00111) == 0)
278 value
&= ~00111; /* Clear the execute bits. */
284 /* Preserve the previous values in `newmode' of bits that are
285 not affected by this change operation. */
286 newmode
= (newmode
& ~changes
->affected
) | value
;
299 /* Free the memory used by the list of file mode change operations
303 mode_free (register struct mode_change
*changes
)
305 register struct mode_change
*next
;
309 next
= changes
->next
;