Fixed failing hunks.
[rsync/rsync-patches.git] / chmod-option.diff
1 After applying this patch and running configure, you MUST run this
2 command before "make":
3
4     make proto
5
6
7 --- orig/Makefile.in    2005-07-07 23:11:07
8 +++ Makefile.in 2004-07-03 20:13:41
9 @@ -33,7 +33,7 @@ ZLIBOBJ=zlib/deflate.o zlib/inffast.o zl
10  OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
11         main.o checksum.o match.o syscall.o log.o backup.o
12  OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
13 -       fileio.o batch.o clientname.o
14 +       fileio.o batch.o clientname.o chmod.o
15  OBJS3=progress.o pipe.o
16  DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
17  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
18 --- orig/chmod.c        2005-07-09 16:09:14
19 +++ chmod.c     2005-07-09 16:09:14
20 @@ -0,0 +1,195 @@
21 +#include "rsync.h"
22 +
23 +extern int orig_umask;
24 +
25 +#define FLAG_X_KEEP (1<<0)
26 +#define FLAG_DIRS_ONLY (1<<1)
27 +#define FLAG_FILES_ONLY (1<<2)
28 +
29 +struct chmod_mode_struct {
30 +       struct chmod_mode_struct *next;
31 +       int ModeAND, ModeOR;
32 +       char flags;
33 +};
34 +
35 +#define CHMOD_ADD 1
36 +#define CHMOD_SUB 2
37 +#define CHMOD_EQ  3
38 +
39 +#define STATE_ERROR 0
40 +#define STATE_1ST_HALF 1
41 +#define STATE_2ND_HALF 2
42 +
43 +/* Parse a chmod-style argument, and break it down into one or more AND/OR
44 + * pairs in a linked list.  We use a state machine to walk through the
45 + * options. */
46 +struct chmod_mode_struct *parse_chmod(char *modestr)
47 +{
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,
51 +           *prev_mode = NULL;
52 +
53 +       while (state != STATE_ERROR) {
54 +               if (!*modestr || *modestr == ',') {
55 +                       int bits;
56 +
57 +                       if (!op) {
58 +                               state = STATE_ERROR;
59 +                               break;
60 +                       }
61 +                       prev_mode = curr_mode;
62 +                       curr_mode = new_array(struct chmod_mode_struct, 1);
63 +                       if (prev_mode)
64 +                               prev_mode->next = curr_mode;
65 +                       else
66 +                               first_mode = curr_mode;
67 +                       curr_mode->next = NULL;
68 +
69 +                       if (where)
70 +                               bits = where * what;
71 +                       else {
72 +                               where = 0111;
73 +                               bits = (where * what) & ~orig_umask;
74 +                       }
75 +
76 +                       switch (op) {
77 +                       case CHMOD_ADD:
78 +                               curr_mode->ModeAND = 07777;
79 +                               curr_mode->ModeOR  = bits + topoct;
80 +                               break;
81 +                       case CHMOD_SUB:
82 +                               curr_mode->ModeAND = 07777 - bits - topoct;
83 +                               curr_mode->ModeOR  = 0;
84 +                               break;
85 +                       case CHMOD_EQ:
86 +                               curr_mode->ModeAND = 07777 - (where * 7) - (topoct ? topbits : 0);
87 +                               curr_mode->ModeOR  = bits + topoct;
88 +                               break;
89 +                       }
90 +
91 +                       curr_mode->flags = flags;
92 +
93 +                       if (!*modestr)
94 +                               break;
95 +                       modestr++;
96 +
97 +                       state = STATE_1ST_HALF;
98 +                       where = what = op = topoct = topbits = flags = 0;
99 +               }
100 +
101 +               if (state != STATE_2ND_HALF) {
102 +                       switch (*modestr) {
103 +                       case 'D':
104 +                               if (flags & FLAG_FILES_ONLY)
105 +                                       state = STATE_ERROR;
106 +                               flags |= FLAG_DIRS_ONLY;
107 +                               break;
108 +                       case 'F':
109 +                               if (flags & FLAG_DIRS_ONLY)
110 +                                       state = STATE_ERROR;
111 +                               flags |= FLAG_FILES_ONLY;
112 +                               break;
113 +                       case 'u':
114 +                               where |= 0100;
115 +                               topbits |= 04000;
116 +                               break;
117 +                       case 'g':
118 +                               where |= 0010;
119 +                               topbits |= 02000;
120 +                               break;
121 +                       case 'o':
122 +                               where |= 0001;
123 +                               break;
124 +                       case 'a':
125 +                               where |= 0111;
126 +                               break;
127 +                       case '+':
128 +                               op = CHMOD_ADD;
129 +                               state = STATE_2ND_HALF;
130 +                               break;
131 +                       case '-':
132 +                               op = CHMOD_SUB;
133 +                               state = STATE_2ND_HALF;
134 +                               break;
135 +                       case '=':
136 +                               op = CHMOD_EQ;
137 +                               state = STATE_2ND_HALF;
138 +                               break;
139 +                       default:
140 +                               state = STATE_ERROR;
141 +                               break;
142 +                       }
143 +               } else {
144 +                       switch (*modestr) {
145 +                       case 'r':
146 +                               what |= 4;
147 +                               break;
148 +                       case 'w':
149 +                               what |= 2;
150 +                               break;
151 +                       case 'X':
152 +                               flags |= FLAG_X_KEEP;
153 +                               /* FALL THROUGH */
154 +                       case 'x':
155 +                               what |= 1;
156 +                               break;
157 +                       case 's':
158 +                               if (topbits)
159 +                                       topoct |= topbits;
160 +                               else
161 +                                       topoct = 04000;
162 +                               break;
163 +                       case 't':
164 +                               topoct |= 01000;
165 +                               break;
166 +                       default:
167 +                               state = STATE_ERROR;
168 +                               break;
169 +                       }
170 +               }
171 +               modestr++;
172 +       }
173 +
174 +       if (state == STATE_ERROR) {
175 +               free_chmod_mode(first_mode);
176 +               first_mode = NULL;
177 +       }
178 +       return first_mode;
179 +}
180 +
181 +
182 +/* Takes an existing file permission and a list of AND/OR changes, and
183 + * create a new permissions. */
184 +int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes)
185 +{
186 +       int IsX = mode & 0111;
187 +       int NonPerm = mode & ~07777;
188 +
189 +       for ( ; chmod_modes; chmod_modes = chmod_modes->next) {
190 +               if ((chmod_modes->flags & FLAG_DIRS_ONLY) && !S_ISDIR(NonPerm))
191 +                       continue;
192 +               if ((chmod_modes->flags & FLAG_FILES_ONLY) && S_ISDIR(NonPerm))
193 +                       continue;
194 +               mode &= chmod_modes->ModeAND;
195 +               if ((chmod_modes->flags & FLAG_X_KEEP) && !IsX && !S_ISDIR(NonPerm))
196 +                       mode |= chmod_modes->ModeOR & ~0111;
197 +               else
198 +                       mode |= chmod_modes->ModeOR;
199 +       }
200 +
201 +       return mode | NonPerm;
202 +}
203 +
204 +/* Free the linked list created by parse_chmod. */
205 +int free_chmod_mode(struct chmod_mode_struct *chmod_modes)
206 +{
207 +       struct chmod_mode_struct *next;
208 +
209 +       while (chmod_modes) {
210 +               next = chmod_modes->next;
211 +               free(chmod_modes);
212 +               chmod_modes = next;
213 +       }
214 +       return 0;
215 +}
216 --- orig/flist.c        2005-08-17 06:45:07
217 +++ flist.c     2004-09-18 01:51:11
218 @@ -62,6 +62,8 @@ extern struct file_list *the_file_list;
219  
220  extern char curr_dir[MAXPATHLEN];
221  
222 +extern struct chmod_mode_struct *chmod_modes;
223 +
224  extern struct filter_list_struct filter_list;
225  extern struct filter_list_struct server_filter_list;
226  
227 @@ -883,7 +885,10 @@ skip_filters:
228         file->flags = flags;
229         file->modtime = st.st_mtime;
230         file->length = st.st_size;
231 -       file->mode = st.st_mode;
232 +       if (chmod_modes && am_sender && (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
233 +               file->mode = tweak_mode(st.st_mode, chmod_modes);
234 +       else
235 +               file->mode = st.st_mode;
236         file->uid = st.st_uid;
237         file->gid = st.st_gid;
238  
239 --- orig/options.c      2005-08-27 21:11:26
240 +++ options.c   2005-08-27 21:18:52
241 @@ -141,6 +141,7 @@ char *log_format = NULL;
242  char *password_file = NULL;
243  char *rsync_path = RSYNC_PATH;
244  char *backup_dir = NULL;
245 +char *chmod_mode = NULL;
246  char backup_dir_buf[MAXPATHLEN];
247  int rsync_port = 0;
248  int compare_dest = 0;
249 @@ -160,6 +161,8 @@ int list_only = 0;
250  #define MAX_BATCH_NAME_LEN 256 /* Must be less than MAXPATHLEN-13 */
251  char *batch_name = NULL;
252  
253 +struct chmod_mode_struct *chmod_modes = NULL;
254 +
255  static int daemon_opt;   /* sets am_daemon after option error-reporting */
256  static int F_option_cnt = 0;
257  static int modify_window_set;
258 @@ -289,6 +292,7 @@ void usage(enum logcode F)
259    rprintf(F," -D, --devices               preserve devices (root only)\n");
260    rprintf(F," -t, --times                 preserve times\n");
261    rprintf(F," -O, --omit-dir-times        omit directories when preserving times\n");
262 +  rprintf(F,"     --chmod=CHMOD           change destination permissions\n");
263    rprintf(F," -S, --sparse                handle sparse files efficiently\n");
264    rprintf(F," -n, --dry-run               show what would have been transferred\n");
265    rprintf(F," -W, --whole-file            copy files whole (without rsync algorithm)\n");
266 @@ -411,6 +415,7 @@ static struct poptOption long_options[] 
267    {"no-relative",      0,  POPT_ARG_VAL,    &relative_paths, 0, 0, 0 },
268    {"no-R",             0,  POPT_ARG_VAL,    &relative_paths, 0, 0, 0 },
269    {"no-implied-dirs",  0,  POPT_ARG_VAL,    &implied_dirs, 0, 0, 0 },
270 +  {"chmod",            0,  POPT_ARG_STRING, &chmod_mode, 0, 0, 0 },
271    {"ignore-times",    'I', POPT_ARG_NONE,   &ignore_times, 0, 0, 0 },
272    {"size-only",        0,  POPT_ARG_NONE,   &size_only, 0, 0, 0 },
273    {"one-file-system", 'x', POPT_ARG_NONE,   &one_file_system, 0, 0, 0 },
274 @@ -1122,6 +1127,13 @@ int parse_arguments(int *argc, const cha
275         if (make_backups && !backup_dir)
276                 omit_dir_times = 1;
277  
278 +       if (chmod_mode && !(chmod_modes = parse_chmod(chmod_mode))) {
279 +               snprintf(err_buf, sizeof err_buf,
280 +                   "Invalid argument passed to chmod\n");
281 +               rprintf(FERROR, "ERROR: %s", err_buf);
282 +               return 0;
283 +       }
284 +
285         if (log_format) {
286                 if (log_format_has(log_format, 'i'))
287                         log_format_has_i = 1;
288 @@ -1507,6 +1519,11 @@ void server_options(char **args,int *arg
289                 }
290         }
291  
292 +       if (chmod_mode && !am_sender) {
293 +               args[ac++] = "--chmod";
294 +               args[ac++] = chmod_mode;
295 +       }
296 +
297         if (files_from && (!am_sender || filesfrom_host)) {
298                 if (filesfrom_host) {
299                         args[ac++] = "--files-from";
300 --- orig/rsync.yo       2005-08-27 21:05:12
301 +++ rsync.yo    2005-01-24 01:48:43
302 @@ -323,6 +323,7 @@ to the detailed description below for a 
303   -D, --devices               preserve devices (root only)
304   -t, --times                 preserve times
305   -O, --omit-dir-times        omit directories when preserving times
306 +     --chmod=CHMOD           change destination permissions
307   -S, --sparse                handle sparse files efficiently
308   -n, --dry-run               show what would have been transferred
309   -W, --whole-file            copy files whole (without rsync algorithm)
310 @@ -695,6 +696,14 @@ it is preserving modification times (see
311  the directories on the receiving side, it is a good idea to use bf(-O).
312  This option is inferred if you use bf(--backup) without bf(--backup-dir).
313  
314 +dit(bf(--chmod)) This options tells rsync to apply the listed "chmod" pattern
315 +to the permission of the files on the destination.  In addition to the normal
316 +parsing rules specified in the chmod manpage, you can specify an item that
317 +should only apply to a directory by prefixing it with a 'D', or specify an
318 +item that should only apply to a file by prefixing it with a 'F'.  For example:
319 +
320 +quote(--chmod=Dg+s,ug+w,Fo-w,+X)
321 +
322  dit(bf(-n, --dry-run)) This tells rsync to not do any file transfers,
323  instead it will just report the actions it would have taken.
324  
325 --- orig/testsuite/chmod-option.test    2005-07-09 15:49:59
326 +++ testsuite/chmod-option.test 2005-07-09 15:49:59
327 @@ -0,0 +1,44 @@
328 +#! /bin/sh
329 +
330 +# Copyright (C) 2002 by Martin Pool <mbp@samba.org>
331 +
332 +# This program is distributable under the terms of the GNU GPL (see
333 +# COPYING).
334 +
335 +# Test that the --chmod option functions correctly.
336 +
337 +. $srcdir/testsuite/rsync.fns
338 +
339 +set -x
340 +
341 +# Build some files
342 +
343 +fromdir="$scratchdir/from"
344 +todir="$scratchdir/to"
345 +checkdir="$scratchdir/check"
346 +
347 +mkdir "$fromdir"
348 +name1="$fromdir/name1"
349 +name2="$fromdir/name2"
350 +dir1="$fromdir/dir1"
351 +dir2="$fromdir/dir2"
352 +echo "This is the file" > "$name1"
353 +echo "This is the other file" > "$name2"
354 +mkdir "$dir1" "$dir2"
355 +
356 +chmod 4700 "$name1" || test_skipped "Can't chmod"
357 +chmod 700 "$dir1"
358 +chmod 770 "$dir2"
359 +
360 +# Copy the files we've created over to another directory
361 +checkit "$RSYNC -avv \"$fromdir/\" \"$checkdir/\"" "$fromdir" "$checkdir"
362 +
363 +# And then manually make the changes which should occur 
364 +umask 002
365 +chmod ug-s,a+rX "$checkdir"/*
366 +chmod +w "$checkdir" "$checkdir"/dir*
367 +
368 +checkit "$RSYNC -avv --chmod ug-s,a+rX,D+w \"$fromdir/\" \"$todir/\"" "$checkdir" "$todir"
369 +
370 +# The script would have aborted on error, so getting here means we've won.
371 +exit 0