749afa67312a15e16a2976b4d6e68ddc62c30734
[rsync/rsync-patches.git] / chmod-option.diff
1 --- Makefile.in 2 May 2004 17:04:14 -0000       1.100
2 +++ Makefile.in 8 May 2004 18:34:49 -0000
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
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 \
12 --- /dev/null   1 Jan 1970 00:00:00 -0000
13 +++ chmod.c     8 May 2004 18:34:49 -0000
14 @@ -0,0 +1,184 @@
15 +#include "rsync.h"
16 +
17 +#define FLAG_X_KEEP (1<<0)
18 +#define FLAG_DIRS_ONLY (1<<1)
19 +#define FLAG_FILES_ONLY (1<<2)
20 +
21 +struct chmod_mode_struct {
22 +       struct chmod_mode_struct *next;
23 +       int ModeAND, ModeOR;
24 +       char flags;
25 +};
26 +
27 +#define CHMOD_ADD 1
28 +#define CHMOD_SUB 2
29 +#define CHMOD_EQ  3
30 +
31 +#define STATE_ERROR 0
32 +#define STATE_1ST_HALF 1
33 +#define STATE_2ND_HALF 2
34 +
35 +/* Parse a chmod-style argument, and break it down into one or more AND/OR
36 + * pairs in a linked list.  We use a state machine to walk through the
37 + * options. */
38 +struct chmod_mode_struct *parse_chmod(char *modestr)
39 +{
40 +       int state = STATE_1ST_HALF;
41 +       int where = 0, what = 0, op = 0, topbits = 0, topoct = 0, flags = 0;
42 +       struct chmod_mode_struct *first_mode = NULL, *curr_mode = NULL,
43 +           *prev_mode = NULL;
44 +
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) {
85 +                       switch (*modestr) {
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;
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;
112 +                               state = STATE_2ND_HALF;
113 +                               break;
114 +                       case '-':
115 +                               op = CHMOD_SUB;
116 +                               state = STATE_2ND_HALF;
117 +                               break;
118 +                       case '=':
119 +                               op = CHMOD_EQ;
120 +                               state = STATE_2ND_HALF;
121 +                               break;
122 +                       default:
123 +                               state = STATE_ERROR;
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':
135 +                               flags |= FLAG_X_KEEP;
136 +                               /* FALL THROUGH */
137 +                       case 'x':
138 +                               what |= 1;
139 +                               break;
140 +                       case 's':
141 +                               if (topbits)
142 +                                       topoct |= topbits;
143 +                               else
144 +                                       topoct = 04000;
145 +                               break;
146 +                       case 't':
147 +                               topoct |= 01000;
148 +                               break;
149 +                       default:
150 +                               state = STATE_ERROR;
151 +                               break;
152 +                       }
153 +               }
154 +               modestr++;
155 +       }
156 +
157 +       if (state == STATE_ERROR) {
158 +               free_chmod_mode(first_mode);
159 +               first_mode = NULL;
160 +       }
161 +       return first_mode;
162 +}
163 +
164 +
165 +/* Takes an existing file permission and a list of AND/OR changes, and
166 + * create a new permissions. */
167 +int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes)
168 +{
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;
180 +               else
181 +                       mode |= chmod_modes->ModeOR;
182 +       }
183 +
184 +       return mode | NonPerm;
185 +}
186 +
187 +/* Free the linked list created by parse_chmod. */
188 +int free_chmod_mode(struct chmod_mode_struct *chmod_modes)
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 +}
199 --- flist.c     3 May 2004 01:24:10 -0000       1.220
200 +++ flist.c     8 May 2004 18:34:49 -0000
201 @@ -33,6 +33,7 @@ extern int verbose;
202  extern int do_progress;
203  extern int am_root;
204  extern int am_server;
205 +extern int am_sender;
206  extern int am_daemon;
207  extern int always_checksum;
208  extern int module_id;
209 @@ -64,6 +65,8 @@ extern int sanitize_paths;
210  extern int read_batch;
211  extern int write_batch;
212  
213 +extern struct chmod_mode_struct *chmod_modes;
214 +
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;
218 @@ -854,7 +857,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) || S_ISDIR(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;
229  
230 --- options.c   6 May 2004 21:08:01 -0000       1.148
231 +++ options.c   8 May 2004 18:34:49 -0000
232 @@ -121,6 +121,7 @@ char *log_format = NULL;
233  char *password_file = NULL;
234  char *rsync_path = RSYNC_PATH;
235  char *backup_dir = NULL;
236 +char *chmod_mode = NULL;
237  char backup_dir_buf[MAXPATHLEN];
238  int rsync_port = RSYNC_PORT;
239  int link_dest = 0;
240 @@ -134,6 +135,8 @@ int list_only = 0;
241  #define MAX_BATCH_PREFIX_LEN 256       /* Must be less than MAXPATHLEN-13 */
242  char *batch_prefix = NULL;
243  
244 +struct chmod_mode_struct *chmod_modes = NULL;
245 +
246  static int daemon_opt;   /* sets am_daemon after option error-reporting */
247  static int modify_window_set;
248  
249 @@ -241,6 +244,7 @@ void usage(enum logcode F)
250    rprintf(F," -g, --group                 preserve group\n");
251    rprintf(F," -D, --devices               preserve devices (root only)\n");
252    rprintf(F," -t, --times                 preserve times\n");
253 +  rprintf(F,"     --chmod=CHMOD           change destination permissions\n");
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");
257 @@ -344,6 +348,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 @@ -720,6 +725,13 @@ int parse_arguments(int *argc, const cha
266                 exit_cleanup(RERR_SYNTAX);
267         }
268  
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;
278  
279 @@ -951,6 +963,11 @@ void server_options(char **args,int *arg
280                  */
281                 args[ac++] = link_dest ? "--link-dest" : "--compare-dest";
282                 args[ac++] = compare_dest;
283 +       }
284 +
285 +       if (chmod_mode && !am_sender) {
286 +               args[ac++] = "--chmod";
287 +               args[ac++] = chmod_mode;
288         }
289  
290         if (files_from && (!am_sender || remote_filesfrom_file)) {
291 --- proto.h     22 Apr 2004 09:58:09 -0000      1.189
292 +++ proto.h     8 May 2004 18:34:49 -0000
293 @@ -25,6 +25,9 @@ void file_checksum(char *fname,char *sum
294  void sum_init(void);
295  void sum_update(char *p, int len);
296  void sum_end(char *sum);
297 +struct chmod_mode_struct *parse_chmod(char *modestr);
298 +int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes);
299 +int free_chmod_mode(struct chmod_mode_struct *chmod_modes);
300  void close_all(void);
301  void _exit_cleanup(int code, const char *file, int line);
302  void cleanup_disable(void);
303 --- rsync.yo    7 May 2004 00:18:37 -0000       1.169
304 +++ rsync.yo    8 May 2004 18:34:50 -0000
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
313 @@ -545,6 +546,14 @@ modified cannot be effective; in other w
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
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)
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.
328 --- /dev/null   1 Jan 1970 00:00:00 -0000
329 +++ testsuite/chmod.test        8 May 2004 18:34:50 -0000
330 @@ -0,0 +1,43 @@
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"
353 +dir1="$fromdir/dir1"
354 +dir2="$fromdir/dir2"
355 +echo "This is the file" > "$name1"
356 +echo "This is the other file" > "$name2"
357 +mkdir "$dir1" "$dir2"
358 +
359 +chmod 4700 "$name1" || test_skipped "Can't chmod"
360 +chmod 700 "$dir1"
361 +chmod 770 "$dir2"
362 +
363 +# Copy the files we've created over to another directory
364 +checkit "$RSYNC -avv \"$fromdir/\" \"$checkdir/\"" "$fromdir" "$checkdir"
365 +
366 +# And then manually make the changes which should occur 
367 +chmod ug-s,a+rX "$checkdir"/*
368 +chmod g+w "$checkdir" "$checkdir"/dir*
369 +
370 +checkit "$RSYNC -avv --chmod ug-s,a+rX,Dg+w \"$fromdir/\" \"$todir/\"" "$checkdir" "$todir"
371 +
372 +# The script would have aborted on error, so getting here means we've won.
373 +exit 0