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