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 @@ -305,6 +306,9 @@ static void send_file_entry(struct file_
36 static time_t modtime;
39 + static uint32 fileflags;
43 static uint32 rdev_major;
44 @@ -335,6 +339,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 @@ -417,6 +427,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 @@ -484,6 +498,9 @@ static struct file_struct *receive_file_
70 static time_t modtime;
73 + static uint32 fileflags;
77 static uint32 rdev_major;
78 @@ -557,9 +574,12 @@ static struct file_struct *receive_file_
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 @@ -610,6 +630,9 @@ static struct file_struct *receive_file_
93 file->modtime = modtime;
94 file->length = file_length;
97 + file->fileflags = fileflags;
102 @@ -870,6 +893,9 @@ struct file_struct *make_file(const char
103 file->modtime = st.st_mtime;
104 file->length = st.st_size;
105 file->mode = st.st_mode;
106 +#ifdef SUPPORT_FLAGS
107 + file->fileflags = st.st_flags;
109 file->uid = st.st_uid;
110 file->gid = st.st_gid;
114 @@ -99,6 +99,12 @@ int non_perishable_cnt = 0;
115 static int deletion_count = 0; /* used to implement --max-delete */
116 static FILE *delete_delay_fp = NULL;
118 +#ifdef SUPPORT_FLAGS
119 +#define FILEFLAGS(ff) ff
121 +#define FILEFLAGS(ff) 0
124 /* For calling delete_item() and delete_dir_contents(). */
125 #define DEL_RECURSE (1<<1) /* recurse */
126 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
127 @@ -114,7 +120,6 @@ enum delret {
128 /* Forward declaration for delete_item(). */
129 static enum delret delete_dir_contents(char *fname, int flags);
132 static int is_backup_file(char *fn)
134 int k = strlen(fn) - backup_suffix_len;
135 @@ -127,17 +132,20 @@ static int is_backup_file(char *fn)
136 * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
137 * a directory! (The buffer is used for recursion, but returned unchanged.)
139 -static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
140 +static enum delret delete_item(char *fbuf, int mode, uint32 fileflags, char *replace, int flags)
147 - rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
148 - fbuf, mode, flags);
149 + rprintf(FINFO, "delete_item(%s) mode=%o fileflags=%o flags=%d\n",
150 + fbuf, mode, fileflags, flags);
153 +#ifdef SUPPORT_FLAGS
154 + make_mutable(fbuf, mode, fileflags);
156 if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
157 ignore_perishable = 1;
158 /* If DEL_RECURSE is not set, this just reports emptiness. */
159 @@ -249,7 +257,7 @@ static enum delret delete_dir_contents(c
160 if (S_ISDIR(fp->mode)
161 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
163 - if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
164 + if (delete_item(fname, fp->mode, FILEFLAGS(fp->fileflags), NULL, flags) != DR_SUCCESS)
168 @@ -401,10 +409,10 @@ static void delete_in_dir(struct file_li
170 if (flist_find(flist, fp) < 0) {
172 - if (delete_delay_fp)
173 + if (delete_delay_fp) /* XXX need to output fileflags value here too */
174 fprintf(delete_delay_fp, "%o %s%c", (short)fp->mode, delbuf, '\0');
176 - delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
177 + delete_item(delbuf, fp->mode, FILEFLAGS(fp->fileflags), NULL, DEL_RECURSE);
181 @@ -1103,7 +1111,7 @@ static void recv_generator(char *fname,
182 * we need to delete it. If it doesn't exist, then
183 * (perhaps recursively) create it. */
184 if (statret == 0 && !S_ISDIR(st.st_mode)) {
185 - if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
186 + if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "directory", del_opts) != 0)
190 @@ -1197,7 +1205,7 @@ static void recv_generator(char *fname,
192 /* Not the right symlink (or not a symlink), so
194 - if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
195 + if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "symlink", del_opts) != 0)
197 } else if (basis_dir[0] != NULL) {
198 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
199 @@ -1268,7 +1276,7 @@ static void recv_generator(char *fname,
200 goto return_with_success;
203 - if (delete_item(fname, st.st_mode, t, del_opts) != 0)
204 + if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), t, del_opts) != 0)
206 } else if (basis_dir[0] != NULL) {
207 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
208 @@ -1354,7 +1362,7 @@ static void recv_generator(char *fname,
209 fnamecmp_type = FNAMECMP_FNAME;
211 if (statret == 0 && !S_ISREG(st.st_mode)) {
212 - if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
213 + if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "regular file", del_opts) != 0)
219 @@ -48,6 +48,7 @@ int copy_links = 0;
220 int preserve_links = 0;
221 int preserve_hard_links = 0;
222 int preserve_perms = 0;
223 +int preserve_fileflags = 0;
224 int preserve_executability = 0;
225 int preserve_devices = 0;
226 int preserve_specials = 0;
227 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
228 char const *hardlinks = "no ";
229 char const *links = "no ";
230 char const *ipv6 = "no ";
231 + char const *fileflags = "no ";
232 STRUCT_STAT *dumstat;
234 #ifdef HAVE_SOCKETPAIR
235 @@ -223,6 +225,10 @@ static void print_rsync_version(enum log
239 +#ifdef SUPPORT_FLAGS
243 rprintf(f, "%s version %s protocol version %d\n",
244 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
245 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
246 @@ -233,8 +239,8 @@ static void print_rsync_version(enum log
247 (int)(sizeof (int64) * 8));
248 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
249 got_socketpair, hardlinks, links, ipv6, have_inplace);
250 - rprintf(f, " %sappend\n",
252 + rprintf(f, " %sappend, %sfile-flags\n",
253 + have_inplace, fileflags);
255 #ifdef MAINTAINER_MODE
256 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
257 @@ -300,6 +306,7 @@ void usage(enum logcode F)
258 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
259 rprintf(F," -H, --hard-links preserve hard links\n");
260 rprintf(F," -p, --perms preserve permissions\n");
261 + rprintf(F," --fileflags preserve file-flags\n");
262 rprintf(F," -E, --executability preserve the file's executability\n");
263 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
264 rprintf(F," -o, --owner preserve owner (super-user only)\n");
265 @@ -421,6 +428,8 @@ static struct poptOption long_options[]
266 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
267 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
268 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
269 + {"fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 1, 0, 0 },
270 + {"no-fileflags", 0, POPT_ARG_VAL, &preserve_fileflags, 0, 0, 0 },
271 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
272 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
273 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
274 @@ -1126,6 +1135,15 @@ int parse_arguments(int *argc, const cha
278 +#ifndef SUPPORT_FLAGS
279 + if (preserve_fileflags) {
280 + snprintf(err_buf, sizeof err_buf,
281 + "file flags are not supported on this %s\n",
282 + am_server ? "server" : "client");
287 if (write_batch && read_batch) {
288 snprintf(err_buf, sizeof err_buf,
289 "--write-batch and --read-batch can not be used together\n");
290 @@ -1580,6 +1598,9 @@ void server_options(char **args,int *arg
291 if (xfer_dirs && !recurse && delete_mode && am_sender)
292 args[ac++] = "--no-r";
294 + if (preserve_fileflags)
295 + args[ac++] = "--flags";
297 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
298 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
303 #include <langinfo.h>
306 +#define NOCHANGE_FLAGS (UF_IMMUTABLE|UF_APPEND|UF_NOUNLINK|SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK)
310 extern int preserve_perms;
311 +extern int preserve_fileflags;
312 extern int preserve_executability;
313 extern int preserve_times;
314 extern int omit_dir_times;
315 @@ -123,6 +126,41 @@ mode_t dest_mode(mode_t flist_mode, mode
319 +#ifdef SUPPORT_FLAGS
320 +/* Set a file's st_flags. */
321 +static int set_fileflags(const char *fname, uint32 fileflags)
323 + if (do_chflags(fname, fileflags) != 0) {
324 + rsyserr(FERROR, errno,
325 + "failed to set file flags on %s",
326 + full_fname(fname));
333 +/* Remove immutable flags from an object, so it can be altered/removed. */
334 +void make_mutable(char *fname, mode_t mode, uint32 fileflags)
336 + if (!preserve_fileflags && S_ISLNK(mode))
339 + if (fileflags & NOCHANGE_FLAGS)
340 + set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
343 +/* Undo a prior make_mutable() call. */
344 +void undo_make_mutable(char *fname, mode_t mode, uint32 fileflags)
346 + if (!preserve_fileflags && S_ISLNK(mode))
349 + if (fileflags & NOCHANGE_FLAGS)
350 + set_fileflags(fname, fileflags);
354 int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
357 @@ -221,6 +259,15 @@ int set_file_attrs(char *fname, struct f
361 +#ifdef SUPPORT_FLAGS
362 + if (preserve_fileflags && !S_ISLNK(st->st_mode)
363 + && st->st_flags != file->fileflags) {
364 + if (!set_fileflags(fname, file->fileflags))
370 if (verbose > 1 && flags & ATTRS_REPORT) {
372 rprintf(FCLIENT, "%s\n", fname);
373 @@ -268,6 +315,9 @@ void finish_transfer(char *fname, char *
374 set_file_attrs(fnametmp, file, NULL,
375 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
377 +#ifdef SUPPORT_FLAGS
378 + make_mutable(fnametmp, file->mode, file->fileflags);
380 /* move tmp file over real file */
382 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
383 @@ -282,6 +332,9 @@ void finish_transfer(char *fname, char *
386 /* The file was moved into place (not copied), so it's done. */
387 +#ifdef SUPPORT_FLAGS
388 + undo_make_mutable(fname, file->mode, file->fileflags);
392 /* The file was copied, so tweak the perms of the copied file. If it
396 #define XMIT_HAS_IDEV_DATA (1<<9)
397 #define XMIT_SAME_DEV (1<<10)
398 #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
399 +#define XMIT_SAME_FLAGS (1<<12)
401 /* These flags are used in the live flist data. */
403 @@ -345,6 +346,10 @@ enum msgcode {
408 +#define SUPPORT_FLAGS 1
411 /* Find a variable that is either exactly 32-bits or longer.
412 * If some code depends on 32-bit truncation, it will need to
413 * take special action in a "#if SIZEOF_INT32 > 4" section. */
414 @@ -528,6 +533,9 @@ struct file_struct {
418 +#ifdef SUPPORT_FLAGS
426 @@ -321,6 +321,7 @@ to the detailed description below for a
427 -K, --keep-dirlinks treat symlinked dir on receiver as dir
428 -H, --hard-links preserve hard links
429 -p, --perms preserve permissions
430 + --flags preserve file flags
431 -E, --executability preserve executability
432 --chmod=CHMOD affect file and/or directory permissions
433 -o, --owner preserve owner (super-user only)
434 @@ -510,7 +511,9 @@ specified, in which case bf(-r) is not i
436 Note that bf(-a) bf(does not preserve hardlinks), because
437 finding multiply-linked files is expensive. You must separately
439 +specify bf(-H). Note also that for compatibility, bf(-a)
440 +currently bf(does not include --flags) (see there) to include preserving
441 +change file flags (if supported by the OS).
443 dit(--no-OPTION) You may turn off one or more implied options by prefixing
444 the option name with "no-". Not all options may be prefixed with a "no-":
445 @@ -805,6 +808,13 @@ quote(itemization(
447 If bf(--perms) is enabled, this option is ignored.
449 +dit(bf(--flags)) This option causes rsync to update the change file flags
450 +to be the same as the source file, if your OS supports the bf(chflags)(2)
451 +system call. In any case, an attempt is made to remove flags that would
452 +prevent a file to be altered. Some flags can only be altered by the
453 +super-user and can only be unset below a certain secure-level (usually
456 dit(bf(--chmod)) This option tells rsync to apply one or more
457 comma-separated "chmod" strings to the permission of the files in the
458 transfer. The resulting value is treated as though it was the permissions
461 @@ -153,6 +153,15 @@ int do_chmod(const char *path, mode_t mo
465 +#ifdef SUPPORT_FLAGS
466 +int do_chflags(const char *path, u_long flags)
468 + if (dry_run) return 0;
469 + RETURN_ERROR_IF_RO_OR_LO;
470 + return chflags(path, flags);
474 int do_rename(const char *fname1, const char *fname2)
476 if (dry_run) return 0;