Updated for the latest source.
[rsync.git/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     ./prepare-source
8     ./configure
9     make
10
11 based-on: 035a13f7e483efa47847c5d77a8b0dd30ebd8925
12 diff --git a/compat.c b/compat.c
13 --- a/compat.c
14 +++ b/compat.c
15 @@ -45,12 +45,14 @@ extern int checksum_seed;
16  extern int basis_dir_cnt;
17  extern int prune_empty_dirs;
18  extern int protocol_version;
19 +extern int force_change;
20  extern int protect_args;
21  extern int preserve_uid;
22  extern int preserve_gid;
23  extern int preserve_atimes;
24  extern int preserve_acls;
25  extern int preserve_xattrs;
26 +extern int preserve_fileflags;
27  extern int need_messages_from_generator;
28  extern int delete_mode, delete_before, delete_during, delete_after;
29  extern char *shell_cmd;
30 @@ -66,7 +68,7 @@ extern char *iconv_opt;
31  #endif
32  
33  /* These index values are for the file-list's extra-attribute array. */
34 -int pathname_ndx, depth_ndx, atimes_ndx, uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
35 +int pathname_ndx, depth_ndx, atimes_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 @@ -152,6 +154,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 diff --git a/configure.ac b/configure.ac
49 --- a/configure.ac
50 +++ b/configure.ac
51 @@ -598,6 +598,7 @@ AC_FUNC_UTIME_NULL
52  AC_FUNC_ALLOCA
53  AC_CHECK_FUNCS(waitpid wait4 getcwd strdup chown chmod lchmod mknod mkfifo \
54      fchmod fstat ftruncate strchr readlink link utime utimes lutimes strftime \
55 +    chflags \
56      memmove lchown vsnprintf snprintf vasprintf asprintf setsid strpbrk \
57      strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
58      setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
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 @@ -382,6 +383,9 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
116  {
117         static time_t modtime, atime;
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 @@ -425,6 +429,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 @@ -572,6 +584,10 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
141                 write_varint(f, F_MOD_NSEC(file));
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 @@ -663,6 +679,9 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
152  {
153         static int64 modtime, atime;
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 @@ -770,6 +789,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
162                         modtime = first->modtime;
163                         modtime_nsec = F_MOD_NSEC_or_0(first);
164                         mode = first->mode;
165 +#ifdef SUPPORT_FILEFLAGS
166 +                       if (preserve_fileflags)
167 +                               fileflags = F_FFLAGS(first);
168 +#endif
169                         if (preserve_uid)
170                                 uid = F_OWNER(first);
171                         if (preserve_gid)
172 @@ -821,6 +844,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 @@ -979,6 +1006,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 @@ -1378,6 +1409,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,8 +43,10 @@ extern int write_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_times;
215 +extern int force_change;
216  extern int delete_mode;
217  extern int delete_before;
218  extern int delete_during;
219 @@ -471,6 +473,10 @@ int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
220                         return 0;
221                 if (perms_differ(file, sxp))
222                         return 0;
223 +#ifdef SUPPORT_FILEFLAGS
224 +               if (preserve_fileflags && sxp->st.st_flags != F_FFLAGS(file))
225 +                       return 0;
226 +#endif
227                 if (ownership_differs(file, sxp))
228                         return 0;
229  #ifdef SUPPORT_ACLS
230 @@ -525,6 +531,11 @@ void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statre
231                 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
232                     && sxp->st.st_gid != (gid_t)F_GROUP(file))
233                         iflags |= ITEM_REPORT_GROUP;
234 +#ifdef SUPPORT_FILEFLAGS
235 +               if (preserve_fileflags && !S_ISLNK(file->mode)
236 +                && sxp->st.st_flags != F_FFLAGS(file))
237 +                       iflags |= ITEM_REPORT_FFLAGS;
238 +#endif
239  #ifdef SUPPORT_ACLS
240                 if (preserve_acls && !S_ISLNK(file->mode)) {
241                         if (!ACL_READY(*sxp))
242 @@ -1421,6 +1432,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
243                         file->mode = dest_mode(file->mode, sx.st.st_mode,
244                                                dflt_perms, statret == 0);
245                 }
246 +#ifdef SUPPORT_FORCE_CHANGE
247 +               if (force_change && !preserve_fileflags)
248 +                       F_FFLAGS(file) = sx.st.st_flags;
249 +#endif
250                 if (statret != 0 && basis_dir[0] != NULL) {
251                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
252                                               itemizing, code);
253 @@ -1465,10 +1480,15 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
254                  * readable and writable permissions during the time we are
255                  * putting files within them.  This is then restored to the
256                  * former permissions after the transfer is done. */
257 +#ifdef SUPPORT_FORCE_CHANGE
258 +               if (force_change && F_FFLAGS(file) & force_change
259 +                && make_mutable(fname, file->mode, F_FFLAGS(file), force_change))
260 +                       need_retouch_dir_perms = 1;
261 +#endif
262  #ifdef HAVE_CHMOD
263                 if (!am_root && (file->mode & S_IRWXU) != S_IRWXU && dir_tweaking) {
264                         mode_t mode = file->mode | S_IRWXU;
265 -                       if (do_chmod(fname, mode) < 0) {
266 +                       if (do_chmod(fname, mode, 0) < 0) {
267                                 rsyserr(FERROR_XFER, errno,
268                                         "failed to modify permissions on %s",
269                                         full_fname(fname));
270 @@ -1504,6 +1524,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
271                 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
272                                        exists);
273         }
274 +#ifdef SUPPORT_FORCE_CHANGE
275 +       if (force_change && !preserve_fileflags)
276 +               F_FFLAGS(file) = sx.st.st_flags;
277 +#endif
278  
279  #ifdef SUPPORT_HARD_LINKS
280         if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
281 @@ -2071,17 +2095,25 @@ static void touch_up_dirs(struct file_list *flist, int ndx)
282                         continue;
283                 fname = f_name(file, NULL);
284                 if (fix_dir_perms)
285 -                       do_chmod(fname, file->mode);
286 +                       do_chmod(fname, file->mode, 0);
287                 if (need_retouch_dir_times) {
288                         STRUCT_STAT st;
289                         if (link_stat(fname, &st, 0) == 0 && time_diff(&st, file)) {
290                                 st.st_mtime = file->modtime;
291  #ifdef ST_MTIME_NSEC
292                                 st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file);
293 +#endif
294 +#ifdef SUPPORT_FORCE_CHANGE
295 +                               st.st_mode = file->mode;
296 +                               st.st_flags = 0;
297  #endif
298                                 set_times(fname, &st);
299                         }
300                 }
301 +#ifdef SUPPORT_FORCE_CHANGE
302 +               if (force_change && F_FFLAGS(file) & force_change)
303 +                       undo_make_mutable(fname, F_FFLAGS(file));
304 +#endif
305                 if (counter >= loopchk_limit) {
306                         if (allowed_lull)
307                                 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
308 diff --git a/log.c b/log.c
309 --- a/log.c
310 +++ b/log.c
311 @@ -719,7 +719,8 @@ static void log_formatted(enum logcode code, const char *format, const char *op,
312                              : S_ISLNK(file->mode) ? 'U' : 'u';
313                         c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
314                         c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
315 -                       c[11] = '\0';
316 +                       c[11] = !(iflags & ITEM_REPORT_FFLAGS) ? '.' : 'f';
317 +                       c[12] = '\0';
318  
319                         if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
320                                 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
321 diff --git a/main.c b/main.c
322 --- a/main.c
323 +++ b/main.c
324 @@ -26,6 +26,9 @@
325  #if defined CONFIG_LOCALE && defined HAVE_LOCALE_H
326  #include <locale.h>
327  #endif
328 +#ifdef SUPPORT_FORCE_CHANGE
329 +#include <sys/sysctl.h>
330 +#endif
331  
332  extern int dry_run;
333  extern int list_only;
334 @@ -52,6 +55,7 @@ extern int copy_unsafe_links;
335  extern int keep_dirlinks;
336  extern int preserve_hard_links;
337  extern int protocol_version;
338 +extern int force_change;
339  extern int file_total;
340  extern int recurse;
341  extern int xfer_dirs;
342 @@ -944,6 +948,22 @@ static int do_recv(int f_in, int f_out, char *local_name)
343          * points to an identical file won't be replaced by the referent. */
344         copy_links = copy_dirlinks = copy_unsafe_links = 0;
345  
346 +#ifdef SUPPORT_FORCE_CHANGE
347 +       if (force_change & SYS_IMMUTABLE) {
348 +               /* Determine whether we'll be able to unlock a system immutable item. */
349 +               int mib[2];
350 +               int securityLevel = 0;
351 +               size_t len = sizeof securityLevel;
352 +
353 +               mib[0] = CTL_KERN;
354 +               mib[1] = KERN_SECURELVL;
355 +               if (sysctl(mib, 2, &securityLevel, &len, NULL, 0) == 0 && securityLevel > 0) {
356 +                       rprintf(FERROR, "System security level is too high to force mutability on system immutable files and directories.\n");
357 +                       exit_cleanup(RERR_UNSUPPORTED);
358 +               }
359 +       }
360 +#endif
361 +
362  #ifdef SUPPORT_HARD_LINKS
363         if (preserve_hard_links && !inc_recurse)
364                 match_hard_links(first_flist);
365 diff --git a/options.c b/options.c
366 --- a/options.c
367 +++ b/options.c
368 @@ -57,6 +57,7 @@ int preserve_hard_links = 0;
369  int preserve_acls = 0;
370  int preserve_xattrs = 0;
371  int preserve_perms = 0;
372 +int preserve_fileflags = 0;
373  int preserve_executability = 0;
374  int preserve_devices = 0;
375  int preserve_specials = 0;
376 @@ -93,6 +94,7 @@ int numeric_ids = 0;
377  int msgs2stderr = 0;
378  int allow_8bit_chars = 0;
379  int force_delete = 0;
380 +int force_change = 0;
381  int io_timeout = 0;
382  int prune_empty_dirs = 0;
383  int use_qsort = 0;
384 @@ -578,6 +580,7 @@ static void print_rsync_version(enum logcode f)
385         char const *links = "no ";
386         char const *iconv = "no ";
387         char const *ipv6 = "no ";
388 +       char const *fileflags = "no ";
389         STRUCT_STAT *dumstat;
390  
391  #if SUBPROTOCOL_VERSION != 0
392 @@ -614,6 +617,9 @@ static void print_rsync_version(enum logcode f)
393  #ifdef CAN_SET_SYMLINK_TIMES
394         symtimes = "";
395  #endif
396 +#ifdef SUPPORT_FILEFLAGS
397 +       fileflags = "";
398 +#endif
399  
400         rprintf(f, "%s  version %s  protocol version %d%s\n",
401                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
402 @@ -627,8 +633,8 @@ static void print_rsync_version(enum logcode f)
403                 (int)(sizeof (int64) * 8));
404         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
405                 got_socketpair, hardlinks, links, ipv6, have_inplace);
406 -       rprintf(f, "    %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc\n",
407 -               have_inplace, acls, xattrs, iconv, symtimes, prealloc);
408 +       rprintf(f, "    %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc, %sfile-flags\n",
409 +               have_inplace, acls, xattrs, iconv, symtimes, prealloc, fileflags);
410  
411  #ifdef MAINTAINER_MODE
412         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
413 @@ -699,6 +705,9 @@ void usage(enum logcode F)
414    rprintf(F," -K, --keep-dirlinks         treat symlinked dir on receiver as dir\n");
415    rprintf(F," -H, --hard-links            preserve hard links\n");
416    rprintf(F," -p, --perms                 preserve permissions\n");
417 +#ifdef SUPPORT_FILEFLAGS
418 +  rprintf(F,"     --fileflags             preserve file-flags (aka chflags)\n");
419 +#endif
420    rprintf(F," -E, --executability         preserve the file's executability\n");
421    rprintf(F,"     --chmod=CHMOD           affect file and/or directory permissions\n");
422  #ifdef SUPPORT_ACLS
423 @@ -748,7 +757,12 @@ void usage(enum logcode F)
424    rprintf(F,"     --ignore-missing-args   ignore missing source args without error\n");
425    rprintf(F,"     --delete-missing-args   delete missing source args from destination\n");
426    rprintf(F,"     --ignore-errors         delete even if there are I/O errors\n");
427 -  rprintf(F,"     --force                 force deletion of directories even if not empty\n");
428 +  rprintf(F,"     --force-delete          force deletion of directories even if not empty\n");
429 +#ifdef SUPPORT_FORCE_CHANGE
430 +  rprintf(F,"     --force-change          affect user-/system-immutable files/dirs\n");
431 +  rprintf(F,"     --force-uchange         affect user-immutable files/dirs\n");
432 +  rprintf(F,"     --force-schange         affect system-immutable files/dirs\n");
433 +#endif
434    rprintf(F,"     --max-delete=NUM        don't delete more than NUM files\n");
435    rprintf(F,"     --max-size=SIZE         don't transfer any file larger than SIZE\n");
436    rprintf(F,"     --min-size=SIZE         don't transfer any file smaller than SIZE\n");
437 @@ -866,6 +880,10 @@ static struct poptOption long_options[] = {
438    {"perms",           'p', POPT_ARG_VAL,    &preserve_perms, 1, 0, 0 },
439    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
440    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
441 +#ifdef SUPPORT_FILEFLAGS
442 +  {"fileflags",        0,  POPT_ARG_VAL,    &preserve_fileflags, 1, 0, 0 },
443 +  {"no-fileflags",     0,  POPT_ARG_VAL,    &preserve_fileflags, 0, 0, 0 },
444 +#endif
445    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
446    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
447    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
448 @@ -959,6 +977,14 @@ static struct poptOption long_options[] = {
449    {"remove-source-files",0,POPT_ARG_VAL,    &remove_source_files, 1, 0, 0 },
450    {"force",            0,  POPT_ARG_VAL,    &force_delete, 1, 0, 0 },
451    {"no-force",         0,  POPT_ARG_VAL,    &force_delete, 0, 0, 0 },
452 +  {"force-delete",     0,  POPT_ARG_VAL,    &force_delete, 1, 0, 0 },
453 +  {"no-force-delete",  0,  POPT_ARG_VAL,    &force_delete, 0, 0, 0 },
454 +#ifdef SUPPORT_FORCE_CHANGE
455 +  {"force-change",     0,  POPT_ARG_VAL,    &force_change, ALL_IMMUTABLE, 0, 0 },
456 +  {"no-force-change",  0,  POPT_ARG_VAL,    &force_change, 0, 0, 0 },
457 +  {"force-uchange",    0,  POPT_ARG_VAL,    &force_change, USR_IMMUTABLE, 0, 0 },
458 +  {"force-schange",    0,  POPT_ARG_VAL,    &force_change, SYS_IMMUTABLE, 0, 0 },
459 +#endif
460    {"ignore-errors",    0,  POPT_ARG_VAL,    &ignore_errors, 1, 0, 0 },
461    {"no-ignore-errors", 0,  POPT_ARG_VAL,    &ignore_errors, 0, 0, 0 },
462    {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
463 @@ -2614,6 +2640,9 @@ void server_options(char **args, int *argc_p)
464         if (xfer_dirs && !recurse && delete_mode && am_sender)
465                 args[ac++] = "--no-r";
466  
467 +       if (preserve_fileflags)
468 +               args[ac++] = "--fileflags";
469 +
470         if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
471                 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
472                         goto oom;
473 @@ -2707,6 +2736,16 @@ void server_options(char **args, int *argc_p)
474                         args[ac++] = "--delete-excluded";
475                 if (force_delete)
476                         args[ac++] = "--force";
477 +#ifdef SUPPORT_FORCE_CHANGE
478 +               if (force_change) {
479 +                       if (force_change == ALL_IMMUTABLE)
480 +                               args[ac++] = "--force-change";
481 +                       else if (force_change == USR_IMMUTABLE)
482 +                               args[ac++] = "--force-uchange";
483 +                       else if (force_change == SYS_IMMUTABLE)
484 +                               args[ac++] = "--force-schange";
485 +               }
486 +#endif
487                 if (write_batch < 0)
488                         args[ac++] = "--only-write-batch=X";
489                 if (am_root > 1)
490 diff --git a/rsync.c b/rsync.c
491 --- a/rsync.c
492 +++ b/rsync.c
493 @@ -31,6 +31,7 @@ extern int dry_run;
494  extern int preserve_acls;
495  extern int preserve_xattrs;
496  extern int preserve_perms;
497 +extern int preserve_fileflags;
498  extern int preserve_executability;
499  extern int preserve_times;
500  extern int am_root;
501 @@ -468,6 +469,39 @@ mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
502         return new_mode;
503  }
504  
505 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
506 +/* Set a file's st_flags. */
507 +static int set_fileflags(const char *fname, uint32 fileflags)
508 +{
509 +       if (do_chflags(fname, fileflags) != 0) {
510 +               rsyserr(FERROR_XFER, errno,
511 +                       "failed to set file flags on %s",
512 +                       full_fname(fname));
513 +               return 0;
514 +       }
515 +
516 +       return 1;
517 +}
518 +
519 +/* Remove immutable flags from an object, so it can be altered/removed. */
520 +int make_mutable(const char *fname, mode_t mode, uint32 fileflags, uint32 iflags)
521 +{
522 +       if (S_ISLNK(mode) || !(fileflags & iflags))
523 +               return 0;
524 +       if (!set_fileflags(fname, fileflags & ~iflags))
525 +               return -1;
526 +       return 1;
527 +}
528 +
529 +/* Undo a prior make_mutable() call that returned a 1. */
530 +int undo_make_mutable(const char *fname, uint32 fileflags)
531 +{
532 +       if (!set_fileflags(fname, fileflags))
533 +               return -1;
534 +       return 1;
535 +}
536 +#endif
537 +
538  int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
539                    const char *fnamecmp, int flags)
540  {
541 @@ -529,7 +563,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
542                 if (am_root >= 0) {
543                         uid_t uid = change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid;
544                         gid_t gid = change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid;
545 -                       if (do_lchown(fname, uid, gid) != 0) {
546 +                       if (do_lchown(fname, uid, gid, sxp->st.st_mode, ST_FLAGS(sxp->st)) != 0) {
547                                 /* We shouldn't have attempted to change uid
548                                  * or gid unless have the privilege. */
549                                 rsyserr(FERROR_XFER, errno, "%s %s failed",
550 @@ -620,7 +654,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
551  
552  #ifdef HAVE_CHMOD
553         if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
554 -               int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
555 +               int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode, ST_FLAGS(sxp->st));
556                 if (ret < 0) {
557                         rsyserr(FERROR_XFER, errno,
558                                 "failed to set permissions on %s",
559 @@ -632,6 +666,19 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
560         }
561  #endif
562  
563 +#ifdef SUPPORT_FILEFLAGS
564 +       if (preserve_fileflags && !S_ISLNK(sxp->st.st_mode)
565 +        && sxp->st.st_flags != F_FFLAGS(file)) {
566 +               uint32 fileflags = F_FFLAGS(file);
567 +               if (flags & ATTRS_DELAY_IMMUTABLE)
568 +                       fileflags &= ~ALL_IMMUTABLE;
569 +               if (sxp->st.st_flags != fileflags
570 +                && !set_fileflags(fname, fileflags))
571 +                       goto cleanup;
572 +               updated = 1;
573 +       }
574 +#endif
575 +
576         if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
577                 if (updated)
578                         rprintf(FCLIENT, "%s\n", fname);
579 @@ -709,7 +756,8 @@ int finish_transfer(const char *fname, const char *fnametmp,
580  
581         /* Change permissions before putting the file into place. */
582         set_file_attrs(fnametmp, file, NULL, fnamecmp,
583 -                      ok_to_set_time ? ATTRS_SET_NANO : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME);
584 +                      ATTRS_DELAY_IMMUTABLE
585 +                      | (ok_to_set_time ? ATTRS_SET_NANO : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME));
586  
587         /* move tmp file over real file */
588         if (DEBUG_GTE(RECV, 1))
589 @@ -726,6 +774,10 @@ int finish_transfer(const char *fname, const char *fnametmp,
590         }
591         if (ret == 0) {
592                 /* The file was moved into place (not copied), so it's done. */
593 +#ifdef SUPPORT_FILEFLAGS
594 +               if (preserve_fileflags && F_FFLAGS(file) & ALL_IMMUTABLE)
595 +                       set_fileflags(fname, F_FFLAGS(file));
596 +#endif
597                 return 1;
598         }
599         /* The file was copied, so tweak the perms of the copied file.  If it
600 diff --git a/rsync.h b/rsync.h
601 --- a/rsync.h
602 +++ b/rsync.h
603 @@ -63,6 +63,7 @@
604  #define XMIT_IO_ERROR_ENDLIST (1<<12)  /* protocols 31*- now (w/XMIT_EXTENDED_FLAGS) (also protocol 30 w/'f' compat flag) */
605  #define XMIT_MOD_NSEC (1<<13)          /* protocols 31 - now */
606  #define XMIT_SAME_ATIME (1<<14)        /* protocols ?? - now */
607 +#define XMIT_SAME_FLAGS (1<<15)                /* protocols ?? - now */
608  
609  /* These flags are used in the live flist data. */
610  
611 @@ -170,6 +171,7 @@
612  #define ATTRS_SKIP_MTIME       (1<<1)
613  #define ATTRS_SET_NANO         (1<<2)
614  #define ATTRS_SKIP_ATIME       (1<<3)
615 +#define ATTRS_DELAY_IMMUTABLE  (1<<4)
616  
617  #define FULL_FLUSH     1
618  #define NORMAL_FLUSH   0
619 @@ -196,6 +198,7 @@
620  #define ITEM_REPORT_GROUP (1<<6)
621  #define ITEM_REPORT_ACL (1<<7)
622  #define ITEM_REPORT_XATTR (1<<8)
623 +#define ITEM_REPORT_FFLAGS (1<<9)
624  #define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
625  #define ITEM_XNAME_FOLLOWS (1<<12)
626  #define ITEM_IS_NEW (1<<13)
627 @@ -536,6 +539,28 @@ typedef unsigned int size_t;
628  #endif
629  #endif
630  
631 +#define NO_FFLAGS ((uint32)-1)
632 +
633 +#ifdef HAVE_CHFLAGS
634 +#define SUPPORT_FILEFLAGS 1
635 +#define SUPPORT_FORCE_CHANGE 1
636 +#endif
637 +
638 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
639 +#ifndef UF_NOUNLINK
640 +#define UF_NOUNLINK 0
641 +#endif
642 +#ifndef SF_NOUNLINK
643 +#define SF_NOUNLINK 0
644 +#endif
645 +#define USR_IMMUTABLE (UF_IMMUTABLE|UF_NOUNLINK|UF_APPEND)
646 +#define SYS_IMMUTABLE (SF_IMMUTABLE|SF_NOUNLINK|SF_APPEND)
647 +#define ALL_IMMUTABLE (USR_IMMUTABLE|SYS_IMMUTABLE)
648 +#define ST_FLAGS(st) ((st).st_flags)
649 +#else
650 +#define ST_FLAGS(st) NO_FFLAGS
651 +#endif
652 +
653  /* Find a variable that is either exactly 32-bits or longer.
654   * If some code depends on 32-bit truncation, it will need to
655   * take special action in a "#if SIZEOF_INT32 > 4" section. */
656 @@ -746,6 +771,7 @@ extern int pathname_ndx;
657  extern int depth_ndx;
658  extern int uid_ndx;
659  extern int gid_ndx;
660 +extern int fileflags_ndx;
661  extern int acls_ndx;
662  extern int xattrs_ndx;
663  
664 @@ -796,6 +822,11 @@ extern int xattrs_ndx;
665  /* When the associated option is on, all entries will have these present: */
666  #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
667  #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
668 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
669 +#define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
670 +#else
671 +#define F_FFLAGS(f) NO_FFLAGS
672 +#endif
673  #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
674  #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
675  #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
676 diff --git a/rsync.yo b/rsync.yo
677 --- a/rsync.yo
678 +++ b/rsync.yo
679 @@ -368,6 +368,7 @@ to the detailed description below for a complete description.  verb(
680   -K, --keep-dirlinks         treat symlinked dir on receiver as dir
681   -H, --hard-links            preserve hard links
682   -p, --perms                 preserve permissions
683 +     --fileflags             preserve file-flags (aka chflags)
684   -E, --executability         preserve executability
685       --chmod=CHMOD           affect file and/or directory permissions
686   -A, --acls                  preserve ACLs (implies -p)
687 @@ -407,7 +408,10 @@ to the detailed description below for a complete description.  verb(
688       --ignore-missing-args   ignore missing source args without error
689       --delete-missing-args   delete missing source args from destination
690       --ignore-errors         delete even if there are I/O errors
691 -     --force                 force deletion of dirs even if not empty
692 +     --force-delete          force deletion of dirs even if not empty
693 +     --force-change          affect user/system immutable files/dirs
694 +     --force-uchange         affect user-immutable files/dirs
695 +     --force-schange         affect system-immutable files/dirs
696       --max-delete=NUM        don't delete more than NUM files
697       --max-size=SIZE         don't transfer any file larger than SIZE
698       --min-size=SIZE         don't transfer any file smaller than SIZE
699 @@ -668,7 +672,8 @@ specified, in which case bf(-r) is not implied.
700  
701  Note that bf(-a) bf(does not preserve hardlinks), because
702  finding multiply-linked files is expensive.  You must separately
703 -specify bf(-H).
704 +specify bf(-H).  Note also that for backward compatibility, bf(-a)
705 +currently does bf(not) imply the bf(--fileflags) option.
706  
707  dit(--no-OPTION) You may turn off one or more implied options by prefixing
708  the option name with "no-".  Not all options may be prefixed with a "no-":
709 @@ -981,7 +986,7 @@ they would be using bf(--copy-links).
710  Without this option, if the sending side has replaced a directory with a
711  symlink to a directory, the receiving side will delete anything that is in
712  the way of the new symlink, including a directory hierarchy (as long as
713 -bf(--force) or bf(--delete) is in effect).
714 +bf(--force-delete) or bf(--delete) is in effect).
715  
716  See also bf(--keep-dirlinks) for an analogous option for the receiving
717  side.
718 @@ -1165,6 +1170,29 @@ Note that the bf(-X) option does not copy rsync's special xattr values (e.g.
719  those used by bf(--fake-super)) unless you repeat the option (e.g. -XX).
720  This "copy all xattrs" mode cannot be used with bf(--fake-super).
721  
722 +dit(bf(--fileflags)) This option causes rsync to update the file-flags to be
723 +the same as the source files and directories (if your OS supports the
724 +bf(chflags)(2) system call).   Some flags can only be altered by the super-user
725 +and some might only be unset below a certain secure-level (usually single-user
726 +mode). It will not make files alterable that are set to immutable on the
727 +receiver.  To do that, see bf(--force-change), bf(--force-uchange), and
728 +bf(--force-schange).
729 +
730 +dit(bf(--force-change)) This option causes rsync to disable both user-immutable
731 +and system-immutable flags on files and directories that are being updated or
732 +deleted on the receiving side.  This option overrides bf(--force-uchange) and
733 +bf(--force-schange).
734 +
735 +dit(bf(--force-uchange)) This option causes rsync to disable user-immutable
736 +flags on files and directories that are being updated or deleted on the
737 +receiving side.  It does not try to affect system flags.  This option overrides
738 +bf(--force-change) and bf(--force-schange).
739 +
740 +dit(bf(--force-schange)) This option causes rsync to disable system-immutable
741 +flags on files and directories that are being updated or deleted on the
742 +receiving side.  It does not try to affect user flags.  This option overrides
743 +bf(--force-change) and bf(--force-uchange).
744 +
745  dit(bf(--chmod)) This option tells rsync to apply one or more
746  comma-separated "chmod" modes to the permission of the files in the
747  transfer.  The resulting value is treated as though it were the permissions
748 @@ -1549,12 +1577,13 @@ display as a "*missing" entry in the bf(--list-only) output.
749  dit(bf(--ignore-errors)) Tells bf(--delete) to go ahead and delete files
750  even when there are I/O errors.
751  
752 -dit(bf(--force)) This option tells rsync to delete a non-empty directory
753 +dit(bf(--force-delete)) This option tells rsync to delete a non-empty directory
754  when it is to be replaced by a non-directory.  This is only relevant if
755  deletions are not active (see bf(--delete) for details).
756  
757 -Note for older rsync versions: bf(--force) used to still be required when
758 -using bf(--delete-after), and it used to be non-functional unless the
759 +This option can be abbreviated bf(--force) for backward compatibility.
760 +Note that some older rsync versions used to still require bf(--force)
761 +when using bf(--delete-after), and it used to be non-functional unless the
762  bf(--recursive) option was also enabled.
763  
764  dit(bf(--max-delete=NUM)) This tells rsync not to delete more than NUM
765 @@ -2228,7 +2257,7 @@ with older versions of rsync, but that also turns on the output of other
766  verbose messages).
767  
768  The "%i" escape has a cryptic output that is 11 letters long.  The general
769 -format is like the string bf(YXcstpoguax), where bf(Y) is replaced by the
770 +format is like the string bf(YXcstpogfax), where bf(Y) is replaced by the
771  type of update being done, bf(X) is replaced by the file-type, and the
772  other letters represent attributes that may be output if they are being
773  modified.
774 diff --git a/syscall.c b/syscall.c
775 --- a/syscall.c
776 +++ b/syscall.c
777 @@ -38,6 +38,7 @@ extern int am_root;
778  extern int am_sender;
779  extern int read_only;
780  extern int list_only;
781 +extern int force_change;
782  extern int inplace;
783  extern int preallocate_files;
784  extern int preserve_perms;
785 @@ -68,7 +69,23 @@ int do_unlink(const char *fname)
786  {
787         if (dry_run) return 0;
788         RETURN_ERROR_IF_RO_OR_LO;
789 -       return unlink(fname);
790 +       if (unlink(fname) == 0)
791 +               return 0;
792 +#ifdef SUPPORT_FORCE_CHANGE
793 +       if (force_change && errno == EPERM) {
794 +               STRUCT_STAT st;
795 +
796 +               if (x_lstat(fname, &st, NULL) == 0
797 +                && make_mutable(fname, st.st_mode, st.st_flags, force_change) > 0) {
798 +                       if (unlink(fname) == 0)
799 +                               return 0;
800 +                       undo_make_mutable(fname, st.st_flags);
801 +               }
802 +               /* TODO: handle immutable directories */
803 +               errno = EPERM;
804 +       }
805 +#endif
806 +       return -1;
807  }
808  
809  #ifdef SUPPORT_LINKS
810 @@ -129,14 +146,37 @@ int do_link(const char *fname1, const char *fname2)
811  }
812  #endif
813  
814 -int do_lchown(const char *path, uid_t owner, gid_t group)
815 +int do_lchown(const char *path, uid_t owner, gid_t group, mode_t mode, uint32 fileflags)
816  {
817         if (dry_run) return 0;
818         RETURN_ERROR_IF_RO_OR_LO;
819  #ifndef HAVE_LCHOWN
820  #define lchown chown
821  #endif
822 -       return lchown(path, owner, group);
823 +       if (lchown(path, owner, group) == 0)
824 +               return 0;
825 +#ifdef SUPPORT_FORCE_CHANGE
826 +       if (force_change && errno == EPERM) {
827 +               if (fileflags == NO_FFLAGS) {
828 +                       STRUCT_STAT st;
829 +                       if (x_lstat(path, &st, NULL) == 0) {
830 +                               mode = st.st_mode;
831 +                               fileflags = st.st_flags;
832 +                       }
833 +               }
834 +               if (fileflags != NO_FFLAGS
835 +                && make_mutable(path, mode, fileflags, force_change) > 0) {
836 +                       int ret = lchown(path, owner, group);
837 +                       undo_make_mutable(path, fileflags);
838 +                       if (ret == 0)
839 +                               return 0;
840 +               }
841 +               errno = EPERM;
842 +       }
843 +#else
844 +       mode = fileflags = 0; /* avoid compiler warning */
845 +#endif
846 +       return -1;
847  }
848  
849  int do_mknod(const char *pathname, mode_t mode, dev_t dev)
850 @@ -176,7 +216,7 @@ int do_mknod(const char *pathname, mode_t mode, dev_t dev)
851                         return -1;
852                 close(sock);
853  #ifdef HAVE_CHMOD
854 -               return do_chmod(pathname, mode);
855 +               return do_chmod(pathname, mode, 0);
856  #else
857                 return 0;
858  #endif
859 @@ -193,7 +233,22 @@ int do_rmdir(const char *pathname)
860  {
861         if (dry_run) return 0;
862         RETURN_ERROR_IF_RO_OR_LO;
863 -       return rmdir(pathname);
864 +       if (rmdir(pathname) == 0)
865 +               return 0;
866 +#ifdef SUPPORT_FORCE_CHANGE
867 +       if (force_change && errno == EPERM) {
868 +               STRUCT_STAT st;
869 +
870 +               if (x_lstat(pathname, &st, NULL) == 0
871 +                && make_mutable(pathname, st.st_mode, st.st_flags, force_change) > 0) {
872 +                       if (rmdir(pathname) == 0)
873 +                               return 0;
874 +                       undo_make_mutable(pathname, st.st_flags);
875 +               }
876 +               errno = EPERM;
877 +       }
878 +#endif
879 +       return -1;
880  }
881  
882  int do_open(const char *pathname, int flags, mode_t mode)
883 @@ -212,7 +267,7 @@ int do_open(const char *pathname, int flags, mode_t mode)
884  }
885  
886  #ifdef HAVE_CHMOD
887 -int do_chmod(const char *path, mode_t mode)
888 +int do_chmod(const char *path, mode_t mode, uint32 fileflags)
889  {
890         int code;
891         if (dry_run) return 0;
892 @@ -235,17 +290,74 @@ int do_chmod(const char *path, mode_t mode)
893         } else
894                 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
895  #endif /* !HAVE_LCHMOD */
896 +#ifdef SUPPORT_FORCE_CHANGE
897 +       if (code < 0 && force_change && errno == EPERM && !S_ISLNK(mode)) {
898 +               if (fileflags == NO_FFLAGS) {
899 +                       STRUCT_STAT st;
900 +                       if (x_lstat(path, &st, NULL) == 0)
901 +                               fileflags = st.st_flags;
902 +               }
903 +               if (fileflags != NO_FFLAGS
904 +                && make_mutable(path, mode, fileflags, force_change) > 0) {
905 +                       code = chmod(path, mode & CHMOD_BITS);
906 +                       undo_make_mutable(path, fileflags);
907 +                       if (code == 0)
908 +                               return 0;
909 +               }
910 +               errno = EPERM;
911 +       }
912 +#else
913 +       fileflags = 0; /* avoid compiler warning */
914 +#endif
915         if (code != 0 && (preserve_perms || preserve_executability))
916                 return code;
917         return 0;
918  }
919  #endif
920  
921 +#ifdef HAVE_CHFLAGS
922 +int do_chflags(const char *path, uint32 fileflags)
923 +{
924 +       if (dry_run) return 0;
925 +       RETURN_ERROR_IF_RO_OR_LO;
926 +       return chflags(path, fileflags);
927 +}
928 +#endif
929 +
930  int do_rename(const char *fname1, const char *fname2)
931  {
932         if (dry_run) return 0;
933         RETURN_ERROR_IF_RO_OR_LO;
934 -       return rename(fname1, fname2);
935 +       if (rename(fname1, fname2) == 0)
936 +               return 0;
937 +#ifdef SUPPORT_FORCE_CHANGE
938 +       if (force_change && errno == EPERM) {
939 +               STRUCT_STAT st1, st2;
940 +               int became_mutable;
941 +
942 +               if (x_lstat(fname1, &st1, NULL) != 0)
943 +                       goto failed;
944 +               became_mutable = make_mutable(fname1, st1.st_mode, st1.st_flags, force_change) > 0;
945 +               if (became_mutable && rename(fname1, fname2) == 0)
946 +                       goto success;
947 +               if (x_lstat(fname2, &st2, NULL) == 0
948 +                && make_mutable(fname2, st2.st_mode, st2.st_flags, force_change) > 0) {
949 +                       if (rename(fname1, fname2) == 0) {
950 +                         success:
951 +                               if (became_mutable) /* Yes, use fname2 and st1! */
952 +                                       undo_make_mutable(fname2, st1.st_flags);
953 +                               return 0;
954 +                       }
955 +                       undo_make_mutable(fname2, st2.st_flags);
956 +               }
957 +               /* TODO: handle immutable directories */
958 +               if (became_mutable)
959 +                       undo_make_mutable(fname1, st1.st_flags);
960 +         failed:
961 +               errno = EPERM;
962 +       }
963 +#endif
964 +       return -1;
965  }
966  
967  #ifdef HAVE_FTRUNCATE
968 diff --git a/t_stub.c b/t_stub.c
969 --- a/t_stub.c
970 +++ b/t_stub.c
971 @@ -28,6 +28,8 @@ int protect_args = 0;
972  int module_id = -1;
973  int relative_paths = 0;
974  int module_dirlen = 0;
975 +int force_change = 0;
976 +int preserve_acls = 0;
977  int preserve_times = 0;
978  int preserve_xattrs = 0;
979  int preserve_perms = 0;
980 @@ -104,3 +106,23 @@ filter_rule_list daemon_filter_list;
981  {
982         return cst || !flg ? 16 : 1;
983  }
984 +
985 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
986 + int make_mutable(UNUSED(const char *fname), UNUSED(mode_t mode), UNUSED(uint32 fileflags), UNUSED(uint32 iflags))
987 +{
988 +       return 0;
989 +}
990 +
991 +/* Undo a prior make_mutable() call that returned a 1. */
992 + int undo_make_mutable(UNUSED(const char *fname), UNUSED(uint32 fileflags))
993 +{
994 +       return 0;
995 +}
996 +#endif
997 +
998 +#ifdef SUPPORT_XATTRS
999 + int x_lstat(UNUSED(const char *fname), UNUSED(STRUCT_STAT *fst), UNUSED(STRUCT_STAT *xst))
1000 +{
1001 +       return -1;
1002 +}
1003 +#endif
1004 diff --git a/util.c b/util.c
1005 --- a/util.c
1006 +++ b/util.c
1007 @@ -33,6 +33,7 @@ extern int relative_paths;
1008  extern int preserve_times;
1009  extern int preserve_xattrs;
1010  extern int preallocate_files;
1011 +extern int force_change;
1012  extern char *module_dir;
1013  extern unsigned int module_dirlen;
1014  extern char *partial_dir;
1015 @@ -115,6 +116,33 @@ void print_child_argv(const char *prefix, char **cmd)
1016         rprintf(FCLIENT, " (%d args)\n", cnt);
1017  }
1018  
1019 +#ifdef SUPPORT_FORCE_CHANGE
1020 +static int try_a_force_change(const char *fname, STRUCT_STAT *stp)
1021 +{
1022 +       uint32 fileflags = ST_FLAGS(*stp);
1023 +       if (fileflags == NO_FFLAGS) {
1024 +               STRUCT_STAT st;
1025 +               if (x_lstat(fname, &st, NULL) == 0)
1026 +                       fileflags = st.st_flags;
1027 +       }
1028 +       if (fileflags != NO_FFLAGS && make_mutable(fname, stp->mode, fileflags, force_change) > 0) {
1029 +               int ret, save_force_change = force_change;
1030 +
1031 +               force_change = 0; /* Make certain we can't come back here. */
1032 +               ret = set_times(fname, stp);
1033 +               force_change = save_force_change;
1034 +
1035 +               undo_make_mutable(fname, fileflags);
1036 +
1037 +               return ret;
1038 +       }
1039 +
1040 +       errno = EPERM;
1041 +
1042 +       return -1;
1043 +}
1044 +#endif
1045 +
1046  /* This returns 0 for success, 1 for a symlink if symlink time-setting
1047   * is not possible, or -1 for any other error. */
1048  int set_times(const char *fname, STRUCT_STAT *stp)
1049 @@ -142,6 +170,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1050  #include "case_N.h"
1051                 if (do_utimensat(fname, stp) == 0)
1052                         break;
1053 +#ifdef SUPPORT_FORCE_CHANGE
1054 +               if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1055 +                       break;
1056 +#endif
1057                 if (errno != ENOSYS)
1058                         return -1;
1059                 switch_step++;
1060 @@ -151,6 +183,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1061  #include "case_N.h"
1062                 if (do_lutimes(fname, stp) == 0)
1063                         break;
1064 +#ifdef SUPPORT_FORCE_CHANGE
1065 +               if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1066 +                       break;
1067 +#endif
1068                 if (errno != ENOSYS)
1069                         return -1;
1070                 switch_step++;
1071 @@ -172,6 +208,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1072                 if (do_utime(fname, stp) == 0)
1073                         break;
1074  #endif
1075 +#ifdef SUPPORT_FORCE_CHANGE
1076 +               if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1077 +                       break;
1078 +#endif
1079  
1080                 return -1;
1081         }
1082 diff --git a/xattrs.c b/xattrs.c
1083 --- a/xattrs.c
1084 +++ b/xattrs.c
1085 @@ -1224,7 +1224,7 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1086         mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1087              | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1088         if (fst.st_mode != mode)
1089 -               do_chmod(fname, mode);
1090 +               do_chmod(fname, mode, ST_FLAGS(fst));
1091         if (!IS_DEVICE(fst.st_mode))
1092                 fst.st_rdev = 0; /* just in case */
1093