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
13 @@ -44,6 +44,7 @@ extern int protocol_version;
14 extern int protect_args;
15 extern int preserve_uid;
16 extern int preserve_gid;
17 +extern int preserve_fileflags;
18 extern int preserve_acls;
19 extern int preserve_xattrs;
20 extern int need_messages_from_generator;
21 @@ -60,7 +61,7 @@ extern iconv_t ic_send, ic_recv;
24 /* These index values are for the file-list's extra-attribute array. */
25 -int uid_ndx, gid_ndx, acls_ndx, xattrs_ndx;
26 +int uid_ndx, gid_ndx, fileflags_ndx, acls_ndx, xattrs_ndx;
30 @@ -125,6 +126,8 @@ void setup_protocol(int f_out,int f_in)
31 uid_ndx = ++file_extra_cnt;
33 gid_ndx = ++file_extra_cnt;
34 + if (preserve_fileflags)
35 + fileflags_ndx = ++file_extra_cnt;
36 if (preserve_acls && !am_sender)
37 acls_ndx = ++file_extra_cnt;
41 @@ -557,7 +557,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strd
42 memmove lchown vsnprintf snprintf vasprintf asprintf setsid glob strpbrk \
43 strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
44 setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
45 - strerror putenv iconv_open locale_charset nl_langinfo getxattr \
46 + chflags strerror putenv iconv_open locale_charset nl_langinfo getxattr \
47 extattr_get_link sigaction sigprocmask setattrlist)
49 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
52 @@ -50,6 +50,7 @@ extern int preserve_links;
53 extern int preserve_hard_links;
54 extern int preserve_devices;
55 extern int preserve_specials;
56 +extern int fileflags_ndx;
60 @@ -335,6 +336,9 @@ static void send_file_entry(int f, struc
62 static time_t modtime;
65 + static uint32 fileflags;
67 #ifdef SUPPORT_HARD_LINKS
70 @@ -393,6 +397,12 @@ static void send_file_entry(int f, struc
71 xflags |= XMIT_SAME_MODE;
75 + if (F_FFLAGS(file) == fileflags)
76 + xflags |= XMIT_SAME_FLAGS;
78 + fileflags = F_FFLAGS(file);
81 if ((preserve_devices && IS_DEVICE(mode))
82 || (preserve_specials && IS_SPECIAL(mode))) {
83 @@ -513,6 +523,10 @@ static void send_file_entry(int f, struc
85 if (!(xflags & XMIT_SAME_MODE))
86 write_int(f, to_wire_mode(mode));
88 + if (fileflags_ndx && !(xflags & XMIT_SAME_FLAGS))
89 + write_int(f, (int)fileflags);
91 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
92 if (protocol_version < 30)
94 @@ -601,6 +615,9 @@ static struct file_struct *recv_file_ent
99 + static uint32 fileflags;
101 #ifdef SUPPORT_HARD_LINKS
104 @@ -732,9 +749,12 @@ static struct file_struct *recv_file_ent
106 if (!(xflags & XMIT_SAME_MODE))
107 mode = from_wire_mode(read_int(f));
109 if (chmod_modes && !S_ISLNK(mode))
110 mode = tweak_mode(mode, chmod_modes);
111 +#ifdef SUPPORT_FLAGS
112 + if (fileflags_ndx && !(xflags & XMIT_SAME_FLAGS))
113 + fileflags = (uint32)read_int(f);
116 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
117 if (protocol_version < 30)
118 @@ -854,6 +874,10 @@ static struct file_struct *recv_file_ent
119 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
122 +#ifdef SUPPORT_FLAGS
124 + F_FFLAGS(file) = fileflags;
129 @@ -1182,6 +1206,10 @@ struct file_struct *make_file(const char
130 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
132 file->mode = st.st_mode;
133 +#ifdef SUPPORT_FLAGS
135 + F_FFLAGS(file) = st.st_flags;
138 F_OWNER(file) = st.st_uid;
142 @@ -114,6 +114,14 @@ static int need_retouch_dir_times;
143 static int need_retouch_dir_perms;
144 static const char *solo_file = NULL;
146 +#ifdef SUPPORT_FLAGS
147 +#define FF_PTR(p) F_FFLAGS(p)
148 +#define FF_STAT(s) s.st_flags
151 +#define FF_STAT(s) 0
154 /* For calling delete_item() and delete_dir_contents(). */
155 #define DEL_RECURSE (1<<1) /* recurse */
156 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
157 @@ -129,7 +137,6 @@ enum delret {
158 /* Forward declaration for delete_item(). */
159 static enum delret delete_dir_contents(char *fname, int flags);
162 static int is_backup_file(char *fn)
164 int k = strlen(fn) - backup_suffix_len;
165 @@ -142,17 +149,20 @@ static int is_backup_file(char *fn)
166 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
167 * a directory! (The buffer is used for recursion, but returned unchanged.)
169 -static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
170 +static enum delret delete_item(char *fbuf, int mode, uint32 fileflags, char *replace, int flags)
177 - rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
178 - fbuf, mode, flags);
179 + rprintf(FINFO, "delete_item(%s) mode=%o fileflags=%o flags=%d\n",
180 + fbuf, mode, fileflags, flags);
183 +#ifdef SUPPORT_FLAGS
184 + make_mutable(fbuf, mode, fileflags);
186 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
187 ignore_perishable = 1;
188 /* If DEL_RECURSE is not set, this just reports emptiness. */
189 @@ -264,7 +274,7 @@ static enum delret delete_dir_contents(c
190 if (S_ISDIR(fp->mode)
191 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
193 - if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
194 + if (delete_item(fname, fp->mode, FF_PTR(fp), NULL, flags) != DR_SUCCESS)
198 @@ -320,8 +330,9 @@ static int remember_delete(struct file_s
201 len = snprintf(deldelay_buf + deldelay_cnt,
202 - deldelay_size - deldelay_cnt,
203 - "%x %s%c", (int)file->mode, fname, '\0');
204 + deldelay_size - deldelay_cnt, "%x %x %s%c",
205 + (int)file->mode, (int)FF_PTR(file),
207 if ((deldelay_cnt += len) <= deldelay_size)
209 if (deldelay_fd < 0 && !start_delete_delay_temp())
210 @@ -334,7 +345,7 @@ static int remember_delete(struct file_s
214 -static int read_delay_line(char *buf)
215 +static int read_delay_line(char *buf, int *fileflags_p)
217 static int read_pos = 0;
219 @@ -376,12 +387,12 @@ static int read_delay_line(char *buf)
221 bp = deldelay_buf + read_pos;
223 - if (sscanf(bp, "%x ", &mode) != 1) {
224 + if (sscanf(bp, "%x %x ", &mode, fileflags_p) != 2) {
226 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
229 - past_space = strchr(bp, ' ') + 1;
230 + past_space = strchr(strchr(bp, ' ') + 1, ' ') + 1;
231 len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
234 @@ -399,15 +410,15 @@ static int read_delay_line(char *buf)
236 static void do_delayed_deletions(char *delbuf)
239 + int mode, fileflags;
241 if (deldelay_fd >= 0) {
242 if (deldelay_cnt && !flush_delete_delay())
244 lseek(deldelay_fd, 0, 0);
246 - while ((mode = read_delay_line(delbuf)) >= 0)
247 - delete_item(delbuf, mode, NULL, DEL_RECURSE);
248 + while ((mode = read_delay_line(delbuf, &fileflags)) >= 0)
249 + delete_item(delbuf, mode, fileflags, NULL, DEL_RECURSE);
250 if (deldelay_fd >= 0)
253 @@ -474,7 +485,7 @@ static void delete_in_dir(char *fbuf, st
254 if (!remember_delete(fp, delbuf))
257 - delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
258 + delete_item(delbuf, fp->mode, FF_PTR(fp), NULL, DEL_RECURSE);
262 @@ -1263,7 +1274,7 @@ static void recv_generator(char *fname,
263 * full later (right before we handle its contents). */
265 && (S_ISDIR(sx.st.st_mode)
266 - || delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0))
267 + || delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), "directory", del_opts) != 0))
268 goto cleanup; /* Any errors get reported later. */
269 if (do_mkdir(fname, file->mode & 0700) == 0)
270 file->flags |= FLAG_DIR_CREATED;
271 @@ -1275,7 +1286,7 @@ static void recv_generator(char *fname,
272 * we need to delete it. If it doesn't exist, then
273 * (perhaps recursively) create it. */
274 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
275 - if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
276 + if (delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), "directory", del_opts) != 0)
277 goto skipping_dir_contents;
280 @@ -1404,7 +1415,7 @@ static void recv_generator(char *fname,
282 /* Not the right symlink (or not a symlink), so
284 - if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
285 + if (delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), "symlink", del_opts) != 0)
287 } else if (basis_dir[0] != NULL) {
288 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
289 @@ -1483,7 +1494,7 @@ static void recv_generator(char *fname,
290 goto return_with_success;
293 - if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
294 + if (delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), t, del_opts) != 0)
296 } else if (basis_dir[0] != NULL) {
297 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
298 @@ -1574,7 +1585,7 @@ static void recv_generator(char *fname,
299 fnamecmp_type = FNAMECMP_FNAME;
301 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
302 - if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
303 + if (delete_item(fname, sx.st.st_mode, FF_STAT(sx.st), "regular file", del_opts) != 0)
309 @@ -51,6 +51,7 @@ int preserve_hard_links = 0;
310 int preserve_acls = 0;
311 int preserve_xattrs = 0;
312 int preserve_perms = 0;
313 +int preserve_fileflags = 0;
314 int preserve_executability = 0;
315 int preserve_devices = 0;
316 int preserve_specials = 0;
317 @@ -221,6 +222,7 @@ static void print_rsync_version(enum log
318 char const *links = "no ";
319 char const *iconv = "no ";
320 char const *ipv6 = "no ";
321 + char const *fileflags = "no ";
322 STRUCT_STAT *dumstat;
324 #if SUBPROTOCOL_VERSION != 0
325 @@ -250,6 +252,9 @@ static void print_rsync_version(enum log
329 +#ifdef SUPPORT_FLAGS
333 rprintf(f, "%s version %s protocol version %d%s\n",
334 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
335 @@ -263,8 +268,8 @@ static void print_rsync_version(enum log
336 (int)(sizeof (int64) * 8));
337 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
338 got_socketpair, hardlinks, links, ipv6, have_inplace);
339 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv\n",
340 - have_inplace, acls, xattrs, iconv);
341 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %sfile-flags\n",
342 + have_inplace, acls, xattrs, iconv, fileflags);
344 #ifdef MAINTAINER_MODE
345 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
346 @@ -331,6 +336,7 @@ void usage(enum logcode F)
347 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
348 rprintf(F," -H, --hard-links preserve hard links\n");
349 rprintf(F," -p, --perms preserve permissions\n");
350 + rprintf(F," --fileflags preserve file-flags\n");
351 rprintf(F," -E, --executability preserve the file's executability\n");
352 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
354 @@ -470,6 +476,8 @@ static struct poptOption long_options[]
355 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
356 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
357 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
358 + {"fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 1, 0, 0 },
359 + {"no-fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 0, 0, 0 },
360 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
361 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
362 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
363 @@ -1284,6 +1292,15 @@ int parse_arguments(int *argc_p, const c
367 +#ifndef SUPPORT_FLAGS
368 + if (preserve_fileflags) {
369 + snprintf(err_buf, sizeof err_buf,
370 + "file flags are not supported on this %s\n",
371 + am_server ? "server" : "client");
376 if (write_batch && read_batch) {
377 snprintf(err_buf, sizeof err_buf,
378 "--write-batch and --read-batch can not be used together\n");
379 @@ -1792,6 +1809,9 @@ void server_options(char **args, int *ar
380 if (xfer_dirs && !recurse && delete_mode && am_sender)
381 args[ac++] = "--no-r";
383 + if (preserve_fileflags)
384 + args[ac++] = "--flags";
386 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
387 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
391 @@ -32,6 +32,7 @@ extern int dry_run;
392 extern int preserve_acls;
393 extern int preserve_xattrs;
394 extern int preserve_perms;
395 +extern int preserve_fileflags;
396 extern int preserve_executability;
397 extern int preserve_times;
399 @@ -60,6 +61,16 @@ iconv_t ic_chck = (iconv_t)-1;
400 iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
403 +#ifdef SUPPORT_FLAGS
405 +#define UF_NOUNLINK 0
408 +#define SF_NOUNLINK 0
410 +#define NOCHANGE_FLAGS (UF_IMMUTABLE|UF_APPEND|UF_NOUNLINK|SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK)
413 static const char *default_charset(void)
415 # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
416 @@ -337,6 +348,41 @@ mode_t dest_mode(mode_t flist_mode, mode
420 +#ifdef SUPPORT_FLAGS
421 +/* Set a file's st_flags. */
422 +static int set_fileflags(const char *fname, uint32 fileflags)
424 + if (do_chflags(fname, fileflags) != 0) {
425 + rsyserr(FERROR, errno,
426 + "failed to set file flags on %s",
427 + full_fname(fname));
434 +/* Remove immutable flags from an object, so it can be altered/removed. */
435 +void make_mutable(const char *fname, mode_t mode, uint32 fileflags)
437 + if (!preserve_fileflags && S_ISLNK(mode))
440 + if (fileflags & NOCHANGE_FLAGS)
441 + set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
444 +/* Undo a prior make_mutable() call. */
445 +void undo_make_mutable(const char *fname, mode_t mode, uint32 fileflags)
447 + if (!preserve_fileflags && S_ISLNK(mode))
450 + if (fileflags & NOCHANGE_FLAGS)
451 + set_fileflags(fname, fileflags);
455 int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
456 const char *fnamecmp, int flags)
458 @@ -468,6 +514,15 @@ int set_file_attrs(const char *fname, st
462 +#ifdef SUPPORT_FLAGS
463 + if (preserve_fileflags && !S_ISLNK(sxp->st.st_mode)
464 + && sxp->st.st_flags != F_FFLAGS(file)) {
465 + if (!set_fileflags(fname, F_FFLAGS(file)))
471 if (verbose > 1 && flags & ATTRS_REPORT) {
473 rprintf(FCLIENT, "%s\n", fname);
474 @@ -527,6 +582,9 @@ void finish_transfer(const char *fname,
475 set_file_attrs(fnametmp, file, NULL, fnamecmp,
476 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
478 +#ifdef SUPPORT_FLAGS
479 + make_mutable(fnametmp, file->mode, F_FFLAGS(file));
481 /* move tmp file over real file */
483 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
484 @@ -541,6 +599,9 @@ void finish_transfer(const char *fname,
487 /* The file was moved into place (not copied), so it's done. */
488 +#ifdef SUPPORT_FLAGS
489 + undo_make_mutable(fname, file->mode, F_FFLAGS(file));
493 /* The file was copied, so tweak the perms of the copied file. If it
497 #define XMIT_RDEV_MINOR_8_pre30 (1<<11) /* protocols 28 - 29 */
498 #define XMIT_GROUP_NAME_FOLLOWS (1<<11) /* protocols 30 - now */
499 #define XMIT_HLINK_FIRST (1<<12) /* protocols 30 - now (HLINKED files only) */
500 +#define XMIT_SAME_FLAGS (1<<14) /* protocols ?? - now */
502 /* These flags are used in the live flist data. */
504 @@ -435,6 +436,10 @@ typedef unsigned int size_t;
509 +#define SUPPORT_FLAGS 1
512 /* Find a variable that is either exactly 32-bits or longer.
513 * If some code depends on 32-bit truncation, it will need to
514 * take special action in a "#if SIZEOF_INT32 > 4" section. */
515 @@ -602,6 +607,7 @@ extern int file_extra_cnt;
516 extern int inc_recurse;
519 +extern int fileflags_ndx;
521 extern int xattrs_ndx;
523 @@ -639,6 +645,7 @@ extern int xattrs_ndx;
524 /* When the associated option is on, all entries will have these present: */
525 #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
526 #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
527 +#define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
528 #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
529 #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
530 #define F_NDX(f) REQ_EXTRA(f, ic_ndx)->num
533 @@ -338,6 +338,7 @@ to the detailed description below for a
534 -K, --keep-dirlinks treat symlinked dir on receiver as dir
535 -H, --hard-links preserve hard links
536 -p, --perms preserve permissions
537 + --flags preserve file flags
538 -E, --executability preserve executability
539 --chmod=CHMOD affect file and/or directory permissions
540 -A, --acls preserve ACLs (implies -p)
541 @@ -539,7 +540,9 @@ specified, in which case bf(-r) is not i
543 Note that bf(-a) bf(does not preserve hardlinks), because
544 finding multiply-linked files is expensive. You must separately
546 +specify bf(-H). Note also that for compatibility, bf(-a)
547 +currently bf(does not include --flags) (see there) to include preserving
548 +change file flags (if supported by the OS).
550 dit(--no-OPTION) You may turn off one or more implied options by prefixing
551 the option name with "no-". Not all options may be prefixed with a "no-":
552 @@ -900,6 +903,13 @@ super-user copies all namespaces except
553 the user.* namespace. To be able to backup and restore non-user namespaces as
554 a normal user, see the bf(--fake-super) option.
556 +dit(bf(--flags)) This option causes rsync to update the change file flags
557 +to be the same as the source file, if your OS supports the bf(chflags)(2)
558 +system call. In any case, an attempt is made to remove flags that would
559 +prevent a file to be altered. Some flags can only be altered by the
560 +super-user and can only be unset below a certain secure-level (usually
563 dit(bf(--chmod)) This option tells rsync to apply one or more
564 comma-separated "chmod" strings to the permission of the files in the
565 transfer. The resulting value is treated as though it was the permissions
568 @@ -173,6 +173,15 @@ int do_chmod(const char *path, mode_t mo
572 +#ifdef SUPPORT_FLAGS
573 +int do_chflags(const char *path, u_long flags)
575 + if (dry_run) return 0;
576 + RETURN_ERROR_IF_RO_OR_LO;
577 + return chflags(path, flags);
581 int do_rename(const char *fname1, const char *fname2)
583 if (dry_run) return 0;