2 * Implement the core of the --chmod option.
4 * Copyright (C) 2002 Scott Howard
5 * Copyright (C) 2005-2007 Wayne Davison
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3 as
9 * published by the Free Software Foundation.
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 along
17 * with this program; if not, visit the http://fsf.org website.
22 extern mode_t orig_umask;
24 #define FLAG_X_KEEP (1<<0)
25 #define FLAG_DIRS_ONLY (1<<1)
26 #define FLAG_FILES_ONLY (1<<2)
28 struct chmod_mode_struct {
29 struct chmod_mode_struct *next;
39 #define STATE_1ST_HALF 1
40 #define STATE_2ND_HALF 2
42 /* Parse a chmod-style argument, and break it down into one or more AND/OR
43 * pairs in a linked list. We return a pointer to new items on succcess
44 * (appending the items to the specified list), or NULL on error. */
45 struct chmod_mode_struct *parse_chmod(const char *modestr,
46 struct chmod_mode_struct **root_mode_ptr)
48 int state = STATE_1ST_HALF;
49 int where = 0, what = 0, op = 0, topbits = 0, topoct = 0, flags = 0;
50 struct chmod_mode_struct *first_mode = NULL, *curr_mode = NULL,
53 while (state != STATE_ERROR) {
54 if (!*modestr || *modestr == ',') {
61 prev_mode = curr_mode;
62 curr_mode = new_array(struct chmod_mode_struct, 1);
64 prev_mode->next = curr_mode;
66 first_mode = curr_mode;
67 curr_mode->next = NULL;
73 bits = (where * what) & ~orig_umask;
78 curr_mode->ModeAND = CHMOD_BITS;
79 curr_mode->ModeOR = bits + topoct;
82 curr_mode->ModeAND = CHMOD_BITS - bits - topoct;
83 curr_mode->ModeOR = 0;
86 curr_mode->ModeAND = CHMOD_BITS - (where * 7) - (topoct ? topbits : 0);
87 curr_mode->ModeOR = bits + topoct;
91 curr_mode->flags = flags;
97 state = STATE_1ST_HALF;
98 where = what = op = topoct = topbits = flags = 0;
101 if (state != STATE_2ND_HALF) {
104 if (flags & FLAG_FILES_ONLY)
106 flags |= FLAG_DIRS_ONLY;
109 if (flags & FLAG_DIRS_ONLY)
111 flags |= FLAG_FILES_ONLY;
129 state = STATE_2ND_HALF;
133 state = STATE_2ND_HALF;
137 state = STATE_2ND_HALF;
152 flags |= FLAG_X_KEEP;
174 if (state == STATE_ERROR) {
175 free_chmod_mode(first_mode);
179 if (!(curr_mode = *root_mode_ptr))
180 *root_mode_ptr = first_mode;
182 while (curr_mode->next)
183 curr_mode = curr_mode->next;
184 curr_mode->next = first_mode;
191 /* Takes an existing file permission and a list of AND/OR changes, and
192 * create a new permissions. */
193 int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes)
195 int IsX = mode & 0111;
196 int NonPerm = mode & ~CHMOD_BITS;
198 for ( ; chmod_modes; chmod_modes = chmod_modes->next) {
199 if ((chmod_modes->flags & FLAG_DIRS_ONLY) && !S_ISDIR(NonPerm))
201 if ((chmod_modes->flags & FLAG_FILES_ONLY) && S_ISDIR(NonPerm))
203 mode &= chmod_modes->ModeAND;
204 if ((chmod_modes->flags & FLAG_X_KEEP) && !IsX && !S_ISDIR(NonPerm))
205 mode |= chmod_modes->ModeOR & ~0111;
207 mode |= chmod_modes->ModeOR;
210 return mode | NonPerm;
213 /* Free the linked list created by parse_chmod. */
214 int free_chmod_mode(struct chmod_mode_struct *chmod_modes)
216 struct chmod_mode_struct *next;
218 while (chmod_modes) {
219 next = chmod_modes->next;