Lets use varchar(128) instead of 256.
[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: 1e9ee19a716b72454dfeab663802c626b81cdf2e
12 diff --git a/Makefile.in b/Makefile.in
13 --- a/Makefile.in
14 +++ b/Makefile.in
15 @@ -43,7 +43,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 @@ -111,7 +111,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 @@ -596,6 +596,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 @@ -776,6 +795,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 @@ -817,6 +840,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 @@ -968,6 +995,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 @@ -1365,6 +1396,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 @@ -461,6 +463,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 @@ -512,6 +518,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 @@ -1383,6 +1394,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 @@ -1427,10 +1442,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 @@ -1465,6 +1485,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 @@ -2029,13 +2053,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 @@ -729,7 +729,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 @@ -839,6 +842,22 @@ static int do_recv(int f_in, int f_out, char *local_name)
345          * points to an identical file won't be replaced by the referent. */
346         copy_links = copy_dirlinks = copy_unsafe_links = 0;
347  
348 +#ifdef SUPPORT_FORCE_CHANGE
349 +       if (force_change & SYS_IMMUTABLE) {
350 +               /* Determine whether we'll be able to unlock a system immutable item. */
351 +               int mib[2];
352 +               int securityLevel = 0;
353 +               size_t len = sizeof securityLevel;
354 +
355 +               mib[0] = CTL_KERN;
356 +               mib[1] = KERN_SECURELVL;
357 +               if (sysctl(mib, 2, &securityLevel, &len, NULL, 0) == 0 && securityLevel > 0) {
358 +                       rprintf(FERROR, "System security level is too high to force mutability on system immutable files and directories.\n");
359 +                       exit_cleanup(RERR_UNSUPPORTED);
360 +               }
361 +       }
362 +#endif
363 +
364  #ifdef SUPPORT_HARD_LINKS
365         if (preserve_hard_links && !inc_recurse)
366                 match_hard_links(first_flist);
367 diff --git a/options.c b/options.c
368 --- a/options.c
369 +++ b/options.c
370 @@ -53,6 +53,7 @@ int preserve_hard_links = 0;
371  int preserve_acls = 0;
372  int preserve_xattrs = 0;
373  int preserve_perms = 0;
374 +int preserve_fileflags = 0;
375  int preserve_executability = 0;
376  int preserve_devices = 0;
377  int preserve_specials = 0;
378 @@ -87,6 +88,7 @@ int numeric_ids = 0;
379  int msgs2stderr = 0;
380  int allow_8bit_chars = 0;
381  int force_delete = 0;
382 +int force_change = 0;
383  int io_timeout = 0;
384  int prune_empty_dirs = 0;
385  int use_qsort = 0;
386 @@ -573,6 +575,7 @@ static void print_rsync_version(enum logcode f)
387         char const *links = "no ";
388         char const *iconv = "no ";
389         char const *ipv6 = "no ";
390 +       char const *fileflags = "no ";
391         STRUCT_STAT *dumstat;
392  
393  #if SUBPROTOCOL_VERSION != 0
394 @@ -609,6 +612,9 @@ static void print_rsync_version(enum logcode f)
395  #ifdef CAN_SET_SYMLINK_TIMES
396         symtimes = "";
397  #endif
398 +#ifdef SUPPORT_FILEFLAGS
399 +       fileflags = "";
400 +#endif
401  
402         rprintf(f, "%s  version %s  protocol version %d%s\n",
403                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
404 @@ -622,8 +628,8 @@ static void print_rsync_version(enum logcode f)
405                 (int)(sizeof (int64) * 8));
406         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
407                 got_socketpair, hardlinks, links, ipv6, have_inplace);
408 -       rprintf(f, "    %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc\n",
409 -               have_inplace, acls, xattrs, iconv, symtimes, prealloc);
410 +       rprintf(f, "    %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc, %sfile-flags\n",
411 +               have_inplace, acls, xattrs, iconv, symtimes, prealloc, fileflags);
412  
413  #ifdef MAINTAINER_MODE
414         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
415 @@ -694,6 +700,9 @@ void usage(enum logcode F)
416    rprintf(F," -K, --keep-dirlinks         treat symlinked dir on receiver as dir\n");
417    rprintf(F," -H, --hard-links            preserve hard links\n");
418    rprintf(F," -p, --perms                 preserve permissions\n");
419 +#ifdef SUPPORT_FILEFLAGS
420 +  rprintf(F,"     --fileflags             preserve file-flags (aka chflags)\n");
421 +#endif
422    rprintf(F," -E, --executability         preserve the file's executability\n");
423    rprintf(F,"     --chmod=CHMOD           affect file and/or directory permissions\n");
424  #ifdef SUPPORT_ACLS
425 @@ -739,7 +748,12 @@ void usage(enum logcode F)
426    rprintf(F,"     --ignore-missing-args   ignore missing source args without error\n");
427    rprintf(F,"     --delete-missing-args   delete missing source args from destination\n");
428    rprintf(F,"     --ignore-errors         delete even if there are I/O errors\n");
429 -  rprintf(F,"     --force                 force deletion of directories even if not empty\n");
430 +  rprintf(F,"     --force-delete          force deletion of directories even if not empty\n");
431 +#ifdef SUPPORT_FORCE_CHANGE
432 +  rprintf(F,"     --force-change          affect user-/system-immutable files/dirs\n");
433 +  rprintf(F,"     --force-uchange         affect user-immutable files/dirs\n");
434 +  rprintf(F,"     --force-schange         affect system-immutable files/dirs\n");
435 +#endif
436    rprintf(F,"     --max-delete=NUM        don't delete more than NUM files\n");
437    rprintf(F,"     --max-size=SIZE         don't transfer any file larger than SIZE\n");
438    rprintf(F,"     --min-size=SIZE         don't transfer any file smaller than SIZE\n");
439 @@ -855,6 +869,10 @@ static struct poptOption long_options[] = {
440    {"perms",           'p', POPT_ARG_VAL,    &preserve_perms, 1, 0, 0 },
441    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
442    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
443 +#ifdef SUPPORT_FILEFLAGS
444 +  {"fileflags",        0,  POPT_ARG_VAL,    &preserve_fileflags, 1, 0, 0 },
445 +  {"no-fileflags",     0,  POPT_ARG_VAL,    &preserve_fileflags, 0, 0, 0 },
446 +#endif
447    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
448    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
449    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
450 @@ -941,6 +959,14 @@ static struct poptOption long_options[] = {
451    {"remove-source-files",0,POPT_ARG_VAL,    &remove_source_files, 1, 0, 0 },
452    {"force",            0,  POPT_ARG_VAL,    &force_delete, 1, 0, 0 },
453    {"no-force",         0,  POPT_ARG_VAL,    &force_delete, 0, 0, 0 },
454 +  {"force-delete",     0,  POPT_ARG_VAL,    &force_delete, 1, 0, 0 },
455 +  {"no-force-delete",  0,  POPT_ARG_VAL,    &force_delete, 0, 0, 0 },
456 +#ifdef SUPPORT_FORCE_CHANGE
457 +  {"force-change",     0,  POPT_ARG_VAL,    &force_change, ALL_IMMUTABLE, 0, 0 },
458 +  {"no-force-change",  0,  POPT_ARG_VAL,    &force_change, 0, 0, 0 },
459 +  {"force-uchange",    0,  POPT_ARG_VAL,    &force_change, USR_IMMUTABLE, 0, 0 },
460 +  {"force-schange",    0,  POPT_ARG_VAL,    &force_change, SYS_IMMUTABLE, 0, 0 },
461 +#endif
462    {"ignore-errors",    0,  POPT_ARG_VAL,    &ignore_errors, 1, 0, 0 },
463    {"no-ignore-errors", 0,  POPT_ARG_VAL,    &ignore_errors, 0, 0, 0 },
464    {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
465 @@ -2522,6 +2548,9 @@ void server_options(char **args, int *argc_p)
466         if (xfer_dirs && !recurse && delete_mode && am_sender)
467                 args[ac++] = "--no-r";
468  
469 +       if (preserve_fileflags)
470 +               args[ac++] = "--fileflags";
471 +
472         if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
473                 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
474                         goto oom;
475 @@ -2609,6 +2638,16 @@ void server_options(char **args, int *argc_p)
476                         args[ac++] = "--delete-excluded";
477                 if (force_delete)
478                         args[ac++] = "--force";
479 +#ifdef SUPPORT_FORCE_CHANGE
480 +               if (force_change) {
481 +                       if (force_change == ALL_IMMUTABLE)
482 +                               args[ac++] = "--force-change";
483 +                       else if (force_change == USR_IMMUTABLE)
484 +                               args[ac++] = "--force-uchange";
485 +                       else if (force_change == SYS_IMMUTABLE)
486 +                               args[ac++] = "--force-schange";
487 +               }
488 +#endif
489                 if (write_batch < 0)
490                         args[ac++] = "--only-write-batch=X";
491                 if (am_root > 1)
492 diff --git a/rsync.c b/rsync.c
493 --- a/rsync.c
494 +++ b/rsync.c
495 @@ -31,6 +31,7 @@ extern int dry_run;
496  extern int preserve_acls;
497  extern int preserve_xattrs;
498  extern int preserve_perms;
499 +extern int preserve_fileflags;
500  extern int preserve_executability;
501  extern int preserve_times;
502  extern int am_root;
503 @@ -452,6 +453,39 @@ mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
504         return new_mode;
505  }
506  
507 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
508 +/* Set a file's st_flags. */
509 +static int set_fileflags(const char *fname, uint32 fileflags)
510 +{
511 +       if (do_chflags(fname, fileflags) != 0) {
512 +               rsyserr(FERROR_XFER, errno,
513 +                       "failed to set file flags on %s",
514 +                       full_fname(fname));
515 +               return 0;
516 +       }
517 +
518 +       return 1;
519 +}
520 +
521 +/* Remove immutable flags from an object, so it can be altered/removed. */
522 +int make_mutable(const char *fname, mode_t mode, uint32 fileflags, uint32 iflags)
523 +{
524 +       if (S_ISLNK(mode) || !(fileflags & iflags))
525 +               return 0;
526 +       if (!set_fileflags(fname, fileflags & ~iflags))
527 +               return -1;
528 +       return 1;
529 +}
530 +
531 +/* Undo a prior make_mutable() call that returned a 1. */
532 +int undo_make_mutable(const char *fname, uint32 fileflags)
533 +{
534 +       if (!set_fileflags(fname, fileflags))
535 +               return -1;
536 +       return 1;
537 +}
538 +#endif
539 +
540  int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
541                    const char *fnamecmp, int flags)
542  {
543 @@ -502,7 +536,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
544                 flags |= ATTRS_SKIP_MTIME;
545         if (!(flags & ATTRS_SKIP_MTIME)
546             && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
547 -               int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode);
548 +               int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode, ST_FLAGS(sxp->st));
549                 if (ret < 0) {
550                         rsyserr(FERROR_XFER, errno, "failed to set times on %s",
551                                 full_fname(fname));
552 @@ -538,7 +572,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
553                 if (am_root >= 0) {
554                         uid_t uid = change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid;
555                         gid_t gid = change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid;
556 -                       if (do_lchown(fname, uid, gid) != 0) {
557 +                       if (do_lchown(fname, uid, gid, sxp->st.st_mode, ST_FLAGS(sxp->st)) != 0) {
558                                 /* We shouldn't have attempted to change uid
559                                  * or gid unless have the privilege. */
560                                 rsyserr(FERROR_XFER, errno, "%s %s failed",
561 @@ -576,7 +610,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
562  
563  #ifdef HAVE_CHMOD
564         if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
565 -               int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
566 +               int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode, ST_FLAGS(sxp->st));
567                 if (ret < 0) {
568                         rsyserr(FERROR_XFER, errno,
569                                 "failed to set permissions on %s",
570 @@ -588,6 +622,19 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
571         }
572  #endif
573  
574 +#ifdef SUPPORT_FILEFLAGS
575 +       if (preserve_fileflags && !S_ISLNK(sxp->st.st_mode)
576 +        && sxp->st.st_flags != F_FFLAGS(file)) {
577 +               uint32 fileflags = F_FFLAGS(file);
578 +               if (flags & ATTRS_DELAY_IMMUTABLE)
579 +                       fileflags &= ~ALL_IMMUTABLE;
580 +               if (sxp->st.st_flags != fileflags
581 +                && !set_fileflags(fname, fileflags))
582 +                       goto cleanup;
583 +               updated = 1;
584 +       }
585 +#endif
586 +
587         if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
588                 if (updated)
589                         rprintf(FCLIENT, "%s\n", fname);
590 @@ -662,7 +709,8 @@ int finish_transfer(const char *fname, const char *fnametmp,
591  
592         /* Change permissions before putting the file into place. */
593         set_file_attrs(fnametmp, file, NULL, fnamecmp,
594 -                      ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
595 +                      ATTRS_DELAY_IMMUTABLE
596 +                      | (ok_to_set_time ? 0 : ATTRS_SKIP_MTIME));
597  
598         /* move tmp file over real file */
599         if (DEBUG_GTE(RECV, 1))
600 @@ -679,6 +727,10 @@ int finish_transfer(const char *fname, const char *fnametmp,
601         }
602         if (ret == 0) {
603                 /* The file was moved into place (not copied), so it's done. */
604 +#ifdef SUPPORT_FILEFLAGS
605 +               if (preserve_fileflags && F_FFLAGS(file) & ALL_IMMUTABLE)
606 +                       set_fileflags(fname, F_FFLAGS(file));
607 +#endif
608                 return 1;
609         }
610         /* The file was copied, so tweak the perms of the copied file.  If it
611 diff --git a/rsync.h b/rsync.h
612 --- a/rsync.h
613 +++ b/rsync.h
614 @@ -62,6 +62,7 @@
615  #define XMIT_HLINK_FIRST (1<<12)       /* protocols 30 - now (HLINKED files only) */
616  #define XMIT_IO_ERROR_ENDLIST (1<<12)  /* protocols 31*- now (w/XMIT_EXTENDED_FLAGS) (also protocol 30 w/'f' compat flag) */
617  #define XMIT_MOD_NSEC (1<<13)          /* protocols 31 - now */
618 +#define XMIT_SAME_FLAGS (1<<14)                /* protocols ?? - now */
619  
620  /* These flags are used in the live flist data. */
621  
622 @@ -165,6 +166,7 @@
623  
624  #define ATTRS_REPORT           (1<<0)
625  #define ATTRS_SKIP_MTIME       (1<<1)
626 +#define ATTRS_DELAY_IMMUTABLE  (1<<2)
627  
628  #define FULL_FLUSH     1
629  #define NORMAL_FLUSH   0
630 @@ -191,6 +193,7 @@
631  #define ITEM_REPORT_GROUP (1<<6)
632  #define ITEM_REPORT_ACL (1<<7)
633  #define ITEM_REPORT_XATTR (1<<8)
634 +#define ITEM_REPORT_FFLAGS (1<<9)
635  #define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
636  #define ITEM_XNAME_FOLLOWS (1<<12)
637  #define ITEM_IS_NEW (1<<13)
638 @@ -521,6 +524,28 @@ typedef unsigned int size_t;
639  #endif
640  #endif
641  
642 +#define NO_FFLAGS ((uint32)-1)
643 +
644 +#ifdef HAVE_CHFLAGS
645 +#define SUPPORT_FILEFLAGS 1
646 +#define SUPPORT_FORCE_CHANGE 1
647 +#endif
648 +
649 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
650 +#ifndef UF_NOUNLINK
651 +#define UF_NOUNLINK 0
652 +#endif
653 +#ifndef SF_NOUNLINK
654 +#define SF_NOUNLINK 0
655 +#endif
656 +#define USR_IMMUTABLE (UF_IMMUTABLE|UF_NOUNLINK|UF_APPEND)
657 +#define SYS_IMMUTABLE (SF_IMMUTABLE|SF_NOUNLINK|SF_APPEND)
658 +#define ALL_IMMUTABLE (USR_IMMUTABLE|SYS_IMMUTABLE)
659 +#define ST_FLAGS(st) (st.st_flags)
660 +#else
661 +#define ST_FLAGS(st) NO_FFLAGS
662 +#endif
663 +
664  /* Find a variable that is either exactly 32-bits or longer.
665   * If some code depends on 32-bit truncation, it will need to
666   * take special action in a "#if SIZEOF_INT32 > 4" section. */
667 @@ -708,6 +733,7 @@ extern int file_extra_cnt;
668  extern int inc_recurse;
669  extern int uid_ndx;
670  extern int gid_ndx;
671 +extern int fileflags_ndx;
672  extern int acls_ndx;
673  extern int xattrs_ndx;
674  
675 @@ -749,6 +775,11 @@ extern int xattrs_ndx;
676  /* When the associated option is on, all entries will have these present: */
677  #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
678  #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
679 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
680 +#define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
681 +#else
682 +#define F_FFLAGS(f) NO_FFLAGS
683 +#endif
684  #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
685  #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
686  #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
687 diff --git a/rsync.yo b/rsync.yo
688 --- a/rsync.yo
689 +++ b/rsync.yo
690 @@ -359,6 +359,7 @@ to the detailed description below for a complete description.  verb(
691   -K, --keep-dirlinks         treat symlinked dir on receiver as dir
692   -H, --hard-links            preserve hard links
693   -p, --perms                 preserve permissions
694 +     --fileflags             preserve file-flags (aka chflags)
695   -E, --executability         preserve executability
696       --chmod=CHMOD           affect file and/or directory permissions
697   -A, --acls                  preserve ACLs (implies -p)
698 @@ -394,7 +395,10 @@ to the detailed description below for a complete description.  verb(
699       --ignore-missing-args   ignore missing source args without error
700       --delete-missing-args   delete missing source args from destination
701       --ignore-errors         delete even if there are I/O errors
702 -     --force                 force deletion of dirs even if not empty
703 +     --force-delete          force deletion of dirs even if not empty
704 +     --force-change          affect user/system immutable files/dirs
705 +     --force-uchange         affect user-immutable files/dirs
706 +     --force-schange         affect system-immutable files/dirs
707       --max-delete=NUM        don't delete more than NUM files
708       --max-size=SIZE         don't transfer any file larger than SIZE
709       --min-size=SIZE         don't transfer any file smaller than SIZE
710 @@ -633,7 +637,8 @@ specified, in which case bf(-r) is not implied.
711  
712  Note that bf(-a) bf(does not preserve hardlinks), because
713  finding multiply-linked files is expensive.  You must separately
714 -specify bf(-H).
715 +specify bf(-H).  Note also that for backward compatibility, bf(-a)
716 +currently does bf(not) imply the bf(--fileflags) option.
717  
718  dit(--no-OPTION) You may turn off one or more implied options by prefixing
719  the option name with "no-".  Not all options may be prefixed with a "no-":
720 @@ -932,7 +937,7 @@ they would be using bf(--copy-links).
721  Without this option, if the sending side has replaced a directory with a
722  symlink to a directory, the receiving side will delete anything that is in
723  the way of the new symlink, including a directory hierarchy (as long as
724 -bf(--force) or bf(--delete) is in effect).
725 +bf(--force-delete) or bf(--delete) is in effect).
726  
727  See also bf(--keep-dirlinks) for an analogous option for the receiving
728  side.
729 @@ -1095,6 +1100,29 @@ Note that this option does not copy rsyncs special xattr values (e.g. those
730  used by bf(--fake-super)) unless you repeat the option (e.g. -XX).  This
731  "copy all xattrs" mode cannot be used with bf(--fake-super).
732  
733 +dit(bf(--fileflags)) This option causes rsync to update the file-flags to be
734 +the same as the source files and directories (if your OS supports the
735 +bf(chflags)(2) system call).   Some flags can only be altered by the super-user
736 +and some might only be unset below a certain secure-level (usually single-user
737 +mode). It will not make files alterable that are set to immutable on the
738 +receiver.  To do that, see bf(--force-change), bf(--force-uchange), and
739 +bf(--force-schange).
740 +
741 +dit(bf(--force-change)) This option causes rsync to disable both user-immutable
742 +and system-immutable flags on files and directories that are being updated or
743 +deleted on the receiving side.  This option overrides bf(--force-uchange) and
744 +bf(--force-schange).
745 +
746 +dit(bf(--force-uchange)) This option causes rsync to disable user-immutable
747 +flags on files and directories that are being updated or deleted on the
748 +receiving side.  It does not try to affect system flags.  This option overrides
749 +bf(--force-change) and bf(--force-schange).
750 +
751 +dit(bf(--force-schange)) This option causes rsync to disable system-immutable
752 +flags on files and directories that are being updated or deleted on the
753 +receiving side.  It does not try to affect user flags.  This option overrides
754 +bf(--force-change) and bf(--force-uchange).
755 +
756  dit(bf(--chmod)) This option tells rsync to apply one or more
757  comma-separated "chmod" modes to the permission of the files in the
758  transfer.  The resulting value is treated as though it were the permissions
759 @@ -1413,12 +1441,13 @@ display as a "*missing" entry in the bf(--list-only) output.
760  dit(bf(--ignore-errors)) Tells bf(--delete) to go ahead and delete files
761  even when there are I/O errors.
762  
763 -dit(bf(--force)) This option tells rsync to delete a non-empty directory
764 +dit(bf(--force-delete)) This option tells rsync to delete a non-empty directory
765  when it is to be replaced by a non-directory.  This is only relevant if
766  deletions are not active (see bf(--delete) for details).
767  
768 -Note for older rsync versions: bf(--force) used to still be required when
769 -using bf(--delete-after), and it used to be non-functional unless the
770 +This option can be abbreviated bf(--force) for backward compatibility.
771 +Note that some older rsync versions used to still require bf(--force)
772 +when using bf(--delete-after), and it used to be non-functional unless the
773  bf(--recursive) option was also enabled.
774  
775  dit(bf(--max-delete=NUM)) This tells rsync not to delete more than NUM
776 @@ -2036,7 +2065,7 @@ with older versions of rsync, but that also turns on the output of other
777  verbose messages).
778  
779  The "%i" escape has a cryptic output that is 11 letters long.  The general
780 -format is like the string bf(YXcstpoguax), where bf(Y) is replaced by the
781 +format is like the string bf(YXcstpogfax), where bf(Y) is replaced by the
782  type of update being done, bf(X) is replaced by the file-type, and the
783  other letters represent attributes that may be output if they are being
784  modified.
785 @@ -2092,7 +2121,7 @@ quote(itemization(
786    sender's value (requires bf(--owner) and super-user privileges).
787    it() A bf(g) means the group is different and is being updated to the
788    sender's value (requires bf(--group) and the authority to set the group).
789 -  it() The bf(u) slot is reserved for future use.
790 +  it() The bf(f) means that the fileflags information changed.
791    it() The bf(a) means that the ACL information changed.
792    it() The bf(x) means that the extended attribute information changed.
793  ))
794 diff --git a/syscall.c b/syscall.c
795 --- a/syscall.c
796 +++ b/syscall.c
797 @@ -38,6 +38,7 @@ extern int am_root;
798  extern int am_sender;
799  extern int read_only;
800  extern int list_only;
801 +extern int force_change;
802  extern int preserve_perms;
803  extern int preserve_executability;
804  
805 @@ -55,7 +56,23 @@ int do_unlink(const char *fname)
806  {
807         if (dry_run) return 0;
808         RETURN_ERROR_IF_RO_OR_LO;
809 -       return unlink(fname);
810 +       if (unlink(fname) == 0)
811 +               return 0;
812 +#ifdef SUPPORT_FORCE_CHANGE
813 +       if (force_change && errno == EPERM) {
814 +               STRUCT_STAT st;
815 +
816 +               if (x_lstat(fname, &st, NULL) == 0
817 +                && make_mutable(fname, st.st_mode, st.st_flags, force_change) > 0) {
818 +                       if (unlink(fname) == 0)
819 +                               return 0;
820 +                       undo_make_mutable(fname, st.st_flags);
821 +               }
822 +               /* TODO: handle immutable directories */
823 +               errno = EPERM;
824 +       }
825 +#endif
826 +       return -1;
827  }
828  
829  #ifdef SUPPORT_LINKS
830 @@ -116,14 +133,37 @@ int do_link(const char *fname1, const char *fname2)
831  }
832  #endif
833  
834 -int do_lchown(const char *path, uid_t owner, gid_t group)
835 +int do_lchown(const char *path, uid_t owner, gid_t group, mode_t mode, uint32 fileflags)
836  {
837         if (dry_run) return 0;
838         RETURN_ERROR_IF_RO_OR_LO;
839  #ifndef HAVE_LCHOWN
840  #define lchown chown
841  #endif
842 -       return lchown(path, owner, group);
843 +       if (lchown(path, owner, group) == 0)
844 +               return 0;
845 +#ifdef SUPPORT_FORCE_CHANGE
846 +       if (force_change && errno == EPERM) {
847 +               if (fileflags == NO_FFLAGS) {
848 +                       STRUCT_STAT st;
849 +                       if (x_lstat(path, &st, NULL) == 0) {
850 +                               mode = st.st_mode;
851 +                               fileflags = st.st_flags;
852 +                       }
853 +               }
854 +               if (fileflags != NO_FFLAGS
855 +                && make_mutable(path, mode, fileflags, force_change) > 0) {
856 +                       int ret = lchown(path, owner, group);
857 +                       undo_make_mutable(path, fileflags);
858 +                       if (ret == 0)
859 +                               return 0;
860 +               }
861 +               errno = EPERM;
862 +       }
863 +#else
864 +       mode = fileflags = 0; /* avoid compiler warning */
865 +#endif
866 +       return -1;
867  }
868  
869  int do_mknod(const char *pathname, mode_t mode, dev_t dev)
870 @@ -163,7 +203,7 @@ int do_mknod(const char *pathname, mode_t mode, dev_t dev)
871                         return -1;
872                 close(sock);
873  #ifdef HAVE_CHMOD
874 -               return do_chmod(pathname, mode);
875 +               return do_chmod(pathname, mode, 0);
876  #else
877                 return 0;
878  #endif
879 @@ -180,7 +220,22 @@ int do_rmdir(const char *pathname)
880  {
881         if (dry_run) return 0;
882         RETURN_ERROR_IF_RO_OR_LO;
883 -       return rmdir(pathname);
884 +       if (rmdir(pathname) == 0)
885 +               return 0;
886 +#ifdef SUPPORT_FORCE_CHANGE
887 +       if (force_change && errno == EPERM) {
888 +               STRUCT_STAT st;
889 +
890 +               if (x_lstat(pathname, &st, NULL) == 0
891 +                && make_mutable(pathname, st.st_mode, st.st_flags, force_change) > 0) {
892 +                       if (rmdir(pathname) == 0)
893 +                               return 0;
894 +                       undo_make_mutable(pathname, st.st_flags);
895 +               }
896 +               errno = EPERM;
897 +       }
898 +#endif
899 +       return -1;
900  }
901  
902  int do_open(const char *pathname, int flags, mode_t mode)
903 @@ -194,7 +249,7 @@ int do_open(const char *pathname, int flags, mode_t mode)
904  }
905  
906  #ifdef HAVE_CHMOD
907 -int do_chmod(const char *path, mode_t mode)
908 +int do_chmod(const char *path, mode_t mode, uint32 fileflags)
909  {
910         int code;
911         if (dry_run) return 0;
912 @@ -217,17 +272,74 @@ int do_chmod(const char *path, mode_t mode)
913         } else
914                 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
915  #endif /* !HAVE_LCHMOD */
916 +#ifdef SUPPORT_FORCE_CHANGE
917 +       if (code < 0 && force_change && errno == EPERM && !S_ISLNK(mode)) {
918 +               if (fileflags == NO_FFLAGS) {
919 +                       STRUCT_STAT st;
920 +                       if (x_lstat(path, &st, NULL) == 0)
921 +                               fileflags = st.st_flags;
922 +               }
923 +               if (fileflags != NO_FFLAGS
924 +                && make_mutable(path, mode, fileflags, force_change) > 0) {
925 +                       code = chmod(path, mode & CHMOD_BITS);
926 +                       undo_make_mutable(path, fileflags);
927 +                       if (code == 0)
928 +                               return 0;
929 +               }
930 +               errno = EPERM;
931 +       }
932 +#else
933 +       fileflags = 0; /* avoid compiler warning */
934 +#endif
935         if (code != 0 && (preserve_perms || preserve_executability))
936                 return code;
937         return 0;
938  }
939  #endif
940  
941 +#ifdef HAVE_CHFLAGS
942 +int do_chflags(const char *path, uint32 fileflags)
943 +{
944 +       if (dry_run) return 0;
945 +       RETURN_ERROR_IF_RO_OR_LO;
946 +       return chflags(path, fileflags);
947 +}
948 +#endif
949 +
950  int do_rename(const char *fname1, const char *fname2)
951  {
952         if (dry_run) return 0;
953         RETURN_ERROR_IF_RO_OR_LO;
954 -       return rename(fname1, fname2);
955 +       if (rename(fname1, fname2) == 0)
956 +               return 0;
957 +#ifdef SUPPORT_FORCE_CHANGE
958 +       if (force_change && errno == EPERM) {
959 +               STRUCT_STAT st1, st2;
960 +               int became_mutable;
961 +
962 +               if (x_lstat(fname1, &st1, NULL) != 0)
963 +                       goto failed;
964 +               became_mutable = make_mutable(fname1, st1.st_mode, st1.st_flags, force_change) > 0;
965 +               if (became_mutable && rename(fname1, fname2) == 0)
966 +                       goto success;
967 +               if (x_lstat(fname2, &st2, NULL) == 0
968 +                && make_mutable(fname2, st2.st_mode, st2.st_flags, force_change) > 0) {
969 +                       if (rename(fname1, fname2) == 0) {
970 +                         success:
971 +                               if (became_mutable) /* Yes, use fname2 and st1! */
972 +                                       undo_make_mutable(fname2, st1.st_flags);
973 +                               return 0;
974 +                       }
975 +                       undo_make_mutable(fname2, st2.st_flags);
976 +               }
977 +               /* TODO: handle immutable directories */
978 +               if (became_mutable)
979 +                       undo_make_mutable(fname1, st1.st_flags);
980 +         failed:
981 +               errno = EPERM;
982 +       }
983 +#endif
984 +       return -1;
985  }
986  
987  #ifdef HAVE_FTRUNCATE
988 diff --git a/t_stub.c b/t_stub.c
989 --- a/t_stub.c
990 +++ b/t_stub.c
991 @@ -26,6 +26,7 @@ int preallocate_files = 0;
992  int module_id = -1;
993  int relative_paths = 0;
994  int module_dirlen = 0;
995 +int force_change = 0;
996  int preserve_acls = 0;
997  int preserve_times = 0;
998  int preserve_xattrs = 0;
999 @@ -96,3 +97,23 @@ filter_rule_list daemon_filter_list;
1000  {
1001         return "tester";
1002  }
1003 +
1004 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
1005 + int make_mutable(UNUSED(const char *fname), UNUSED(mode_t mode), UNUSED(uint32 fileflags), UNUSED(uint32 iflags))
1006 +{
1007 +       return 0;
1008 +}
1009 +
1010 +/* Undo a prior make_mutable() call that returned a 1. */
1011 + int undo_make_mutable(UNUSED(const char *fname), UNUSED(uint32 fileflags))
1012 +{
1013 +       return 0;
1014 +}
1015 +#endif
1016 +
1017 +#ifdef SUPPORT_XATTRS
1018 + int x_lstat(UNUSED(const char *fname), UNUSED(STRUCT_STAT *fst), UNUSED(STRUCT_STAT *xst))
1019 +{
1020 +       return -1;
1021 +}
1022 +#endif
1023 diff --git a/util.c b/util.c
1024 --- a/util.c
1025 +++ b/util.c
1026 @@ -31,6 +31,7 @@ extern int relative_paths;
1027  extern int preserve_times;
1028  extern int preserve_xattrs;
1029  extern int preallocate_files;
1030 +extern int force_change;
1031  extern char *module_dir;
1032  extern unsigned int module_dirlen;
1033  extern char *partial_dir;
1034 @@ -125,9 +126,36 @@ NORETURN void overflow_exit(const char *str)
1035         exit_cleanup(RERR_MALLOC);
1036  }
1037  
1038 +#ifdef SUPPORT_FORCE_CHANGE
1039 +static int try_a_force_change(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode, uint32 fileflags)
1040 +{
1041 +       if (fileflags == NO_FFLAGS) {
1042 +               STRUCT_STAT st;
1043 +               if (x_lstat(fname, &st, NULL) == 0)
1044 +                       fileflags = st.st_flags;
1045 +       }
1046 +
1047 +       if (fileflags != NO_FFLAGS && make_mutable(fname, mode, fileflags, force_change) > 0) {
1048 +               int ret, save_force_change = force_change;
1049 +
1050 +               force_change = 0; /* Make certain we can't come back here. */
1051 +               ret = set_modtime(fname, modtime, mod_nsec, mode, fileflags);
1052 +               force_change = save_force_change;
1053 +
1054 +               undo_make_mutable(fname, fileflags);
1055 +
1056 +               return ret;
1057 +       }
1058 +
1059 +       errno = EPERM;
1060 +
1061 +       return -1;
1062 +}
1063 +#endif
1064 +
1065  /* This returns 0 for success, 1 for a symlink if symlink time-setting
1066   * is not possible, or -1 for any other error. */
1067 -int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1068 +int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode, uint32 fileflags)
1069  {
1070         static int switch_step = 0;
1071  
1072 @@ -142,6 +170,11 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1073  #include "case_N.h"
1074                 if (do_utimensat(fname, modtime, mod_nsec) == 0)
1075                         break;
1076 +#ifdef SUPPORT_FORCE_CHANGE
1077 +               if (force_change && errno == EPERM
1078 +                && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1079 +                       break;
1080 +#endif
1081                 if (errno != ENOSYS)
1082                         return -1;
1083                 switch_step++;
1084 @@ -152,6 +185,11 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1085  #include "case_N.h"
1086                 if (do_lutimes(fname, modtime, mod_nsec) == 0)
1087                         break;
1088 +#ifdef SUPPORT_FORCE_CHANGE
1089 +               if (force_change && errno == EPERM
1090 +                && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1091 +                       break;
1092 +#endif
1093                 if (errno != ENOSYS)
1094                         return -1;
1095                 switch_step++;
1096 @@ -175,6 +213,13 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1097                 if (do_utime(fname, modtime, mod_nsec) == 0)
1098                         break;
1099  #endif
1100 +#ifdef SUPPORT_FORCE_CHANGE
1101 +               if (force_change && errno == EPERM
1102 +                && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1103 +                       break;
1104 +#else
1105 +               fileflags = 0; /* avoid compiler warning */
1106 +#endif
1107  
1108                 return -1;
1109         }
1110 diff --git a/xattrs.c b/xattrs.c
1111 --- a/xattrs.c
1112 +++ b/xattrs.c
1113 @@ -1043,7 +1043,7 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1114         mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1115              | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1116         if (fst.st_mode != mode)
1117 -               do_chmod(fname, mode);
1118 +               do_chmod(fname, mode, ST_FLAGS(fst));
1119         if (!IS_DEVICE(fst.st_mode))
1120                 fst.st_rdev = 0; /* just in case */
1121