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');
127 /* Help the compiler optimize the usual case where mode_t uses
128 the traditional octal representation. */
129 mode = ((S_ISUID == SUID && S_ISGID == SGID && S_ISVTX == SVTX
130 && S_IRUSR == RUSR && S_IWUSR == WUSR && S_IXUSR == XUSR
131 && S_IRGRP == RGRP && S_IWGRP == WGRP && S_IXGRP == XGRP
132 && S_IROTH == ROTH && S_IWOTH == WOTH && S_IXOTH == XOTH)
134 : (mode_t) ((octal_value & SUID ? S_ISUID : 0)
135 | (octal_value & SGID ? S_ISGID : 0)
136 | (octal_value & SVTX ? S_ISVTX : 0)
137 | (octal_value & RUSR ? S_IRUSR : 0)
138 | (octal_value & WUSR ? S_IWUSR : 0)
139 | (octal_value & XUSR ? S_IXUSR : 0)
140 | (octal_value & RGRP ? S_IRGRP : 0)
141 | (octal_value & WGRP ? S_IWGRP : 0)
142 | (octal_value & XGRP ? S_IXGRP : 0)
143 | (octal_value & ROTH ? S_IROTH : 0)
144 | (octal_value & WOTH ? S_IWOTH : 0)
145 | (octal_value & XOTH ? S_IXOTH : 0)));
147 return make_node_op_equals (mode);
150 /* Allocate enough space to hold the result. */
154 for (p = mode_string; *p; p++)
155 needed += (*p == '=' || *p == '+' || *p == '-');
156 mc = xnmalloc (needed, sizeof *mc);
159 /* One loop iteration for each `[ugoa]*([-+=]([rwxXst]*|[ugo]))+'. */
160 for (;; mode_string++)
162 /* Which bits in the mode are operated on. */
165 /* Turn on all the bits in `affected' for each group given. */
166 for (;; mode_string++)
167 switch (*mode_string)
172 affected |= S_ISUID | S_IRWXU;
175 affected |= S_ISGID | S_IRWXG;
178 affected |= S_ISVTX | S_IRWXO;
181 affected |= CHMOD_MODE_BITS;
183 case '=': case '+': case '-':
184 goto no_more_affected;
190 char op = *mode_string++;
192 char flag = MODE_COPY_EXISTING;
193 struct mode_change *change;
195 switch (*mode_string++)
198 /* Set the affected bits to the value of the `u' bits
203 /* Set the affected bits to the value of the `g' bits
208 /* Set the affected bits to the value of the `o' bits
215 flag = MODE_ORDINARY_CHANGE;
217 for (mode_string--;; mode_string++)
218 switch (*mode_string)
221 value |= S_IRUSR | S_IRGRP | S_IROTH;
224 value |= S_IWUSR | S_IWGRP | S_IWOTH;
227 value |= S_IXUSR | S_IXGRP | S_IXOTH;
230 flag = MODE_X_IF_ANY_X;
233 /* Set the setuid/gid bits if `u' or `g' is selected. */
234 value |= S_ISUID | S_ISGID;
237 /* Set the "save text image" bit if `o' is selected. */
246 change = &mc[used++];
249 change->affected = affected;
250 change->value = value;
252 while (*mode_string == '=' || *mode_string == '+'
253 || *mode_string == '-');
255 if (*mode_string != ',')
259 if (*mode_string == 0)
261 mc[used].flag = MODE_DONE;
270 /* Return a file mode change operation that sets permissions to match those
271 of REF_FILE. Return NULL (setting errno) if REF_FILE can't be accessed. */
274 mode_create_from_ref (const char *ref_file)
276 struct stat ref_stats;
278 if (stat (ref_file, &ref_stats) != 0)
280 return make_node_op_equals (ref_stats.st_mode);
283 /* Return file mode OLDMODE, adjusted as indicated by the list of change
284 operations CHANGES, which are interpreted assuming the umask is
285 UMASK_VALUE. If OLDMODE is a directory, the type `X'
286 change affects it even if no execute bits were set in OLDMODE.
287 The returned value has the S_IFMT bits cleared. */
290 mode_adjust (mode_t oldmode, struct mode_change const *changes,
293 /* The adjusted mode. */
294 mode_t newmode = oldmode & CHMOD_MODE_BITS;
296 for (; changes->flag != MODE_DONE; changes++)
298 mode_t affected = changes->affected;
299 mode_t value = changes->value;
301 switch (changes->flag)
303 case MODE_ORDINARY_CHANGE:
306 case MODE_COPY_EXISTING:
307 /* Isolate in `value' the bits in `newmode' to copy. */
310 /* Copy the isolated bits to the other two parts. */
311 value |= ((value & (S_IRUSR | S_IRGRP | S_IROTH)
312 ? S_IRUSR | S_IRGRP | S_IROTH : 0)
313 | (value & (S_IWUSR | S_IWGRP | S_IWOTH)
314 ? S_IWUSR | S_IWGRP | S_IWOTH : 0)
315 | (value & (S_IXUSR | S_IXGRP | S_IXOTH)
316 ? S_IXUSR | S_IXGRP | S_IXOTH : 0));
319 case MODE_X_IF_ANY_X:
320 /* Affect the execute bits if execute bits are already set
321 or if the file is a directory. */
322 if ((newmode & (S_IXUSR | S_IXGRP | S_IXOTH)) || S_ISDIR (oldmode))
323 value |= S_IXUSR | S_IXGRP | S_IXOTH;
327 /* If WHO was specified, limit the change to the affected bits.
328 Otherwise, apply the umask. */
329 value &= (affected ? affected : ~umask_value);
334 /* If WHO was specified, preserve the previous values of
335 bits that are not affected by this change operation.
336 Otherwise, clear all the bits. */
337 newmode = (affected ? newmode & ~affected : 0);