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