bc7111f9a952f7db0865a4e6c9d0cf9d5cbe77c5
[rsync-patches.git] / fileflags.diff
1 This patch provides --fileflags, which preserves the st_flags stat() field.
2 Modified from a patch that was written by Rolf Grossmann.
3
4 To use this patch, run these commands for a successful build:
5
6     patch -p1 <patches/fileflags.diff
7     ./configure                         (optional if already run)
8     make
9
10 based-on: 9a06b2edb0ea1a226bcc642682c07bacd2ea47d3
11 diff --git a/compat.c b/compat.c
12 --- a/compat.c
13 +++ b/compat.c
14 @@ -40,6 +40,7 @@ extern int checksum_seed;
15  extern int basis_dir_cnt;
16  extern int prune_empty_dirs;
17  extern int protocol_version;
18 +extern int force_change;
19  extern int protect_args;
20  extern int preserve_uid;
21  extern int preserve_gid;
22 @@ -47,6 +48,7 @@ extern int preserve_atimes;
23  extern int preserve_crtimes;
24  extern int preserve_acls;
25  extern int preserve_xattrs;
26 +extern int preserve_fileflags;
27  extern int xfer_flags_as_varint;
28  extern int need_messages_from_generator;
29  extern int delete_mode, delete_before, delete_during, delete_after;
30 @@ -86,7 +88,7 @@ struct name_num_item *xattr_sum_nni;
31  int xattr_sum_len = 0;
32  
33  /* These index values are for the file-list's extra-attribute array. */
34 -int pathname_ndx, depth_ndx, atimes_ndx, crtimes_ndx, uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
35 +int pathname_ndx, depth_ndx, atimes_ndx, crtimes_ndx, uid_ndx, gid_ndx, fileflags_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
36  
37  int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
38  int sender_symlink_iconv = 0;  /* sender should convert symlink content */
39 @@ -588,6 +590,8 @@ void setup_protocol(int f_out,int f_in)
40                 uid_ndx = ++file_extra_cnt;
41         if (preserve_gid)
42                 gid_ndx = ++file_extra_cnt;
43 +       if (preserve_fileflags || (force_change && !am_sender))
44 +               fileflags_ndx = ++file_extra_cnt;
45         if (preserve_acls && !am_sender)
46                 acls_ndx = ++file_extra_cnt;
47         if (preserve_xattrs)
48 @@ -751,6 +755,10 @@ void setup_protocol(int f_out,int f_in)
49                         fprintf(stderr, "Both rsync versions must be at least 3.2.0 for --crtimes.\n");
50                         exit_cleanup(RERR_PROTOCOL);
51                 }
52 +               if (!xfer_flags_as_varint && preserve_fileflags) {
53 +                       fprintf(stderr, "Both rsync versions must be at least 3.2.0 for --fileflags.\n");
54 +                       exit_cleanup(RERR_PROTOCOL);
55 +               }
56                 if (am_sender) {
57                         receiver_symlink_times = am_server
58                             ? strchr(client_info, 'L') != NULL
59 diff --git a/delete.c b/delete.c
60 --- a/delete.c
61 +++ b/delete.c
62 @@ -25,6 +25,7 @@
63  extern int am_root;
64  extern int make_backups;
65  extern int max_delete;
66 +extern int force_change;
67  extern char *backup_dir;
68  extern char *backup_suffix;
69  extern int backup_suffix_len;
70 @@ -97,8 +98,12 @@ static enum delret delete_dir_contents(char *fname, uint16 flags)
71                 }
72  
73                 strlcpy(p, fp->basename, remainder);
74 +#ifdef SUPPORT_FORCE_CHANGE
75 +               if (force_change)
76 +                       make_mutable(fname, fp->mode, F_FFLAGS(fp), force_change);
77 +#endif
78                 if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
79 -                       do_chmod(fname, fp->mode | S_IWUSR);
80 +                       do_chmod(fname, fp->mode | S_IWUSR, NO_FFLAGS);
81                 /* Save stack by recursing to ourself directly. */
82                 if (S_ISDIR(fp->mode)) {
83                         if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
84 @@ -139,11 +144,18 @@ enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
85         }
86  
87         if (flags & DEL_NO_UID_WRITE)
88 -               do_chmod(fbuf, mode | S_IWUSR);
89 +               do_chmod(fbuf, mode | S_IWUSR, NO_FFLAGS);
90  
91         if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
92                 /* This only happens on the first call to delete_item() since
93                  * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
94 +#ifdef SUPPORT_FORCE_CHANGE
95 +               if (force_change) {
96 +                       STRUCT_STAT st;
97 +                       if (x_lstat(fbuf, &st, NULL) == 0)
98 +                               make_mutable(fbuf, st.st_mode, st.st_flags, force_change);
99 +               }
100 +#endif
101                 ignore_perishable = 1;
102                 /* If DEL_RECURSE is not set, this just reports emptiness. */
103                 ret = delete_dir_contents(fbuf, flags);
104 diff --git a/flist.c b/flist.c
105 --- a/flist.c
106 +++ b/flist.c
107 @@ -52,6 +52,7 @@ extern int preserve_links;
108  extern int preserve_hard_links;
109  extern int preserve_devices;
110  extern int preserve_specials;
111 +extern int preserve_fileflags;
112  extern int delete_during;
113  extern int missing_args;
114  extern int eol_nulls;
115 @@ -388,6 +389,9 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
116         static time_t crtime;
117  #endif
118         static mode_t mode;
119 +#ifdef SUPPORT_FILEFLAGS
120 +       static uint32 fileflags;
121 +#endif
122  #ifdef SUPPORT_HARD_LINKS
123         static int64 dev;
124  #endif
125 @@ -431,6 +435,14 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
126                 xflags |= XMIT_SAME_MODE;
127         else
128                 mode = file->mode;
129 +#ifdef SUPPORT_FILEFLAGS
130 +       if (preserve_fileflags) {
131 +               if (F_FFLAGS(file) == fileflags)
132 +                       xflags |= XMIT_SAME_FLAGS;
133 +               else
134 +                       fileflags = F_FFLAGS(file);
135 +       }
136 +#endif
137  
138         if (preserve_devices && IS_DEVICE(mode)) {
139                 if (protocol_version < 28) {
140 @@ -592,6 +604,10 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
141  #endif
142         if (!(xflags & XMIT_SAME_MODE))
143                 write_int(f, to_wire_mode(mode));
144 +#ifdef SUPPORT_FILEFLAGS
145 +       if (preserve_fileflags && !(xflags & XMIT_SAME_FLAGS))
146 +               write_int(f, (int)fileflags);
147 +#endif
148         if (atimes_ndx && !S_ISDIR(mode) && !(xflags & XMIT_SAME_ATIME))
149                 write_varlong(f, atime, 4);
150         if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
151 @@ -686,6 +702,9 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
152         static time_t crtime;
153  #endif
154         static mode_t mode;
155 +#ifdef SUPPORT_FILEFLAGS
156 +       static uint32 fileflags;
157 +#endif
158  #ifdef SUPPORT_HARD_LINKS
159         static int64 dev;
160  #endif
161 @@ -803,6 +822,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
162  #ifdef SUPPORT_CRTIMES
163                         if (crtimes_ndx)
164                                 crtime = F_CRTIME(first);
165 +#endif
166 +#ifdef SUPPORT_FILEFLAGS
167 +                       if (preserve_fileflags)
168 +                               fileflags = F_FFLAGS(first);
169  #endif
170                         if (preserve_uid)
171                                 uid = F_OWNER(first);
172 @@ -876,6 +899,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
173  
174         if (chmod_modes && !S_ISLNK(mode) && mode)
175                 mode = tweak_mode(mode, chmod_modes);
176 +#ifdef SUPPORT_FILEFLAGS
177 +       if (preserve_fileflags && !(xflags & XMIT_SAME_FLAGS))
178 +               fileflags = (uint32)read_int(f);
179 +#endif
180  
181         if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
182                 if (protocol_version < 30)
183 @@ -1057,6 +1084,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
184         }
185  #endif
186         file->mode = mode;
187 +#ifdef SUPPORT_FILEFLAGS
188 +       if (preserve_fileflags)
189 +               F_FFLAGS(file) = fileflags;
190 +#endif
191         if (preserve_uid)
192                 F_OWNER(file) = uid;
193         if (preserve_gid) {
194 @@ -1470,6 +1501,10 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
195         }
196  #endif
197         file->mode = st.st_mode;
198 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
199 +       if (fileflags_ndx)
200 +               F_FFLAGS(file) = st.st_flags;
201 +#endif
202         if (preserve_uid)
203                 F_OWNER(file) = st.st_uid;
204         if (preserve_gid)
205 diff --git a/generator.c b/generator.c
206 --- a/generator.c
207 +++ b/generator.c
208 @@ -43,10 +43,12 @@ extern int preserve_devices;
209  extern int preserve_specials;
210  extern int preserve_hard_links;
211  extern int preserve_executability;
212 +extern int preserve_fileflags;
213  extern int preserve_perms;
214  extern int preserve_mtimes;
215  extern int omit_dir_times;
216  extern int omit_link_times;
217 +extern int force_change;
218  extern int delete_mode;
219  extern int delete_before;
220  extern int delete_during;
221 @@ -486,6 +488,10 @@ int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
222                         return 0;
223                 if (perms_differ(file, sxp))
224                         return 0;
225 +#ifdef SUPPORT_FILEFLAGS
226 +               if (preserve_fileflags && sxp->st.st_flags != F_FFLAGS(file))
227 +                       return 0;
228 +#endif
229                 if (ownership_differs(file, sxp))
230                         return 0;
231  #ifdef SUPPORT_ACLS
232 @@ -547,6 +553,11 @@ void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statre
233                         iflags |= ITEM_REPORT_OWNER;
234                 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != (gid_t)F_GROUP(file))
235                         iflags |= ITEM_REPORT_GROUP;
236 +#ifdef SUPPORT_FILEFLAGS
237 +               if (preserve_fileflags && !S_ISLNK(file->mode)
238 +                && sxp->st.st_flags != F_FFLAGS(file))
239 +                       iflags |= ITEM_REPORT_FFLAGS;
240 +#endif
241  #ifdef SUPPORT_ACLS
242                 if (preserve_acls && !S_ISLNK(file->mode)) {
243                         if (!ACL_READY(*sxp))
244 @@ -1454,6 +1465,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
245                 if (!preserve_perms) { /* See comment in non-dir code below. */
246                         file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms, statret == 0);
247                 }
248 +#ifdef SUPPORT_FORCE_CHANGE
249 +               if (force_change && !preserve_fileflags)
250 +                       F_FFLAGS(file) = sx.st.st_flags;
251 +#endif
252                 if (statret != 0 && basis_dir[0] != NULL) {
253                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx, itemizing, code);
254                         if (j == -2) {
255 @@ -1496,10 +1511,15 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
256                  * readable and writable permissions during the time we are
257                  * putting files within them.  This is then restored to the
258                  * former permissions after the transfer is done. */
259 +#ifdef SUPPORT_FORCE_CHANGE
260 +               if (force_change && F_FFLAGS(file) & force_change
261 +                && make_mutable(fname, file->mode, F_FFLAGS(file), force_change))
262 +                       need_retouch_dir_perms = 1;
263 +#endif
264  #ifdef HAVE_CHMOD
265                 if (!am_root && (file->mode & S_IRWXU) != S_IRWXU && dir_tweaking) {
266                         mode_t mode = file->mode | S_IRWXU;
267 -                       if (do_chmod(fname, mode) < 0) {
268 +                       if (do_chmod(fname, mode, 0) < 0) {
269                                 rsyserr(FERROR_XFER, errno,
270                                         "failed to modify permissions on %s",
271                                         full_fname(fname));
272 @@ -1534,6 +1554,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
273                 int exists = statret == 0 && stype != FT_DIR;
274                 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms, exists);
275         }
276 +#ifdef SUPPORT_FORCE_CHANGE
277 +       if (force_change && !preserve_fileflags)
278 +               F_FFLAGS(file) = sx.st.st_flags;
279 +#endif
280  
281  #ifdef SUPPORT_HARD_LINKS
282         if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
283 @@ -2107,17 +2131,25 @@ static void touch_up_dirs(struct file_list *flist, int ndx)
284                         continue;
285                 fname = f_name(file, NULL);
286                 if (fix_dir_perms)
287 -                       do_chmod(fname, file->mode);
288 +                       do_chmod(fname, file->mode, 0);
289                 if (need_retouch_dir_times) {
290                         STRUCT_STAT st;
291                         if (link_stat(fname, &st, 0) == 0 && mtime_differs(&st, file)) {
292                                 st.st_mtime = file->modtime;
293  #ifdef ST_MTIME_NSEC
294                                 st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file);
295 +#endif
296 +#ifdef SUPPORT_FORCE_CHANGE
297 +                               st.st_mode = file->mode;
298 +                               st.st_flags = 0;
299  #endif
300                                 set_times(fname, &st);
301                         }
302                 }
303 +#ifdef SUPPORT_FORCE_CHANGE
304 +               if (force_change && F_FFLAGS(file) & force_change)
305 +                       undo_make_mutable(fname, F_FFLAGS(file));
306 +#endif
307                 if (counter >= loopchk_limit) {
308                         if (allowed_lull)
309                                 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
310 diff --git a/log.c b/log.c
311 --- a/log.c
312 +++ b/log.c
313 @@ -725,7 +725,8 @@ static void log_formatted(enum logcode code, const char *format, const char *op,
314                              : iflags & ITEM_REPORT_ATIME ? 'u' : 'n';
315                         c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
316                         c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
317 -                       c[11] = '\0';
318 +                       c[11] = !(iflags & ITEM_REPORT_FFLAGS) ? '.' : 'f';
319 +                       c[12] = '\0';
320  
321                         if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
322                                 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
323 diff --git a/main.c b/main.c
324 --- a/main.c
325 +++ b/main.c
326 @@ -31,6 +31,9 @@
327  #ifdef __TANDEM
328  #include <floss.h(floss_execlp)>
329  #endif
330 +#ifdef SUPPORT_FORCE_CHANGE
331 +#include <sys/sysctl.h>
332 +#endif
333  
334  extern int dry_run;
335  extern int list_only;
336 @@ -49,6 +52,7 @@ extern int need_messages_from_generator;
337  extern int kluge_around_eof;
338  extern int got_xfer_error;
339  extern int old_style_args;
340 +extern int force_change;
341  extern int msgs2stderr;
342  extern int module_id;
343  extern int read_only;
344 @@ -977,6 +981,22 @@ static int do_recv(int f_in, int f_out, char *local_name)
345          * points to an identical file won't be replaced by the referent. */
346         copy_links = copy_dirlinks = copy_unsafe_links = 0;
347  
348 +#ifdef SUPPORT_FORCE_CHANGE
349 +       if (force_change & SYS_IMMUTABLE) {
350 +               /* Determine whether we'll be able to unlock a system immutable item. */
351 +               int mib[2];
352 +               int securityLevel = 0;
353 +               size_t len = sizeof securityLevel;
354 +
355 +               mib[0] = CTL_KERN;
356 +               mib[1] = KERN_SECURELVL;
357 +               if (sysctl(mib, 2, &securityLevel, &len, NULL, 0) == 0 && securityLevel > 0) {
358 +                       rprintf(FERROR, "System security level is too high to force mutability on system immutable files and directories.\n");
359 +                       exit_cleanup(RERR_UNSUPPORTED);
360 +               }
361 +       }
362 +#endif
363 +
364  #ifdef SUPPORT_HARD_LINKS
365         if (preserve_hard_links && !inc_recurse)
366                 match_hard_links(first_flist);
367 diff --git a/options.c b/options.c
368 --- a/options.c
369 +++ b/options.c
370 @@ -56,6 +56,7 @@ int preserve_hard_links = 0;
371  int preserve_acls = 0;
372  int preserve_xattrs = 0;
373  int preserve_perms = 0;
374 +int preserve_fileflags = 0;
375  int preserve_executability = 0;
376  int preserve_devices = 0;
377  int preserve_specials = 0;
378 @@ -98,6 +99,7 @@ int msgs2stderr = 2; /* Default: send errors to stderr for local & remote-shell
379  int saw_stderr_opt = 0;
380  int allow_8bit_chars = 0;
381  int force_delete = 0;
382 +int force_change = 0;
383  int io_timeout = 0;
384  int prune_empty_dirs = 0;
385  int use_qsort = 0;
386 @@ -623,6 +625,8 @@ static struct poptOption long_options[] = {
387    {"perms",           'p', POPT_ARG_VAL,    &preserve_perms, 1, 0, 0 },
388    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
389    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
390 +  {"fileflags",        0,  POPT_ARG_VAL,    &preserve_fileflags, 1, 0, 0 },
391 +  {"no-fileflags",     0,  POPT_ARG_VAL,    &preserve_fileflags, 0, 0, 0 },
392    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
393    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
394    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
395 @@ -721,6 +725,12 @@ static struct poptOption long_options[] = {
396    {"remove-source-files",0,POPT_ARG_VAL,    &remove_source_files, 1, 0, 0 },
397    {"force",            0,  POPT_ARG_VAL,    &force_delete, 1, 0, 0 },
398    {"no-force",         0,  POPT_ARG_VAL,    &force_delete, 0, 0, 0 },
399 +  {"force-delete",     0,  POPT_ARG_VAL,    &force_delete, 1, 0, 0 },
400 +  {"no-force-delete",  0,  POPT_ARG_VAL,    &force_delete, 0, 0, 0 },
401 +  {"force-change",     0,  POPT_ARG_VAL,    &force_change, ALL_IMMUTABLE, 0, 0 },
402 +  {"no-force-change",  0,  POPT_ARG_VAL,    &force_change, 0, 0, 0 },
403 +  {"force-uchange",    0,  POPT_ARG_VAL,    &force_change, USR_IMMUTABLE, 0, 0 },
404 +  {"force-schange",    0,  POPT_ARG_VAL,    &force_change, SYS_IMMUTABLE, 0, 0 },
405    {"ignore-errors",    0,  POPT_ARG_VAL,    &ignore_errors, 1, 0, 0 },
406    {"no-ignore-errors", 0,  POPT_ARG_VAL,    &ignore_errors, 0, 0, 0 },
407    {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
408 @@ -1016,6 +1026,14 @@ static void set_refuse_options(void)
409  #ifndef SUPPORT_CRTIMES
410         parse_one_refuse_match(0, "crtimes", list_end);
411  #endif
412 +#ifndef SUPPORT_FILEFLAGS
413 +       parse_one_refuse_match(0, "fileflags", list_end);
414 +#endif
415 +#ifndef SUPPORT_FORCE_CHANGE
416 +       parse_one_refuse_match(0, "force-change", list_end);
417 +       parse_one_refuse_match(0, "force-uchange", list_end);
418 +       parse_one_refuse_match(0, "force-schange", list_end);
419 +#endif
420  
421         /* Now we use the descrip values to actually mark the options for refusal. */
422         for (op = long_options; op != list_end; op++) {
423 @@ -2732,6 +2750,9 @@ void server_options(char **args, int *argc_p)
424         if (xfer_dirs && !recurse && delete_mode && am_sender)
425                 args[ac++] = "--no-r";
426  
427 +       if (preserve_fileflags)
428 +               args[ac++] = "--fileflags";
429 +
430         if (do_compression && do_compression_level != CLVL_NOT_SPECIFIED) {
431                 if (asprintf(&arg, "--compress-level=%d", do_compression_level) < 0)
432                         goto oom;
433 @@ -2827,6 +2848,16 @@ void server_options(char **args, int *argc_p)
434                         args[ac++] = "--delete-excluded";
435                 if (force_delete)
436                         args[ac++] = "--force";
437 +#ifdef SUPPORT_FORCE_CHANGE
438 +               if (force_change) {
439 +                       if (force_change == ALL_IMMUTABLE)
440 +                               args[ac++] = "--force-change";
441 +                       else if (force_change == USR_IMMUTABLE)
442 +                               args[ac++] = "--force-uchange";
443 +                       else if (force_change == SYS_IMMUTABLE)
444 +                               args[ac++] = "--force-schange";
445 +               }
446 +#endif
447                 if (write_batch < 0)
448                         args[ac++] = "--only-write-batch=X";
449                 if (am_root > 1)
450 diff --git a/rsync.1.md b/rsync.1.md
451 --- a/rsync.1.md
452 +++ b/rsync.1.md
453 @@ -446,6 +446,7 @@ has its own detailed description later in this manpage.
454  --keep-dirlinks, -K      treat symlinked dir on receiver as dir
455  --hard-links, -H         preserve hard links
456  --perms, -p              preserve permissions
457 +--fileflags              preserve file-flags (aka chflags)
458  --executability, -E      preserve executability
459  --chmod=CHMOD            affect file and/or directory permissions
460  --acls, -A               preserve ACLs (implies --perms)
461 @@ -487,7 +488,10 @@ has its own detailed description later in this manpage.
462  --ignore-missing-args    ignore missing source args without error
463  --delete-missing-args    delete missing source args from destination
464  --ignore-errors          delete even if there are I/O errors
465 ---force                  force deletion of dirs even if not empty
466 +--force-delete           force deletion of directories even if not empty
467 +--force-change           affect user-/system-immutable files/dirs
468 +--force-uchange          affect user-immutable files/dirs
469 +--force-schange          affect system-immutable files/dirs
470  --max-delete=NUM         don't delete more than NUM files
471  --max-size=SIZE          don't transfer any file larger than SIZE
472  --min-size=SIZE          don't transfer any file smaller than SIZE
473 @@ -831,6 +835,7 @@ expand it.
474      recursion and want to preserve almost everything.  Be aware that it does
475      **not** include preserving ACLs (`-A`), xattrs (`-X`), atimes (`-U`),
476      crtimes (`-N`), nor the finding and preserving of hardlinks (`-H`).
477 +    It also does **not** imply [`--fileflags`](#opt).
478  
479      The only exception to the above equivalence is when [`--files-from`](#opt)
480      is specified, in which case [`-r`](#opt) is not implied.
481 @@ -1295,7 +1300,7 @@ expand it.
482      Without this option, if the sending side has replaced a directory with a
483      symlink to a directory, the receiving side will delete anything that is in
484      the way of the new symlink, including a directory hierarchy (as long as
485 -    [`--force`](#opt) or [`--delete`](#opt) is in effect).
486 +    [`--force-delete`](#opt) or [`--delete`](#opt) is in effect).
487  
488      See also [`--keep-dirlinks`](#opt) for an analogous option for the
489      receiving side.
490 @@ -1490,6 +1495,37 @@ expand it.
491      those used by [`--fake-super`](#opt)) unless you repeat the option (e.g. `-XX`).
492      This "copy all xattrs" mode cannot be used with [`--fake-super`](#opt).
493  
494 +0.  `--fileflags`
495 +
496 +    This option causes rsync to update the file-flags to be the same as the
497 +    source files and directories (if your OS supports the **chflags**(2) system
498 +    call).   Some flags can only be altered by the super-user and some might
499 +    only be unset below a certain secure-level (usually single-user mode). It
500 +    will not make files alterable that are set to immutable on the receiver.
501 +    To do that, see [`--force-change`](#opt), [`--force-uchange`](#opt), and
502 +    [`--force-schange`](#opt).
503 +
504 +0.  `--force-change`
505 +
506 +    This option causes rsync to disable both user-immutable and
507 +    system-immutable flags on files and directories that are being updated or
508 +    deleted on the receiving side.  This option overrides
509 +    [`--force-uchange`](#opt) and [`--force-schange`](#opt).
510 +
511 +0.  `--force-uchange`
512 +
513 +    This option causes rsync to disable user-immutable flags on files and
514 +    directories that are being updated or deleted on the receiving side.  It
515 +    does not try to affect system flags.  This option overrides
516 +    [`--force-change`](#opt) and [`--force-schange`](#opt).
517 +
518 +0.  `--force-schange`
519 +
520 +    This option causes rsync to disable system-immutable flags on files and
521 +    directories that are being updated or deleted on the receiving side.  It
522 +    does not try to affect user flags.  This option overrides
523 +    [`--force-change`](#opt) and [`--force-uchange`](#opt).
524 +
525  0.  `--chmod=CHMOD`
526  
527      This option tells rsync to apply one or more comma-separated "chmod" modes
528 @@ -2019,8 +2055,8 @@ expand it.
529      [`--ignore-missing-args`](#opt) option a step farther: each missing arg
530      will become a deletion request of the corresponding destination file on the
531      receiving side (should it exist).  If the destination file is a non-empty
532 -    directory, it will only be successfully deleted if [`--force`](#opt) or
533 -    [`--delete`](#opt) are in effect.  Other than that, this option is
534 +    directory, it will only be successfully deleted if [`--force-delete`](#opt)
535 +    or [`--delete`](#opt) are in effect.  Other than that, this option is
536      independent of any other type of delete processing.
537  
538      The missing source files are represented by special file-list entries which
539 @@ -2031,14 +2067,14 @@ expand it.
540      Tells [`--delete`](#opt) to go ahead and delete files even when there are
541      I/O errors.
542  
543 -0.  `--force`
544 +0.  `--force-delete`, `--force`
545  
546      This option tells rsync to delete a non-empty directory when it is to be
547      replaced by a non-directory.  This is only relevant if deletions are not
548      active (see [`--delete`](#opt) for details).
549  
550 -    Note for older rsync versions: `--force` used to still be required when
551 -    using [`--delete-after`](#opt), and it used to be non-functional unless the
552 +    Note that some older rsync versions used to require `--force` when using
553 +    [`--delete-after`](#opt), and it used to be non-functional unless the
554      [`--recursive`](#opt) option was also enabled.
555  
556  0.  `--max-delete=NUM`
557 @@ -3098,7 +3134,7 @@ expand it.
558      also turns on the output of other verbose messages).
559  
560      The "%i" escape has a cryptic output that is 11 letters long.  The general
561 -    format is like the string `YXcstpoguax`, where **Y** is replaced by the type
562 +    format is like the string `YXcstpoguaxf`, where **Y** is replaced by the type
563      of update being done, **X** is replaced by the file-type, and the other
564      letters represent attributes that may be output if they are being modified.
565  
566 diff --git a/rsync.c b/rsync.c
567 --- a/rsync.c
568 +++ b/rsync.c
569 @@ -31,6 +31,7 @@ extern int dry_run;
570  extern int preserve_acls;
571  extern int preserve_xattrs;
572  extern int preserve_perms;
573 +extern int preserve_fileflags;
574  extern int preserve_executability;
575  extern int preserve_mtimes;
576  extern int omit_dir_times;
577 @@ -468,6 +469,39 @@ mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
578         return new_mode;
579  }
580  
581 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
582 +/* Set a file's st_flags. */
583 +static int set_fileflags(const char *fname, uint32 fileflags)
584 +{
585 +       if (do_chflags(fname, fileflags) != 0) {
586 +               rsyserr(FERROR_XFER, errno,
587 +                       "failed to set file flags on %s",
588 +                       full_fname(fname));
589 +               return 0;
590 +       }
591 +
592 +       return 1;
593 +}
594 +
595 +/* Remove immutable flags from an object, so it can be altered/removed. */
596 +int make_mutable(const char *fname, mode_t mode, uint32 fileflags, uint32 iflags)
597 +{
598 +       if (S_ISLNK(mode) || !(fileflags & iflags))
599 +               return 0;
600 +       if (!set_fileflags(fname, fileflags & ~iflags))
601 +               return -1;
602 +       return 1;
603 +}
604 +
605 +/* Undo a prior make_mutable() call that returned a 1. */
606 +int undo_make_mutable(const char *fname, uint32 fileflags)
607 +{
608 +       if (!set_fileflags(fname, fileflags))
609 +               return -1;
610 +       return 1;
611 +}
612 +#endif
613 +
614  static int same_mtime(struct file_struct *file, STRUCT_STAT *st, int extra_accuracy)
615  {
616  #ifdef ST_MTIME_NSEC
617 @@ -544,7 +578,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
618                 if (am_root >= 0) {
619                         uid_t uid = change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid;
620                         gid_t gid = change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid;
621 -                       if (do_lchown(fname, uid, gid) != 0) {
622 +                       if (do_lchown(fname, uid, gid, sxp->st.st_mode, ST_FLAGS(sxp->st)) != 0) {
623                                 /* We shouldn't have attempted to change uid
624                                  * or gid unless have the privilege. */
625                                 rsyserr(FERROR_XFER, errno, "%s %s failed",
626 @@ -654,7 +688,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
627  
628  #ifdef HAVE_CHMOD
629         if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
630 -               int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
631 +               int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode, ST_FLAGS(sxp->st));
632                 if (ret < 0) {
633                         rsyserr(FERROR_XFER, errno,
634                                 "failed to set permissions on %s",
635 @@ -666,6 +700,19 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
636         }
637  #endif
638  
639 +#ifdef SUPPORT_FILEFLAGS
640 +       if (preserve_fileflags && !S_ISLNK(sxp->st.st_mode)
641 +        && sxp->st.st_flags != F_FFLAGS(file)) {
642 +               uint32 fileflags = F_FFLAGS(file);
643 +               if (flags & ATTRS_DELAY_IMMUTABLE)
644 +                       fileflags &= ~ALL_IMMUTABLE;
645 +               if (sxp->st.st_flags != fileflags
646 +                && !set_fileflags(fname, fileflags))
647 +                       goto cleanup;
648 +               updated = 1;
649 +       }
650 +#endif
651 +
652         if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
653                 if (updated)
654                         rprintf(FCLIENT, "%s\n", fname);
655 @@ -743,7 +790,8 @@ int finish_transfer(const char *fname, const char *fnametmp,
656  
657         /* Change permissions before putting the file into place. */
658         set_file_attrs(fnametmp, file, NULL, fnamecmp,
659 -                      ok_to_set_time ? ATTRS_ACCURATE_TIME : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME | ATTRS_SKIP_CRTIME);
660 +                      ATTRS_DELAY_IMMUTABLE
661 +                      | (ok_to_set_time ? ATTRS_ACCURATE_TIME : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME | ATTRS_SKIP_CRTIME));
662  
663         /* move tmp file over real file */
664         if (DEBUG_GTE(RECV, 1))
665 @@ -760,6 +808,10 @@ int finish_transfer(const char *fname, const char *fnametmp,
666         }
667         if (ret == 0) {
668                 /* The file was moved into place (not copied), so it's done. */
669 +#ifdef SUPPORT_FILEFLAGS
670 +               if (preserve_fileflags && F_FFLAGS(file) & ALL_IMMUTABLE)
671 +                       set_fileflags(fname, F_FFLAGS(file));
672 +#endif
673                 return 1;
674         }
675         /* The file was copied, so tweak the perms of the copied file.  If it
676 diff --git a/rsync.h b/rsync.h
677 --- a/rsync.h
678 +++ b/rsync.h
679 @@ -69,7 +69,7 @@
680  
681  /* The following XMIT flags require an rsync that uses a varint for the flag values */
682  
683 -#define XMIT_RESERVED_16 (1<<16)       /* reserved for future fileflags use */
684 +#define XMIT_SAME_FLAGS (1<<16)        /* any protocol - restricted by command-line option */
685  #define XMIT_CRTIME_EQ_MTIME (1<<17)   /* any protocol - restricted by command-line option */
686  
687  /* These flags are used in the live flist data. */
688 @@ -192,6 +192,7 @@
689  #define ATTRS_SKIP_MTIME       (1<<1)
690  #define ATTRS_ACCURATE_TIME    (1<<2)
691  #define ATTRS_SKIP_ATIME       (1<<3)
692 +#define ATTRS_DELAY_IMMUTABLE  (1<<4)
693  #define ATTRS_SKIP_CRTIME      (1<<5)
694  
695  #define MSG_FLUSH      2
696 @@ -220,6 +221,7 @@
697  #define ITEM_REPORT_GROUP (1<<6)
698  #define ITEM_REPORT_ACL (1<<7)
699  #define ITEM_REPORT_XATTR (1<<8)
700 +#define ITEM_REPORT_FFLAGS (1<<9)
701  #define ITEM_REPORT_CRTIME (1<<10)
702  #define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
703  #define ITEM_XNAME_FOLLOWS (1<<12)
704 @@ -587,6 +589,31 @@ typedef unsigned int size_t;
705  #define SUPPORT_CRTIMES 1
706  #endif
707  
708 +#define NO_FFLAGS ((uint32)-1)
709 +
710 +#ifdef HAVE_CHFLAGS
711 +#define SUPPORT_FILEFLAGS 1
712 +#define SUPPORT_FORCE_CHANGE 1
713 +#endif
714 +
715 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
716 +#ifndef UF_NOUNLINK
717 +#define UF_NOUNLINK 0
718 +#endif
719 +#ifndef SF_NOUNLINK
720 +#define SF_NOUNLINK 0
721 +#endif
722 +#define USR_IMMUTABLE (UF_IMMUTABLE|UF_NOUNLINK|UF_APPEND)
723 +#define SYS_IMMUTABLE (SF_IMMUTABLE|SF_NOUNLINK|SF_APPEND)
724 +#define ALL_IMMUTABLE (USR_IMMUTABLE|SYS_IMMUTABLE)
725 +#define ST_FLAGS(st) ((st).st_flags)
726 +#else
727 +#define USR_IMMUTABLE 0
728 +#define SYS_IMMUTABLE 0
729 +#define ALL_IMMUTABLE 0
730 +#define ST_FLAGS(st) NO_FFLAGS
731 +#endif
732 +
733  /* Find a variable that is either exactly 32-bits or longer.
734   * If some code depends on 32-bit truncation, it will need to
735   * take special action in a "#if SIZEOF_INT32 > 4" section. */
736 @@ -818,6 +845,7 @@ extern int pathname_ndx;
737  extern int depth_ndx;
738  extern int uid_ndx;
739  extern int gid_ndx;
740 +extern int fileflags_ndx;
741  extern int acls_ndx;
742  extern int xattrs_ndx;
743  extern int file_sum_extra_cnt;
744 @@ -873,6 +901,11 @@ extern int file_sum_extra_cnt;
745  /* When the associated option is on, all entries will have these present: */
746  #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
747  #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
748 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
749 +#define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
750 +#else
751 +#define F_FFLAGS(f) NO_FFLAGS
752 +#endif
753  #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
754  #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
755  #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
756 diff --git a/syscall.c b/syscall.c
757 --- a/syscall.c
758 +++ b/syscall.c
759 @@ -38,6 +38,7 @@ extern int am_root;
760  extern int am_sender;
761  extern int read_only;
762  extern int list_only;
763 +extern int force_change;
764  extern int inplace;
765  extern int preallocate_files;
766  extern int preserve_perms;
767 @@ -81,7 +82,23 @@ int do_unlink(const char *path)
768  {
769         if (dry_run) return 0;
770         RETURN_ERROR_IF_RO_OR_LO;
771 -       return unlink(path);
772 +       if (unlink(path) == 0)
773 +               return 0;
774 +#ifdef SUPPORT_FORCE_CHANGE
775 +       if (force_change && errno == EPERM) {
776 +               STRUCT_STAT st;
777 +
778 +               if (x_lstat(path, &st, NULL) == 0
779 +                && make_mutable(path, st.st_mode, st.st_flags, force_change) > 0) {
780 +                       if (unlink(path) == 0)
781 +                               return 0;
782 +                       undo_make_mutable(path, st.st_flags);
783 +               }
784 +               /* TODO: handle immutable directories */
785 +               errno = EPERM;
786 +       }
787 +#endif
788 +       return -1;
789  }
790  
791  #ifdef SUPPORT_LINKS
792 @@ -146,14 +163,35 @@ int do_link(const char *old_path, const char *new_path)
793  }
794  #endif
795  
796 -int do_lchown(const char *path, uid_t owner, gid_t group)
797 +int do_lchown(const char *path, uid_t owner, gid_t group, UNUSED(mode_t mode), UNUSED(uint32 fileflags))
798  {
799         if (dry_run) return 0;
800         RETURN_ERROR_IF_RO_OR_LO;
801  #ifndef HAVE_LCHOWN
802  #define lchown chown
803  #endif
804 -       return lchown(path, owner, group);
805 +       if (lchown(path, owner, group) == 0)
806 +               return 0;
807 +#ifdef SUPPORT_FORCE_CHANGE
808 +       if (force_change && errno == EPERM) {
809 +               if (fileflags == NO_FFLAGS) {
810 +                       STRUCT_STAT st;
811 +                       if (x_lstat(path, &st, NULL) == 0) {
812 +                               mode = st.st_mode;
813 +                               fileflags = st.st_flags;
814 +                       }
815 +               }
816 +               if (fileflags != NO_FFLAGS
817 +                && make_mutable(path, mode, fileflags, force_change) > 0) {
818 +                       int ret = lchown(path, owner, group);
819 +                       undo_make_mutable(path, fileflags);
820 +                       if (ret == 0)
821 +                               return 0;
822 +               }
823 +               errno = EPERM;
824 +       }
825 +#endif
826 +       return -1;
827  }
828  
829  int do_mknod(const char *pathname, mode_t mode, dev_t dev)
830 @@ -193,7 +231,7 @@ int do_mknod(const char *pathname, mode_t mode, dev_t dev)
831                         return -1;
832                 close(sock);
833  #ifdef HAVE_CHMOD
834 -               return do_chmod(pathname, mode);
835 +               return do_chmod(pathname, mode, 0);
836  #else
837                 return 0;
838  #endif
839 @@ -210,7 +248,22 @@ int do_rmdir(const char *pathname)
840  {
841         if (dry_run) return 0;
842         RETURN_ERROR_IF_RO_OR_LO;
843 -       return rmdir(pathname);
844 +       if (rmdir(pathname) == 0)
845 +               return 0;
846 +#ifdef SUPPORT_FORCE_CHANGE
847 +       if (force_change && errno == EPERM) {
848 +               STRUCT_STAT st;
849 +
850 +               if (x_lstat(pathname, &st, NULL) == 0
851 +                && make_mutable(pathname, st.st_mode, st.st_flags, force_change) > 0) {
852 +                       if (rmdir(pathname) == 0)
853 +                               return 0;
854 +                       undo_make_mutable(pathname, st.st_flags);
855 +               }
856 +               errno = EPERM;
857 +       }
858 +#endif
859 +       return -1;
860  }
861  
862  int do_open(const char *pathname, int flags, mode_t mode)
863 @@ -229,7 +282,7 @@ int do_open(const char *pathname, int flags, mode_t mode)
864  }
865  
866  #ifdef HAVE_CHMOD
867 -int do_chmod(const char *path, mode_t mode)
868 +int do_chmod(const char *path, mode_t mode, UNUSED(uint32 fileflags))
869  {
870         static int switch_step = 0;
871         int code;
872 @@ -268,17 +321,72 @@ int do_chmod(const char *path, mode_t mode)
873                         code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
874                 break;
875         }
876 +#ifdef SUPPORT_FORCE_CHANGE
877 +       if (code < 0 && force_change && errno == EPERM && !S_ISLNK(mode)) {
878 +               if (fileflags == NO_FFLAGS) {
879 +                       STRUCT_STAT st;
880 +                       if (x_lstat(path, &st, NULL) == 0)
881 +                               fileflags = st.st_flags;
882 +               }
883 +               if (fileflags != NO_FFLAGS
884 +                && make_mutable(path, mode, fileflags, force_change) > 0) {
885 +                       code = chmod(path, mode & CHMOD_BITS);
886 +                       undo_make_mutable(path, fileflags);
887 +                       if (code == 0)
888 +                               return 0;
889 +               }
890 +               errno = EPERM;
891 +       }
892 +#endif
893         if (code != 0 && (preserve_perms || preserve_executability))
894                 return code;
895         return 0;
896  }
897  #endif
898  
899 +#ifdef HAVE_CHFLAGS
900 +int do_chflags(const char *path, uint32 fileflags)
901 +{
902 +       if (dry_run) return 0;
903 +       RETURN_ERROR_IF_RO_OR_LO;
904 +       return chflags(path, fileflags);
905 +}
906 +#endif
907 +
908  int do_rename(const char *old_path, const char *new_path)
909  {
910         if (dry_run) return 0;
911         RETURN_ERROR_IF_RO_OR_LO;
912 -       return rename(old_path, new_path);
913 +       if (rename(old_path, new_path) == 0)
914 +               return 0;
915 +#ifdef SUPPORT_FORCE_CHANGE
916 +       if (force_change && errno == EPERM) {
917 +               STRUCT_STAT st1, st2;
918 +               int became_mutable;
919 +
920 +               if (x_lstat(old_path, &st1, NULL) != 0)
921 +                       goto failed;
922 +               became_mutable = make_mutable(old_path, st1.st_mode, st1.st_flags, force_change) > 0;
923 +               if (became_mutable && rename(old_path, new_path) == 0)
924 +                       goto success;
925 +               if (x_lstat(new_path, &st2, NULL) == 0
926 +                && make_mutable(new_path, st2.st_mode, st2.st_flags, force_change) > 0) {
927 +                       if (rename(old_path, new_path) == 0) {
928 +                         success:
929 +                               if (became_mutable) /* Yes, use new_path and st1! */
930 +                                       undo_make_mutable(new_path, st1.st_flags);
931 +                               return 0;
932 +                       }
933 +                       undo_make_mutable(new_path, st2.st_flags);
934 +               }
935 +               /* TODO: handle immutable directories */
936 +               if (became_mutable)
937 +                       undo_make_mutable(old_path, st1.st_flags);
938 +         failed:
939 +               errno = EPERM;
940 +       }
941 +#endif
942 +       return -1;
943  }
944  
945  #ifdef HAVE_FTRUNCATE
946 diff --git a/t_stub.c b/t_stub.c
947 --- a/t_stub.c
948 +++ b/t_stub.c
949 @@ -29,6 +29,8 @@ int protect_args = 0;
950  int module_id = -1;
951  int relative_paths = 0;
952  int module_dirlen = 0;
953 +int force_change = 0;
954 +int preserve_acls = 0;
955  int preserve_xattrs = 0;
956  int preserve_perms = 0;
957  int preserve_executability = 0;
958 @@ -111,3 +113,23 @@ filter_rule_list daemon_filter_list;
959  {
960         return cst ? 0 : 0;
961  }
962 +
963 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
964 + int make_mutable(UNUSED(const char *fname), UNUSED(mode_t mode), UNUSED(uint32 fileflags), UNUSED(uint32 iflags))
965 +{
966 +       return 0;
967 +}
968 +
969 +/* Undo a prior make_mutable() call that returned a 1. */
970 + int undo_make_mutable(UNUSED(const char *fname), UNUSED(uint32 fileflags))
971 +{
972 +       return 0;
973 +}
974 +#endif
975 +
976 +#ifdef SUPPORT_XATTRS
977 + int x_lstat(UNUSED(const char *fname), UNUSED(STRUCT_STAT *fst), UNUSED(STRUCT_STAT *xst))
978 +{
979 +       return -1;
980 +}
981 +#endif
982 diff --git a/testsuite/rsync.fns b/testsuite/rsync.fns
983 --- a/testsuite/rsync.fns
984 +++ b/testsuite/rsync.fns
985 @@ -26,9 +26,9 @@ chkfile="$scratchdir/rsync.chk"
986  outfile="$scratchdir/rsync.out"
987  
988  # For itemized output:
989 -all_plus='+++++++++'
990 -allspace='         '
991 -dots='.....' # trailing dots after changes
992 +all_plus='++++++++++'
993 +allspace='          '
994 +dots='......' # trailing dots after changes
995  tab_ch='       ' # a single tab character
996  
997  # Berkley's nice.
998 diff --git a/usage.c b/usage.c
999 --- a/usage.c
1000 +++ b/usage.c
1001 @@ -138,6 +138,11 @@ static void print_info_flags(enum logcode f)
1002  #endif
1003                         "crtimes",
1004  
1005 +#ifndef SUPPORT_FILEFLAGS
1006 +               "no "
1007 +#endif
1008 +                       "file-flags",
1009 +
1010         "*Optimizations",
1011  
1012  #ifndef USE_ROLL_SIMD
1013 diff --git a/util1.c b/util1.c
1014 --- a/util1.c
1015 +++ b/util1.c
1016 @@ -34,6 +34,7 @@ extern int relative_paths;
1017  extern int preserve_xattrs;
1018  extern int omit_link_times;
1019  extern int preallocate_files;
1020 +extern int force_change;
1021  extern char *module_dir;
1022  extern unsigned int module_dirlen;
1023  extern char *partial_dir;
1024 @@ -116,6 +117,33 @@ void print_child_argv(const char *prefix, char **cmd)
1025         rprintf(FCLIENT, " (%d args)\n", cnt);
1026  }
1027  
1028 +#ifdef SUPPORT_FORCE_CHANGE
1029 +static int try_a_force_change(const char *fname, STRUCT_STAT *stp)
1030 +{
1031 +       uint32 fileflags = ST_FLAGS(*stp);
1032 +       if (fileflags == NO_FFLAGS) {
1033 +               STRUCT_STAT st;
1034 +               if (x_lstat(fname, &st, NULL) == 0)
1035 +                       fileflags = st.st_flags;
1036 +       }
1037 +       if (fileflags != NO_FFLAGS && make_mutable(fname, stp->st_mode, fileflags, force_change) > 0) {
1038 +               int ret, save_force_change = force_change;
1039 +
1040 +               force_change = 0; /* Make certain we can't come back here. */
1041 +               ret = set_times(fname, stp);
1042 +               force_change = save_force_change;
1043 +
1044 +               undo_make_mutable(fname, fileflags);
1045 +
1046 +               return ret;
1047 +       }
1048 +
1049 +       errno = EPERM;
1050 +
1051 +       return -1;
1052 +}
1053 +#endif
1054 +
1055  /* This returns 0 for success, 1 for a symlink if symlink time-setting
1056   * is not possible, or -1 for any other error. */
1057  int set_times(const char *fname, STRUCT_STAT *stp)
1058 @@ -143,6 +171,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1059  #include "case_N.h"
1060                 if (do_utimensat(fname, stp) == 0)
1061                         break;
1062 +#ifdef SUPPORT_FORCE_CHANGE
1063 +               if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1064 +                       break;
1065 +#endif
1066                 if (errno != ENOSYS)
1067                         return -1;
1068                 switch_step++;
1069 @@ -152,6 +184,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1070  #include "case_N.h"
1071                 if (do_lutimes(fname, stp) == 0)
1072                         break;
1073 +#ifdef SUPPORT_FORCE_CHANGE
1074 +               if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1075 +                       break;
1076 +#endif
1077                 if (errno != ENOSYS)
1078                         return -1;
1079                 switch_step++;
1080 @@ -173,6 +209,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1081                 if (do_utime(fname, stp) == 0)
1082                         break;
1083  #endif
1084 +#ifdef SUPPORT_FORCE_CHANGE
1085 +               if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1086 +                       break;
1087 +#endif
1088  
1089                 return -1;
1090         }
1091 diff --git a/xattrs.c b/xattrs.c
1092 --- a/xattrs.c
1093 +++ b/xattrs.c
1094 @@ -1086,7 +1086,7 @@ int set_xattr(const char *fname, const struct file_struct *file, const char *fna
1095          && !S_ISLNK(sxp->st.st_mode)
1096  #endif
1097          && access(fname, W_OK) < 0
1098 -        && do_chmod(fname, (sxp->st.st_mode & CHMOD_BITS) | S_IWUSR) == 0)
1099 +        && do_chmod(fname, (sxp->st.st_mode & CHMOD_BITS) | S_IWUSR, ST_FLAGS(sxp->st)) == 0)
1100                 added_write_perm = 1;
1101  
1102         ndx = F_XATTR(file);
1103 @@ -1094,7 +1094,7 @@ int set_xattr(const char *fname, const struct file_struct *file, const char *fna
1104         lst = &glst->xa_items;
1105         int return_value = rsync_xal_set(fname, lst, fnamecmp, sxp);
1106         if (added_write_perm) /* remove the temporary write permission */
1107 -               do_chmod(fname, sxp->st.st_mode);
1108 +               do_chmod(fname, sxp->st.st_mode, ST_FLAGS(sxp->st));
1109         return return_value;
1110  }
1111  
1112 @@ -1211,7 +1211,7 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1113         mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1114              | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1115         if (fst.st_mode != mode)
1116 -               do_chmod(fname, mode);
1117 +               do_chmod(fname, mode, ST_FLAGS(fst));
1118         if (!IS_DEVICE(fst.st_mode))
1119                 fst.st_rdev = 0; /* just in case */
1120