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
11 based-on: 4c8eb5f9511dd1393b147c4e993ee15448faedf5
12 diff --git a/Makefile.in b/Makefile.in
15 @@ -45,7 +45,7 @@ popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
16 popt/popthelp.o popt/poptparse.o
17 OBJS=$(OBJS1) $(OBJS2) $(OBJS3) $(DAEMON_OBJ) $(LIBOBJ) @BUILD_ZLIB@ @BUILD_POPT@
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@
22 # Programs we must have to run the test cases
23 CHECK_PROGS = rsync$(EXEEXT) tls$(EXEEXT) getgroups$(EXEEXT) getfsdev$(EXEEXT) \
24 @@ -127,7 +127,7 @@ getgroups$(EXEEXT): getgroups.o
25 getfsdev$(EXEEXT): getfsdev.o
26 $(CC) $(CFLAGS) $(LDFLAGS) -o $@ getfsdev.o $(LIBS)
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)
33 diff --git a/compat.c 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;
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;
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;
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;
66 diff --git a/configure.ac b/configure.ac
69 @@ -597,6 +597,7 @@ AC_FUNC_UTIME_NULL
71 AC_CHECK_FUNCS(waitpid wait4 getcwd strdup chown chmod lchmod mknod mkfifo \
72 fchmod fstat ftruncate strchr readlink link utime utimes lutimes strftime \
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
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)
91 strlcpy(p, fp->basename, remainder);
92 +#ifdef SUPPORT_FORCE_CHANGE
94 + make_mutable(fname, fp->mode, F_FFLAGS(fp), force_change);
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)
105 if (flags & DEL_NO_UID_WRITE)
106 - do_chmod(fbuf, mode | S_IWUSR);
107 + do_chmod(fbuf, mode | S_IWUSR, NO_FFLAGS);
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) {
115 + if (x_lstat(fbuf, &st, NULL) == 0)
116 + make_mutable(fbuf, st.st_mode, st.st_flags, force_change);
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
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,
135 static time_t modtime;
137 +#ifdef SUPPORT_FILEFLAGS
138 + static uint32 fileflags;
140 #ifdef SUPPORT_HARD_LINKS
143 @@ -442,6 +446,14 @@ static void send_file_entry(int f, const char *fname, struct file_struct *file,
144 xflags |= XMIT_SAME_MODE;
147 +#ifdef SUPPORT_FILEFLAGS
148 + if (preserve_fileflags) {
149 + if (F_FFLAGS(file) == fileflags)
150 + xflags |= XMIT_SAME_FLAGS;
152 + fileflags = F_FFLAGS(file);
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);
166 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
167 if (protocol_version < 30)
169 @@ -672,6 +688,9 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
171 static int64 modtime;
173 +#ifdef SUPPORT_FILEFLAGS
174 + static uint32 fileflags;
176 #ifdef SUPPORT_HARD_LINKS
179 @@ -779,6 +798,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
180 modtime = first->modtime;
181 modtime_nsec = F_MOD_NSEC(first);
183 +#ifdef SUPPORT_FILEFLAGS
184 + if (preserve_fileflags)
185 + fileflags = F_FFLAGS(first);
188 uid = F_OWNER(first);
190 @@ -820,6 +843,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
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);
199 if (preserve_uid && !(xflags & XMIT_SAME_UID)) {
200 if (protocol_version < 30)
201 @@ -971,6 +998,10 @@ static struct file_struct *recv_file_entry(int f, struct file_list *flist, int x
205 +#ifdef SUPPORT_FILEFLAGS
206 + if (preserve_fileflags)
207 + F_FFLAGS(file) = fileflags;
212 @@ -1368,6 +1399,10 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
215 file->mode = st.st_mode;
216 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
218 + F_FFLAGS(file) = st.st_flags;
221 F_OWNER(file) = st.st_uid;
223 diff --git a/generator.c b/generator.c
226 @@ -42,8 +42,10 @@ extern int preserve_devices;
227 extern int preserve_specials;
228 extern int preserve_hard_links;
229 extern int preserve_executability;
230 +extern int preserve_fileflags;
231 extern int preserve_perms;
232 extern int preserve_times;
233 +extern int force_change;
234 extern int delete_mode;
235 extern int delete_before;
236 extern int delete_during;
237 @@ -464,6 +466,10 @@ int unchanged_attrs(const char *fname, struct file_struct *file, stat_x *sxp)
239 if (perms_differ(file, sxp))
241 +#ifdef SUPPORT_FILEFLAGS
242 + if (preserve_fileflags && sxp->st.st_flags != F_FFLAGS(file))
245 if (ownership_differs(file, sxp))
248 @@ -515,6 +521,11 @@ void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statre
249 if (gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
250 && sxp->st.st_gid != (gid_t)F_GROUP(file))
251 iflags |= ITEM_REPORT_GROUP;
252 +#ifdef SUPPORT_FILEFLAGS
253 + if (preserve_fileflags && !S_ISLNK(file->mode)
254 + && sxp->st.st_flags != F_FFLAGS(file))
255 + iflags |= ITEM_REPORT_FFLAGS;
258 if (preserve_acls && !S_ISLNK(file->mode)) {
259 if (!ACL_READY(*sxp))
260 @@ -1392,6 +1403,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
261 file->mode = dest_mode(file->mode, sx.st.st_mode,
262 dflt_perms, statret == 0);
264 +#ifdef SUPPORT_FORCE_CHANGE
265 + if (force_change && !preserve_fileflags)
266 + F_FFLAGS(file) = sx.st.st_flags;
268 if (statret != 0 && basis_dir[0] != NULL) {
269 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
271 @@ -1436,10 +1451,15 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
272 * readable and writable permissions during the time we are
273 * putting files within them. This is then restored to the
274 * former permissions after the transfer is done. */
275 +#ifdef SUPPORT_FORCE_CHANGE
276 + if (force_change && F_FFLAGS(file) & force_change
277 + && make_mutable(fname, file->mode, F_FFLAGS(file), force_change))
278 + need_retouch_dir_perms = 1;
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",
288 @@ -1474,6 +1494,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
289 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
292 +#ifdef SUPPORT_FORCE_CHANGE
293 + if (force_change && !preserve_fileflags)
294 + F_FFLAGS(file) = sx.st.st_flags;
297 #ifdef SUPPORT_HARD_LINKS
298 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
299 @@ -2043,13 +2067,17 @@ static void touch_up_dirs(struct file_list *flist, int ndx)
301 fname = f_name(file, NULL);
303 - do_chmod(fname, file->mode);
304 + do_chmod(fname, file->mode, 0);
305 if (need_retouch_dir_times) {
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);
312 +#ifdef SUPPORT_FORCE_CHANGE
313 + if (force_change && F_FFLAGS(file) & force_change)
314 + undo_make_mutable(fname, F_FFLAGS(file));
316 if (counter >= loopchk_limit) {
318 maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH);
319 diff --git a/log.c b/log.c
322 @@ -720,7 +720,7 @@ static void log_formatted(enum logcode code, const char *format, const char *op,
323 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
324 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
325 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
326 - c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
327 + c[8] = !(iflags & ITEM_REPORT_FFLAGS) ? '.' : 'f';
328 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
329 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
331 diff --git a/main.c b/main.c
335 #if defined CONFIG_LOCALE && defined HAVE_LOCALE_H
338 +#ifdef SUPPORT_FORCE_CHANGE
339 +#include <sys/sysctl.h>
343 extern int list_only;
344 @@ -51,6 +54,7 @@ extern int copy_unsafe_links;
345 extern int keep_dirlinks;
346 extern int preserve_hard_links;
347 extern int protocol_version;
348 +extern int force_change;
349 extern int file_total;
351 extern int xfer_dirs;
352 @@ -839,6 +843,22 @@ static int do_recv(int f_in, int f_out, char *local_name)
353 * points to an identical file won't be replaced by the referent. */
354 copy_links = copy_dirlinks = copy_unsafe_links = 0;
356 +#ifdef SUPPORT_FORCE_CHANGE
357 + if (force_change & SYS_IMMUTABLE) {
358 + /* Determine whether we'll be able to unlock a system immutable item. */
360 + int securityLevel = 0;
361 + size_t len = sizeof securityLevel;
364 + mib[1] = KERN_SECURELVL;
365 + if (sysctl(mib, 2, &securityLevel, &len, NULL, 0) == 0 && securityLevel > 0) {
366 + rprintf(FERROR, "System security level is too high to force mutability on system immutable files and directories.\n");
367 + exit_cleanup(RERR_UNSUPPORTED);
372 #ifdef SUPPORT_HARD_LINKS
373 if (preserve_hard_links && !inc_recurse)
374 match_hard_links(first_flist);
375 diff --git a/options.c b/options.c
378 @@ -55,6 +55,7 @@ int preserve_hard_links = 0;
379 int preserve_acls = 0;
380 int preserve_xattrs = 0;
381 int preserve_perms = 0;
382 +int preserve_fileflags = 0;
383 int preserve_executability = 0;
384 int preserve_devices = 0;
385 int preserve_specials = 0;
386 @@ -89,6 +90,7 @@ int numeric_ids = 0;
388 int allow_8bit_chars = 0;
389 int force_delete = 0;
390 +int force_change = 0;
392 int prune_empty_dirs = 0;
394 @@ -574,6 +576,7 @@ static void print_rsync_version(enum logcode f)
395 char const *links = "no ";
396 char const *iconv = "no ";
397 char const *ipv6 = "no ";
398 + char const *fileflags = "no ";
399 STRUCT_STAT *dumstat;
401 #if SUBPROTOCOL_VERSION != 0
402 @@ -610,6 +613,9 @@ static void print_rsync_version(enum logcode f)
403 #ifdef CAN_SET_SYMLINK_TIMES
406 +#ifdef SUPPORT_FILEFLAGS
410 rprintf(f, "%s version %s protocol version %d%s\n",
411 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
412 @@ -623,8 +629,8 @@ static void print_rsync_version(enum logcode f)
413 (int)(sizeof (int64) * 8));
414 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
415 got_socketpair, hardlinks, links, ipv6, have_inplace);
416 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc\n",
417 - have_inplace, acls, xattrs, iconv, symtimes, prealloc);
418 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sprealloc, %sfile-flags\n",
419 + have_inplace, acls, xattrs, iconv, symtimes, prealloc, fileflags);
421 #ifdef MAINTAINER_MODE
422 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
423 @@ -695,6 +701,9 @@ void usage(enum logcode F)
424 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
425 rprintf(F," -H, --hard-links preserve hard links\n");
426 rprintf(F," -p, --perms preserve permissions\n");
427 +#ifdef SUPPORT_FILEFLAGS
428 + rprintf(F," --fileflags preserve file-flags (aka chflags)\n");
430 rprintf(F," -E, --executability preserve the file's executability\n");
431 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
433 @@ -740,7 +749,12 @@ void usage(enum logcode F)
434 rprintf(F," --ignore-missing-args ignore missing source args without error\n");
435 rprintf(F," --delete-missing-args delete missing source args from destination\n");
436 rprintf(F," --ignore-errors delete even if there are I/O errors\n");
437 - rprintf(F," --force force deletion of directories even if not empty\n");
438 + rprintf(F," --force-delete force deletion of directories even if not empty\n");
439 +#ifdef SUPPORT_FORCE_CHANGE
440 + rprintf(F," --force-change affect user-/system-immutable files/dirs\n");
441 + rprintf(F," --force-uchange affect user-immutable files/dirs\n");
442 + rprintf(F," --force-schange affect system-immutable files/dirs\n");
444 rprintf(F," --max-delete=NUM don't delete more than NUM files\n");
445 rprintf(F," --max-size=SIZE don't transfer any file larger than SIZE\n");
446 rprintf(F," --min-size=SIZE don't transfer any file smaller than SIZE\n");
447 @@ -857,6 +871,10 @@ static struct poptOption long_options[] = {
448 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
449 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
450 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
451 +#ifdef SUPPORT_FILEFLAGS
452 + {"fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 1, 0, 0 },
453 + {"no-fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 0, 0, 0 },
455 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
456 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
457 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
458 @@ -943,6 +961,14 @@ static struct poptOption long_options[] = {
459 {"remove-source-files",0,POPT_ARG_VAL, &remove_source_files, 1, 0, 0 },
460 {"force", 0, POPT_ARG_VAL, &force_delete, 1, 0, 0 },
461 {"no-force", 0, POPT_ARG_VAL, &force_delete, 0, 0, 0 },
462 + {"force-delete", 0, POPT_ARG_VAL, &force_delete, 1, 0, 0 },
463 + {"no-force-delete", 0, POPT_ARG_VAL, &force_delete, 0, 0, 0 },
464 +#ifdef SUPPORT_FORCE_CHANGE
465 + {"force-change", 0, POPT_ARG_VAL, &force_change, ALL_IMMUTABLE, 0, 0 },
466 + {"no-force-change", 0, POPT_ARG_VAL, &force_change, 0, 0, 0 },
467 + {"force-uchange", 0, POPT_ARG_VAL, &force_change, USR_IMMUTABLE, 0, 0 },
468 + {"force-schange", 0, POPT_ARG_VAL, &force_change, SYS_IMMUTABLE, 0, 0 },
470 {"ignore-errors", 0, POPT_ARG_VAL, &ignore_errors, 1, 0, 0 },
471 {"no-ignore-errors", 0, POPT_ARG_VAL, &ignore_errors, 0, 0, 0 },
472 {"max-delete", 0, POPT_ARG_INT, &max_delete, 0, 0, 0 },
473 @@ -2536,6 +2562,9 @@ void server_options(char **args, int *argc_p)
474 if (xfer_dirs && !recurse && delete_mode && am_sender)
475 args[ac++] = "--no-r";
477 + if (preserve_fileflags)
478 + args[ac++] = "--fileflags";
480 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
481 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
483 @@ -2623,6 +2652,16 @@ void server_options(char **args, int *argc_p)
484 args[ac++] = "--delete-excluded";
486 args[ac++] = "--force";
487 +#ifdef SUPPORT_FORCE_CHANGE
488 + if (force_change) {
489 + if (force_change == ALL_IMMUTABLE)
490 + args[ac++] = "--force-change";
491 + else if (force_change == USR_IMMUTABLE)
492 + args[ac++] = "--force-uchange";
493 + else if (force_change == SYS_IMMUTABLE)
494 + args[ac++] = "--force-schange";
498 args[ac++] = "--only-write-batch=X";
500 diff --git a/rsync.c b/rsync.c
503 @@ -31,6 +31,7 @@ extern int dry_run;
504 extern int preserve_acls;
505 extern int preserve_xattrs;
506 extern int preserve_perms;
507 +extern int preserve_fileflags;
508 extern int preserve_executability;
509 extern int preserve_times;
511 @@ -452,6 +453,39 @@ mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
515 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
516 +/* Set a file's st_flags. */
517 +static int set_fileflags(const char *fname, uint32 fileflags)
519 + if (do_chflags(fname, fileflags) != 0) {
520 + rsyserr(FERROR_XFER, errno,
521 + "failed to set file flags on %s",
522 + full_fname(fname));
529 +/* Remove immutable flags from an object, so it can be altered/removed. */
530 +int make_mutable(const char *fname, mode_t mode, uint32 fileflags, uint32 iflags)
532 + if (S_ISLNK(mode) || !(fileflags & iflags))
534 + if (!set_fileflags(fname, fileflags & ~iflags))
539 +/* Undo a prior make_mutable() call that returned a 1. */
540 +int undo_make_mutable(const char *fname, uint32 fileflags)
542 + if (!set_fileflags(fname, fileflags))
548 int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
549 const char *fnamecmp, int flags)
551 @@ -513,7 +547,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
553 uid_t uid = change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid;
554 gid_t gid = change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid;
555 - if (do_lchown(fname, uid, gid) != 0) {
556 + if (do_lchown(fname, uid, gid, sxp->st.st_mode, ST_FLAGS(sxp->st)) != 0) {
557 /* We shouldn't have attempted to change uid
558 * or gid unless have the privilege. */
559 rsyserr(FERROR_XFER, errno, "%s %s failed",
560 @@ -549,7 +583,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
561 flags |= ATTRS_SKIP_MTIME;
562 if (!(flags & ATTRS_SKIP_MTIME)
563 && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
564 - int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode);
565 + int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), sxp->st.st_mode, ST_FLAGS(sxp->st));
567 rsyserr(FERROR_XFER, errno, "failed to set times on %s",
569 @@ -576,7 +610,7 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
572 if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
573 - int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
574 + int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode, ST_FLAGS(sxp->st));
576 rsyserr(FERROR_XFER, errno,
577 "failed to set permissions on %s",
578 @@ -588,6 +622,19 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
582 +#ifdef SUPPORT_FILEFLAGS
583 + if (preserve_fileflags && !S_ISLNK(sxp->st.st_mode)
584 + && sxp->st.st_flags != F_FFLAGS(file)) {
585 + uint32 fileflags = F_FFLAGS(file);
586 + if (flags & ATTRS_DELAY_IMMUTABLE)
587 + fileflags &= ~ALL_IMMUTABLE;
588 + if (sxp->st.st_flags != fileflags
589 + && !set_fileflags(fname, fileflags))
595 if (INFO_GTE(NAME, 2) && flags & ATTRS_REPORT) {
597 rprintf(FCLIENT, "%s\n", fname);
598 @@ -662,7 +709,8 @@ int finish_transfer(const char *fname, const char *fnametmp,
600 /* Change permissions before putting the file into place. */
601 set_file_attrs(fnametmp, file, NULL, fnamecmp,
602 - ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
603 + ATTRS_DELAY_IMMUTABLE
604 + | (ok_to_set_time ? 0 : ATTRS_SKIP_MTIME));
606 /* move tmp file over real file */
607 if (DEBUG_GTE(RECV, 1))
608 @@ -679,6 +727,10 @@ int finish_transfer(const char *fname, const char *fnametmp,
611 /* The file was moved into place (not copied), so it's done. */
612 +#ifdef SUPPORT_FILEFLAGS
613 + if (preserve_fileflags && F_FFLAGS(file) & ALL_IMMUTABLE)
614 + set_fileflags(fname, F_FFLAGS(file));
618 /* The file was copied, so tweak the perms of the copied file. If it
619 diff --git a/rsync.h b/rsync.h
623 #define XMIT_HLINK_FIRST (1<<12) /* protocols 30 - now (HLINKED files only) */
624 #define XMIT_IO_ERROR_ENDLIST (1<<12) /* protocols 31*- now (w/XMIT_EXTENDED_FLAGS) (also protocol 30 w/'f' compat flag) */
625 #define XMIT_MOD_NSEC (1<<13) /* protocols 31 - now */
626 +#define XMIT_SAME_FLAGS (1<<14) /* protocols ?? - now */
628 /* These flags are used in the live flist data. */
632 #define ATTRS_REPORT (1<<0)
633 #define ATTRS_SKIP_MTIME (1<<1)
634 +#define ATTRS_DELAY_IMMUTABLE (1<<2)
637 #define NORMAL_FLUSH 0
639 #define ITEM_REPORT_GROUP (1<<6)
640 #define ITEM_REPORT_ACL (1<<7)
641 #define ITEM_REPORT_XATTR (1<<8)
642 +#define ITEM_REPORT_FFLAGS (1<<9)
643 #define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
644 #define ITEM_XNAME_FOLLOWS (1<<12)
645 #define ITEM_IS_NEW (1<<13)
646 @@ -522,6 +525,28 @@ typedef unsigned int size_t;
650 +#define NO_FFLAGS ((uint32)-1)
653 +#define SUPPORT_FILEFLAGS 1
654 +#define SUPPORT_FORCE_CHANGE 1
657 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
659 +#define UF_NOUNLINK 0
662 +#define SF_NOUNLINK 0
664 +#define USR_IMMUTABLE (UF_IMMUTABLE|UF_NOUNLINK|UF_APPEND)
665 +#define SYS_IMMUTABLE (SF_IMMUTABLE|SF_NOUNLINK|SF_APPEND)
666 +#define ALL_IMMUTABLE (USR_IMMUTABLE|SYS_IMMUTABLE)
667 +#define ST_FLAGS(st) (st.st_flags)
669 +#define ST_FLAGS(st) NO_FFLAGS
672 /* Find a variable that is either exactly 32-bits or longer.
673 * If some code depends on 32-bit truncation, it will need to
674 * take special action in a "#if SIZEOF_INT32 > 4" section. */
675 @@ -709,6 +734,7 @@ extern int file_extra_cnt;
676 extern int inc_recurse;
679 +extern int fileflags_ndx;
681 extern int xattrs_ndx;
683 @@ -750,6 +776,11 @@ extern int xattrs_ndx;
684 /* When the associated option is on, all entries will have these present: */
685 #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
686 #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
687 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
688 +#define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
690 +#define F_FFLAGS(f) NO_FFLAGS
692 #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
693 #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
694 #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
695 diff --git a/rsync.yo b/rsync.yo
698 @@ -359,6 +359,7 @@ to the detailed description below for a complete description. verb(
699 -K, --keep-dirlinks treat symlinked dir on receiver as dir
700 -H, --hard-links preserve hard links
701 -p, --perms preserve permissions
702 + --fileflags preserve file-flags (aka chflags)
703 -E, --executability preserve executability
704 --chmod=CHMOD affect file and/or directory permissions
705 -A, --acls preserve ACLs (implies -p)
706 @@ -394,7 +395,10 @@ to the detailed description below for a complete description. verb(
707 --ignore-missing-args ignore missing source args without error
708 --delete-missing-args delete missing source args from destination
709 --ignore-errors delete even if there are I/O errors
710 - --force force deletion of dirs even if not empty
711 + --force-delete force deletion of dirs even if not empty
712 + --force-change affect user/system immutable files/dirs
713 + --force-uchange affect user-immutable files/dirs
714 + --force-schange affect system-immutable files/dirs
715 --max-delete=NUM don't delete more than NUM files
716 --max-size=SIZE don't transfer any file larger than SIZE
717 --min-size=SIZE don't transfer any file smaller than SIZE
718 @@ -641,7 +645,8 @@ specified, in which case bf(-r) is not implied.
720 Note that bf(-a) bf(does not preserve hardlinks), because
721 finding multiply-linked files is expensive. You must separately
723 +specify bf(-H). Note also that for backward compatibility, bf(-a)
724 +currently does bf(not) imply the bf(--fileflags) option.
726 dit(--no-OPTION) You may turn off one or more implied options by prefixing
727 the option name with "no-". Not all options may be prefixed with a "no-":
728 @@ -940,7 +945,7 @@ they would be using bf(--copy-links).
729 Without this option, if the sending side has replaced a directory with a
730 symlink to a directory, the receiving side will delete anything that is in
731 the way of the new symlink, including a directory hierarchy (as long as
732 -bf(--force) or bf(--delete) is in effect).
733 +bf(--force-delete) or bf(--delete) is in effect).
735 See also bf(--keep-dirlinks) for an analogous option for the receiving
737 @@ -1103,6 +1108,29 @@ Note that this option does not copy rsyncs special xattr values (e.g. those
738 used by bf(--fake-super)) unless you repeat the option (e.g. -XX). This
739 "copy all xattrs" mode cannot be used with bf(--fake-super).
741 +dit(bf(--fileflags)) This option causes rsync to update the file-flags to be
742 +the same as the source files and directories (if your OS supports the
743 +bf(chflags)(2) system call). Some flags can only be altered by the super-user
744 +and some might only be unset below a certain secure-level (usually single-user
745 +mode). It will not make files alterable that are set to immutable on the
746 +receiver. To do that, see bf(--force-change), bf(--force-uchange), and
747 +bf(--force-schange).
749 +dit(bf(--force-change)) This option causes rsync to disable both user-immutable
750 +and system-immutable flags on files and directories that are being updated or
751 +deleted on the receiving side. This option overrides bf(--force-uchange) and
752 +bf(--force-schange).
754 +dit(bf(--force-uchange)) This option causes rsync to disable user-immutable
755 +flags on files and directories that are being updated or deleted on the
756 +receiving side. It does not try to affect system flags. This option overrides
757 +bf(--force-change) and bf(--force-schange).
759 +dit(bf(--force-schange)) This option causes rsync to disable system-immutable
760 +flags on files and directories that are being updated or deleted on the
761 +receiving side. It does not try to affect user flags. This option overrides
762 +bf(--force-change) and bf(--force-uchange).
764 dit(bf(--chmod)) This option tells rsync to apply one or more
765 comma-separated "chmod" modes to the permission of the files in the
766 transfer. The resulting value is treated as though it were the permissions
767 @@ -1432,12 +1460,13 @@ display as a "*missing" entry in the bf(--list-only) output.
768 dit(bf(--ignore-errors)) Tells bf(--delete) to go ahead and delete files
769 even when there are I/O errors.
771 -dit(bf(--force)) This option tells rsync to delete a non-empty directory
772 +dit(bf(--force-delete)) This option tells rsync to delete a non-empty directory
773 when it is to be replaced by a non-directory. This is only relevant if
774 deletions are not active (see bf(--delete) for details).
776 -Note for older rsync versions: bf(--force) used to still be required when
777 -using bf(--delete-after), and it used to be non-functional unless the
778 +This option can be abbreviated bf(--force) for backward compatibility.
779 +Note that some older rsync versions used to still require bf(--force)
780 +when using bf(--delete-after), and it used to be non-functional unless the
781 bf(--recursive) option was also enabled.
783 dit(bf(--max-delete=NUM)) This tells rsync not to delete more than NUM
784 @@ -2071,7 +2100,7 @@ with older versions of rsync, but that also turns on the output of other
787 The "%i" escape has a cryptic output that is 11 letters long. The general
788 -format is like the string bf(YXcstpoguax), where bf(Y) is replaced by the
789 +format is like the string bf(YXcstpogfax), where bf(Y) is replaced by the
790 type of update being done, bf(X) is replaced by the file-type, and the
791 other letters represent attributes that may be output if they are being
793 @@ -2127,7 +2156,7 @@ quote(itemization(
794 sender's value (requires bf(--owner) and super-user privileges).
795 it() A bf(g) means the group is different and is being updated to the
796 sender's value (requires bf(--group) and the authority to set the group).
797 - it() The bf(u) slot is reserved for future use.
798 + it() The bf(f) means that the fileflags information changed.
799 it() The bf(a) means that the ACL information changed.
800 it() The bf(x) means that the extended attribute information changed.
802 diff --git a/syscall.c b/syscall.c
805 @@ -38,6 +38,7 @@ extern int am_root;
806 extern int am_sender;
807 extern int read_only;
808 extern int list_only;
809 +extern int force_change;
810 extern int preserve_perms;
811 extern int preserve_executability;
813 @@ -55,7 +56,23 @@ int do_unlink(const char *fname)
815 if (dry_run) return 0;
816 RETURN_ERROR_IF_RO_OR_LO;
817 - return unlink(fname);
818 + if (unlink(fname) == 0)
820 +#ifdef SUPPORT_FORCE_CHANGE
821 + if (force_change && errno == EPERM) {
824 + if (x_lstat(fname, &st, NULL) == 0
825 + && make_mutable(fname, st.st_mode, st.st_flags, force_change) > 0) {
826 + if (unlink(fname) == 0)
828 + undo_make_mutable(fname, st.st_flags);
830 + /* TODO: handle immutable directories */
838 @@ -116,14 +133,37 @@ int do_link(const char *fname1, const char *fname2)
842 -int do_lchown(const char *path, uid_t owner, gid_t group)
843 +int do_lchown(const char *path, uid_t owner, gid_t group, mode_t mode, uint32 fileflags)
845 if (dry_run) return 0;
846 RETURN_ERROR_IF_RO_OR_LO;
850 - return lchown(path, owner, group);
851 + if (lchown(path, owner, group) == 0)
853 +#ifdef SUPPORT_FORCE_CHANGE
854 + if (force_change && errno == EPERM) {
855 + if (fileflags == NO_FFLAGS) {
857 + if (x_lstat(path, &st, NULL) == 0) {
859 + fileflags = st.st_flags;
862 + if (fileflags != NO_FFLAGS
863 + && make_mutable(path, mode, fileflags, force_change) > 0) {
864 + int ret = lchown(path, owner, group);
865 + undo_make_mutable(path, fileflags);
872 + mode = fileflags = 0; /* avoid compiler warning */
877 int do_mknod(const char *pathname, mode_t mode, dev_t dev)
878 @@ -163,7 +203,7 @@ int do_mknod(const char *pathname, mode_t mode, dev_t dev)
882 - return do_chmod(pathname, mode);
883 + return do_chmod(pathname, mode, 0);
887 @@ -180,7 +220,22 @@ int do_rmdir(const char *pathname)
889 if (dry_run) return 0;
890 RETURN_ERROR_IF_RO_OR_LO;
891 - return rmdir(pathname);
892 + if (rmdir(pathname) == 0)
894 +#ifdef SUPPORT_FORCE_CHANGE
895 + if (force_change && errno == EPERM) {
898 + if (x_lstat(pathname, &st, NULL) == 0
899 + && make_mutable(pathname, st.st_mode, st.st_flags, force_change) > 0) {
900 + if (rmdir(pathname) == 0)
902 + undo_make_mutable(pathname, st.st_flags);
910 int do_open(const char *pathname, int flags, mode_t mode)
911 @@ -194,7 +249,7 @@ int do_open(const char *pathname, int flags, mode_t mode)
915 -int do_chmod(const char *path, mode_t mode)
916 +int do_chmod(const char *path, mode_t mode, uint32 fileflags)
919 if (dry_run) return 0;
920 @@ -217,17 +272,74 @@ int do_chmod(const char *path, mode_t mode)
922 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
923 #endif /* !HAVE_LCHMOD */
924 +#ifdef SUPPORT_FORCE_CHANGE
925 + if (code < 0 && force_change && errno == EPERM && !S_ISLNK(mode)) {
926 + if (fileflags == NO_FFLAGS) {
928 + if (x_lstat(path, &st, NULL) == 0)
929 + fileflags = st.st_flags;
931 + if (fileflags != NO_FFLAGS
932 + && make_mutable(path, mode, fileflags, force_change) > 0) {
933 + code = chmod(path, mode & CHMOD_BITS);
934 + undo_make_mutable(path, fileflags);
941 + fileflags = 0; /* avoid compiler warning */
943 if (code != 0 && (preserve_perms || preserve_executability))
950 +int do_chflags(const char *path, uint32 fileflags)
952 + if (dry_run) return 0;
953 + RETURN_ERROR_IF_RO_OR_LO;
954 + return chflags(path, fileflags);
958 int do_rename(const char *fname1, const char *fname2)
960 if (dry_run) return 0;
961 RETURN_ERROR_IF_RO_OR_LO;
962 - return rename(fname1, fname2);
963 + if (rename(fname1, fname2) == 0)
965 +#ifdef SUPPORT_FORCE_CHANGE
966 + if (force_change && errno == EPERM) {
967 + STRUCT_STAT st1, st2;
968 + int became_mutable;
970 + if (x_lstat(fname1, &st1, NULL) != 0)
972 + became_mutable = make_mutable(fname1, st1.st_mode, st1.st_flags, force_change) > 0;
973 + if (became_mutable && rename(fname1, fname2) == 0)
975 + if (x_lstat(fname2, &st2, NULL) == 0
976 + && make_mutable(fname2, st2.st_mode, st2.st_flags, force_change) > 0) {
977 + if (rename(fname1, fname2) == 0) {
979 + if (became_mutable) /* Yes, use fname2 and st1! */
980 + undo_make_mutable(fname2, st1.st_flags);
983 + undo_make_mutable(fname2, st2.st_flags);
985 + /* TODO: handle immutable directories */
986 + if (became_mutable)
987 + undo_make_mutable(fname1, st1.st_flags);
995 #ifdef HAVE_FTRUNCATE
996 diff --git a/t_stub.c b/t_stub.c
999 @@ -28,6 +28,7 @@ int module_id = -1;
1000 int checksum_len = 0;
1001 int relative_paths = 0;
1002 int module_dirlen = 0;
1003 +int force_change = 0;
1004 int preserve_acls = 0;
1005 int preserve_times = 0;
1006 int preserve_xattrs = 0;
1007 @@ -97,3 +98,23 @@ filter_rule_list daemon_filter_list;
1012 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
1013 + int make_mutable(UNUSED(const char *fname), UNUSED(mode_t mode), UNUSED(uint32 fileflags), UNUSED(uint32 iflags))
1018 +/* Undo a prior make_mutable() call that returned a 1. */
1019 + int undo_make_mutable(UNUSED(const char *fname), UNUSED(uint32 fileflags))
1025 +#ifdef SUPPORT_XATTRS
1026 + int x_lstat(UNUSED(const char *fname), UNUSED(STRUCT_STAT *fst), UNUSED(STRUCT_STAT *xst))
1031 diff --git a/util.c b/util.c
1034 @@ -33,6 +33,7 @@ extern int relative_paths;
1035 extern int preserve_times;
1036 extern int preserve_xattrs;
1037 extern int preallocate_files;
1038 +extern int force_change;
1039 extern char *module_dir;
1040 extern unsigned int module_dirlen;
1041 extern char *partial_dir;
1042 @@ -115,9 +116,36 @@ void print_child_argv(const char *prefix, char **cmd)
1043 rprintf(FCLIENT, " (%d args)\n", cnt);
1046 +#ifdef SUPPORT_FORCE_CHANGE
1047 +static int try_a_force_change(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode, uint32 fileflags)
1049 + if (fileflags == NO_FFLAGS) {
1051 + if (x_lstat(fname, &st, NULL) == 0)
1052 + fileflags = st.st_flags;
1055 + if (fileflags != NO_FFLAGS && make_mutable(fname, mode, fileflags, force_change) > 0) {
1056 + int ret, save_force_change = force_change;
1058 + force_change = 0; /* Make certain we can't come back here. */
1059 + ret = set_modtime(fname, modtime, mod_nsec, mode, fileflags);
1060 + force_change = save_force_change;
1062 + undo_make_mutable(fname, fileflags);
1073 /* This returns 0 for success, 1 for a symlink if symlink time-setting
1074 * is not possible, or -1 for any other error. */
1075 -int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1076 +int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode, uint32 fileflags)
1078 static int switch_step = 0;
1080 @@ -132,6 +160,11 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1082 if (do_utimensat(fname, modtime, mod_nsec) == 0)
1084 +#ifdef SUPPORT_FORCE_CHANGE
1085 + if (force_change && errno == EPERM
1086 + && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1089 if (errno != ENOSYS)
1092 @@ -142,6 +175,11 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1094 if (do_lutimes(fname, modtime, mod_nsec) == 0)
1096 +#ifdef SUPPORT_FORCE_CHANGE
1097 + if (force_change && errno == EPERM
1098 + && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1101 if (errno != ENOSYS)
1104 @@ -165,6 +203,13 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
1105 if (do_utime(fname, modtime, mod_nsec) == 0)
1108 +#ifdef SUPPORT_FORCE_CHANGE
1109 + if (force_change && errno == EPERM
1110 + && try_a_force_change(fname, modtime, mod_nsec, mode, fileflags) == 0)
1113 + fileflags = 0; /* avoid compiler warning */
1118 diff --git a/xattrs.c b/xattrs.c
1121 @@ -1045,7 +1045,7 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
1122 mode = (fst.st_mode & _S_IFMT) | (fmode & ACCESSPERMS)
1123 | (S_ISDIR(fst.st_mode) ? 0700 : 0600);
1124 if (fst.st_mode != mode)
1125 - do_chmod(fname, mode);
1126 + do_chmod(fname, mode, ST_FLAGS(fst));
1127 if (!IS_DEVICE(fst.st_mode))
1128 fst.st_rdev = 0; /* just in case */