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