1 This patch provides --fileflags, which preserves the st_flags stat() field.
2 Modified from a patch that was written by Rolf Grossmann.
4 To use this patch, run these commands for a successful build:
6 patch -p1 <patches/fileflags.diff
7 ./configure (optional if already run)
10 based-on: 364d302bca6b9701ccf731feb1d5e91febdd9277
11 diff --git a/compat.c 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 @@ -75,7 +77,7 @@ int inplace_partial = 0;
30 int do_negotiated_strings = 0;
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;
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 @@ -471,6 +473,8 @@ void setup_protocol(int f_out,int f_in)
39 uid_ndx = ++file_extra_cnt;
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;
47 @@ -626,6 +630,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);
56 receiver_symlink_times = am_server
57 ? strchr(client_info, 'L') != NULL
58 diff --git a/delete.c b/delete.c
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)
72 strlcpy(p, fp->basename, remainder);
73 +#ifdef SUPPORT_FORCE_CHANGE
75 + make_mutable(fname, fp->mode, F_FFLAGS(fp), force_change);
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)
86 if (flags & DEL_NO_UID_WRITE)
87 - do_chmod(fbuf, mode | S_IWUSR);
88 + do_chmod(fbuf, mode | S_IWUSR, NO_FFLAGS);
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
96 + if (x_lstat(fbuf, &st, NULL) == 0)
97 + make_mutable(fbuf, st.st_mode, st.st_flags, force_change);
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
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,
116 static time_t modtime, atime;
118 +#ifdef SUPPORT_FILEFLAGS
119 + static uint32 fileflags;
121 #ifdef SUPPORT_HARD_LINKS
124 @@ -425,6 +429,14 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
125 xflags |= XMIT_SAME_MODE;
128 +#ifdef SUPPORT_FILEFLAGS
129 + if (preserve_fileflags) {
130 + if (F_FFLAGS(file) == fileflags)
131 + xflags |= XMIT_SAME_FLAGS;
133 + fileflags = F_FFLAGS(file);
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);
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
152 static int64 modtime, atime;
154 +#ifdef SUPPORT_FILEFLAGS
155 + static uint32 fileflags;
157 #ifdef SUPPORT_HARD_LINKS
160 @@ -775,6 +794,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
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);
169 uid = F_OWNER(first);
171 @@ -826,6 +849,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
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);
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
186 +#ifdef SUPPORT_FILEFLAGS
187 + if (preserve_fileflags)
188 + F_FFLAGS(file) = fileflags;
193 @@ -1383,6 +1414,10 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
196 file->mode = st.st_mode;
197 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
199 + F_FFLAGS(file) = st.st_flags;
202 F_OWNER(file) = st.st_uid;
204 diff --git a/generator.c 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 @@ -471,6 +473,10 @@ int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
220 if (perms_differ(file, sxp))
222 +#ifdef SUPPORT_FILEFLAGS
223 + if (preserve_fileflags && sxp->st.st_flags != F_FFLAGS(file))
226 if (ownership_differs(file, sxp))
229 @@ -525,6 +531,11 @@ void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statre
230 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
231 && 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;
239 if (preserve_acls && !S_ISLNK(file->mode)) {
240 if (!ACL_READY(*sxp))
241 @@ -1427,6 +1438,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
242 file->mode = dest_mode(file->mode, sx.st.st_mode,
243 dflt_perms, statret == 0);
245 +#ifdef SUPPORT_FORCE_CHANGE
246 + if (force_change && !preserve_fileflags)
247 + F_FFLAGS(file) = sx.st.st_flags;
249 if (statret != 0 && basis_dir[0] != NULL) {
250 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
252 @@ -1471,10 +1486,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;
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",
269 @@ -1510,6 +1530,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
270 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
273 +#ifdef SUPPORT_FORCE_CHANGE
274 + if (force_change && !preserve_fileflags)
275 + F_FFLAGS(file) = sx.st.st_flags;
278 #ifdef SUPPORT_HARD_LINKS
279 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
280 @@ -2077,17 +2101,25 @@ static void touch_up_dirs(struct file_list *flist, int ndx)
282 fname = f_name(file, NULL);
284 - do_chmod(fname, file->mode);
285 + do_chmod(fname, file->mode, 0);
286 if (need_retouch_dir_times) {
288 if (link_stat(fname, &st, 0) == 0 && time_diff(&st, file)) {
289 st.st_mtime = file->modtime;
291 st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file);
293 +#ifdef SUPPORT_FORCE_CHANGE
294 + st.st_mode = file->mode;
297 set_times(fname, &st);
300 +#ifdef SUPPORT_FORCE_CHANGE
301 + if (force_change && F_FFLAGS(file) & force_change)
302 + undo_make_mutable(fname, F_FFLAGS(file));
304 if (counter >= loopchk_limit) {
306 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
307 diff --git a/log.c b/log.c
310 @@ -724,7 +724,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';
315 + c[11] = !(iflags & ITEM_REPORT_FFLAGS) ? '.' : 'f';
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
324 #if defined CONFIG_LOCALE && defined HAVE_LOCALE_H
327 +#ifdef SUPPORT_FORCE_CHANGE
328 +#include <sys/sysctl.h>
332 extern int list_only;
333 @@ -52,6 +55,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;
340 extern int xfer_dirs;
341 @@ -946,6 +950,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;
345 +#ifdef SUPPORT_FORCE_CHANGE
346 + if (force_change & SYS_IMMUTABLE) {
347 + /* Determine whether we'll be able to unlock a system immutable item. */
349 + int securityLevel = 0;
350 + size_t len = sizeof securityLevel;
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);
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
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;
377 int allow_8bit_chars = 0;
378 int force_delete = 0;
379 +int force_change = 0;
381 int prune_empty_dirs = 0;
383 @@ -647,6 +649,11 @@ static void print_capabilities(enum logcode f)
387 +#ifndef SUPPORT_FILEFLAGS
395 @@ -757,6 +764,9 @@ void usage(enum logcode F)
396 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
397 rprintf(F," -H, --hard-links preserve hard links\n");
398 rprintf(F," -p, --perms preserve permissions\n");
399 +#ifdef SUPPORT_FILEFLAGS
400 + rprintf(F," --fileflags preserve file-flags (aka chflags)\n");
402 rprintf(F," -E, --executability preserve the file's executability\n");
403 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
405 @@ -806,7 +816,12 @@ void usage(enum logcode F)
406 rprintf(F," --ignore-missing-args ignore missing source args without error\n");
407 rprintf(F," --delete-missing-args delete missing source args from destination\n");
408 rprintf(F," --ignore-errors delete even if there are I/O errors\n");
409 - rprintf(F," --force force deletion of directories even if not empty\n");
410 + rprintf(F," --force-delete force deletion of directories even if not empty\n");
411 +#ifdef SUPPORT_FORCE_CHANGE
412 + rprintf(F," --force-change affect user-/system-immutable files/dirs\n");
413 + rprintf(F," --force-uchange affect user-immutable files/dirs\n");
414 + rprintf(F," --force-schange affect system-immutable files/dirs\n");
416 rprintf(F," --max-delete=NUM don't delete more than NUM files\n");
417 rprintf(F," --max-size=SIZE don't transfer any file larger than SIZE\n");
418 rprintf(F," --min-size=SIZE don't transfer any file smaller than SIZE\n");
419 @@ -925,6 +940,10 @@ static struct poptOption long_options[] = {
420 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
421 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
422 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
423 +#ifdef SUPPORT_FILEFLAGS
424 + {"fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 1, 0, 0 },
425 + {"no-fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 0, 0, 0 },
427 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
428 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
429 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
430 @@ -1018,6 +1037,14 @@ static struct poptOption long_options[] = {
431 {"remove-source-files",0,POPT_ARG_VAL, &remove_source_files, 1, 0, 0 },
432 {"force", 0, POPT_ARG_VAL, &force_delete, 1, 0, 0 },
433 {"no-force", 0, POPT_ARG_VAL, &force_delete, 0, 0, 0 },
434 + {"force-delete", 0, POPT_ARG_VAL, &force_delete, 1, 0, 0 },
435 + {"no-force-delete", 0, POPT_ARG_VAL, &force_delete, 0, 0, 0 },
436 +#ifdef SUPPORT_FORCE_CHANGE
437 + {"force-change", 0, POPT_ARG_VAL, &force_change, ALL_IMMUTABLE, 0, 0 },
438 + {"no-force-change", 0, POPT_ARG_VAL, &force_change, 0, 0, 0 },
439 + {"force-uchange", 0, POPT_ARG_VAL, &force_change, USR_IMMUTABLE, 0, 0 },
440 + {"force-schange", 0, POPT_ARG_VAL, &force_change, SYS_IMMUTABLE, 0, 0 },
442 {"ignore-errors", 0, POPT_ARG_VAL, &ignore_errors, 1, 0, 0 },
443 {"no-ignore-errors", 0, POPT_ARG_VAL, &ignore_errors, 0, 0, 0 },
444 {"max-delete", 0, POPT_ARG_INT, &max_delete, 0, 0, 0 },
445 @@ -2740,6 +2767,9 @@ void server_options(char **args, int *argc_p)
446 if (xfer_dirs && !recurse && delete_mode && am_sender)
447 args[ac++] = "--no-r";
449 + if (preserve_fileflags)
450 + args[ac++] = "--fileflags";
452 if (do_compression && do_compression_level != CLVL_NOT_SPECIFIED) {
453 if (asprintf(&arg, "--compress-level=%d", do_compression_level) < 0)
455 @@ -2843,6 +2873,16 @@ void server_options(char **args, int *argc_p)
456 args[ac++] = "--delete-excluded";
458 args[ac++] = "--force";
459 +#ifdef SUPPORT_FORCE_CHANGE
460 + if (force_change) {
461 + if (force_change == ALL_IMMUTABLE)
462 + args[ac++] = "--force-change";
463 + else if (force_change == USR_IMMUTABLE)
464 + args[ac++] = "--force-uchange";
465 + else if (force_change == SYS_IMMUTABLE)
466 + args[ac++] = "--force-schange";
470 args[ac++] = "--only-write-batch=X";
472 diff --git a/rsync.c b/rsync.c
475 @@ -31,6 +31,7 @@ extern int dry_run;
476 extern int preserve_acls;
477 extern int preserve_xattrs;
478 extern int preserve_perms;
479 +extern int preserve_fileflags;
480 extern int preserve_executability;
481 extern int preserve_times;
483 @@ -468,6 +469,39 @@ mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
487 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
488 +/* Set a file's st_flags. */
489 +static int set_fileflags(const char *fname, uint32 fileflags)
491 + if (do_chflags(fname, fileflags) != 0) {
492 + rsyserr(FERROR_XFER, errno,
493 + "failed to set file flags on %s",
494 + full_fname(fname));
501 +/* Remove immutable flags from an object, so it can be altered/removed. */
502 +int make_mutable(const char *fname, mode_t mode, uint32 fileflags, uint32 iflags)
504 + if (S_ISLNK(mode) || !(fileflags & iflags))
506 + if (!set_fileflags(fname, fileflags & ~iflags))
511 +/* Undo a prior make_mutable() call that returned a 1. */
512 +int undo_make_mutable(const char *fname, uint32 fileflags)
514 + if (!set_fileflags(fname, fileflags))
520 int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
521 const char *fnamecmp, int flags)
523 @@ -529,7 +563,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
525 uid_t uid = change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid;
526 gid_t gid = change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid;
527 - if (do_lchown(fname, uid, gid) != 0) {
528 + if (do_lchown(fname, uid, gid, sxp->st.st_mode, ST_FLAGS(sxp->st)) != 0) {
529 /* We shouldn't have attempted to change uid
530 * or gid unless have the privilege. */
531 rsyserr(FERROR_XFER, errno, "%s %s failed",
532 @@ -620,7 +654,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
535 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
536 - int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
537 + int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode, ST_FLAGS(sxp->st));
539 rsyserr(FERROR_XFER, errno,
540 "failed to set permissions on %s",
541 @@ -632,6 +666,19 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
545 +#ifdef SUPPORT_FILEFLAGS
546 + if (preserve_fileflags && !S_ISLNK(sxp->st.st_mode)
547 + && sxp->st.st_flags != F_FFLAGS(file)) {
548 + uint32 fileflags = F_FFLAGS(file);
549 + if (flags & ATTRS_DELAY_IMMUTABLE)
550 + fileflags &= ~ALL_IMMUTABLE;
551 + if (sxp->st.st_flags != fileflags
552 + && !set_fileflags(fname, fileflags))
558 if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
560 rprintf(FCLIENT, "%s\n", fname);
561 @@ -709,7 +756,8 @@ int finish_transfer(const char *fname, const char *fnametmp,
563 /* Change permissions before putting the file into place. */
564 set_file_attrs(fnametmp, file, NULL, fnamecmp,
565 - ok_to_set_time ? ATTRS_SET_NANO : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME);
566 + ATTRS_DELAY_IMMUTABLE
567 + | (ok_to_set_time ? ATTRS_SET_NANO : ATTRS_SKIP_MTIME | ATTRS_SKIP_ATIME));
569 /* move tmp file over real file */
570 if (DEBUG_GTE(RECV, 1))
571 @@ -726,6 +774,10 @@ int finish_transfer(const char *fname, const char *fnametmp,
574 /* The file was moved into place (not copied), so it's done. */
575 +#ifdef SUPPORT_FILEFLAGS
576 + if (preserve_fileflags && F_FFLAGS(file) & ALL_IMMUTABLE)
577 + set_fileflags(fname, F_FFLAGS(file));
581 /* The file was copied, so tweak the perms of the copied file. If it
582 diff --git a/rsync.h b/rsync.h
587 /* The following XMIT flags require an rsync that uses a varint for the flag values */
589 -#define XMIT_RESERVED_16 (1<<16) /* reserved for future fileflags use */
590 +#define XMIT_SAME_FLAGS (1<<16) /* any protocol - restricted by command-line option */
591 #define XMIT_RESERVED_17 (1<<17) /* reserved for future crtimes use */
593 /* These flags are used in the live flist data. */
595 #define ATTRS_SKIP_MTIME (1<<1)
596 #define ATTRS_SET_NANO (1<<2)
597 #define ATTRS_SKIP_ATIME (1<<3)
598 +#define ATTRS_DELAY_IMMUTABLE (1<<4)
603 #define ITEM_REPORT_GROUP (1<<6)
604 #define ITEM_REPORT_ACL (1<<7)
605 #define ITEM_REPORT_XATTR (1<<8)
606 +#define ITEM_REPORT_FFLAGS (1<<9)
607 #define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
608 #define ITEM_XNAME_FOLLOWS (1<<12)
609 #define ITEM_IS_NEW (1<<13)
610 @@ -544,6 +546,28 @@ typedef unsigned int size_t;
614 +#define NO_FFLAGS ((uint32)-1)
617 +#define SUPPORT_FILEFLAGS 1
618 +#define SUPPORT_FORCE_CHANGE 1
621 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
623 +#define UF_NOUNLINK 0
626 +#define SF_NOUNLINK 0
628 +#define USR_IMMUTABLE (UF_IMMUTABLE|UF_NOUNLINK|UF_APPEND)
629 +#define SYS_IMMUTABLE (SF_IMMUTABLE|SF_NOUNLINK|SF_APPEND)
630 +#define ALL_IMMUTABLE (USR_IMMUTABLE|SYS_IMMUTABLE)
631 +#define ST_FLAGS(st) ((st).st_flags)
633 +#define ST_FLAGS(st) NO_FFLAGS
636 /* Find a variable that is either exactly 32-bits or longer.
637 * If some code depends on 32-bit truncation, it will need to
638 * take special action in a "#if SIZEOF_INT32 > 4" section. */
639 @@ -754,6 +778,7 @@ extern int pathname_ndx;
640 extern int depth_ndx;
643 +extern int fileflags_ndx;
645 extern int xattrs_ndx;
647 @@ -804,6 +829,11 @@ extern int xattrs_ndx;
648 /* When the associated option is on, all entries will have these present: */
649 #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
650 #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
651 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
652 +#define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
654 +#define F_FFLAGS(f) NO_FFLAGS
656 #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
657 #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
658 #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
659 diff --git a/rsync.yo b/rsync.yo
662 @@ -368,6 +368,7 @@ to the detailed description below for a complete description. verb(
663 -K, --keep-dirlinks treat symlinked dir on receiver as dir
664 -H, --hard-links preserve hard links
665 -p, --perms preserve permissions
666 + --fileflags preserve file-flags (aka chflags)
667 -E, --executability preserve executability
668 --chmod=CHMOD affect file and/or directory permissions
669 -A, --acls preserve ACLs (implies -p)
670 @@ -407,7 +408,10 @@ to the detailed description below for a complete description. verb(
671 --ignore-missing-args ignore missing source args without error
672 --delete-missing-args delete missing source args from destination
673 --ignore-errors delete even if there are I/O errors
674 - --force force deletion of dirs even if not empty
675 + --force-delete force deletion of dirs even if not empty
676 + --force-change affect user/system immutable files/dirs
677 + --force-uchange affect user-immutable files/dirs
678 + --force-schange affect system-immutable files/dirs
679 --max-delete=NUM don't delete more than NUM files
680 --max-size=SIZE don't transfer any file larger than SIZE
681 --min-size=SIZE don't transfer any file smaller than SIZE
682 @@ -673,7 +677,8 @@ specified, in which case bf(-r) is not implied.
684 Note that bf(-a) bf(does not preserve hardlinks), because
685 finding multiply-linked files is expensive. You must separately
687 +specify bf(-H). Note also that for backward compatibility, bf(-a)
688 +currently does bf(not) imply the bf(--fileflags) option.
690 dit(--no-OPTION) You may turn off one or more implied options by prefixing
691 the option name with "no-". Not all options may be prefixed with a "no-":
692 @@ -986,7 +991,7 @@ they would be using bf(--copy-links).
693 Without this option, if the sending side has replaced a directory with a
694 symlink to a directory, the receiving side will delete anything that is in
695 the way of the new symlink, including a directory hierarchy (as long as
696 -bf(--force) or bf(--delete) is in effect).
697 +bf(--force-delete) or bf(--delete) is in effect).
699 See also bf(--keep-dirlinks) for an analogous option for the receiving
701 @@ -1170,6 +1175,29 @@ Note that the bf(-X) option does not copy rsync's special xattr values (e.g.
702 those used by bf(--fake-super)) unless you repeat the option (e.g. -XX).
703 This "copy all xattrs" mode cannot be used with bf(--fake-super).
705 +dit(bf(--fileflags)) This option causes rsync to update the file-flags to be
706 +the same as the source files and directories (if your OS supports the
707 +bf(chflags)(2) system call). Some flags can only be altered by the super-user
708 +and some might only be unset below a certain secure-level (usually single-user
709 +mode). It will not make files alterable that are set to immutable on the
710 +receiver. To do that, see bf(--force-change), bf(--force-uchange), and
711 +bf(--force-schange).
713 +dit(bf(--force-change)) This option causes rsync to disable both user-immutable
714 +and system-immutable flags on files and directories that are being updated or
715 +deleted on the receiving side. This option overrides bf(--force-uchange) and
716 +bf(--force-schange).
718 +dit(bf(--force-uchange)) This option causes rsync to disable user-immutable
719 +flags on files and directories that are being updated or deleted on the
720 +receiving side. It does not try to affect system flags. This option overrides
721 +bf(--force-change) and bf(--force-schange).
723 +dit(bf(--force-schange)) This option causes rsync to disable system-immutable
724 +flags on files and directories that are being updated or deleted on the
725 +receiving side. It does not try to affect user flags. This option overrides
726 +bf(--force-change) and bf(--force-uchange).
728 dit(bf(--chmod)) This option tells rsync to apply one or more
729 comma-separated "chmod" modes to the permission of the files in the
730 transfer. The resulting value is treated as though it were the permissions
731 @@ -1577,12 +1605,13 @@ display as a "*missing" entry in the bf(--list-only) output.
732 dit(bf(--ignore-errors)) Tells bf(--delete) to go ahead and delete files
733 even when there are I/O errors.
735 -dit(bf(--force)) This option tells rsync to delete a non-empty directory
736 +dit(bf(--force-delete)) This option tells rsync to delete a non-empty directory
737 when it is to be replaced by a non-directory. This is only relevant if
738 deletions are not active (see bf(--delete) for details).
740 -Note for older rsync versions: bf(--force) used to still be required when
741 -using bf(--delete-after), and it used to be non-functional unless the
742 +This option can be abbreviated bf(--force) for backward compatibility.
743 +Note that some older rsync versions used to still require bf(--force)
744 +when using bf(--delete-after), and it used to be non-functional unless the
745 bf(--recursive) option was also enabled.
747 dit(bf(--max-delete=NUM)) This tells rsync not to delete more than NUM
748 @@ -2281,7 +2310,7 @@ with older versions of rsync, but that also turns on the output of other
751 The "%i" escape has a cryptic output that is 11 letters long. The general
752 -format is like the string bf(YXcstpoguax), where bf(Y) is replaced by the
753 +format is like the string bf(YXcstpogfax), where bf(Y) is replaced by the
754 type of update being done, bf(X) is replaced by the file-type, and the
755 other letters represent attributes that may be output if they are being
757 diff --git a/syscall.c b/syscall.c
760 @@ -38,6 +38,7 @@ extern int am_root;
761 extern int am_sender;
762 extern int read_only;
763 extern int list_only;
764 +extern int force_change;
766 extern int preallocate_files;
767 extern int preserve_perms;
768 @@ -68,7 +69,23 @@ int do_unlink(const char *fname)
770 if (dry_run) return 0;
771 RETURN_ERROR_IF_RO_OR_LO;
772 - return unlink(fname);
773 + if (unlink(fname) == 0)
775 +#ifdef SUPPORT_FORCE_CHANGE
776 + if (force_change && errno == EPERM) {
779 + if (x_lstat(fname, &st, NULL) == 0
780 + && make_mutable(fname, st.st_mode, st.st_flags, force_change) > 0) {
781 + if (unlink(fname) == 0)
783 + undo_make_mutable(fname, st.st_flags);
785 + /* TODO: handle immutable directories */
793 @@ -129,14 +146,35 @@ int do_link(const char *fname1, const char *fname2)
797 -int do_lchown(const char *path, uid_t owner, gid_t group)
798 +int do_lchown(const char *path, uid_t owner, gid_t group, UNUSED(mode_t mode), UNUSED(uint32 fileflags))
800 if (dry_run) return 0;
801 RETURN_ERROR_IF_RO_OR_LO;
805 - return lchown(path, owner, group);
806 + if (lchown(path, owner, group) == 0)
808 +#ifdef SUPPORT_FORCE_CHANGE
809 + if (force_change && errno == EPERM) {
810 + if (fileflags == NO_FFLAGS) {
812 + if (x_lstat(path, &st, NULL) == 0) {
814 + fileflags = st.st_flags;
817 + if (fileflags != NO_FFLAGS
818 + && make_mutable(path, mode, fileflags, force_change) > 0) {
819 + int ret = lchown(path, owner, group);
820 + undo_make_mutable(path, fileflags);
830 int do_mknod(const char *pathname, mode_t mode, dev_t dev)
831 @@ -176,7 +214,7 @@ int do_mknod(const char *pathname, mode_t mode, dev_t dev)
835 - return do_chmod(pathname, mode);
836 + return do_chmod(pathname, mode, 0);
840 @@ -193,7 +231,22 @@ int do_rmdir(const char *pathname)
842 if (dry_run) return 0;
843 RETURN_ERROR_IF_RO_OR_LO;
844 - return rmdir(pathname);
845 + if (rmdir(pathname) == 0)
847 +#ifdef SUPPORT_FORCE_CHANGE
848 + if (force_change && errno == EPERM) {
851 + if (x_lstat(pathname, &st, NULL) == 0
852 + && make_mutable(pathname, st.st_mode, st.st_flags, force_change) > 0) {
853 + if (rmdir(pathname) == 0)
855 + undo_make_mutable(pathname, st.st_flags);
863 int do_open(const char *pathname, int flags, mode_t mode)
864 @@ -212,7 +265,7 @@ int do_open(const char *pathname, int flags, mode_t mode)
868 -int do_chmod(const char *path, mode_t mode)
869 +int do_chmod(const char *path, mode_t mode, UNUSED(uint32 fileflags))
872 if (dry_run) return 0;
873 @@ -235,17 +288,72 @@ int do_chmod(const char *path, mode_t mode)
875 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
876 #endif /* !HAVE_LCHMOD */
877 +#ifdef SUPPORT_FORCE_CHANGE
878 + if (code < 0 && force_change && errno == EPERM && !S_ISLNK(mode)) {
879 + if (fileflags == NO_FFLAGS) {
881 + if (x_lstat(path, &st, NULL) == 0)
882 + fileflags = st.st_flags;
884 + if (fileflags != NO_FFLAGS
885 + && make_mutable(path, mode, fileflags, force_change) > 0) {
886 + code = chmod(path, mode & CHMOD_BITS);
887 + undo_make_mutable(path, fileflags);
894 if (code != 0 && (preserve_perms || preserve_executability))
901 +int do_chflags(const char *path, uint32 fileflags)
903 + if (dry_run) return 0;
904 + RETURN_ERROR_IF_RO_OR_LO;
905 + return chflags(path, fileflags);
909 int do_rename(const char *fname1, const char *fname2)
911 if (dry_run) return 0;
912 RETURN_ERROR_IF_RO_OR_LO;
913 - return rename(fname1, fname2);
914 + if (rename(fname1, fname2) == 0)
916 +#ifdef SUPPORT_FORCE_CHANGE
917 + if (force_change && errno == EPERM) {
918 + STRUCT_STAT st1, st2;
919 + int became_mutable;
921 + if (x_lstat(fname1, &st1, NULL) != 0)
923 + became_mutable = make_mutable(fname1, st1.st_mode, st1.st_flags, force_change) > 0;
924 + if (became_mutable && rename(fname1, fname2) == 0)
926 + if (x_lstat(fname2, &st2, NULL) == 0
927 + && make_mutable(fname2, st2.st_mode, st2.st_flags, force_change) > 0) {
928 + if (rename(fname1, fname2) == 0) {
930 + if (became_mutable) /* Yes, use fname2 and st1! */
931 + undo_make_mutable(fname2, st1.st_flags);
934 + undo_make_mutable(fname2, st2.st_flags);
936 + /* TODO: handle immutable directories */
937 + if (became_mutable)
938 + undo_make_mutable(fname1, st1.st_flags);
946 #ifdef HAVE_FTRUNCATE
947 diff --git a/t_stub.c b/t_stub.c
950 @@ -28,6 +28,8 @@ int protect_args = 0;
952 int relative_paths = 0;
953 int module_dirlen = 0;
954 +int force_change = 0;
955 +int preserve_acls = 0;
956 int preserve_times = 0;
957 int preserve_xattrs = 0;
958 int preserve_perms = 0;
959 @@ -109,3 +111,23 @@ filter_rule_list daemon_filter_list;
964 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
965 + int make_mutable(UNUSED(const char *fname), UNUSED(mode_t mode), UNUSED(uint32 fileflags), UNUSED(uint32 iflags))
970 +/* Undo a prior make_mutable() call that returned a 1. */
971 + int undo_make_mutable(UNUSED(const char *fname), UNUSED(uint32 fileflags))
977 +#ifdef SUPPORT_XATTRS
978 + int x_lstat(UNUSED(const char *fname), UNUSED(STRUCT_STAT *fst), UNUSED(STRUCT_STAT *xst))
983 diff --git a/testsuite/rsync.fns b/testsuite/rsync.fns
984 --- a/testsuite/rsync.fns
985 +++ b/testsuite/rsync.fns
986 @@ -23,9 +23,9 @@ todir="$tmpdir/to"
989 # For itemized output:
990 -all_plus='+++++++++'
992 -dots='.....' # trailing dots after changes
993 +all_plus='++++++++++'
995 +dots='......' # trailing dots after changes
996 tab_ch=' ' # a single tab character
999 diff --git a/util.c b/util.c
1002 @@ -33,6 +33,7 @@ extern int relative_paths;
1003 extern int preserve_times;
1004 extern int preserve_xattrs;
1005 extern int preallocate_files;
1006 +extern int force_change;
1007 extern char *module_dir;
1008 extern unsigned int module_dirlen;
1009 extern char *partial_dir;
1010 @@ -115,6 +116,33 @@ void print_child_argv(const char *prefix, char **cmd)
1011 rprintf(FCLIENT, " (%d args)\n", cnt);
1014 +#ifdef SUPPORT_FORCE_CHANGE
1015 +static int try_a_force_change(const char *fname, STRUCT_STAT *stp)
1017 + uint32 fileflags = ST_FLAGS(*stp);
1018 + if (fileflags == NO_FFLAGS) {
1020 + if (x_lstat(fname, &st, NULL) == 0)
1021 + fileflags = st.st_flags;
1023 + if (fileflags != NO_FFLAGS && make_mutable(fname, stp->st_mode, fileflags, force_change) > 0) {
1024 + int ret, save_force_change = force_change;
1026 + force_change = 0; /* Make certain we can't come back here. */
1027 + ret = set_times(fname, stp);
1028 + force_change = save_force_change;
1030 + undo_make_mutable(fname, fileflags);
1041 /* This returns 0 for success, 1 for a symlink if symlink time-setting
1042 * is not possible, or -1 for any other error. */
1043 int set_times(const char *fname, STRUCT_STAT *stp)
1044 @@ -142,6 +170,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1046 if (do_utimensat(fname, stp) == 0)
1048 +#ifdef SUPPORT_FORCE_CHANGE
1049 + if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1052 if (errno != ENOSYS)
1055 @@ -151,6 +183,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1057 if (do_lutimes(fname, stp) == 0)
1059 +#ifdef SUPPORT_FORCE_CHANGE
1060 + if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1063 if (errno != ENOSYS)
1066 @@ -172,6 +208,10 @@ int set_times(const char *fname, STRUCT_STAT *stp)
1067 if (do_utime(fname, stp) == 0)
1070 +#ifdef SUPPORT_FORCE_CHANGE
1071 + if (force_change && errno == EPERM && try_a_force_change(fname, stp) == 0)
1077 diff --git a/xattrs.c b/xattrs.c
1080 @@ -1224,7 +1224,7 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1081 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1082 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1083 if (fst.st_mode != mode)
1084 - do_chmod(fname, mode);
1085 + do_chmod(fname, mode, ST_FLAGS(fst));
1086 if (!IS_DEVICE(fst.st_mode))
1087 fst.st_rdev = 0; /* just in case */