1 /* modechange.c -- file mode manipulation
3 Copyright (C) 1989, 1990, 1997, 1998, 1999, 2001, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Written by David MacKenzie <djm@ai.mit.edu> */
22 /* The ASCII mode string is compiled into an array of `struct
23 modechange', which can then be applied to each file to be changed.
24 We do this instead of re-parsing the ASCII string for each file
25 because the compiled form requires less computation to use; when
26 changing the mode of many files, this probably results in a
33 #include "modechange.h"
35 #include "stat-macros.h"
39 /* The traditional octal values corresponding to each mode bit. */
52 #define ALLM 07777 /* all octal mode bits */
54 /* Special operations flags. */
57 /* For the sentinel at the end of the mode changes array. */
60 /* The typical case. */
63 /* In addition to the typical case, affect the execute bits if at
64 least one execute bit is set already, or if the file is a
68 /* Instead of the typical case, copy some existing permissions for
69 u, g, or o onto the other two. Which of u, g, or o is copied
70 is determined by which bits are set in the `value' field. */
74 /* Description of a mode change. */
77 char op; /* One of "=+-". */
78 char flag; /* Special operations flag. */
79 mode_t affected; /* Set for u, g, o, or a. */
80 mode_t value; /* Bits to add/remove. */
83 /* Return a mode_change array with the specified `=ddd'-style
84 mode change operation, where NEW_MODE is `ddd'. */
86 static struct mode_change *
87 make_node_op_equals (mode_t new_mode)
89 struct mode_change *p = xmalloc (2 * sizeof *p);
91 p->flag = MODE_ORDINARY_CHANGE;
92 p->affected = CHMOD_MODE_BITS;
94 p[1].flag = MODE_DONE;
98 /* Return a pointer to an array of file mode change operations created from
99 MODE_STRING, an ASCII string that contains either an octal number
100 specifying an absolute mode, or symbolic mode change operations with
102 [ugoa...][[+-=][rwxXstugo...]...][,...]
104 Return NULL if `mode_string' does not contain a valid
105 representation of file mode change operations. */
108 mode_compile (char const *mode_string)
110 /* The array of mode-change directives to be returned. */
111 struct mode_change *mc;
114 if ('0' <= *mode_string && *mode_string < '8')
117 unsigned int octal_value = 0;
121 octal_value = 8 * octal_value + *mode_string++ - '0';
122 if (ALLM < octal_value)
125 while ('0' <= *mode_string && *mode_string < '8');
130 /* Help the compiler optimize the usual case where mode_t uses
131 the traditional octal representation. */
132 mode = ((S_ISUID == SUID && S_ISGID == SGID && S_ISVTX == SVTX
133 && S_IRUSR == RUSR && S_IWUSR == WUSR && S_IXUSR == XUSR
134 && S_IRGRP == RGRP && S_IWGRP == WGRP && S_IXGRP == XGRP
135 && S_IROTH == ROTH && S_IWOTH == WOTH && S_IXOTH == XOTH)
137 : (mode_t) ((octal_value & SUID ? S_ISUID : 0)
138 | (octal_value & SGID ? S_ISGID : 0)
139 | (octal_value & SVTX ? S_ISVTX : 0)
140 | (octal_value & RUSR ? S_IRUSR : 0)
141 | (octal_value & WUSR ? S_IWUSR : 0)
142 | (octal_value & XUSR ? S_IXUSR : 0)
143 | (octal_value & RGRP ? S_IRGRP : 0)
144 | (octal_value & WGRP ? S_IWGRP : 0)
145 | (octal_value & XGRP ? S_IXGRP : 0)
146 | (octal_value & ROTH ? S_IROTH : 0)
147 | (octal_value & WOTH ? S_IWOTH : 0)
148 | (octal_value & XOTH ? S_IXOTH : 0)));
150 return make_node_op_equals (mode);
153 /* Allocate enough space to hold the result. */
157 for (p = mode_string; *p; p++)
158 needed += (*p == '=' || *p == '+' || *p == '-');
159 mc = xnmalloc (needed, sizeof *mc);
162 /* One loop iteration for each `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'. */
163 for (;; mode_string++)
165 /* Which bits in the mode are operated on. */
168 /* Turn on all the bits in `affected' for each group given. */
169 for (;; mode_string++)
170 switch (*mode_string)
175 affected |= S_ISUID | S_IRWXU;
178 affected |= S_ISGID | S_IRWXG;
181 affected |= S_ISVTX | S_IRWXO;
184 affected |= CHMOD_MODE_BITS;
186 case '=': case '+': case '-':
187 goto no_more_affected;
193 char op = *mode_string++;
195 char flag = MODE_COPY_EXISTING;
196 struct mode_change *change;
198 switch (*mode_string++)
201 /* Set the affected bits to the value of the `u' bits
206 /* Set the affected bits to the value of the `g' bits
211 /* Set the affected bits to the value of the `o' bits
218 flag = MODE_ORDINARY_CHANGE;
220 for (mode_string--;; mode_string++)
221 switch (*mode_string)
224 value |= S_IRUSR | S_IRGRP | S_IROTH;
227 value |= S_IWUSR | S_IWGRP | S_IWOTH;
230 value |= S_IXUSR | S_IXGRP | S_IXOTH;
233 flag = MODE_X_IF_ANY_X;
236 /* Set the setuid/gid bits if `u' or `g' is selected. */
237 value |= S_ISUID | S_ISGID;
240 /* Set the "save text image" bit if `o' is selected. */
249 change = &mc[used++];
252 change->affected = affected;
253 change->value = value;
255 while (*mode_string == '=' || *mode_string == '+'
256 || *mode_string == '-');
258 if (*mode_string != ',')
262 if (*mode_string == 0)
264 mc[used].flag = MODE_DONE;
273 /* Return a file mode change operation that sets permissions to match those
274 of REF_FILE. Return NULL (setting errno) if REF_FILE can't be accessed. */
277 mode_create_from_ref (const char *ref_file)
279 struct stat ref_stats;
281 if (stat (ref_file, &ref_stats) != 0)
283 return make_node_op_equals (ref_stats.st_mode);
286 /* Return file mode OLDMODE, adjusted as indicated by the list of change
287 operations CHANGES, which are interpreted assuming the umask is
288 UMASK_VALUE. If OLDMODE is a directory, the type `X'
289 change affects it even if no execute bits were set in OLDMODE.
290 The returned value has the S_IFMT bits cleared. */
293 mode_adjust (mode_t oldmode, struct mode_change const *changes,
296 /* The adjusted mode. */
297 mode_t newmode = oldmode & CHMOD_MODE_BITS;
299 for (; changes->flag != MODE_DONE; changes++)
301 mode_t affected = changes->affected;
302 mode_t value = changes->value;
304 switch (changes->flag)
306 case MODE_ORDINARY_CHANGE:
309 case MODE_COPY_EXISTING:
310 /* Isolate in `value' the bits in `newmode' to copy. */
313 /* Copy the isolated bits to the other two parts. */
314 value |= ((value & (S_IRUSR | S_IRGRP | S_IROTH)
315 ? S_IRUSR | S_IRGRP | S_IROTH : 0)
316 | (value & (S_IWUSR | S_IWGRP | S_IWOTH)
317 ? S_IWUSR | S_IWGRP | S_IWOTH : 0)
318 | (value & (S_IXUSR | S_IXGRP | S_IXOTH)
319 ? S_IXUSR | S_IXGRP | S_IXOTH : 0));
322 case MODE_X_IF_ANY_X:
323 /* Affect the execute bits if execute bits are already set
324 or if the file is a directory. */
325 if ((newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) || S_ISDIR (oldmode))
326 value |= S_IXUSR | S_IXGRP | S_IXOTH;
330 /* If WHO was specified, limit the change to the affected bits.
331 Otherwise, apply the umask. */
332 value &= (affected ? affected : ~umask_value);
337 /* If WHO was specified, preserve the previous values of
338 bits that are not affected by this change operation.
339 Otherwise, clear all the bits. */
340 newmode = (affected ? newmode & ~affected : 0);