The patches for 3.1.1pre2.
[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: 4c8eb5f9511dd1393b147c4e993ee15448faedf5
12 diff --git a/Makefile.in b/Makefile.in
13 --- a/Makefile.in
14 +++ b/Makefile.in
15 @@ -45,7 +45,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 @@ -43,9 +43,11 @@ 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_fileflags;
45  extern int preserve_acls;
46  extern int preserve_xattrs;
47  extern int need_messages_from_generator;
48 @@ -63,7 +65,7 @@ extern char *iconv_opt;
49  #endif
50  
51  /* These index values are for the file-list's extra-attribute array. */
52 -int uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
53 +int uid_ndx, gid_ndx, fileflags_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
54  
55  int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
56  int sender_symlink_iconv = 0;  /* sender should convert symlink content */
57 @@ -140,6 +142,8 @@ void setup_protocol(int f_out,int f_in)
58                 uid_ndx = ++file_extra_cnt;
59         if (preserve_gid)
60                 gid_ndx = ++file_extra_cnt;
61 +       if (preserve_fileflags || (force_change && !am_sender))
62 +               fileflags_ndx = ++file_extra_cnt;
63         if (preserve_acls && !am_sender)
64                 acls_ndx = ++file_extra_cnt;
65         if (preserve_xattrs)
66 diff --git a/configure.ac b/configure.ac
67 --- a/configure.ac
68 +++ b/configure.ac
69 @@ -597,6 +597,7 @@ AC_FUNC_UTIME_NULL
70  AC_FUNC_ALLOCA
71  AC_CHECK_FUNCS(waitpid wait4 getcwd strdup chown chmod lchmod mknod mkfifo \
72      fchmod fstat ftruncate strchr readlink link utime utimes lutimes strftime \
73 +    chflags \
74      memmove lchown vsnprintf snprintf vasprintf asprintf setsid strpbrk \
75      strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
76      setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
77 diff --git a/delete.c b/delete.c
78 --- a/delete.c
79 +++ b/delete.c
80 @@ -25,6 +25,7 @@
81  extern int am_root;
82  extern int make_backups;
83  extern int max_delete;
84 +extern int force_change;
85  extern char *backup_dir;
86  extern char *backup_suffix;
87  extern int backup_suffix_len;
88 @@ -97,8 +98,12 @@ static enum delret delete_dir_contents(char *fname, uint16 flags)
89                 }
90  
91                 strlcpy(p, fp->basename, remainder);
92 +#ifdef SUPPORT_FORCE_CHANGE
93 +               if (force_change)
94 +                       make_mutable(fname, fp->mode, F_FFLAGS(fp), force_change);
95 +#endif
96                 if (!(fp->mode & S_IWUSR) && !am_root && fp->flags & FLAG_OWNED_BY_US)
97 -                       do_chmod(fname, fp->mode | S_IWUSR);
98 +                       do_chmod(fname, fp->mode | S_IWUSR, NO_FFLAGS);
99                 /* Save stack by recursing to ourself directly. */
100                 if (S_ISDIR(fp->mode)) {
101                         if (delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
102 @@ -139,11 +144,18 @@ enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
103         }
104  
105         if (flags & DEL_NO_UID_WRITE)
106 -               do_chmod(fbuf, mode | S_IWUSR);
107 +               do_chmod(fbuf, mode | S_IWUSR, NO_FFLAGS);
108  
109         if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
110                 /* This only happens on the first call to delete_item() since
111                  * delete_dir_contents() always calls us w/DEL_DIR_IS_EMPTY. */
112 +#ifdef SUPPORT_FORCE_CHANGE
113 +               if (force_change) {
114 +                       STRUCT_STAT st;
115 +                       if (x_lstat(fbuf, &st, NULL) == 0)
116 +                               make_mutable(fbuf, st.st_mode, st.st_flags, force_change);
117 +               }
118 +#endif
119                 ignore_perishable = 1;
120                 /* If DEL_RECURSE is not set, this just reports emptiness. */
121                 ret = delete_dir_contents(fbuf, flags);
122 diff --git a/flist.c b/flist.c
123 --- a/flist.c
124 +++ b/flist.c
125 @@ -50,6 +50,7 @@ extern int preserve_links;
126  extern int preserve_hard_links;
127  extern int preserve_devices;
128  extern int preserve_specials;
129 +extern int preserve_fileflags;
130  extern int delete_during;
131  extern int missing_args;
132  extern int eol_nulls;
133 @@ -399,6 +400,9 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
134  {
135         static time_t modtime;
136         static mode_t mode;
137 +#ifdef SUPPORT_FILEFLAGS
138 +       static uint32 fileflags;
139 +#endif
140  #ifdef SUPPORT_HARD_LINKS
141         static int64 dev;
142  #endif
143 @@ -442,6 +446,14 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
144                 xflags |= XMIT_SAME_MODE;
145         else
146                 mode = file->mode;
147 +#ifdef SUPPORT_FILEFLAGS
148 +       if (preserve_fileflags) {
149 +               if (F_FFLAGS(file) == fileflags)
150 +                       xflags |= XMIT_SAME_FLAGS;
151 +               else
152 +                       fileflags = F_FFLAGS(file);
153 +       }
154 +#endif
155  
156         if (preserve_devices && IS_DEVICE(mode)) {
157                 if (protocol_version < 28) {
158 @@ -583,6 +595,10 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
159                 write_varint(f, F_MOD_NSEC(file));
160         if (!(xflags & XMIT_SAME_MODE))
161                 write_int(f, to_wire_mode(mode));
162 +#ifdef SUPPORT_FILEFLAGS
163 +       if (preserve_fileflags && !(xflags & XMIT_SAME_FLAGS))
164 +               write_int(f, (int)fileflags);
165 +#endif
166         if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
167                 if (protocol_version < 30)
168                         write_int(f, uid);
169 @@ -672,6 +688,9 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
170  {
171         static int64 modtime;
172         static mode_t mode;
173 +#ifdef SUPPORT_FILEFLAGS
174 +       static uint32 fileflags;
175 +#endif
176  #ifdef SUPPORT_HARD_LINKS
177         static int64 dev;
178  #endif
179 @@ -779,6 +798,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
180                         modtime = first->modtime;
181                         modtime_nsec = F_MOD_NSEC(first);
182                         mode = first->mode;
183 +#ifdef SUPPORT_FILEFLAGS
184 +                       if (preserve_fileflags)
185 +                               fileflags = F_FFLAGS(first);
186 +#endif
187                         if (preserve_uid)
188                                 uid = F_OWNER(first);
189                         if (preserve_gid)
190 @@ -820,6 +843,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
191  
192         if (chmod_modes && !S_ISLNK(mode) && mode)
193                 mode = tweak_mode(mode, chmod_modes);
194 +#ifdef SUPPORT_FILEFLAGS
195 +       if (preserve_fileflags && !(xflags & XMIT_SAME_FLAGS))
196 +               fileflags = (uint32)read_int(f);
197 +#endif
198  
199         if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
200                 if (protocol_version < 30)
201 @@ -971,6 +998,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
202         }
203  #endif
204         file->mode = mode;
205 +#ifdef SUPPORT_FILEFLAGS
206 +       if (preserve_fileflags)
207 +               F_FFLAGS(file) = fileflags;
208 +#endif
209         if (preserve_uid)
210                 F_OWNER(file) = uid;
211         if (preserve_gid) {
212 @@ -1368,6 +1399,10 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
213         }
214  #endif
215         file->mode = st.st_mode;
216 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
217 +       if (fileflags_ndx)
218 +               F_FFLAGS(file) = st.st_flags;
219 +#endif
220         if (preserve_uid)
221                 F_OWNER(file) = st.st_uid;
222         if (preserve_gid)
223 diff --git a/generator.c b/generator.c
224 --- a/generator.c
225 +++ b/generator.c
226 @@ -42,8 +42,10 @@ extern int preserve_devices;
227  extern int preserve_specials;
228  extern int preserve_hard_links;
229  extern int preserve_executability;
230 +extern int preserve_fileflags;
231  extern int preserve_perms;
232  extern int preserve_times;
233 +extern int force_change;
234  extern int delete_mode;
235  extern int delete_before;
236  extern int delete_during;
237 @@ -464,6 +466,10 @@ int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
238                         return 0;
239                 if (perms_differ(file, sxp))
240                         return 0;
241 +#ifdef SUPPORT_FILEFLAGS
242 +               if (preserve_fileflags && sxp->st.st_flags != F_FFLAGS(file))
243 +                       return 0;
244 +#endif
245                 if (ownership_differs(file, sxp))
246                         return 0;
247  #ifdef SUPPORT_ACLS
248 @@ -515,6 +521,11 @@ void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statre
249                 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
250                     && sxp->st.st_gid != (gid_t)F_GROUP(file))
251                         iflags |= ITEM_REPORT_GROUP;
252 +#ifdef SUPPORT_FILEFLAGS
253 +               if (preserve_fileflags && !S_ISLNK(file->mode)
254 +                && sxp->st.st_flags != F_FFLAGS(file))
255 +                       iflags |= ITEM_REPORT_FFLAGS;
256 +#endif
257  #ifdef SUPPORT_ACLS
258                 if (preserve_acls && !S_ISLNK(file->mode)) {
259                         if (!ACL_READY(*sxp))
260 @@ -1392,6 +1403,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
261                         file->mode = dest_mode(file->mode, sx.st.st_mode,
262                                                dflt_perms, statret == 0);
263                 }
264 +#ifdef SUPPORT_FORCE_CHANGE
265 +               if (force_change && !preserve_fileflags)
266 +                       F_FFLAGS(file) = sx.st.st_flags;
267 +#endif
268                 if (statret != 0 && basis_dir[0] != NULL) {
269                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
270                                               itemizing, code);
271 @@ -1436,10 +1451,15 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
272                  * readable and writable permissions during the time we are
273                  * putting files within them.  This is then restored to the
274                  * former permissions after the transfer is done. */
275 +#ifdef SUPPORT_FORCE_CHANGE
276 +               if (force_change && F_FFLAGS(file) & force_change
277 +                && make_mutable(fname, file->mode, F_FFLAGS(file), force_change))
278 +                       need_retouch_dir_perms = 1;
279 +#endif
280  #ifdef HAVE_CHMOD
281                 if (!am_root && (file->mode & S_IRWXU) != S_IRWXU && dir_tweaking) {
282                         mode_t mode = file->mode | S_IRWXU;
283 -                       if (do_chmod(fname, mode) < 0) {
284 +                       if (do_chmod(fname, mode, 0) < 0) {
285                                 rsyserr(FERROR_XFER, errno,
286                                         "failed to modify permissions on %s",
287                                         full_fname(fname));
288 @@ -1474,6 +1494,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
289                 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
290                                        exists);
291         }
292 +#ifdef SUPPORT_FORCE_CHANGE
293 +       if (force_change && !preserve_fileflags)
294 +               F_FFLAGS(file) = sx.st.st_flags;
295 +#endif
296  
297  #ifdef SUPPORT_HARD_LINKS
298         if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
299 @@ -2043,13 +2067,17 @@ static void touch_up_dirs(struct file_list *flist, int ndx)
300                         continue;
301                 fname = f_name(file, NULL);
302                 if (fix_dir_perms)
303 -                       do_chmod(fname, file->mode);
304 +                       do_chmod(fname, file->mode, 0);
305                 if (need_retouch_dir_times) {
306                         STRUCT_STAT st;
307                         if (link_stat(fname, &st, 0) == 0
308                          && cmp_time(st.st_mtime, file->modtime) != 0)
309 -                               set_modtime(fname, file->modtime, F_MOD_NSEC(file), file->mode);
310 +                               set_modtime(fname, file->modtime, F_MOD_NSEC(file), file->mode, 0);
311                 }
312 +#ifdef SUPPORT_FORCE_CHANGE
313 +               if (force_change && F_FFLAGS(file) & force_change)
314 +                       undo_make_mutable(fname, F_FFLAGS(file));
315 +#endif
316                 if (counter >= loopchk_limit) {
317                         if (allowed_lull)
318                                 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
319 diff --git a/log.c b/log.c
320 --- a/log.c
321 +++ b/log.c
322 @@ -720,7 +720,7 @@ static void log_formatted(enum logcode code, const char *format, const char *op,
323                         c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
324                         c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
325                         c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
326 -                       c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
327 +                       c[8] = !(iflags & ITEM_REPORT_FFLAGS) ? '.' : 'f';
328                         c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
329                         c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
330                         c[11] = '\0';
331 diff --git a/main.c b/main.c
332 --- a/main.c
333 +++ b/main.c
334 @@ -26,6 +26,9 @@
335  #if defined CONFIG_LOCALE && defined HAVE_LOCALE_H
336  #include <locale.h>
337  #endif
338 +#ifdef SUPPORT_FORCE_CHANGE
339 +#include <sys/sysctl.h>
340 +#endif
341  
342  extern int dry_run;
343  extern int list_only;
344 @@ -51,6 +54,7 @@ extern int copy_unsafe_links;
345  extern int keep_dirlinks;
346  extern int preserve_hard_links;
347  extern int protocol_version;
348 +extern int force_change;
349  extern int file_total;
350  extern int recurse;
351  extern int xfer_dirs;
352 @@ -839,6 +843,22 @@ static int do_recv(int f_in, int f_out, char *local_name)
353          * points to an identical file won't be replaced by the referent. */
354         copy_links = copy_dirlinks = copy_unsafe_links = 0;
355  
356 +#ifdef SUPPORT_FORCE_CHANGE
357 +       if (force_change & SYS_IMMUTABLE) {
358 +               /* Determine whether we'll be able to unlock a system immutable item. */
359 +               int mib[2];
360 +               int securityLevel = 0;
361 +               size_t len = sizeof securityLevel;
362 +
363 +               mib[0] = CTL_KERN;
364 +               mib[1] = KERN_SECURELVL;
365 +               if (sysctl(mib, 2, &securityLevel, &len, NULL, 0) == 0 && securityLevel > 0) {
366 +                       rprintf(FERROR, "System security level is too high to force mutability on system immutable files and directories.\n");
367 +                       exit_cleanup(RERR_UNSUPPORTED);
368 +               }
369 +       }
370 +#endif
371 +
372  #ifdef SUPPORT_HARD_LINKS
373         if (preserve_hard_links && !inc_recurse)
374                 match_hard_links(first_flist);
375 diff --git a/options.c b/options.c
376 --- a/options.c
377 +++ b/options.c
378 @@ -55,6 +55,7 @@ int preserve_hard_links = 0;
379  int preserve_acls = 0;
380  int preserve_xattrs = 0;
381  int preserve_perms = 0;
382 +int preserve_fileflags = 0;
383  int preserve_executability = 0;
384  int preserve_devices = 0;
385  int preserve_specials = 0;
386 @@ -89,6 +90,7 @@ int numeric_ids = 0;
387  int msgs2stderr = 0;
388  int allow_8bit_chars = 0;
389  int force_delete = 0;
390 +int force_change = 0;
391  int io_timeout = 0;
392  int prune_empty_dirs = 0;
393  int use_qsort = 0;
394 @@ -574,6 +576,7 @@ static void print_rsync_version(enum logcode f)
395         char const *links = "no ";
396         char const *iconv = "no ";
397         char const *ipv6 = "no ";
398 +       char const *fileflags = "no ";
399         STRUCT_STAT *dumstat;
400  
401  #if SUBPROTOCOL_VERSION != 0
402 @@ -610,6 +613,9 @@ static void print_rsync_version(enum logcode f)
403  #ifdef CAN_SET_SYMLINK_TIMES
404         symtimes = "";
405  #endif
406 +#ifdef SUPPORT_FILEFLAGS
407 +       fileflags = "";
408 +#endif
409  
410         rprintf(f, "%s  version %s  protocol version %d%s\n",
411                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
412 @@ -623,8 +629,8 @@ static void print_rsync_version(enum logcode f)
413                 (int)(sizeof (int64) * 8));
414         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
415                 got_socketpair, hardlinks, links, ipv6, have_inplace);
416 -       rprintf(f, "    %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc\n",
417 -               have_inplace, acls, xattrs, iconv, symtimes, prealloc);
418 +       rprintf(f, "    %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc, %sfile-flags\n",
419 +               have_inplace, acls, xattrs, iconv, symtimes, prealloc, fileflags);
420  
421  #ifdef MAINTAINER_MODE
422         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
423 @@ -695,6 +701,9 @@ void usage(enum logcode F)
424    rprintf(F," -K, --keep-dirlinks         treat symlinked dir on receiver as dir\n");
425    rprintf(F," -H, --hard-links            preserve hard links\n");
426    rprintf(F," -p, --perms                 preserve permissions\n");
427 +#ifdef SUPPORT_FILEFLAGS
428 +  rprintf(F,"     --fileflags             preserve file-flags (aka chflags)\n");
429 +#endif
430    rprintf(F," -E, --executability         preserve the file's executability\n");
431    rprintf(F,"     --chmod=CHMOD           affect file and/or directory permissions\n");
432  #ifdef SUPPORT_ACLS
433 @@ -740,7 +749,12 @@ void usage(enum logcode F)
434    rprintf(F,"     --ignore-missing-args   ignore missing source args without error\n");
435    rprintf(F,"     --delete-missing-args   delete missing source args from destination\n");
436    rprintf(F,"     --ignore-errors         delete even if there are I/O errors\n");
437 -  rprintf(F,"     --force                 force deletion of directories even if not empty\n");
438 +  rprintf(F,"     --force-delete          force deletion of directories even if not empty\n");
439 +#ifdef SUPPORT_FORCE_CHANGE
440 +  rprintf(F,"     --force-change          affect user-/system-immutable files/dirs\n");
441 +  rprintf(F,"     --force-uchange         affect user-immutable files/dirs\n");
442 +  rprintf(F,"     --force-schange         affect system-immutable files/dirs\n");
443 +#endif
444    rprintf(F,"     --max-delete=NUM        don't delete more than NUM files\n");
445    rprintf(F,"     --max-size=SIZE         don't transfer any file larger than SIZE\n");
446    rprintf(F,"     --min-size=SIZE         don't transfer any file smaller than SIZE\n");
447 @@ -857,6 +871,10 @@ static struct poptOption long_options[] = {
448    {"perms",           'p', POPT_ARG_VAL,    &preserve_perms, 1, 0, 0 },
449    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
450    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
451 +#ifdef SUPPORT_FILEFLAGS
452 +  {"fileflags",        0,  POPT_ARG_VAL,    &preserve_fileflags, 1, 0, 0 },
453 +  {"no-fileflags",     0,  POPT_ARG_VAL,    &preserve_fileflags, 0, 0, 0 },
454 +#endif
455    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
456    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
457    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
458 @@ -943,6 +961,14 @@ static struct poptOption long_options[] = {
459    {"remove-source-files",0,POPT_ARG_VAL,    &remove_source_files, 1, 0, 0 },
460    {"force",            0,  POPT_ARG_VAL,    &force_delete, 1, 0, 0 },
461    {"no-force",         0,  POPT_ARG_VAL,    &force_delete, 0, 0, 0 },
462 +  {"force-delete",     0,  POPT_ARG_VAL,    &force_delete, 1, 0, 0 },
463 +  {"no-force-delete",  0,  POPT_ARG_VAL,    &force_delete, 0, 0, 0 },
464 +#ifdef SUPPORT_FORCE_CHANGE
465 +  {"force-change",     0,  POPT_ARG_VAL,    &force_change, ALL_IMMUTABLE, 0, 0 },
466 +  {"no-force-change",  0,  POPT_ARG_VAL,    &force_change, 0, 0, 0 },
467 +  {"force-uchange",    0,  POPT_ARG_VAL,    &force_change, USR_IMMUTABLE, 0, 0 },
468 +  {"force-schange",    0,  POPT_ARG_VAL,    &force_change, SYS_IMMUTABLE, 0, 0 },
469 +#endif
470    {"ignore-errors",    0,  POPT_ARG_VAL,    &ignore_errors, 1, 0, 0 },
471    {"no-ignore-errors", 0,  POPT_ARG_VAL,    &ignore_errors, 0, 0, 0 },
472    {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
473 @@ -2536,6 +2562,9 @@ void server_options(char **args, int *argc_p)
474         if (xfer_dirs && !recurse && delete_mode && am_sender)
475                 args[ac++] = "--no-r";
476  
477 +       if (preserve_fileflags)
478 +               args[ac++] = "--fileflags";
479 +
480         if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
481                 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
482                         goto oom;
483 @@ -2623,6 +2652,16 @@ void server_options(char **args, int *argc_p)
484                         args[ac++] = "--delete-excluded";
485                 if (force_delete)
486                         args[ac++] = "--force";
487 +#ifdef SUPPORT_FORCE_CHANGE
488 +               if (force_change) {
489 +                       if (force_change == ALL_IMMUTABLE)
490 +                               args[ac++] = "--force-change";
491 +                       else if (force_change == USR_IMMUTABLE)
492 +                               args[ac++] = "--force-uchange";
493 +                       else if (force_change == SYS_IMMUTABLE)
494 +                               args[ac++] = "--force-schange";
495 +               }
496 +#endif
497                 if (write_batch < 0)
498                         args[ac++] = "--only-write-batch=X";
499                 if (am_root > 1)
500 diff --git a/rsync.c b/rsync.c
501 --- a/rsync.c
502 +++ b/rsync.c
503 @@ -31,6 +31,7 @@ extern int dry_run;
504  extern int preserve_acls;
505  extern int preserve_xattrs;
506  extern int preserve_perms;
507 +extern int preserve_fileflags;
508  extern int preserve_executability;
509  extern int preserve_times;
510  extern int am_root;
511 @@ -452,6 +453,39 @@ mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
512         return new_mode;
513  }
514  
515 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
516 +/* Set a file's st_flags. */
517 +static int set_fileflags(const char *fname, uint32 fileflags)
518 +{
519 +       if (do_chflags(fname, fileflags) != 0) {
520 +               rsyserr(FERROR_XFER, errno,
521 +                       "failed to set file flags on %s",
522 +                       full_fname(fname));
523 +               return 0;
524 +       }
525 +
526 +       return 1;
527 +}
528 +
529 +/* Remove immutable flags from an object, so it can be altered/removed. */
530 +int make_mutable(const char *fname, mode_t mode, uint32 fileflags, uint32 iflags)
531 +{
532 +       if (S_ISLNK(mode) || !(fileflags & iflags))
533 +               return 0;
534 +       if (!set_fileflags(fname, fileflags & ~iflags))
535 +               return -1;
536 +       return 1;
537 +}
538 +
539 +/* Undo a prior make_mutable() call that returned a 1. */
540 +int undo_make_mutable(const char *fname, uint32 fileflags)
541 +{
542 +       if (!set_fileflags(fname, fileflags))
543 +               return -1;
544 +       return 1;
545 +}
546 +#endif
547 +
548  int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
549                    const char *fnamecmp, int flags)
550  {
551 @@ -513,7 +547,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
552                 if (am_root >= 0) {
553                         uid_t uid = change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid;
554                         gid_t gid = change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid;
555 -                       if (do_lchown(fname, uid, gid) != 0) {
556 +                       if (do_lchown(fname, uid, gid, sxp->st.st_mode, ST_FLAGS(sxp->st)) != 0) {
557                                 /* We shouldn't have attempted to change uid
558                                  * or gid unless have the privilege. */
559                                 rsyserr(FERROR_XFER, errno, "%s %s failed",
560 @@ -549,7 +583,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
561                 flags |= ATTRS_SKIP_MTIME;
562         if (!(flags & ATTRS_SKIP_MTIME)
563             && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
564 -               int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode);
565 +               int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode, ST_FLAGS(sxp->st));
566                 if (ret < 0) {
567                         rsyserr(FERROR_XFER, errno, "failed to set times on %s",
568                                 full_fname(fname));
569 @@ -576,7 +610,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 @@ -588,6 +622,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 @@ -662,7 +709,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 ? 0 : ATTRS_SKIP_MTIME);
603 +                      ATTRS_DELAY_IMMUTABLE
604 +                      | (ok_to_set_time ? 0 : ATTRS_SKIP_MTIME));
605  
606         /* move tmp file over real file */
607         if (DEBUG_GTE(RECV, 1))
608 @@ -679,6 +727,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 @@ -62,6 +62,7 @@
623  #define XMIT_HLINK_FIRST (1<<12)       /* protocols 30 - now (HLINKED files only) */
624  #define XMIT_IO_ERROR_ENDLIST (1<<12)  /* protocols 31*- now (w/XMIT_EXTENDED_FLAGS) (also protocol 30 w/'f' compat flag) */
625  #define XMIT_MOD_NSEC (1<<13)          /* protocols 31 - now */
626 +#define XMIT_SAME_FLAGS (1<<14)                /* protocols ?? - now */
627  
628  /* These flags are used in the live flist data. */
629  
630 @@ -165,6 +166,7 @@
631  
632  #define ATTRS_REPORT           (1<<0)
633  #define ATTRS_SKIP_MTIME       (1<<1)
634 +#define ATTRS_DELAY_IMMUTABLE  (1<<2)
635  
636  #define FULL_FLUSH     1
637  #define NORMAL_FLUSH   0
638 @@ -191,6 +193,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 @@ -522,6 +525,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 @@ -709,6 +734,7 @@ extern int file_extra_cnt;
676  extern int inc_recurse;
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 @@ -750,6 +776,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 @@ -359,6 +359,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 @@ -394,7 +395,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 @@ -641,7 +645,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 @@ -940,7 +945,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 @@ -1103,6 +1108,29 @@ Note that this option does not copy rsyncs special xattr values (e.g. those
738  used by bf(--fake-super)) unless you repeat the option (e.g. -XX).  This
739  "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 @@ -1432,12 +1460,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 @@ -2071,7 +2100,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 @@ -2127,7 +2156,7 @@ quote(itemization(
794    sender's value (requires bf(--owner) and super-user privileges).
795    it() A bf(g) means the group is different and is being updated to the
796    sender's value (requires bf(--group) and the authority to set the group).
797 -  it() The bf(u) slot is reserved for future use.
798 +  it() The bf(f) means that the fileflags information changed.
799    it() The bf(a) means that the ACL information changed.
800    it() The bf(x) means that the extended attribute information changed.
801  ))
802 diff --git a/syscall.c b/syscall.c
803 --- a/syscall.c
804 +++ b/syscall.c
805 @@ -38,6 +38,7 @@ extern int am_root;
806  extern int am_sender;
807  extern int read_only;
808  extern int list_only;
809 +extern int force_change;
810  extern int preserve_perms;
811  extern int preserve_executability;
812  
813 @@ -55,7 +56,23 @@ int do_unlink(const char *fname)
814  {
815         if (dry_run) return 0;
816         RETURN_ERROR_IF_RO_OR_LO;
817 -       return unlink(fname);
818 +       if (unlink(fname) == 0)
819 +               return 0;
820 +#ifdef SUPPORT_FORCE_CHANGE
821 +       if (force_change && errno == EPERM) {
822 +               STRUCT_STAT st;
823 +
824 +               if (x_lstat(fname, &st, NULL) == 0
825 +                && make_mutable(fname, st.st_mode, st.st_flags, force_change) > 0) {
826 +                       if (unlink(fname) == 0)
827 +                               return 0;
828 +                       undo_make_mutable(fname, st.st_flags);
829 +               }
830 +               /* TODO: handle immutable directories */
831 +               errno = EPERM;
832 +       }
833 +#endif
834 +       return -1;
835  }
836  
837  #ifdef SUPPORT_LINKS
838 @@ -116,14 +133,37 @@ int do_link(const char *fname1, const char *fname2)
839  }
840  #endif
841  
842 -int do_lchown(const char *path, uid_t owner, gid_t group)
843 +int do_lchown(const char *path, uid_t owner, gid_t group, mode_t mode, uint32 fileflags)
844  {
845         if (dry_run) return 0;
846         RETURN_ERROR_IF_RO_OR_LO;
847  #ifndef HAVE_LCHOWN
848  #define lchown chown
849  #endif
850 -       return lchown(path, owner, group);
851 +       if (lchown(path, owner, group) == 0)
852 +               return 0;
853 +#ifdef SUPPORT_FORCE_CHANGE
854 +       if (force_change && errno == EPERM) {
855 +               if (fileflags == NO_FFLAGS) {
856 +                       STRUCT_STAT st;
857 +                       if (x_lstat(path, &st, NULL) == 0) {
858 +                               mode = st.st_mode;
859 +                               fileflags = st.st_flags;
860 +                       }
861 +               }
862 +               if (fileflags != NO_FFLAGS
863 +                && make_mutable(path, mode, fileflags, force_change) > 0) {
864 +                       int ret = lchown(path, owner, group);
865 +                       undo_make_mutable(path, fileflags);
866 +                       if (ret == 0)
867 +                               return 0;
868 +               }
869 +               errno = EPERM;
870 +       }
871 +#else
872 +       mode = fileflags = 0; /* avoid compiler warning */
873 +#endif
874 +       return -1;
875  }
876  
877  int do_mknod(const char *pathname, mode_t mode, dev_t dev)
878 @@ -163,7 +203,7 @@ int do_mknod(const char *pathname, mode_t mode, dev_t dev)
879                         return -1;
880                 close(sock);
881  #ifdef HAVE_CHMOD
882 -               return do_chmod(pathname, mode);
883 +               return do_chmod(pathname, mode, 0);
884  #else
885                 return 0;
886  #endif
887 @@ -180,7 +220,22 @@ int do_rmdir(const char *pathname)
888  {
889         if (dry_run) return 0;
890         RETURN_ERROR_IF_RO_OR_LO;
891 -       return rmdir(pathname);
892 +       if (rmdir(pathname) == 0)
893 +               return 0;
894 +#ifdef SUPPORT_FORCE_CHANGE
895 +       if (force_change && errno == EPERM) {
896 +               STRUCT_STAT st;
897 +
898 +               if (x_lstat(pathname, &st, NULL) == 0
899 +                && make_mutable(pathname, st.st_mode, st.st_flags, force_change) > 0) {
900 +                       if (rmdir(pathname) == 0)
901 +                               return 0;
902 +                       undo_make_mutable(pathname, st.st_flags);
903 +               }
904 +               errno = EPERM;
905 +       }
906 +#endif
907 +       return -1;
908  }
909  
910  int do_open(const char *pathname, int flags, mode_t mode)
911 @@ -194,7 +249,7 @@ int do_open(const char *pathname, int flags, mode_t mode)
912  }
913  
914  #ifdef HAVE_CHMOD
915 -int do_chmod(const char *path, mode_t mode)
916 +int do_chmod(const char *path, mode_t mode, uint32 fileflags)
917  {
918         int code;
919         if (dry_run) return 0;
920 @@ -217,17 +272,74 @@ int do_chmod(const char *path, mode_t mode)
921         } else
922                 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
923  #endif /* !HAVE_LCHMOD */
924 +#ifdef SUPPORT_FORCE_CHANGE
925 +       if (code < 0 && force_change && errno == EPERM && !S_ISLNK(mode)) {
926 +               if (fileflags == NO_FFLAGS) {
927 +                       STRUCT_STAT st;
928 +                       if (x_lstat(path, &st, NULL) == 0)
929 +                               fileflags = st.st_flags;
930 +               }
931 +               if (fileflags != NO_FFLAGS
932 +                && make_mutable(path, mode, fileflags, force_change) > 0) {
933 +                       code = chmod(path, mode & CHMOD_BITS);
934 +                       undo_make_mutable(path, fileflags);
935 +                       if (code == 0)
936 +                               return 0;
937 +               }
938 +               errno = EPERM;
939 +       }
940 +#else
941 +       fileflags = 0; /* avoid compiler warning */
942 +#endif
943         if (code != 0 && (preserve_perms || preserve_executability))
944                 return code;
945         return 0;
946  }
947  #endif
948  
949 +#ifdef HAVE_CHFLAGS
950 +int do_chflags(const char *path, uint32 fileflags)
951 +{
952 +       if (dry_run) return 0;
953 +       RETURN_ERROR_IF_RO_OR_LO;
954 +       return chflags(path, fileflags);
955 +}
956 +#endif
957 +
958  int do_rename(const char *fname1, const char *fname2)
959  {
960         if (dry_run) return 0;
961         RETURN_ERROR_IF_RO_OR_LO;
962 -       return rename(fname1, fname2);
963 +       if (rename(fname1, fname2) == 0)
964 +               return 0;
965 +#ifdef SUPPORT_FORCE_CHANGE
966 +       if (force_change && errno == EPERM) {
967 +               STRUCT_STAT st1, st2;
968 +               int became_mutable;
969 +
970 +               if (x_lstat(fname1, &st1, NULL) != 0)
971 +                       goto failed;
972 +               became_mutable = make_mutable(fname1, st1.st_mode, st1.st_flags, force_change) > 0;
973 +               if (became_mutable && rename(fname1, fname2) == 0)
974 +                       goto success;
975 +               if (x_lstat(fname2, &st2, NULL) == 0
976 +                && make_mutable(fname2, st2.st_mode, st2.st_flags, force_change) > 0) {
977 +                       if (rename(fname1, fname2) == 0) {
978 +                         success:
979 +                               if (became_mutable) /* Yes, use fname2 and st1! */
980 +                                       undo_make_mutable(fname2, st1.st_flags);
981 +                               return 0;
982 +                       }
983 +                       undo_make_mutable(fname2, st2.st_flags);
984 +               }
985 +               /* TODO: handle immutable directories */
986 +               if (became_mutable)
987 +                       undo_make_mutable(fname1, st1.st_flags);
988 +         failed:
989 +               errno = EPERM;
990 +       }
991 +#endif
992 +       return -1;
993  }
994  
995  #ifdef HAVE_FTRUNCATE
996 diff --git a/t_stub.c b/t_stub.c
997 --- a/t_stub.c
998 +++ b/t_stub.c
999 @@ -28,6 +28,7 @@ int module_id = -1;
1000  int checksum_len = 0;
1001  int relative_paths = 0;
1002  int module_dirlen = 0;
1003 +int force_change = 0;
1004  int preserve_acls = 0;
1005  int preserve_times = 0;
1006  int preserve_xattrs = 0;
1007 @@ -97,3 +98,23 @@ filter_rule_list daemon_filter_list;
1008  {
1009         return "tester";
1010  }
1011 +
1012 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
1013 + int make_mutable(UNUSED(const char *fname), UNUSED(mode_t mode), UNUSED(uint32 fileflags), UNUSED(uint32 iflags))
1014 +{
1015 +       return 0;
1016 +}
1017 +
1018 +/* Undo a prior make_mutable() call that returned a 1. */
1019 + int undo_make_mutable(UNUSED(const char *fname), UNUSED(uint32 fileflags))
1020 +{
1021 +       return 0;
1022 +}
1023 +#endif
1024 +
1025 +#ifdef SUPPORT_XATTRS
1026 + int x_lstat(UNUSED(const char *fname), UNUSED(STRUCT_STAT *fst), UNUSED(STRUCT_STAT *xst))
1027 +{
1028 +       return -1;
1029 +}
1030 +#endif
1031 diff --git a/util.c b/util.c
1032 --- a/util.c
1033 +++ b/util.c
1034 @@ -33,6 +33,7 @@ extern int relative_paths;
1035  extern int preserve_times;
1036  extern int preserve_xattrs;
1037  extern int preallocate_files;
1038 +extern int force_change;
1039  extern char *module_dir;
1040  extern unsigned int module_dirlen;
1041  extern char *partial_dir;
1042 @@ -115,9 +116,36 @@ void print_child_argv(const char *prefix, char **cmd)
1043         rprintf(FCLIENT, " (%d args)\n", cnt);
1044  }
1045  
1046 +#ifdef SUPPORT_FORCE_CHANGE
1047 +static int try_a_force_change(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode, uint32 fileflags)
1048 +{
1049 +       if (fileflags == NO_FFLAGS) {
1050 +               STRUCT_STAT st;
1051 +               if (x_lstat(fname, &st, NULL) == 0)
1052 +                       fileflags = st.st_flags;
1053 +       }
1054 +
1055 +       if (fileflags != NO_FFLAGS && make_mutable(fname, mode, fileflags, force_change) > 0) {
1056 +               int ret, save_force_change = force_change;
1057 +
1058 +               force_change = 0; /* Make certain we can't come back here. */
1059 +               ret = set_modtime(fname, modtime, mod_nsec, mode, fileflags);
1060 +               force_change = save_force_change;
1061 +
1062 +               undo_make_mutable(fname, fileflags);
1063 +
1064 +               return ret;
1065 +       }
1066 +
1067 +       errno = EPERM;
1068 +
1069 +       return -1;
1070 +}
1071 +#endif
1072 +
1073  /* This returns 0 for success, 1 for a symlink if symlink time-setting
1074   * is not possible, or -1 for any other error. */
1075 -int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1076 +int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode, uint32 fileflags)
1077  {
1078         static int switch_step = 0;
1079  
1080 @@ -132,6 +160,11 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1081  #include "case_N.h"
1082                 if (do_utimensat(fname, modtime, mod_nsec) == 0)
1083                         break;
1084 +#ifdef SUPPORT_FORCE_CHANGE
1085 +               if (force_change && errno == EPERM
1086 +                && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1087 +                       break;
1088 +#endif
1089                 if (errno != ENOSYS)
1090                         return -1;
1091                 switch_step++;
1092 @@ -142,6 +175,11 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1093  #include "case_N.h"
1094                 if (do_lutimes(fname, modtime, mod_nsec) == 0)
1095                         break;
1096 +#ifdef SUPPORT_FORCE_CHANGE
1097 +               if (force_change && errno == EPERM
1098 +                && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1099 +                       break;
1100 +#endif
1101                 if (errno != ENOSYS)
1102                         return -1;
1103                 switch_step++;
1104 @@ -165,6 +203,13 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1105                 if (do_utime(fname, modtime, mod_nsec) == 0)
1106                         break;
1107  #endif
1108 +#ifdef SUPPORT_FORCE_CHANGE
1109 +               if (force_change && errno == EPERM
1110 +                && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1111 +                       break;
1112 +#else
1113 +               fileflags = 0; /* avoid compiler warning */
1114 +#endif
1115  
1116                 return -1;
1117         }
1118 diff --git a/xattrs.c b/xattrs.c
1119 --- a/xattrs.c
1120 +++ b/xattrs.c
1121 @@ -1045,7 +1045,7 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1122         mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1123              | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1124         if (fst.st_mode != mode)
1125 -               do_chmod(fname, mode);
1126 +               do_chmod(fname, mode, ST_FLAGS(fst));
1127         if (!IS_DEVICE(fst.st_mode))
1128                 fst.st_rdev = 0; /* just in case */
1129