r898: Applied Bernard Jungen's latest patch:
[rox-filer.git] / ROX-Filer / src / modechange.c
blob6b6637b5f60b4bab080746082874fc345d218355
1 /*
2 * $Id$
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)
10 * any later version.
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
15 * more details.
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
35 performance gain. */
37 #include "config.h"
39 #include <sys/types.h>
40 #include <sys/stat.h>
42 #include <glib.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. */
52 static int
53 oatoi (const char *s)
55 register int i;
57 if (*s == 0 || strlen (s) > 4)
58 return -1;
59 for (i = 0; isodigit (*s); ++s)
60 i = i * 8 + *s - '0';
61 if (*s)
62 return -1;
63 return i;
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
69 the form:
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. */
78 struct mode_change *
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);
90 if (i >= 0)
92 if (i > 07777)
93 return NULL;
94 head = g_new(struct mode_change, 1);
95 head->next = NULL;
96 head->op = '=';
97 head->flags = 0;
98 head->value = i;
99 head->affected = 07777; /* Affect all permissions. */
100 return head;
103 umask_value = umask (0);
104 umask (umask_value); /* Restore the old value. */
106 head = NULL;
107 change = NULL;
108 --mode_string;
110 /* One loop iteration for each "ugoa...=+-rwxXstugo...[=+-rwxXstugo...]". */
113 affected_bits = 0;
114 ops_to_mask = 0;
115 /* Turn on all the bits in `affected_bits' for each group given. */
116 for (++mode_string;; ++mode_string)
117 switch (*mode_string)
119 case 'u':
120 affected_bits |= 04700;
121 break;
122 case 'g':
123 affected_bits |= 02070;
124 break;
125 case 'o':
126 affected_bits |= 01007;
127 break;
128 case 'a':
129 affected_bits |= 07777;
130 break;
131 default:
132 goto no_more_affected;
135 no_more_affected:
136 /* If none specified, affect all bits, except perhaps those
137 set in the umask. */
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. */
148 if (head == NULL)
150 head = g_new(struct mode_change, 1);
151 change = head;
153 else
155 change->next = g_new(struct mode_change, 1);
156 change = change->next;
159 change->next = NULL;
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
164 : MODE_MASK_MINUS))
165 affected_masked &= ~umask_value;
166 change->affected = affected_masked;
167 change->value = 0;
168 change->flags = 0;
170 /* Set `value' according to the bits set in `affected_masked'. */
171 for (++mode_string;; ++mode_string)
172 switch (*mode_string)
174 case 'r':
175 change->value |= 00444 & affected_masked;
176 break;
177 case 'w':
178 change->value |= 00222 & affected_masked;
179 break;
180 case 'X':
181 change->flags |= MODE_X_IF_ANY_X;
182 /* Fall through. */
183 case 'x':
184 change->value |= 00111 & affected_masked;
185 break;
186 case 's':
187 /* Set the setuid/gid bits if `u' or `g' is selected. */
188 change->value |= 06000 & affected_masked;
189 break;
190 case 't':
191 /* Set the "save text image" bit if `o' is selected. */
192 change->value |= 01000 & affected_masked;
193 break;
194 case 'u':
195 /* Set the affected bits to the value of the `u' bits
196 on the same file. */
197 if (change->value)
198 goto invalid;
199 change->value = 00700;
200 change->flags |= MODE_COPY_EXISTING;
201 break;
202 case 'g':
203 /* Set the affected bits to the value of the `g' bits
204 on the same file. */
205 if (change->value)
206 goto invalid;
207 change->value = 00070;
208 change->flags |= MODE_COPY_EXISTING;
209 break;
210 case 'o':
211 /* Set the affected bits to the value of the `o' bits
212 on the same file. */
213 if (change->value)
214 goto invalid;
215 change->value = 00007;
216 change->flags |= MODE_COPY_EXISTING;
217 break;
218 default:
219 goto no_more_values;
221 no_more_values:;
223 } while (*mode_string == ',');
224 if (*mode_string == 0)
225 return head;
226 invalid:
227 mode_free (head);
228 return NULL;
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. */
236 unsigned short
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);
258 else
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;
269 else
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. */
280 switch (changes->op)
282 case '=':
283 /* Preserve the previous values in `newmode' of bits that are
284 not affected by this change operation. */
285 newmode = (newmode & ~changes->affected) | value;
286 break;
287 case '+':
288 newmode |= value;
289 break;
290 case '-':
291 newmode &= ~value;
292 break;
295 return newmode;
298 /* Free the memory used by the list of file mode change operations
299 CHANGES. */
301 void
302 mode_free (register struct mode_change *changes)
304 register struct mode_change *next;
306 while (changes)
308 next = changes->next;
309 g_free (changes);
310 changes = next;