Fixed one problem with the new chmod functionality and improved the
[rsync-patches.git] / chmod-option.diff
1 --- Makefile.in 10 Feb 2004 17:06:11 -0000      1.98
2 +++ Makefile.in 20 Mar 2004 17:56:44 -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 --- chmod.c     2004-02-13 17:08:33.000000000 -0800
13 +++ chmod.c     2004-03-20 10:04:19.000000000 -0800
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     11 Feb 2004 02:48:58 -0000      1.205
200 +++ flist.c     20 Mar 2004 18:26:01 -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 always_checksum;
207  extern int module_id;
208  extern int ignore_errors;
209 @@ -63,6 +64,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_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) || 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   22 Feb 2004 08:56:43 -0000      1.139
231 +++ options.c   20 Mar 2004 17:56:45 -0000
232 @@ -119,6 +119,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 @@ -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;
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 @@ -239,6 +242,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 @@ -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         }
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 @@ -932,6 +944,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     17 Feb 2004 23:13:06 -0000      1.184
292 +++ proto.h     20 Mar 2004 17:56:45 -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.1     2 Feb 2004 18:23:09 -0000       1.163
304 +++ rsync.1     20 Mar 2004 18:12:57 -0000
305 @@ -336,6 +336,7 @@ to the detailed description below for a 
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 @@ -614,6 +615,17 @@ 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 +.IP 
318 +.IP "\fB--chmod\fP" 
319 +This options tells rsync to apply the listed "chmod" pattern
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 
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
332 +++ rsync.yo    20 Mar 2004 18:12:58 -0000
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
341 @@ -534,6 +535,14 @@ modified cannot be effective; in other w
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
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)
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.
356 --- testsuite/chmod.test        2004-02-13 17:08:33.000000000 -0800
357 +++ testsuite/chmod.test        Sat Mar 20 10:30:48 2004
358 @@ -0,0 +1,43 @@
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 +dir1="$fromdir/dir1"
382 +dir2="$fromdir/dir2"
383 +echo "This is the file" > "$name1"
384 +echo "This is the other file" > "$name2"
385 +mkdir "$dir1" "$dir2"
386 +
387 +chmod 4700 "$name1" || test_skipped "Can't chmod"
388 +chmod 700 "$dir1"
389 +chmod 770 "$dir2"
390 +
391 +# Copy the files we've created over to another directory
392 +checkit "$RSYNC -avv \"$fromdir/\" \"$checkdir/\"" "$fromdir" "$checkdir"
393 +
394 +# And then manually make the changes which should occur 
395 +chmod ug-s,a+rX "$checkdir"/*
396 +chmod g+w "$checkdir" "$checkdir"/dir*
397 +
398 +checkit "$RSYNC -avv --chmod ug-s,a+rX,Dg+w \"$fromdir/\" \"$todir/\"" "$checkdir" "$todir"
399 +
400 +# The script would have aborted on error, so getting here means we've won.
401 +exit 0