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/flags.diff
11 diff --git a/compat.c b/compat.c
14 @@ -44,6 +44,7 @@ extern int protocol_version;
15 extern int protect_args;
16 extern int preserve_uid;
17 extern int preserve_gid;
18 +extern int preserve_fileflags;
19 extern int preserve_acls;
20 extern int preserve_xattrs;
21 extern int need_messages_from_generator;
22 @@ -60,7 +61,7 @@ extern iconv_t ic_send, ic_recv;
25 /* These index values are for the file-list's extra-attribute array. */
26 -int uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
27 +int uid_ndx, gid_ndx, fileflags_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
29 int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
31 @@ -134,6 +135,8 @@ void setup_protocol(int f_out,int f_in)
32 uid_ndx = ++file_extra_cnt;
34 gid_ndx = ++file_extra_cnt;
35 + if (preserve_fileflags)
36 + fileflags_ndx = ++file_extra_cnt;
37 if (preserve_acls && !am_sender)
38 acls_ndx = ++file_extra_cnt;
40 diff --git a/configure.in b/configure.in
43 @@ -551,7 +551,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strdup chown chmod lchmod mknod mkfifo \
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 setattrlist)
51 dnl cygwin iconv.h defines iconv_open as libiconv_open
52 diff --git a/flist.c b/flist.c
55 @@ -50,6 +50,7 @@ extern int preserve_links;
56 extern int preserve_hard_links;
57 extern int preserve_devices;
58 extern int preserve_specials;
59 +extern int fileflags_ndx;
63 @@ -344,6 +345,9 @@ static void send_file_entry(int f, struct file_struct *file, int ndx, int first_
65 static time_t modtime;
68 + static uint32 fileflags;
70 #ifdef SUPPORT_HARD_LINKS
73 @@ -403,6 +407,14 @@ static void send_file_entry(int f, struct file_struct *file, int ndx, int first_
74 xflags |= XMIT_SAME_MODE;
78 + if (fileflags_ndx) {
79 + if (F_FFLAGS(file) == fileflags)
80 + xflags |= XMIT_SAME_FLAGS;
82 + fileflags = F_FFLAGS(file);
86 if ((preserve_devices && IS_DEVICE(mode))
87 || (preserve_specials && IS_SPECIAL(mode))) {
88 @@ -522,6 +534,10 @@ static void send_file_entry(int f, struct file_struct *file, int ndx, int first_
90 if (!(xflags & XMIT_SAME_MODE))
91 write_int(f, to_wire_mode(mode));
93 + if (fileflags_ndx && !(xflags & XMIT_SAME_FLAGS))
94 + write_int(f, (int)fileflags);
96 if (uid_ndx && !(xflags & XMIT_SAME_UID)) {
97 if (protocol_version < 30)
99 @@ -610,6 +626,9 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
101 static int64 modtime;
103 +#ifdef SUPPORT_FLAGS
104 + static uint32 fileflags;
106 #ifdef SUPPORT_HARD_LINKS
109 @@ -744,6 +763,10 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
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 @@ -865,6 +888,10 @@ static struct file_struct *recv_file_entry(struct file_list *flist,
121 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
124 +#ifdef SUPPORT_FLAGS
126 + F_FFLAGS(file) = fileflags;
131 @@ -1199,6 +1226,10 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
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;
142 diff --git a/generator.c b/generator.c
145 @@ -124,6 +124,7 @@ static const char *solo_file = NULL;
146 #define DEL_FOR_SYMLINK (1<<5) /* making room for a replacement symlink */
147 #define DEL_FOR_DEVICE (1<<6) /* making room for a replacement device */
148 #define DEL_FOR_SPECIAL (1<<7) /* making room for a replacement special */
149 +#define DEL_AN_IMMUTABLE (1<<8) /* item has an immutable flag set */
151 #define DEL_MAKE_ROOM (DEL_FOR_FILE|DEL_FOR_DIR|DEL_FOR_SYMLINK|DEL_FOR_DEVICE|DEL_FOR_SPECIAL)
153 @@ -163,6 +164,10 @@ static enum delret delete_item(char *fbuf, int mode, int flags)
155 if (!am_root && !(mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
156 do_chmod(fbuf, mode |= S_IWUSR);
157 +#ifdef SUPPORT_FLAGS
158 + if (fileflags_ndx && flags & DEL_AN_IMMUTABLE)
159 + make_mutable(fbuf, mode, NODELETE_FLAGS);
162 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
163 ignore_perishable = 1;
164 @@ -284,6 +289,14 @@ static enum delret delete_dir_contents(char *fname, int flags)
165 flags |= DEL_OWNED_BY_US;
167 flags &= ~DEL_OWNED_BY_US;
168 +#ifdef SUPPORT_FLAGS
169 + if (fileflags_ndx) {
170 + if (F_FFLAGS(fp) & NODELETE_FLAGS)
171 + flags |= DEL_AN_IMMUTABLE;
173 + flags &= ~DEL_AN_IMMUTABLE;
176 /* Save stack by recursing to ourself directly. */
177 if (S_ISDIR(fp->mode)) {
178 if (!am_root && !(fp->mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
179 @@ -343,15 +356,22 @@ static int flush_delete_delay(void)
181 static int remember_delete(struct file_struct *file, const char *fname, int flags)
183 - const char *plus = (!am_root && !(file->mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
185 + char buf[16], *bp = buf;
188 + if (!am_root && !(file->mode & S_IWUSR) && flags & DEL_OWNED_BY_US)
190 +#ifdef SUPPORT_FLAGS
191 + if (flags & DEL_AN_IMMUTABLE)
197 len = snprintf(deldelay_buf + deldelay_cnt,
198 deldelay_size - deldelay_cnt,
200 - plus, (int)file->mode, fname, '\0');
201 + buf, (int)file->mode, fname, '\0');
202 if ((deldelay_cnt += len) <= deldelay_size)
204 if (deldelay_fd < 0 && !start_delete_delay_temp())
205 @@ -364,10 +384,10 @@ static int remember_delete(struct file_struct *file, const char *fname, int flag
209 -static int read_delay_line(char *buf, int *own_flag_p)
210 +static int read_delay_line(char *buf, int *flags_p)
212 static int read_pos = 0;
214 + int j, len, mode, flags = 0;
215 char *bp, *past_space;
218 @@ -407,9 +427,15 @@ static int read_delay_line(char *buf, int *own_flag_p)
219 bp = deldelay_buf + read_pos;
222 - *own_flag_p = DEL_OWNED_BY_US;
225 + flags |= DEL_OWNED_BY_US;
227 +#ifdef SUPPORT_FLAGS
230 + flags |= DEL_AN_IMMUTABLE;
235 if (sscanf(bp, "%x ", &mode) != 1) {
237 @@ -434,15 +460,15 @@ static int read_delay_line(char *buf, int *own_flag_p)
239 static void do_delayed_deletions(char *delbuf)
241 - int mode, own_flag;
244 if (deldelay_fd >= 0) {
245 if (deldelay_cnt && !flush_delete_delay())
247 lseek(deldelay_fd, 0, 0);
249 - while ((mode = read_delay_line(delbuf, &own_flag)) >= 0)
250 - delete_item(delbuf, mode, own_flag | DEL_RECURSE);
251 + while ((mode = read_delay_line(delbuf, &flags)) >= 0)
252 + delete_item(delbuf, mode, DEL_RECURSE | flags);
253 if (deldelay_fd >= 0)
256 @@ -505,6 +531,9 @@ static void delete_in_dir(char *fbuf, struct file_struct *file, dev_t *fs_dev)
258 if (flist_find(cur_flist, fp) < 0) {
259 int flags = DEL_RECURSE
260 +#ifdef SUPPORT_FLAGS
261 + | (fileflags_ndx && F_FFLAGS(fp) & NODELETE_FLAGS ? DEL_AN_IMMUTABLE : 0)
263 | (!uid_ndx || (uid_t)F_OWNER(fp) == our_uid ? DEL_OWNED_BY_US : 0);
265 if (delete_during == 2) {
266 @@ -1329,6 +1358,10 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
268 if (statret == 0 && sx.st.st_uid == our_uid)
269 del_opts |= DEL_OWNED_BY_US;
270 +#ifdef SUPPORT_FLAGS
271 + if (statret == 0 && fileflags_ndx && sx.st.st_flags & NODELETE_FLAGS)
272 + del_opts |= DEL_AN_IMMUTABLE;
276 if (!implied_dirs && file->flags & FLAG_IMPLIED_DIR)
277 diff --git a/options.c b/options.c
280 @@ -52,6 +52,7 @@ int preserve_hard_links = 0;
281 int preserve_acls = 0;
282 int preserve_xattrs = 0;
283 int preserve_perms = 0;
284 +int preserve_fileflags = 0;
285 int preserve_executability = 0;
286 int preserve_devices = 0;
287 int preserve_specials = 0;
288 @@ -224,6 +225,7 @@ static void print_rsync_version(enum logcode f)
289 char const *links = "no ";
290 char const *iconv = "no ";
291 char const *ipv6 = "no ";
292 + char const *fileflags = "no ";
293 STRUCT_STAT *dumstat;
295 #if SUBPROTOCOL_VERSION != 0
296 @@ -256,6 +258,9 @@ static void print_rsync_version(enum logcode f)
297 #if defined HAVE_LUTIMES && defined HAVE_UTIMES
300 +#ifdef SUPPORT_FLAGS
304 rprintf(f, "%s version %s protocol version %d%s\n",
305 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION, subprotocol);
306 @@ -269,8 +274,8 @@ static void print_rsync_version(enum logcode f)
307 (int)(sizeof (int64) * 8));
308 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
309 got_socketpair, hardlinks, links, ipv6, have_inplace);
310 - rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes\n",
311 - have_inplace, acls, xattrs, iconv, symtimes);
312 + rprintf(f, " %sappend, %sACLs, %sxattrs, %siconv, %ssymtimes, %sfile-flags\n",
313 + have_inplace, acls, xattrs, iconv, symtimes, fileflags);
315 #ifdef MAINTAINER_MODE
316 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
317 @@ -337,6 +342,7 @@ void usage(enum logcode F)
318 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
319 rprintf(F," -H, --hard-links preserve hard links\n");
320 rprintf(F," -p, --perms preserve permissions\n");
321 + rprintf(F," --fileflags preserve file-flags\n");
322 rprintf(F," -E, --executability preserve the file's executability\n");
323 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
325 @@ -477,6 +483,8 @@ static struct poptOption long_options[] = {
326 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
327 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
328 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
329 + {"fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 1, 0, 0 },
330 + {"no-fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 0, 0, 0 },
331 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
332 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
333 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
334 @@ -1311,6 +1319,15 @@ int parse_arguments(int *argc_p, const char ***argv_p, int frommain)
338 +#ifndef SUPPORT_FLAGS
339 + if (preserve_fileflags) {
340 + snprintf(err_buf, sizeof err_buf,
341 + "the --fileflags option is not supported on this %s\n",
342 + am_server ? "server" : "client");
347 if (write_batch && read_batch) {
348 snprintf(err_buf, sizeof err_buf,
349 "--write-batch and --read-batch can not be used together\n");
350 @@ -1826,6 +1843,9 @@ void server_options(char **args, int *argc_p)
351 if (xfer_dirs && !recurse && delete_mode && am_sender)
352 args[ac++] = "--no-r";
354 + if (preserve_fileflags)
355 + args[ac++] = "--fileflags";
357 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
358 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
360 diff --git a/rsync.c b/rsync.c
363 @@ -32,6 +32,7 @@ extern int dry_run;
364 extern int preserve_acls;
365 extern int preserve_xattrs;
366 extern int preserve_perms;
367 +extern int preserve_fileflags;
368 extern int preserve_executability;
369 extern int preserve_times;
371 @@ -339,6 +340,41 @@ mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
375 +#ifdef SUPPORT_FLAGS
376 +/* Set a file's st_flags. */
377 +static int set_fileflags(const char *fname, uint32 fileflags)
379 + if (do_chflags(fname, fileflags) != 0) {
380 + rsyserr(FERROR_XFER, errno,
381 + "failed to set file flags on %s",
382 + full_fname(fname));
389 +/* Remove immutable flags from an object, so it can be altered/removed. */
390 +void make_mutable(const char *fname, mode_t mode, uint32 fileflags)
395 + if (fileflags & NOCHANGE_FLAGS)
396 + set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
399 +/* Undo a prior make_mutable() call. */
400 +void undo_make_mutable(const char *fname, mode_t mode, uint32 fileflags)
405 + if (fileflags & NOCHANGE_FLAGS)
406 + set_fileflags(fname, fileflags);
410 int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
411 const char *fnamecmp, int flags)
413 @@ -472,6 +508,15 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
417 +#ifdef SUPPORT_FLAGS
418 + if (preserve_fileflags && !S_ISLNK(sxp->st.st_mode)
419 + && sxp->st.st_flags != F_FFLAGS(file)) {
420 + if (!set_fileflags(fname, F_FFLAGS(file)))
426 if (verbose > 1 && flags & ATTRS_REPORT) {
428 rprintf(FCLIENT, "%s\n", fname);
429 @@ -533,6 +578,10 @@ int finish_transfer(const char *fname, const char *fnametmp,
430 set_file_attrs(fnametmp, file, NULL, fnamecmp,
431 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
433 +#ifdef SUPPORT_FLAGS
434 + if (preserve_fileflags)
435 + make_mutable(fnametmp, file->mode, F_FFLAGS(file));
437 /* move tmp file over real file */
439 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
440 @@ -550,6 +599,10 @@ int finish_transfer(const char *fname, const char *fnametmp,
443 /* The file was moved into place (not copied), so it's done. */
444 +#ifdef SUPPORT_FLAGS
445 + if (preserve_fileflags)
446 + undo_make_mutable(fname, file->mode, F_FFLAGS(file));
450 /* The file was copied, so tweak the perms of the copied file. If it
451 diff --git a/rsync.h b/rsync.h
455 #define XMIT_RDEV_MINOR_8_pre30 (1<<11) /* protocols 28 - 29 */
456 #define XMIT_GROUP_NAME_FOLLOWS (1<<11) /* protocols 30 - now */
457 #define XMIT_HLINK_FIRST (1<<12) /* protocols 30 - now (HLINKED files only) */
458 +#define XMIT_SAME_FLAGS (1<<14) /* protocols ?? - now */
460 /* These flags are used in the live flist data. */
462 @@ -452,6 +453,21 @@ typedef unsigned int size_t;
467 +#define SUPPORT_FLAGS 1
470 +#ifdef SUPPORT_FLAGS
472 +#define UF_NOUNLINK 0
475 +#define SF_NOUNLINK 0
477 +#define NODELETE_FLAGS (UF_IMMUTABLE|UF_NOUNLINK|SF_IMMUTABLE|SF_NOUNLINK)
478 +#define NOCHANGE_FLAGS (NODELETE_FLAGS|UF_APPEND|SF_APPEND)
481 /* Find a variable that is either exactly 32-bits or longer.
482 * If some code depends on 32-bit truncation, it will need to
483 * take special action in a "#if SIZEOF_INT32 > 4" section. */
484 @@ -620,6 +636,7 @@ extern int file_extra_cnt;
485 extern int inc_recurse;
488 +extern int fileflags_ndx;
490 extern int xattrs_ndx;
492 @@ -657,6 +674,7 @@ extern int xattrs_ndx;
493 /* When the associated option is on, all entries will have these present: */
494 #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
495 #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
496 +#define F_FFLAGS(f) REQ_EXTRA(f, fileflags_ndx)->unum
497 #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
498 #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
499 #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
500 diff --git a/rsync.yo b/rsync.yo
503 @@ -338,6 +338,7 @@ to the detailed description below for a complete description. verb(
504 -K, --keep-dirlinks treat symlinked dir on receiver as dir
505 -H, --hard-links preserve hard links
506 -p, --perms preserve permissions
507 + --fileflags preserve file-flags
508 -E, --executability preserve executability
509 --chmod=CHMOD affect file and/or directory permissions
510 -A, --acls preserve ACLs (implies -p)
511 @@ -540,7 +541,8 @@ specified, in which case bf(-r) is not implied.
513 Note that bf(-a) bf(does not preserve hardlinks), because
514 finding multiply-linked files is expensive. You must separately
516 +specify bf(-H). Note also that for backward compatibility, bf(-a)
517 +currently does bf(not) imply the bf(--fileflags) option.
519 dit(--no-OPTION) You may turn off one or more implied options by prefixing
520 the option name with "no-". Not all options may be prefixed with a "no-":
521 @@ -922,6 +924,13 @@ super-user copies all namespaces except system.*. A normal user only copies
522 the user.* namespace. To be able to backup and restore non-user namespaces as
523 a normal user, see the bf(--fake-super) option.
525 +dit(bf(--fileflags)) This option causes rsync to update the file-flags
526 +to be the same as the source file, if your OS supports the bf(chflags)(2)
527 +system call. In any case, an attempt is made to remove flags that would
528 +prevent a file to be altered. Some flags can only be altered by the
529 +super-user and can only be unset below a certain secure-level (usually
532 dit(bf(--chmod)) This option tells rsync to apply one or more
533 comma-separated "chmod" strings to the permission of the files in the
534 transfer. The resulting value is treated as though it was the permissions
535 diff --git a/syscall.c b/syscall.c
538 @@ -174,6 +174,15 @@ int do_chmod(const char *path, mode_t mode)
542 +#ifdef SUPPORT_FLAGS
543 +int do_chflags(const char *path, u_long flags)
545 + if (dry_run) return 0;
546 + RETURN_ERROR_IF_RO_OR_LO;
547 + return chflags(path, flags);
551 int do_rename(const char *fname1, const char *fname2)
553 if (dry_run) return 0;