1 This patch provides --flags, which preserves the st_flags 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/flags.diff
11 TODO: fix --delete-delay to work with --flags option.
15 @@ -44,6 +44,7 @@ extern int protocol_version;
16 extern int protect_args;
17 extern int preserve_uid;
18 extern int preserve_gid;
19 +extern int preserve_fileflags;
20 extern int preserve_acls;
21 extern int preserve_xattrs;
22 extern int need_messages_from_generator;
23 @@ -63,7 +64,7 @@ extern iconv_t ic_send, ic_recv;
26 /* These index values are for the file-list's extra-attribute array. */
27 -int uid_ndx, gid_ndx, acls_ndx, xattrs_ndx;
28 +int uid_ndx, gid_ndx, fileflags_ndx, acls_ndx, xattrs_ndx;
32 @@ -115,6 +116,8 @@ void setup_protocol(int f_out,int f_in)
33 uid_ndx = ++file_extra_cnt;
35 gid_ndx = ++file_extra_cnt;
36 + if (preserve_fileflags)
37 + fileflags_ndx = ++file_extra_cnt;
38 if (preserve_acls && !am_sender)
39 acls_ndx = ++file_extra_cnt;
43 @@ -560,7 +560,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strd
44 memmove lchown vsnprintf snprintf vasprintf asprintf setsid glob strpbrk \
45 strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
46 setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
47 - strerror putenv iconv_open locale_charset nl_langinfo getxattr \
48 + chflags strerror putenv iconv_open locale_charset nl_langinfo getxattr \
49 extattr_get_link sigaction sigprocmask)
51 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
54 @@ -50,6 +50,7 @@ extern int preserve_links;
55 extern int preserve_hard_links;
56 extern int preserve_devices;
57 extern int preserve_specials;
58 +extern int fileflags_ndx;
62 @@ -364,6 +365,9 @@ static void send_file_entry(int f, struc
64 static time_t modtime;
67 + static uint32 fileflags;
71 static uint32 rdev_major;
72 @@ -411,6 +415,12 @@ static void send_file_entry(int f, struc
73 xflags |= XMIT_SAME_MODE;
77 + if (F_FFLAGS(file) == fileflags)
78 + xflags |= XMIT_SAME_FLAGS;
80 + fileflags = F_FFLAGS(file);
83 if (protocol_version >= 30 && S_ISDIR(mode) && !(file->flags & FLAG_XFER_DIR))
84 xflags |= XMIT_NON_XFER_DIR;
85 @@ -534,6 +544,10 @@ static void send_file_entry(int f, struc
87 if (!(xflags & XMIT_SAME_MODE))
88 write_int(f, to_wire_mode(mode));
90 + if (fileflags_ndx && !(xflags & XMIT_SAME_FLAGS))
91 + write_int(f, (int)fileflags);
93 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
94 if (protocol_version < 30)
96 @@ -622,6 +636,9 @@ static struct file_struct *recv_file_ent
100 +#ifdef SUPPORT_FLAGS
101 + static uint32 fileflags;
105 static uint32 rdev_major;
106 @@ -751,9 +768,12 @@ static struct file_struct *recv_file_ent
108 if (!(xflags & XMIT_SAME_MODE))
109 mode = from_wire_mode(read_int(f));
111 if (chmod_modes && !S_ISLNK(mode))
112 mode = tweak_mode(mode, chmod_modes);
113 +#ifdef SUPPORT_FLAGS
114 + if (fileflags_ndx && !(xflags & XMIT_SAME_FLAGS))
115 + fileflags = (uint32)read_int(f);
118 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
119 if (protocol_version < 30)
120 @@ -873,6 +893,10 @@ static struct file_struct *recv_file_ent
121 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
124 +#ifdef SUPPORT_FLAGS
126 + F_FFLAGS(file) = fileflags;
131 @@ -1192,6 +1216,10 @@ struct file_struct *make_file(const char
132 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
134 file->mode = st.st_mode;
135 +#ifdef SUPPORT_FLAGS
137 + F_FFLAGS(file) = st.st_flags;
140 F_OWNER(file) = st.st_uid;
144 @@ -114,6 +114,14 @@ static int need_retouch_dir_times;
145 static int need_retouch_dir_perms;
146 static const char *solo_file = NULL;
148 +#ifdef SUPPORT_FLAGS
149 +#define FF_PTR(p) F_FFLAGS(p)
150 +#define FF_STAT(s) s.st_flags
153 +#define FF_STAT(s) 0
156 /* For calling delete_item() and delete_dir_contents(). */
157 #define DEL_RECURSE (1<<1) /* recurse */
158 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
159 @@ -129,7 +137,6 @@ enum delret {
160 /* Forward declaration for delete_item(). */
161 static enum delret delete_dir_contents(char *fname, int flags);
164 static int is_backup_file(char *fn)
166 int k = strlen(fn) - backup_suffix_len;
167 @@ -142,17 +149,20 @@ static int is_backup_file(char *fn)
168 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
169 * a directory! (The buffer is used for recursion, but returned unchanged.)
171 -static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
172 +static enum delret delete_item(char *fbuf, int mode, uint32 fileflags, char *replace, int flags)
179 - rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
180 - fbuf, mode, flags);
181 + rprintf(FINFO, "delete_item(%s) mode=%o fileflags=%o flags=%d\n",
182 + fbuf, mode, fileflags, flags);
185 +#ifdef SUPPORT_FLAGS
186 + make_mutable(fbuf, mode, fileflags);
188 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
189 ignore_perishable = 1;
190 /* If DEL_RECURSE is not set, this just reports emptiness. */
191 @@ -264,7 +274,7 @@ static enum delret delete_dir_contents(c
192 if (S_ISDIR(fp->mode)
193 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
195 - if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
196 + if (delete_item(fname, fp->mode, FF_PTR(fp), NULL, flags) != DR_SUCCESS)
200 @@ -320,8 +330,9 @@ static int remember_delete(struct file_s
203 len = snprintf(deldelay_buf + deldelay_cnt,
204 - deldelay_size - deldelay_cnt,
205 - "%x %s%c", (int)file->mode, fname, '\0');
206 + deldelay_size - deldelay_cnt, "%x %x %s%c",
207 + (int)file->mode, (int)FF_PTR(file),
209 if ((deldelay_cnt += len) <= deldelay_size)
211 if (deldelay_fd < 0 && !start_delete_delay_temp())
212 @@ -334,7 +345,7 @@ static int remember_delete(struct file_s
216 -static int read_delay_line(char *buf)
217 +static int read_delay_line(char *buf, int *fileflags_p)
219 static int read_pos = 0;
221 @@ -376,7 +387,7 @@ static int read_delay_line(char *buf)
223 bp = deldelay_buf + read_pos;
225 - if (sscanf(bp, "%x ", &mode) != 1) {
226 + if (sscanf(bp, "%x %x ", &mode, fileflags_p) != 2) {
228 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
230 @@ -399,15 +410,15 @@ static int read_delay_line(char *buf)
232 static void do_delayed_deletions(char *delbuf)
235 + int mode, fileflags;
237 if (deldelay_fd >= 0) {
238 if (deldelay_cnt && !flush_delete_delay())
240 lseek(deldelay_fd, 0, 0);
242 - while ((mode = read_delay_line(delbuf)) >= 0)
243 - delete_item(delbuf, mode, NULL, DEL_RECURSE);
244 + while ((mode = read_delay_line(delbuf, &fileflags)) >= 0)
245 + delete_item(delbuf, mode, fileflags, NULL, DEL_RECURSE);
246 if (deldelay_fd >= 0)
249 @@ -474,7 +485,7 @@ static void delete_in_dir(char *fbuf, st
250 if (!remember_delete(fp, delbuf))
253 - delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
254 + delete_item(delbuf, fp->mode, FF_PTR(fp), NULL, DEL_RECURSE);
258 @@ -1233,7 +1244,7 @@ static void recv_generator(char *fname,
259 * we need to delete it. If it doesn't exist, then
260 * (perhaps recursively) create it. */
261 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
262 - if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
263 + if (delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), "directory", del_opts) != 0)
264 goto skipping_dir_contents;
267 @@ -1362,7 +1373,7 @@ static void recv_generator(char *fname,
269 /* Not the right symlink (or not a symlink), so
271 - if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
272 + if (delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), "symlink", del_opts) != 0)
274 } else if (basis_dir[0] != NULL) {
275 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
276 @@ -1441,7 +1452,7 @@ static void recv_generator(char *fname,
277 goto return_with_success;
280 - if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
281 + if (delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), t, del_opts) != 0)
283 } else if (basis_dir[0] != NULL) {
284 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
285 @@ -1532,7 +1543,7 @@ static void recv_generator(char *fname,
286 fnamecmp_type = FNAMECMP_FNAME;
288 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
289 - if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
290 + if (delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), "regular file", del_opts) != 0)
296 @@ -50,6 +50,7 @@ int preserve_hard_links = 0;
297 int preserve_acls = 0;
298 int preserve_xattrs = 0;
299 int preserve_perms = 0;
300 +int preserve_fileflags = 0;
301 int preserve_executability = 0;
302 int preserve_devices = 0;
303 int preserve_specials = 0;
304 @@ -220,6 +221,7 @@ static void print_rsync_version(enum log
305 char const *links = "no ";
306 char const *iconv = "no ";
307 char const *ipv6 = "no ";
308 + char const *fileflags = "no ";
309 STRUCT_STAT *dumstat;
311 #if SUBPROTOCOL_VERSION != 0
312 @@ -249,6 +251,9 @@ static void print_rsync_version(enum log
316 +#ifdef SUPPORT_FLAGS
320 rprintf(f, "%s version %s protocol version %d%s\n",
321 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
322 @@ -262,8 +267,8 @@ static void print_rsync_version(enum log
323 (int)(sizeof (int64) * 8));
324 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
325 got_socketpair, hardlinks, links, ipv6, have_inplace);
326 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv\n",
327 - have_inplace, acls, xattrs, iconv);
328 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %sfile-flags\n",
329 + have_inplace, acls, xattrs, iconv, fileflags);
331 #ifdef MAINTAINER_MODE
332 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
333 @@ -330,6 +335,7 @@ void usage(enum logcode F)
334 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
335 rprintf(F," -H, --hard-links preserve hard links\n");
336 rprintf(F," -p, --perms preserve permissions\n");
337 + rprintf(F," --fileflags preserve file-flags\n");
338 rprintf(F," -E, --executability preserve the file's executability\n");
339 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
341 @@ -470,6 +476,8 @@ static struct poptOption long_options[]
342 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
343 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
344 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
345 + {"fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 1, 0, 0 },
346 + {"no-fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 0, 0, 0 },
347 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
348 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
349 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
350 @@ -1283,6 +1291,15 @@ int parse_arguments(int *argc_p, const c
354 +#ifndef SUPPORT_FLAGS
355 + if (preserve_fileflags) {
356 + snprintf(err_buf, sizeof err_buf,
357 + "file flags are not supported on this %s\n",
358 + am_server ? "server" : "client");
363 if (write_batch && read_batch) {
364 snprintf(err_buf, sizeof err_buf,
365 "--write-batch and --read-batch can not be used together\n");
366 @@ -1794,6 +1811,9 @@ void server_options(char **args, int *ar
367 if (xfer_dirs && !recurse && delete_mode && am_sender)
368 args[ac++] = "--no-r";
370 + if (preserve_fileflags)
371 + args[ac++] = "--flags";
373 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
374 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
378 @@ -31,6 +31,7 @@ extern int dry_run;
379 extern int preserve_acls;
380 extern int preserve_xattrs;
381 extern int preserve_perms;
382 +extern int preserve_fileflags;
383 extern int preserve_executability;
384 extern int preserve_times;
386 @@ -59,6 +60,16 @@ iconv_t ic_chck = (iconv_t)-1;
387 iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
390 +#ifdef SUPPORT_FLAGS
392 +#define UF_NOUNLINK 0
395 +#define SF_NOUNLINK 0
397 +#define NOCHANGE_FLAGS (UF_IMMUTABLE|UF_APPEND|UF_NOUNLINK|SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK)
400 static const char *default_charset(void)
402 # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
403 @@ -336,6 +347,41 @@ mode_t dest_mode(mode_t flist_mode, mode
407 +#ifdef SUPPORT_FLAGS
408 +/* Set a file's st_flags. */
409 +static int set_fileflags(const char *fname, uint32 fileflags)
411 + if (do_chflags(fname, fileflags) != 0) {
412 + rsyserr(FERROR, errno,
413 + "failed to set file flags on %s",
414 + full_fname(fname));
421 +/* Remove immutable flags from an object, so it can be altered/removed. */
422 +void make_mutable(char *fname, mode_t mode, uint32 fileflags)
424 + if (!preserve_fileflags && S_ISLNK(mode))
427 + if (fileflags & NOCHANGE_FLAGS)
428 + set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
431 +/* Undo a prior make_mutable() call. */
432 +void undo_make_mutable(char *fname, mode_t mode, uint32 fileflags)
434 + if (!preserve_fileflags && S_ISLNK(mode))
437 + if (fileflags & NOCHANGE_FLAGS)
438 + set_fileflags(fname, fileflags);
442 int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp,
443 const char *fnamecmp, int flags)
445 @@ -467,6 +513,15 @@ int set_file_attrs(const char *fname, st
449 +#ifdef SUPPORT_FLAGS
450 + if (preserve_fileflags && !S_ISLNK(sxp->st.st_mode)
451 + && sxp->st.st_flags != F_FFLAGS(file)) {
452 + if (!set_fileflags(fname, F_FFLAGS(file)))
458 if (verbose > 1 && flags & ATTRS_REPORT) {
460 rprintf(FCLIENT, "%s\n", fname);
461 @@ -526,6 +581,9 @@ void finish_transfer(const char *fname,
462 set_file_attrs(fnametmp, file, NULL, fnamecmp,
463 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
465 +#ifdef SUPPORT_FLAGS
466 + make_mutable(fnametmp, file->mode, F_FFLAGS(file));
468 /* move tmp file over real file */
470 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
471 @@ -540,6 +598,9 @@ void finish_transfer(const char *fname,
474 /* The file was moved into place (not copied), so it's done. */
475 +#ifdef SUPPORT_FLAGS
476 + undo_make_mutable(fname, file->mode, F_FFLAGS(file));
480 /* The file was copied, so tweak the perms of the copied file. If it
484 #define XMIT_RDEV_MINOR_8_pre30 (1<<11) /* protocols 28 - 29 */
485 #define XMIT_GROUP_NAME_FOLLOWS (1<<11) /* protocols 30 - now */
486 #define XMIT_HLINK_FIRST (1<<12) /* protocols 30 - now (HLINKED files only) */
487 +#define XMIT_SAME_FLAGS (1<<14) /* protocols ?? - now */
489 /* These flags are used in the live flist data. */
491 @@ -406,6 +407,10 @@ enum msgcode {
496 +#define SUPPORT_FLAGS 1
499 /* Find a variable that is either exactly 32-bits or longer.
500 * If some code depends on 32-bit truncation, it will need to
501 * take special action in a "#if SIZEOF_INT32 > 4" section. */
502 @@ -573,6 +578,7 @@ extern int file_extra_cnt;
503 extern int inc_recurse;
506 +extern int fileflags_ndx;
508 extern int xattrs_ndx;
510 @@ -610,6 +616,7 @@ extern int xattrs_ndx;
511 /* When the associated option is on, all entries will have these present: */
512 #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
513 #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
514 +#define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
515 #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
516 #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
517 #define F_NDX(f) REQ_EXTRA(f, ic_ndx)->num
520 @@ -343,6 +343,7 @@ to the detailed description below for a
521 -K, --keep-dirlinks treat symlinked dir on receiver as dir
522 -H, --hard-links preserve hard links
523 -p, --perms preserve permissions
524 + --flags preserve file flags
525 -E, --executability preserve executability
526 --chmod=CHMOD affect file and/or directory permissions
527 -A, --acls preserve ACLs (implies -p)
528 @@ -543,7 +544,9 @@ specified, in which case bf(-r) is not i
530 Note that bf(-a) bf(does not preserve hardlinks), because
531 finding multiply-linked files is expensive. You must separately
533 +specify bf(-H). Note also that for compatibility, bf(-a)
534 +currently bf(does not include --flags) (see there) to include preserving
535 +change file flags (if supported by the OS).
537 dit(--no-OPTION) You may turn off one or more implied options by prefixing
538 the option name with "no-". Not all options may be prefixed with a "no-":
539 @@ -883,6 +886,13 @@ dit(bf(-X, --xattrs)) This option causes
540 extended attributes to be the same as the local ones. This will work
541 only if the remote machine's rsync also supports this option.
543 +dit(bf(--flags)) This option causes rsync to update the change file flags
544 +to be the same as the source file, if your OS supports the bf(chflags)(2)
545 +system call. In any case, an attempt is made to remove flags that would
546 +prevent a file to be altered. Some flags can only be altered by the
547 +super-user and can only be unset below a certain secure-level (usually
550 dit(bf(--chmod)) This option tells rsync to apply one or more
551 comma-separated "chmod" strings to the permission of the files in the
552 transfer. The resulting value is treated as though it was the permissions
555 @@ -162,6 +162,15 @@ int do_chmod(const char *path, mode_t mo
559 +#ifdef SUPPORT_FLAGS
560 +int do_chflags(const char *path, u_long flags)
562 + if (dry_run) return 0;
563 + RETURN_ERROR_IF_RO_OR_LO;
564 + return chflags(path, flags);
568 int do_rename(const char *fname1, const char *fname2)
570 if (dry_run) return 0;