Craig Barratt's --checksum-seed option.
[rsync/rsync-patches.git] / chmod-option.diff
CommitLineData
2ece3e77 1--- Makefile.in 10 Feb 2004 17:06:11 -0000 1.98
bd8bf8b1 2+++ Makefile.in 22 Apr 2004 23:39:42 -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 \
bd8bf8b1
WD
12--- /dev/null 1 Jan 1970 00:00:00 -0000
13+++ chmod.c 22 Apr 2004 23:39:43 -0000
548aa120 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+}
bd8bf8b1
WD
199--- flist.c 22 Apr 2004 22:17:15 -0000 1.216
200+++ flist.c 22 Apr 2004 23:39:43 -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;
54691942 206 extern int am_daemon;
bd006671 207 extern int always_checksum;
2ece3e77 208 extern int module_id;
54691942 209@@ -64,6 +65,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+
54691942
WD
215 extern struct exclude_list_struct exclude_list;
216 extern struct exclude_list_struct server_exclude_list;
217 extern struct exclude_list_struct local_exclude_list;
bd8bf8b1 218@@ -853,7 +856,10 @@ skip_excludes:
2ece3e77
WD
219 file->flags = flags;
220 file->modtime = st.st_mtime;
221 file->length = st.st_size;
222- file->mode = st.st_mode;
46b46be7 223+ if (chmod_modes && am_sender && (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
2ece3e77
WD
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
bd8bf8b1
WD
230--- options.c 17 Apr 2004 17:07:23 -0000 1.147
231+++ options.c 22 Apr 2004 23:39:43 -0000
54691942 232@@ -121,6 +121,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 239 int link_dest = 0;
54691942 240@@ -134,6 +135,8 @@ int list_only = 0;
2ece3e77
WD
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
54691942 249@@ -241,6 +244,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");
54691942 257@@ -344,6 +348,7 @@ static struct poptOption long_options[]
2ece3e77
WD
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 },
54691942 265@@ -720,6 +725,13 @@ int parse_arguments(int *argc, const cha
2ece3e77
WD
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
54691942 279@@ -951,6 +963,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 290 if (files_from && (!am_sender || remote_filesfrom_file)) {
bd8bf8b1
WD
291--- proto.h 22 Apr 2004 09:58:09 -0000 1.189
292+++ proto.h 22 Apr 2004 23:39:43 -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);
103bcb1d
WD
303--- rsync.yo 24 Apr 2004 06:16:04 -0000 1.164
304+++ rsync.yo 24 Apr 2004 07:32:05 -0000
2ece3e77
WD
305@@ -299,6 +299,7 @@ verb(
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
103bcb1d 313@@ -543,6 +544,14 @@ modified cannot be effective; in other w
bd006671
MP
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+
318+dit(bf(--chmod)) This options tells rsync to apply the listed "chmod" pattern
548aa120
WD
319+to the permission of the files on the destination. In addition to the normal
320+parsing rules specified in the chmod manpage, you can specify an item that
321+should only apply to a directory by prefixing it with a 'D', or specify an
322+item that should only apply to a file by prefixing it with a 'F'. For example:
323+
324+quote(--chmod=Dg+s,ug+w,Fo-w,+X)
bd006671
MP
325
326 dit(bf(-n, --dry-run)) This tells rsync to not do any file transfers,
327 instead it will just report the actions it would have taken.
bd8bf8b1
WD
328--- /dev/null 1 Jan 1970 00:00:00 -0000
329+++ testsuite/chmod.test 22 Apr 2004 23:39:44 -0000
46b46be7 330@@ -0,0 +1,43 @@
bd006671
MP
331+#! /bin/sh
332+
333+# Copyright (C) 2002 by Martin Pool <mbp@samba.org>
334+
335+# This program is distributable under the terms of the GNU GPL (see
336+# COPYING).
337+
338+# Test that the --chmod option functions correctly.
339+
340+. $srcdir/testsuite/rsync.fns
341+
342+set -x
343+
344+# Build some files
345+
346+fromdir="$scratchdir/from"
347+todir="$scratchdir/to"
348+checkdir="$scratchdir/check"
349+
350+mkdir "$fromdir"
351+name1="$fromdir/name1"
352+name2="$fromdir/name2"
46b46be7
WD
353+dir1="$fromdir/dir1"
354+dir2="$fromdir/dir2"
bd006671
MP
355+echo "This is the file" > "$name1"
356+echo "This is the other file" > "$name2"
46b46be7 357+mkdir "$dir1" "$dir2"
bd006671 358+
46b46be7
WD
359+chmod 4700 "$name1" || test_skipped "Can't chmod"
360+chmod 700 "$dir1"
361+chmod 770 "$dir2"
bd006671
MP
362+
363+# Copy the files we've created over to another directory
2ece3e77 364+checkit "$RSYNC -avv \"$fromdir/\" \"$checkdir/\"" "$fromdir" "$checkdir"
bd006671
MP
365+
366+# And then manually make the changes which should occur
46b46be7
WD
367+chmod ug-s,a+rX "$checkdir"/*
368+chmod g+w "$checkdir" "$checkdir"/dir*
bd006671 369+
46b46be7 370+checkit "$RSYNC -avv --chmod ug-s,a+rX,Dg+w \"$fromdir/\" \"$todir/\"" "$checkdir" "$todir"
bd006671 371+
2ece3e77 372+# The script would have aborted on error, so getting here means we've won.
bd006671 373+exit 0