Got rid of the (unneeded) ./prepare_source instructions.
[rsync/rsync-patches.git] / flags.diff
1 This patch provides --flags, which preserves the st_flags field.
2 Modified from a patch that was written by Rolf Grossmann:
3
4     http://www.progtech.net/rsync.flags-patch
5
6 --- old/configure.in
7 +++ new/configure.in
8 @@ -527,7 +527,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strd
9      memmove lchown vsnprintf snprintf vasprintf asprintf setsid glob strpbrk \
10      strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
11      setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
12 -    strerror putenv iconv_open locale_charset nl_langinfo \
13 +    chflags strerror putenv iconv_open locale_charset nl_langinfo \
14      sigaction sigprocmask)
15  
16  AC_CHECK_FUNCS(getpgrp tcgetpgrp)
17 --- old/flist.c
18 +++ new/flist.c
19 @@ -44,6 +44,7 @@ extern int preserve_links;
20  extern int preserve_hard_links;
21  extern int preserve_devices;
22  extern int preserve_specials;
23 +extern int preserve_flags;
24  extern int preserve_uid;
25  extern int preserve_gid;
26  extern int relative_paths;
27 @@ -303,6 +304,9 @@ static void send_file_entry(struct file_
28         unsigned short flags;
29         static time_t modtime;
30         static mode_t mode;
31 +#ifdef SUPPORT_FLAGS
32 +       static uint32 fileflags;
33 +#endif
34         static int64 dev;
35         static dev_t rdev;
36         static uint32 rdev_major;
37 @@ -333,6 +337,12 @@ static void send_file_entry(struct file_
38                 flags |= XMIT_SAME_MODE;
39         else
40                 mode = file->mode;
41 +#ifdef SUPPORT_FLAGS
42 +       if (file->fileflags == fileflags)
43 +               flags |= XMIT_SAME_FLAGS;
44 +       else
45 +               fileflags = file->fileflags;
46 +#endif
47         if ((preserve_devices && IS_DEVICE(mode))
48          || (preserve_specials && IS_SPECIAL(mode))) {
49                 if (protocol_version < 28) {
50 @@ -416,6 +426,10 @@ static void send_file_entry(struct file_
51                 write_int(f, modtime);
52         if (!(flags & XMIT_SAME_MODE))
53                 write_int(f, to_wire_mode(mode));
54 +#ifdef SUPPORT_FLAGS
55 +       if (preserve_flags && !(flags & XMIT_SAME_FLAGS))
56 +               write_int(f, (int)fileflags);
57 +#endif
58         if (preserve_uid && !(flags & XMIT_SAME_UID)) {
59                 if (!numeric_ids)
60                         add_uid(uid);
61 @@ -483,6 +497,9 @@ static struct file_struct *receive_file_
62  {
63         static time_t modtime;
64         static mode_t mode;
65 +#ifdef SUPPORT_FLAGS
66 +       static uint32 fileflags;
67 +#endif
68         static int64 dev;
69         static dev_t rdev;
70         static uint32 rdev_major;
71 @@ -556,9 +573,12 @@ static struct file_struct *receive_file_
72                 modtime = (time_t)read_int(f);
73         if (!(flags & XMIT_SAME_MODE))
74                 mode = from_wire_mode(read_int(f));
75 -
76         if (chmod_modes && !S_ISLNK(mode))
77                 mode = tweak_mode(mode, chmod_modes);
78 +#ifdef SUPPORT_FLAGS
79 +       if (preserve_flags && !(flags & XMIT_SAME_FLAGS))
80 +               fileflags = (uint32)read_int(f);
81 +#endif
82  
83         if (preserve_uid && !(flags & XMIT_SAME_UID))
84                 uid = (uid_t)read_int(f);
85 @@ -609,6 +629,9 @@ static struct file_struct *receive_file_
86         file->modtime = modtime;
87         file->length = file_length;
88         file->mode = mode;
89 +#ifdef SUPPORT_FLAGS
90 +       file->fileflags = fileflags;
91 +#endif
92         file->uid = uid;
93         file->gid = gid;
94  
95 @@ -862,6 +885,9 @@ struct file_struct *make_file(char *fnam
96         file->modtime = st.st_mtime;
97         file->length = st.st_size;
98         file->mode = st.st_mode;
99 +#ifdef SUPPORT_FLAGS
100 +       file->fileflags = st.st_flags;
101 +#endif
102         file->uid = st.st_uid;
103         file->gid = st.st_gid;
104  
105 --- old/generator.c
106 +++ new/generator.c
107 @@ -99,6 +99,12 @@ static int deletion_count = 0; /* used t
108  #define DEL_FORCE_RECURSE      (1<<1) /* recurse even w/o --force */
109  #define DEL_TERSE              (1<<3)
110  
111 +#ifdef SUPPORT_FLAGS
112 +#define FILEFLAGS(ff) ff
113 +#else
114 +#define FILEFLAGS(ff) 0
115 +#endif
116 +
117  
118  static int is_backup_file(char *fn)
119  {
120 @@ -113,7 +119,7 @@ static int is_backup_file(char *fn)
121   * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's
122   * a directory! (The buffer is used for recursion, but returned unchanged.)
123   */
124 -static int delete_item(char *fname, int mode, int flags)
125 +static int delete_item(char *fname, int mode, uint32 fileflags, int flags)
126  {
127         struct file_list *dirlist;
128         int j, dlen, zap_dir, ok;
129 @@ -124,6 +130,9 @@ static int delete_item(char *fname, int 
130         if (!S_ISDIR(mode)) {
131                 if (max_delete && ++deletion_count > max_delete)
132                         return 0;
133 +#ifdef SUPPORT_FLAGS
134 +               make_mutable(fname, mode, fileflags);
135 +#endif
136                 if (make_backups && (backup_dir || !is_backup_file(fname)))
137                         ok = make_backup(fname);
138                 else
139 @@ -148,10 +157,17 @@ static int delete_item(char *fname, int 
140                 ok = 0;
141                 errno = ENOTEMPTY;
142         } else if (make_backups && !backup_dir && !is_backup_file(fname)
143 -           && !(flags & DEL_FORCE_RECURSE))
144 +           && !(flags & DEL_FORCE_RECURSE)) {
145 +#ifdef SUPPORT_FLAGS
146 +               make_mutable(fname, mode, fileflags);
147 +#endif
148                 ok = make_backup(fname);
149 -       else
150 +       } else {
151 +#ifdef SUPPORT_FLAGS
152 +               make_mutable(fname, mode, fileflags);
153 +#endif
154                 ok = do_rmdir(fname) == 0;
155 +       }
156         if (ok) {
157                 if (!(flags & DEL_TERSE))
158                         log_delete(fname, mode);
159 @@ -186,7 +202,7 @@ static int delete_item(char *fname, int 
160                         continue;
161  
162                 strlcpy(p, fp->basename, remainder);
163 -               delete_item(fname, fp->mode, flags & ~DEL_TERSE);
164 +               delete_item(fname, fp->mode, FILEFLAGS(fp->fileflags), flags & ~DEL_TERSE);
165         }
166         flist_free(dirlist);
167  
168 @@ -276,7 +292,7 @@ static void delete_in_dir(struct file_li
169                         continue;
170                 if (flist_find(flist, fp) < 0) {
171                         f_name(fp, delbuf);
172 -                       delete_item(delbuf, fp->mode, DEL_FORCE_RECURSE);
173 +                       delete_item(delbuf, fp->mode, FILEFLAGS(fp->fileflags), DEL_FORCE_RECURSE);
174                 }
175         }
176  
177 @@ -905,7 +921,7 @@ static void recv_generator(char *fname, 
178                  * we need to delete it.  If it doesn't exist, then
179                  * (perhaps recursively) create it. */
180                 if (statret == 0 && !S_ISDIR(st.st_mode)) {
181 -                       if (delete_item(fname, st.st_mode, del_opts) < 0)
182 +                       if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), del_opts) < 0)
183                                 return;
184                         statret = -1;
185                 }
186 @@ -996,7 +1012,7 @@ static void recv_generator(char *fname, 
187                         }
188                         /* Not the right symlink (or not a symlink), so
189                          * delete it. */
190 -                       if (delete_item(fname, st.st_mode, del_opts) < 0)
191 +                       if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), del_opts) < 0)
192                                 return;
193                         if (!S_ISLNK(st.st_mode))
194                                 statret = -1;
195 @@ -1060,7 +1076,7 @@ static void recv_generator(char *fname, 
196                  || (st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
197                  || st.st_rdev != file->u.rdev) {
198                         if (statret == 0
199 -                        && delete_item(fname, st.st_mode, del_opts) < 0)
200 +                        && delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), del_opts) < 0)
201                                 return;
202                         if (preserve_hard_links && file->link_u.links
203                             && hard_link_check(file, ndx, fname, -1, &st,
204 @@ -1145,7 +1161,7 @@ static void recv_generator(char *fname, 
205         fnamecmp_type = FNAMECMP_FNAME;
206  
207         if (statret == 0 && !S_ISREG(st.st_mode)) {
208 -               if (delete_item(fname, st.st_mode, del_opts) != 0)
209 +               if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), del_opts) != 0)
210                         return;
211                 statret = -1;
212                 stat_errno = ENOENT;
213 --- old/options.c
214 +++ new/options.c
215 @@ -48,6 +48,7 @@ int copy_links = 0;
216  int preserve_links = 0;
217  int preserve_hard_links = 0;
218  int preserve_perms = 0;
219 +int preserve_flags = 0;
220  int preserve_executability = 0;
221  int preserve_devices = 0;
222  int preserve_specials = 0;
223 @@ -200,6 +201,7 @@ static void print_rsync_version(enum log
224         char const *hardlinks = "no ";
225         char const *links = "no ";
226         char const *ipv6 = "no ";
227 +       char const *flags = "no ";
228         STRUCT_STAT *dumstat;
229  
230  #ifdef HAVE_SOCKETPAIR
231 @@ -222,6 +224,10 @@ static void print_rsync_version(enum log
232         ipv6 = "";
233  #endif
234  
235 +#ifdef SUPPORT_FLAGS
236 +       flags = "";
237 +#endif
238 +
239         rprintf(f, "%s  version %s  protocol version %d\n",
240                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
241         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
242 @@ -234,9 +240,9 @@ static void print_rsync_version(enum log
243         /* Note that this field may not have type ino_t.  It depends
244          * on the complicated interaction between largefile feature
245          * macros. */
246 -       rprintf(f, "              %sinplace, %sIPv6, "
247 +       rprintf(f, "              %sinplace, %sIPv6, %sfile flags, "
248                 "%d-bit system inums, %d-bit internal inums\n",
249 -               have_inplace, ipv6,
250 +               have_inplace, ipv6, flags,
251                 (int) (sizeof dumstat->st_ino * 8),
252                 (int) (sizeof (int64) * 8));
253  #ifdef MAINTAINER_MODE
254 @@ -302,6 +308,7 @@ void usage(enum logcode F)
255    rprintf(F," -K, --keep-dirlinks         treat symlinked dir on receiver as dir\n");
256    rprintf(F," -H, --hard-links            preserve hard links\n");
257    rprintf(F," -p, --perms                 preserve permissions\n");
258 +  rprintf(F,"     --flags                 preserve file flags\n");
259    rprintf(F," -E, --executability         preserve the file's executability\n");
260    rprintf(F,"     --chmod=CHMOD           affect file and/or directory permissions\n");
261    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
262 @@ -420,6 +427,8 @@ static struct poptOption long_options[] 
263    {"perms",           'p', POPT_ARG_VAL,    &preserve_perms, 1, 0, 0 },
264    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
265    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
266 +  {"flags",            0,  POPT_ARG_VAL,    &preserve_flags, 1, 0, 0 },
267 +  {"no-flags",         0,  POPT_ARG_VAL,    &preserve_flags, 0, 0, 0 },
268    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
269    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
270    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
271 @@ -1124,6 +1133,15 @@ int parse_arguments(int *argc, const cha
272         }
273  #endif
274  
275 +#ifndef SUPPORT_FLAGS
276 +       if (preserve_flags) {
277 +               snprintf(err_buf, sizeof err_buf,
278 +                        "file flags are not supported on this %s\n",
279 +                        am_server ? "server" : "client");
280 +               return 0;
281 +       }
282 +#endif
283 +
284         if (write_batch && read_batch) {
285                 snprintf(err_buf, sizeof err_buf,
286                         "--write-batch and --read-batch can not be used together\n");
287 @@ -1577,6 +1595,9 @@ void server_options(char **args,int *arg
288         if (xfer_dirs && !recurse && delete_mode && am_sender)
289                 args[ac++] = "--no-r";
290  
291 +       if (preserve_flags)
292 +               args[ac++] = "--flags";
293 +
294         if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
295                 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
296                         goto oom;
297 --- old/rsync.c
298 +++ new/rsync.c
299 @@ -30,9 +30,12 @@
300  #include <langinfo.h>
301  #endif
302  
303 +#define NOCHANGE_FLAGS (UF_IMMUTABLE|UF_APPEND|UF_NOUNLINK|SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK)
304 +
305  extern int verbose;
306  extern int dry_run;
307  extern int preserve_perms;
308 +extern int preserve_flags;
309  extern int preserve_executability;
310  extern int preserve_times;
311  extern int omit_dir_times;
312 @@ -123,6 +126,41 @@ mode_t dest_mode(mode_t flist_mode, mode
313         return new_mode;
314  }
315  
316 +#ifdef SUPPORT_FLAGS
317 +/* Set a file's st_flags. */
318 +static int set_fileflags(const char *fname, uint32 fileflags)
319 +{
320 +       if (do_chflags(fname, fileflags) != 0) {
321 +               rsyserr(FERROR, errno,
322 +                       "failed to set file flags on %s",
323 +                       full_fname(fname));
324 +               return 0;
325 +       }
326 +
327 +       return 1;
328 +}
329 +
330 +/* Remove immutable flags from an object, so it can be altered/removed. */
331 +void make_mutable(char *fname, mode_t mode, uint32 fileflags)
332 +{
333 +       if (!preserve_flags && S_ISLNK(mode))
334 +               return;
335 +
336 +       if (fileflags & NOCHANGE_FLAGS)
337 +               set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
338 +}
339 +
340 +/* Undo a prior make_mutable() call. */
341 +void undo_make_mutable(char *fname, mode_t mode, uint32 fileflags)
342 +{
343 +       if (!preserve_flags && S_ISLNK(mode))
344 +               return;
345 +
346 +       if (fileflags & NOCHANGE_FLAGS)
347 +               set_fileflags(fname, fileflags);
348 +}
349 +#endif
350 +
351  int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
352                    int flags)
353  {
354 @@ -221,6 +259,15 @@ int set_file_attrs(char *fname, struct f
355         }
356  #endif
357  
358 +#ifdef SUPPORT_FLAGS
359 +       if (preserve_flags && !S_ISLNK(st->st_mode)
360 +        && st->st_flags != file->fileflags) {
361 +               if (!set_fileflags(fname, file->fileflags))
362 +                       return 0;
363 +               updated = 1;
364 +       }
365 +#endif
366 +
367         if (verbose > 1 && flags & ATTRS_REPORT) {
368                 if (updated)
369                         rprintf(FCLIENT, "%s\n", fname);
370 @@ -268,6 +315,9 @@ void finish_transfer(char *fname, char *
371         set_file_attrs(fnametmp, file, NULL,
372                        ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
373  
374 +#ifdef SUPPORT_FLAGS
375 +       make_mutable(fname, file->mode, file->fileflags);
376 +#endif
377         /* move tmp file over real file */
378         if (verbose > 2)
379                 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
380 @@ -282,6 +332,9 @@ void finish_transfer(char *fname, char *
381         }
382         if (ret == 0) {
383                 /* The file was moved into place (not copied), so it's done. */
384 +#ifdef SUPPORT_FLAGS
385 +               undo_make_mutable(fname, file->mode, file->fileflags);
386 +#endif
387                 return;
388         }
389         /* The file was copied, so tweak the perms of the copied file.  If it
390 --- old/rsync.h
391 +++ new/rsync.h
392 @@ -54,6 +54,7 @@
393  #define XMIT_HAS_IDEV_DATA (1<<9)
394  #define XMIT_SAME_DEV (1<<10)
395  #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
396 +#define XMIT_SAME_FLAGS (1<<12)
397  
398  /* These flags are used in the live flist data. */
399  
400 @@ -344,6 +345,10 @@ enum msgcode {
401  #define schar char
402  #endif
403  
404 +#ifdef HAVE_CHFLAGS
405 +#define SUPPORT_FLAGS 1
406 +#endif
407 +
408  /* Find a variable that is either exactly 32-bits or longer.
409   * If some code depends on 32-bit truncation, it will need to
410   * take special action in a "#if SIZEOF_INT32 > 4" section. */
411 @@ -527,6 +532,9 @@ struct file_struct {
412                 struct hlink *links;
413         } link_u;
414         time_t modtime;
415 +#ifdef SUPPORT_FLAGS
416 +       uint32 fileflags;
417 +#endif
418         uid_t uid;
419         gid_t gid;
420         mode_t mode;
421 --- old/rsync.yo
422 +++ new/rsync.yo
423 @@ -320,6 +320,7 @@ to the detailed description below for a 
424   -K, --keep-dirlinks         treat symlinked dir on receiver as dir
425   -H, --hard-links            preserve hard links
426   -p, --perms                 preserve permissions
427 +     --flags                 preserve file flags
428   -E, --executability         preserve executability
429       --chmod=CHMOD           affect file and/or directory permissions
430   -o, --owner                 preserve owner (super-user only)
431 @@ -501,7 +502,9 @@ specified, in which case bf(-r) is not i
432  
433  Note that bf(-a) bf(does not preserve hardlinks), because
434  finding multiply-linked files is expensive.  You must separately
435 -specify bf(-H).
436 +specify bf(-H).  Note also that for compatibility, bf(-a)
437 +currently bf(does not include --flags) (see there) to include preserving
438 +change file flags (if supported by the OS).
439  
440  dit(--no-OPTION) You may turn off one or more implied options by prefixing
441  the option name with "no-".  Not all options may be prefixed with a "no-":
442 @@ -796,6 +799,13 @@ quote(itemization(
443  
444  If bf(--perms) is enabled, this option is ignored.
445  
446 +dit(bf(--flags)) This option causes rsync to update the change file flags
447 +to be the same as the source file, if your OS supports the bf(chflags)(2)
448 +system call.  In any case, an attempt is made to remove flags that would
449 +prevent a file to be altered.  Some flags can only be altered by the
450 +super-user and can only be unset below a certain secure-level (usually
451 +single-user mode).
452 +
453  dit(bf(--chmod)) This option tells rsync to apply one or more
454  comma-separated "chmod" strings to the permission of the files in the
455  transfer.  The resulting value is treated as though it was the permissions
456 --- old/syscall.c
457 +++ new/syscall.c
458 @@ -152,6 +152,15 @@ int do_chmod(const char *path, mode_t mo
459  }
460  #endif
461  
462 +#ifdef SUPPORT_FLAGS
463 +int do_chflags(const char *path, u_long flags)
464 +{
465 +       if (dry_run) return 0;
466 +       RETURN_ERROR_IF_RO_OR_LO;
467 +       return chflags(path, flags);
468 +}
469 +#endif
470 +
471  int do_rename(const char *fname1, const char *fname2)
472  {
473         if (dry_run) return 0;