Added ability to limit certain chmod directives to just dirs or files.
[rsync/rsync-patches.git] / chmod-option.diff
CommitLineData
2ece3e77 1--- Makefile.in 10 Feb 2004 17:06:11 -0000 1.98
548aa120 2+++ Makefile.in 20 Mar 2004 17:56:44 -0000
2ece3e77
WD
3@@ -34,7 +34,7 @@ ZLIBOBJ=zlib/deflate.o zlib/infblock.o z
4 OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
5 main.o checksum.o match.o syscall.o log.o backup.o
6 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
7- fileio.o batch.o clientname.o
8+ fileio.o batch.o clientname.o chmod.o
9 OBJS3=progress.o pipe.o
bd006671
MP
10 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
11 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
2ece3e77 12--- chmod.c 2004-02-13 17:08:33.000000000 -0800
548aa120
WD
13+++ chmod.c 2004-03-20 10:04:19.000000000 -0800
14@@ -0,0 +1,184 @@
bd006671
MP
15+#include "rsync.h"
16+
548aa120
WD
17+#define FLAG_X_KEEP (1<<0)
18+#define FLAG_DIRS_ONLY (1<<1)
19+#define FLAG_FILES_ONLY (1<<2)
20+
bd006671 21+struct chmod_mode_struct {
bd006671 22+ struct chmod_mode_struct *next;
548aa120
WD
23+ int ModeAND, ModeOR;
24+ char flags;
bd006671
MP
25+};
26+
27+#define CHMOD_ADD 1
28+#define CHMOD_SUB 2
29+#define CHMOD_EQ 3
30+
548aa120
WD
31+#define STATE_ERROR 0
32+#define STATE_1ST_HALF 1
33+#define STATE_2ND_HALF 2
34+
bd006671 35+/* Parse a chmod-style argument, and break it down into one or more AND/OR
548aa120
WD
36+ * pairs in a linked list. We use a state machine to walk through the
37+ * options. */
2ece3e77 38+struct chmod_mode_struct *parse_chmod(char *modestr)
bd006671 39+{
548aa120
WD
40+ int state = STATE_1ST_HALF;
41+ int where = 0, what = 0, op = 0, topbits = 0, topoct = 0, flags = 0;
2ece3e77
WD
42+ struct chmod_mode_struct *first_mode = NULL, *curr_mode = NULL,
43+ *prev_mode = NULL;
bd006671 44+
548aa120
WD
45+ while (state != STATE_ERROR) {
46+ if (!*modestr || *modestr == ',') {
47+ if (!op) {
48+ state = STATE_ERROR;
49+ break;
50+ }
51+ prev_mode = curr_mode;
52+ curr_mode = new_array(struct chmod_mode_struct, 1);
53+ if (prev_mode)
54+ prev_mode->next = curr_mode;
55+ else
56+ first_mode = curr_mode;
57+ curr_mode->next = NULL;
58+
59+ switch (op) {
60+ case CHMOD_ADD:
61+ curr_mode->ModeAND = 07777;
62+ curr_mode->ModeOR = (where * what) + topoct;
63+ break;
64+ case CHMOD_SUB:
65+ curr_mode->ModeAND = 07777 - (where * what) - topoct;
66+ curr_mode->ModeOR = 0;
67+ break;
68+ case CHMOD_EQ:
69+ curr_mode->ModeAND = 07777 - (where * 7);
70+ curr_mode->ModeOR = where * what - topoct;
71+ break;
72+ }
73+
74+ curr_mode->flags = flags;
75+
76+ if (!*modestr)
77+ break;
78+ modestr++;
79+
80+ state = STATE_1ST_HALF;
81+ where = what = op = topoct = topbits = flags = 0;
82+ }
83+
84+ if (state != STATE_2ND_HALF) {
2ece3e77 85+ switch (*modestr) {
548aa120
WD
86+ case 'D':
87+ if (flags & FLAG_FILES_ONLY)
88+ state = STATE_ERROR;
89+ flags |= FLAG_DIRS_ONLY;
90+ break;
91+ case 'F':
92+ if (flags & FLAG_DIRS_ONLY)
93+ state = STATE_ERROR;
94+ flags |= FLAG_FILES_ONLY;
95+ break;
2ece3e77
WD
96+ case 'u':
97+ where |= 0100;
98+ topbits |= 04000;
99+ break;
100+ case 'g':
101+ where |= 0010;
102+ topbits |= 02000;
103+ break;
104+ case 'o':
105+ where |= 0001;
106+ break;
107+ case 'a':
108+ where |= 0111;
109+ break;
110+ case '+':
111+ op = CHMOD_ADD;
548aa120 112+ state = STATE_2ND_HALF;
2ece3e77
WD
113+ break;
114+ case '-':
115+ op = CHMOD_SUB;
548aa120 116+ state = STATE_2ND_HALF;
2ece3e77
WD
117+ break;
118+ case '=':
119+ op = CHMOD_EQ;
548aa120 120+ state = STATE_2ND_HALF;
2ece3e77
WD
121+ break;
122+ default:
548aa120 123+ state = STATE_ERROR;
2ece3e77
WD
124+ break;
125+ }
126+ } else {
127+ switch (*modestr) {
128+ case 'r':
129+ what |= 4;
130+ break;
131+ case 'w':
132+ what |= 2;
133+ break;
134+ case 'X':
548aa120 135+ flags |= FLAG_X_KEEP;
2ece3e77
WD
136+ /* FALL THROUGH */
137+ case 'x':
138+ what |= 1;
139+ break;
140+ case 's':
141+ if (topbits)
bd006671 142+ topoct |= topbits;
2ece3e77 143+ else
bd006671 144+ topoct = 04000;
2ece3e77
WD
145+ break;
146+ case 't':
147+ topoct |= 01000;
148+ break;
149+ default:
548aa120 150+ state = STATE_ERROR;
2ece3e77
WD
151+ break;
152+ }
bd006671 153+ }
2ece3e77 154+ modestr++;
bd006671
MP
155+ }
156+
548aa120 157+ if (state == STATE_ERROR) {
bd006671 158+ free_chmod_mode(first_mode);
2ece3e77 159+ first_mode = NULL;
bd006671
MP
160+ }
161+ return first_mode;
162+}
163+
164+
2ece3e77
WD
165+/* Takes an existing file permission and a list of AND/OR changes, and
166+ * create a new permissions. */
548aa120 167+int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes)
bd006671 168+{
548aa120
WD
169+ int IsX = mode & 0111;
170+ int NonPerm = mode & ~07777;
171+
172+ for ( ; chmod_modes; chmod_modes = chmod_modes->next) {
173+ if ((chmod_modes->flags & FLAG_DIRS_ONLY) && !S_ISDIR(NonPerm))
174+ continue;
175+ if ((chmod_modes->flags & FLAG_FILES_ONLY) && S_ISDIR(NonPerm))
176+ continue;
177+ mode &= chmod_modes->ModeAND;
178+ if ((chmod_modes->flags & FLAG_X_KEEP) && !IsX && !S_ISDIR(NonPerm))
179+ mode |= chmod_modes->ModeOR & ~0111;
2ece3e77 180+ else
548aa120 181+ mode |= chmod_modes->ModeOR;
bd006671
MP
182+ }
183+
548aa120 184+ return mode | NonPerm;
bd006671
MP
185+}
186+
2ece3e77
WD
187+/* Free the linked list created by parse_chmod. */
188+int free_chmod_mode(struct chmod_mode_struct *chmod_modes)
bd006671
MP
189+{
190+ struct chmod_mode_struct *next;
191+
192+ while (chmod_modes) {
193+ next = chmod_modes->next;
194+ free(chmod_modes);
195+ chmod_modes = next;
196+ }
197+ return 0;
198+}
2ece3e77 199--- flist.c 11 Feb 2004 02:48:58 -0000 1.205
548aa120 200+++ flist.c 20 Mar 2004 17:56:45 -0000
2ece3e77 201@@ -33,6 +33,7 @@ extern int verbose;
bd006671 202 extern int do_progress;
2ece3e77 203 extern int am_root;
bd006671
MP
204 extern int am_server;
205+extern int am_sender;
206 extern int always_checksum;
2ece3e77
WD
207 extern int module_id;
208 extern int ignore_errors;
209@@ -63,6 +64,8 @@ extern int sanitize_paths;
bd006671
MP
210 extern int read_batch;
211 extern int write_batch;
212
213+extern struct chmod_mode_struct *chmod_modes;
214+
2ece3e77
WD
215 extern struct exclude_struct **exclude_list;
216 extern struct exclude_struct **server_exclude_list;
217 extern struct exclude_struct **local_exclude_list;
218@@ -831,7 +834,10 @@ skip_excludes:
219 file->flags = flags;
220 file->modtime = st.st_mtime;
221 file->length = st.st_size;
222- file->mode = st.st_mode;
223+ if (chmod_modes && am_sender && S_ISREG(st.st_mode))
224+ file->mode = tweak_mode(st.st_mode, chmod_modes);
225+ else
226+ file->mode = st.st_mode;
227 file->uid = st.st_uid;
228 file->gid = st.st_gid;
bd006671 229
2ece3e77 230--- options.c 22 Feb 2004 08:56:43 -0000 1.139
548aa120 231+++ options.c 20 Mar 2004 17:56:45 -0000
2ece3e77 232@@ -119,6 +119,7 @@ char *log_format = NULL;
bd006671
MP
233 char *password_file = NULL;
234 char *rsync_path = RSYNC_PATH;
235 char *backup_dir = NULL;
236+char *chmod_mode = NULL;
2ece3e77 237 char backup_dir_buf[MAXPATHLEN];
bd006671 238 int rsync_port = RSYNC_PORT;
2ece3e77
WD
239 int link_dest = 0;
240@@ -132,6 +133,8 @@ int list_only = 0;
241 #define MAX_BATCH_PREFIX_LEN 256 /* Must be less than MAXPATHLEN-13 */
242 char *batch_prefix = NULL;
bd006671
MP
243
244+struct chmod_mode_struct *chmod_modes = NULL;
2ece3e77
WD
245+
246 static int daemon_opt; /* sets am_daemon after option error-reporting */
247 static int modify_window_set;
bd006671 248
2ece3e77 249@@ -239,6 +242,7 @@ void usage(enum logcode F)
bd006671
MP
250 rprintf(F," -g, --group preserve group\n");
251 rprintf(F," -D, --devices preserve devices (root only)\n");
2ece3e77 252 rprintf(F," -t, --times preserve times\n");
548aa120 253+ rprintf(F," --chmod=CHMOD change destination permissions\n");
bd006671
MP
254 rprintf(F," -S, --sparse handle sparse files efficiently\n");
255 rprintf(F," -n, --dry-run show what would have been transferred\n");
256 rprintf(F," -W, --whole-file copy whole files, no incremental checks\n");
2ece3e77
WD
257@@ -342,6 +346,7 @@ static struct poptOption long_options[]
258 {"perms", 'p', POPT_ARG_NONE, &preserve_perms, 0, 0, 0 },
259 {"owner", 'o', POPT_ARG_NONE, &preserve_uid, 0, 0, 0 },
260 {"group", 'g', POPT_ARG_NONE, &preserve_gid, 0, 0, 0 },
261+ {"chmod", 0, POPT_ARG_STRING, &chmod_mode, 0, 0, 0 },
262 {"devices", 'D', POPT_ARG_NONE, &preserve_devices, 0, 0, 0 },
263 {"times", 't', POPT_ARG_NONE, &preserve_times, 0, 0, 0 },
264 {"checksum", 'c', POPT_ARG_NONE, &always_checksum, 0, 0, 0 },
265@@ -687,6 +692,13 @@ int parse_arguments(int *argc, const cha
266 exit_cleanup(RERR_SYNTAX);
267 }
bd006671 268
2ece3e77
WD
269+ if (chmod_mode && !(chmod_modes = parse_chmod(chmod_mode))) {
270+ snprintf(err_buf, sizeof err_buf,
271+ "Invalid argument passed to chmod\n");
272+ rprintf(FERROR, "ERROR: %s", err_buf);
273+ return 0;
274+ }
275+
276 if (do_progress && !verbose)
277 verbose = 1;
bd006671 278
2ece3e77 279@@ -932,6 +944,11 @@ void server_options(char **args,int *arg
bd006671 280 */
2ece3e77 281 args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
bd006671
MP
282 args[ac++] = compare_dest;
283+ }
284+
285+ if (chmod_mode && !am_sender) {
286+ args[ac++] = "--chmod";
287+ args[ac++] = chmod_mode;
288 }
289
2ece3e77
WD
290 if (files_from && (!am_sender || remote_filesfrom_file)) {
291--- proto.h 17 Feb 2004 23:13:06 -0000 1.184
548aa120 292+++ proto.h 20 Mar 2004 17:56:45 -0000
2ece3e77 293@@ -25,6 +25,9 @@ void file_checksum(char *fname,char *sum
bd006671 294 void sum_init(void);
2ece3e77 295 void sum_update(char *p, int len);
bd006671 296 void sum_end(char *sum);
2ece3e77 297+struct chmod_mode_struct *parse_chmod(char *modestr);
548aa120 298+int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes);
2ece3e77
WD
299+int free_chmod_mode(struct chmod_mode_struct *chmod_modes);
300 void close_all(void);
bd006671
MP
301 void _exit_cleanup(int code, const char *file, int line);
302 void cleanup_disable(void);
2ece3e77 303--- rsync.1 2 Feb 2004 18:23:09 -0000 1.163
548aa120 304+++ rsync.1 20 Mar 2004 18:12:57 -0000
2ece3e77 305@@ -336,6 +336,7 @@ to the detailed description below for a
bd006671
MP
306 -g, --group preserve group
307 -D, --devices preserve devices (root only)
308 -t, --times preserve times
309+ --chmod=CHMOD change destination permissions
310 -S, --sparse handle sparse files efficiently
311 -n, --dry-run show what would have been transferred
312 -W, --whole-file copy whole files, no incremental checks
548aa120 313@@ -614,6 +615,17 @@ modified cannot be effective; in other w
2ece3e77
WD
314 cause the next transfer to behave as if it used -I, and all files will have
315 their checksums compared and show up in log messages even if they haven\&'t
316 changed\&.
317+.IP
318+.IP "\fB--chmod\fP"
319+This options tells rsync to apply the listed "chmod" pattern
548aa120
WD
320+to the permission of the files on the destination\&. In addition to the normal
321+parsing rules specified in the chmod manpage, you can specify an item that
322+should only apply to a directory by prefixing it with a \&'D\&', or specify an
323+item that should only apply to a file by prefixing it with a \&'F\&'\&. For example:
324+.IP
325+.RS
326+--chmod=Dg+s,ug+w,Fo-w,+X
327+.RE
2ece3e77
WD
328 .IP
329 .IP "\fB-n, --dry-run\fP"
330 This tells rsync to not do any file transfers,
331--- rsync.yo 2 Feb 2004 18:23:09 -0000 1.147
548aa120 332+++ rsync.yo 20 Mar 2004 18:12:58 -0000
2ece3e77
WD
333@@ -299,6 +299,7 @@ verb(
334 -g, --group preserve group
335 -D, --devices preserve devices (root only)
336 -t, --times preserve times
337+ --chmod=CHMOD change destination permissions
338 -S, --sparse handle sparse files efficiently
339 -n, --dry-run show what would have been transferred
340 -W, --whole-file copy whole files, no incremental checks
548aa120 341@@ -534,6 +535,14 @@ modified cannot be effective; in other w
bd006671
MP
342 cause the next transfer to behave as if it used -I, and all files will have
343 their checksums compared and show up in log messages even if they haven't
344 changed.
345+
346+dit(bf(--chmod)) This options tells rsync to apply the listed "chmod" pattern
548aa120
WD
347+to the permission of the files on the destination. In addition to the normal
348+parsing rules specified in the chmod manpage, you can specify an item that
349+should only apply to a directory by prefixing it with a 'D', or specify an
350+item that should only apply to a file by prefixing it with a 'F'. For example:
351+
352+quote(--chmod=Dg+s,ug+w,Fo-w,+X)
bd006671
MP
353
354 dit(bf(-n, --dry-run)) This tells rsync to not do any file transfers,
355 instead it will just report the actions it would have taken.
2ece3e77 356--- testsuite/chmod.test 2004-02-13 17:08:33.000000000 -0800
548aa120 357+++ testsuite/chmod.test 2004-03-20 08:58:14.000000000 -0800
2ece3e77 358@@ -0,0 +1,37 @@
bd006671
MP
359+#! /bin/sh
360+
361+# Copyright (C) 2002 by Martin Pool <mbp@samba.org>
362+
363+# This program is distributable under the terms of the GNU GPL (see
364+# COPYING).
365+
366+# Test that the --chmod option functions correctly.
367+
368+. $srcdir/testsuite/rsync.fns
369+
370+set -x
371+
372+# Build some files
373+
374+fromdir="$scratchdir/from"
375+todir="$scratchdir/to"
376+checkdir="$scratchdir/check"
377+
378+mkdir "$fromdir"
379+name1="$fromdir/name1"
380+name2="$fromdir/name2"
381+echo "This is the file" > "$name1"
382+echo "This is the other file" > "$name2"
383+
384+chmod 4700 "$name1" || test_skipped "Can't chown"
385+
386+# Copy the files we've created over to another directory
2ece3e77 387+checkit "$RSYNC -avv \"$fromdir/\" \"$checkdir/\"" "$fromdir" "$checkdir"
bd006671
MP
388+
389+# And then manually make the changes which should occur
390+chmod ug-s,a+rX $checkdir/*
391+
2ece3e77 392+checkit "$RSYNC -avv --chmod ug-s,a+rX \"$fromdir/\" \"$todir/\"" "$checkdir" "$todir"
bd006671 393+
2ece3e77 394+# The script would have aborted on error, so getting here means we've won.
bd006671 395+exit 0