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 @@ -549,7 +549,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strd
16 memmove lchown vsnprintf snprintf vasprintf asprintf setsid glob strpbrk \
17 strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
18 setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
19 - strerror putenv iconv_open locale_charset nl_langinfo \
20 + chflags strerror putenv iconv_open locale_charset nl_langinfo \
21 sigaction sigprocmask)
23 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
26 @@ -44,6 +44,7 @@ extern int preserve_links;
27 extern int preserve_hard_links;
28 extern int preserve_devices;
29 extern int preserve_specials;
30 +extern int preserve_fileflags;
31 extern int preserve_uid;
32 extern int preserve_gid;
33 extern int relative_paths;
34 @@ -312,6 +313,9 @@ static void send_file_entry(struct file_
36 static time_t modtime;
39 + static uint32 fileflags;
43 static uint32 rdev_major;
44 @@ -342,6 +346,12 @@ static void send_file_entry(struct file_
45 flags |= XMIT_SAME_MODE;
49 + if (file->fileflags == fileflags)
50 + flags |= XMIT_SAME_FLAGS;
52 + fileflags = file->fileflags;
54 if ((preserve_devices && IS_DEVICE(mode))
55 || (preserve_specials && IS_SPECIAL(mode))) {
56 if (protocol_version < 28) {
57 @@ -428,6 +438,10 @@ static void send_file_entry(struct file_
58 write_int(f, modtime);
59 if (!(flags & XMIT_SAME_MODE))
60 write_int(f, to_wire_mode(mode));
62 + if (preserve_fileflags && !(flags & XMIT_SAME_FLAGS))
63 + write_int(f, (int)fileflags);
65 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
68 @@ -496,6 +510,9 @@ static struct file_struct *recv_file_ent
70 static time_t modtime;
73 + static uint32 fileflags;
77 static uint32 rdev_major;
78 @@ -570,9 +587,12 @@ static struct file_struct *recv_file_ent
79 modtime = (time_t)read_int(f);
80 if (!(flags & XMIT_SAME_MODE))
81 mode = from_wire_mode(read_int(f));
83 if (chmod_modes && !S_ISLNK(mode))
84 mode = tweak_mode(mode, chmod_modes);
86 + if (preserve_fileflags && !(flags & XMIT_SAME_FLAGS))
87 + fileflags = (uint32)read_int(f);
90 if (preserve_uid && !(flags & XMIT_SAME_UID))
91 uid = (uid_t)read_int(f);
92 @@ -650,6 +670,10 @@ static struct file_struct *recv_file_ent
93 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
97 + if (preserve_fileflags)
98 + F_FFLAGS(file) = fileflags;
103 @@ -927,6 +951,10 @@ struct file_struct *make_file(const char
104 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
106 file->mode = st.st_mode;
107 +#ifdef SUPPORT_FLAGS
108 + if (preserve_fileflags)
109 + F_FFLAGS(file) = st.st_flags;
112 F_UID(file) = st.st_uid;
116 @@ -102,6 +102,12 @@ static char *deldelay_buf = NULL;
117 static int deldelay_fd = -1;
118 static BOOL solo_file = 0;
120 +#ifdef SUPPORT_FLAGS
121 +#define FILEFLAGS(ff) ff
123 +#define FILEFLAGS(ff) 0
126 /* For calling delete_item() and delete_dir_contents(). */
127 #define DEL_RECURSE (1<<1) /* recurse */
128 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
129 @@ -117,7 +123,6 @@ enum delret {
130 /* Forward declaration for delete_item(). */
131 static enum delret delete_dir_contents(char *fname, int flags);
134 static int is_backup_file(char *fn)
136 int k = strlen(fn) - backup_suffix_len;
137 @@ -130,17 +135,20 @@ static int is_backup_file(char *fn)
138 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
139 * a directory! (The buffer is used for recursion, but returned unchanged.)
141 -static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
142 +static enum delret delete_item(char *fbuf, int mode, uint32 fileflags, char *replace, int flags)
149 - rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
150 - fbuf, mode, flags);
151 + rprintf(FINFO, "delete_item(%s) mode=%o fileflags=%o flags=%d\n",
152 + fbuf, mode, fileflags, flags);
155 +#ifdef SUPPORT_FLAGS
156 + make_mutable(fbuf, mode, fileflags);
158 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
159 ignore_perishable = 1;
160 /* If DEL_RECURSE is not set, this just reports emptiness. */
161 @@ -252,7 +260,7 @@ static enum delret delete_dir_contents(c
162 if (S_ISDIR(fp->mode)
163 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
165 - if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
166 + if (delete_item(fname, fp->mode, FILEFLAGS(fp->fileflags), NULL, flags) != DR_SUCCESS)
170 @@ -308,8 +316,9 @@ static int remember_delete(struct file_s
173 len = snprintf(deldelay_buf + deldelay_cnt,
174 - deldelay_size - deldelay_cnt,
175 - "%x %s%c", (int)file->mode, fname, '\0');
176 + deldelay_size - deldelay_cnt, "%x %x %s%c",
177 + (int)file->mode, (int)FILEFLAGS(file->fileflags),
179 if ((deldelay_cnt += len) <= deldelay_size)
181 if (deldelay_fd < 0 && !start_delete_delay_temp())
182 @@ -322,7 +331,7 @@ static int remember_delete(struct file_s
186 -static int read_delay_line(char *buf)
187 +static int read_delay_line(char *buf, int *fileflags_p)
189 static int read_pos = 0;
191 @@ -364,7 +373,7 @@ static int read_delay_line(char *buf)
193 bp = deldelay_buf + read_pos;
195 - if (sscanf(bp, "%x ", &mode) != 1) {
196 + if (sscanf(bp, "%x %x ", &mode, fileflags_p) != 2) {
198 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
200 @@ -387,15 +396,15 @@ static int read_delay_line(char *buf)
202 static void do_delayed_deletions(char *delbuf)
205 + int mode, fileflags;
207 if (deldelay_fd >= 0) {
208 if (deldelay_cnt && !flush_delete_delay())
210 lseek(deldelay_fd, 0, 0);
212 - while ((mode = read_delay_line(delbuf)) >= 0)
213 - delete_item(delbuf, mode, NULL, DEL_RECURSE);
214 + while ((mode = read_delay_line(delbuf, &fileflags)) >= 0)
215 + delete_item(delbuf, mode, fileflags, NULL, DEL_RECURSE);
216 if (deldelay_fd >= 0)
219 @@ -476,7 +485,7 @@ static void delete_in_dir(struct file_li
220 if (!remember_delete(fp, delbuf))
223 - delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
224 + delete_item(delbuf, fp->mode, FILEFLAGS(fp->fileflags), NULL, DEL_RECURSE);
228 @@ -1177,7 +1186,7 @@ static void recv_generator(char *fname,
229 * we need to delete it. If it doesn't exist, then
230 * (perhaps recursively) create it. */
231 if (statret == 0 && !S_ISDIR(st.st_mode)) {
232 - if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
233 + if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "directory", del_opts) != 0)
237 @@ -1275,7 +1284,7 @@ static void recv_generator(char *fname,
239 /* Not the right symlink (or not a symlink), so
241 - if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
242 + if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "symlink", del_opts) != 0)
244 } else if (basis_dir[0] != NULL) {
245 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
246 @@ -1354,7 +1363,7 @@ static void recv_generator(char *fname,
247 goto return_with_success;
250 - if (delete_item(fname, st.st_mode, t, del_opts) != 0)
251 + if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), t, del_opts) != 0)
253 } else if (basis_dir[0] != NULL) {
254 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
255 @@ -1445,7 +1454,7 @@ static void recv_generator(char *fname,
256 fnamecmp_type = FNAMECMP_FNAME;
258 if (statret == 0 && !S_ISREG(st.st_mode)) {
259 - if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
260 + if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "regular file", del_opts) != 0)
266 @@ -48,6 +48,7 @@ int copy_links = 0;
267 int preserve_links = 0;
268 int preserve_hard_links = 0;
269 int preserve_perms = 0;
270 +int preserve_fileflags = 0;
271 int preserve_executability = 0;
272 int preserve_devices = 0;
273 int preserve_specials = 0;
274 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
275 char const *hardlinks = "no ";
276 char const *links = "no ";
277 char const *ipv6 = "no ";
278 + char const *fileflags = "no ";
279 STRUCT_STAT *dumstat;
281 #ifdef HAVE_SOCKETPAIR
282 @@ -223,6 +225,10 @@ static void print_rsync_version(enum log
286 +#ifdef SUPPORT_FLAGS
290 rprintf(f, "%s version %s protocol version %d\n",
291 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
292 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
293 @@ -233,8 +239,8 @@ static void print_rsync_version(enum log
294 (int)(sizeof (int64) * 8));
295 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
296 got_socketpair, hardlinks, links, ipv6, have_inplace);
297 - rprintf(f, " %sappend\n",
299 + rprintf(f, " %sappend, %sfile-flags\n",
300 + have_inplace, fileflags);
302 #ifdef MAINTAINER_MODE
303 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
304 @@ -300,6 +306,7 @@ void usage(enum logcode F)
305 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
306 rprintf(F," -H, --hard-links preserve hard links\n");
307 rprintf(F," -p, --perms preserve permissions\n");
308 + rprintf(F," --fileflags preserve file-flags\n");
309 rprintf(F," -E, --executability preserve the file's executability\n");
310 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
311 rprintf(F," -o, --owner preserve owner (super-user only)\n");
312 @@ -421,6 +428,8 @@ static struct poptOption long_options[]
313 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
314 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
315 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
316 + {"fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 1, 0, 0 },
317 + {"no-fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 0, 0, 0 },
318 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
319 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
320 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
321 @@ -1126,6 +1135,15 @@ int parse_arguments(int *argc, const cha
325 +#ifndef SUPPORT_FLAGS
326 + if (preserve_fileflags) {
327 + snprintf(err_buf, sizeof err_buf,
328 + "file flags are not supported on this %s\n",
329 + am_server ? "server" : "client");
334 if (write_batch && read_batch) {
335 snprintf(err_buf, sizeof err_buf,
336 "--write-batch and --read-batch can not be used together\n");
337 @@ -1223,6 +1241,8 @@ int parse_arguments(int *argc, const cha
338 preserve_uid = ++flist_extra_cnt;
340 preserve_gid = ++flist_extra_cnt;
341 + if (preserve_fileflags)
342 + preserve_fileflags = ++flist_extra_cnt;
344 *argv = poptGetArgs(pc);
345 *argc = count_args(*argv);
346 @@ -1585,6 +1605,9 @@ void server_options(char **args,int *arg
347 if (xfer_dirs && !recurse && delete_mode && am_sender)
348 args[ac++] = "--no-r";
350 + if (preserve_fileflags)
351 + args[ac++] = "--flags";
353 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
354 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
359 #include <langinfo.h>
362 +#define NOCHANGE_FLAGS (UF_IMMUTABLE|UF_APPEND|UF_NOUNLINK|SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK)
366 extern int preserve_perms;
367 +extern int preserve_fileflags;
368 extern int preserve_executability;
369 extern int preserve_times;
370 extern int omit_dir_times;
371 @@ -176,6 +179,41 @@ mode_t dest_mode(mode_t flist_mode, mode
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, 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(char *fname, mode_t mode, uint32 fileflags)
392 + if (!preserve_fileflags && S_ISLNK(mode))
395 + if (fileflags & NOCHANGE_FLAGS)
396 + set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
399 +/* Undo a prior make_mutable() call. */
400 +void undo_make_mutable(char *fname, mode_t mode, uint32 fileflags)
402 + if (!preserve_fileflags && S_ISLNK(mode))
405 + if (fileflags & NOCHANGE_FLAGS)
406 + set_fileflags(fname, fileflags);
410 int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
413 @@ -274,6 +312,15 @@ int set_file_attrs(char *fname, struct f
417 +#ifdef SUPPORT_FLAGS
418 + if (preserve_fileflags && !S_ISLNK(st->st_mode)
419 + && st->st_flags != file->fileflags) {
420 + if (!set_fileflags(fname, file->fileflags))
426 if (verbose > 1 && flags & ATTRS_REPORT) {
428 rprintf(FCLIENT, "%s\n", fname);
429 @@ -321,6 +368,9 @@ void finish_transfer(char *fname, char *
430 set_file_attrs(fnametmp, file, NULL,
431 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
433 +#ifdef SUPPORT_FLAGS
434 + make_mutable(fnametmp, file->mode, file->fileflags);
436 /* move tmp file over real file */
438 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
439 @@ -335,6 +385,9 @@ void finish_transfer(char *fname, char *
442 /* The file was moved into place (not copied), so it's done. */
443 +#ifdef SUPPORT_FLAGS
444 + undo_make_mutable(fname, file->mode, file->fileflags);
448 /* The file was copied, so tweak the perms of the copied file. If it
452 #define XMIT_HAS_IDEV_DATA (1<<9)
453 #define XMIT_SAME_DEV (1<<10)
454 #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
455 +#define XMIT_SAME_FLAGS (1<<12)
457 /* These flags are used in the live flist data. */
459 @@ -348,6 +349,10 @@ enum msgcode {
464 +#define SUPPORT_FLAGS 1
467 /* Find a variable that is either exactly 32-bits or longer.
468 * If some code depends on 32-bit truncation, it will need to
469 * take special action in a "#if SIZEOF_INT32 > 4" section. */
470 @@ -553,6 +558,7 @@ extern int preserve_gid;
471 /* When the associated option is on, all entries will have these present: */
472 #define F_UID(f) REQ_EXTRA(f, preserve_uid)->uid
473 #define F_GID(f) REQ_EXTRA(f, preserve_gid)->gid
474 +#define F_FFLAGS(f) REQ_EXTRA(f, preserve_fileflags).num
476 /* These items are per-entry optional and mutally exclusive: */
477 #define F_HL_IDEV(f) OPT_EXTRA(f, LEN64_BUMP(f))->idev
480 @@ -321,6 +321,7 @@ to the detailed description below for a
481 -K, --keep-dirlinks treat symlinked dir on receiver as dir
482 -H, --hard-links preserve hard links
483 -p, --perms preserve permissions
484 + --flags preserve file flags
485 -E, --executability preserve executability
486 --chmod=CHMOD affect file and/or directory permissions
487 -o, --owner preserve owner (super-user only)
488 @@ -510,7 +511,9 @@ specified, in which case bf(-r) is not i
490 Note that bf(-a) bf(does not preserve hardlinks), because
491 finding multiply-linked files is expensive. You must separately
493 +specify bf(-H). Note also that for compatibility, bf(-a)
494 +currently bf(does not include --flags) (see there) to include preserving
495 +change file flags (if supported by the OS).
497 dit(--no-OPTION) You may turn off one or more implied options by prefixing
498 the option name with "no-". Not all options may be prefixed with a "no-":
499 @@ -805,6 +808,13 @@ quote(itemization(
501 If bf(--perms) is enabled, this option is ignored.
503 +dit(bf(--flags)) This option causes rsync to update the change file flags
504 +to be the same as the source file, if your OS supports the bf(chflags)(2)
505 +system call. In any case, an attempt is made to remove flags that would
506 +prevent a file to be altered. Some flags can only be altered by the
507 +super-user and can only be unset below a certain secure-level (usually
510 dit(bf(--chmod)) This option tells rsync to apply one or more
511 comma-separated "chmod" strings to the permission of the files in the
512 transfer. The resulting value is treated as though it was the permissions
515 @@ -153,6 +153,15 @@ int do_chmod(const char *path, mode_t mo
519 +#ifdef SUPPORT_FLAGS
520 +int do_chflags(const char *path, u_long flags)
522 + if (dry_run) return 0;
523 + RETURN_ERROR_IF_RO_OR_LO;
524 + return chflags(path, flags);
528 int do_rename(const char *fname1, const char *fname2)
530 if (dry_run) return 0;