Matt added "const" to a couple char pointers.
[rsync/rsync-patches.git] / flags.diff
CommitLineData
31611b92 1This patch provides --flags, which preserves the st_flags field.
9f62ddf8 2Modified from a patch that was written by Rolf Grossmann.
31611b92 3
03019e41 4To use this patch, run these commands for a successful build:
9f62ddf8 5
03019e41 6 patch -p1 <patches/flags.diff
9f62ddf8
WD
7 ./prepare-source
8 ./configure
9 make
31611b92 10
5bafda30
WD
11TODO: fix --delete-delay to work with --flags option.
12
31611b92
WD
13--- old/configure.in
14+++ new/configure.in
f813befd 15@@ -549,7 +549,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strd
31611b92
WD
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)
22
23 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
24--- old/flist.c
25+++ new/flist.c
1898c899 26@@ -44,6 +44,7 @@ extern int preserve_links;
31611b92
WD
27 extern int preserve_hard_links;
28 extern int preserve_devices;
29 extern int preserve_specials;
30+extern int preserve_flags;
31 extern int preserve_uid;
32 extern int preserve_gid;
33 extern int relative_paths;
d16b5fd6 34@@ -305,6 +306,9 @@ static void send_file_entry(struct file_
31611b92
WD
35 unsigned short flags;
36 static time_t modtime;
37 static mode_t mode;
38+#ifdef SUPPORT_FLAGS
39+ static uint32 fileflags;
40+#endif
41 static int64 dev;
42 static dev_t rdev;
43 static uint32 rdev_major;
d16b5fd6 44@@ -335,6 +339,12 @@ static void send_file_entry(struct file_
31611b92
WD
45 flags |= XMIT_SAME_MODE;
46 else
47 mode = file->mode;
48+#ifdef SUPPORT_FLAGS
49+ if (file->fileflags == fileflags)
50+ flags |= XMIT_SAME_FLAGS;
51+ else
52+ fileflags = file->fileflags;
53+#endif
54 if ((preserve_devices && IS_DEVICE(mode))
55 || (preserve_specials && IS_SPECIAL(mode))) {
56 if (protocol_version < 28) {
d16b5fd6 57@@ -418,6 +428,10 @@ static void send_file_entry(struct file_
31611b92
WD
58 write_int(f, modtime);
59 if (!(flags & XMIT_SAME_MODE))
60 write_int(f, to_wire_mode(mode));
61+#ifdef SUPPORT_FLAGS
62+ if (preserve_flags && !(flags & XMIT_SAME_FLAGS))
63+ write_int(f, (int)fileflags);
64+#endif
65 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
66 if (!numeric_ids)
67 add_uid(uid);
d16b5fd6 68@@ -485,6 +499,9 @@ static struct file_struct *receive_file_
31611b92
WD
69 {
70 static time_t modtime;
71 static mode_t mode;
72+#ifdef SUPPORT_FLAGS
73+ static uint32 fileflags;
74+#endif
75 static int64 dev;
76 static dev_t rdev;
77 static uint32 rdev_major;
d16b5fd6 78@@ -558,9 +575,12 @@ static struct file_struct *receive_file_
31611b92
WD
79 modtime = (time_t)read_int(f);
80 if (!(flags & XMIT_SAME_MODE))
81 mode = from_wire_mode(read_int(f));
82-
83 if (chmod_modes && !S_ISLNK(mode))
84 mode = tweak_mode(mode, chmod_modes);
85+#ifdef SUPPORT_FLAGS
86+ if (preserve_flags && !(flags & XMIT_SAME_FLAGS))
87+ fileflags = (uint32)read_int(f);
88+#endif
89
90 if (preserve_uid && !(flags & XMIT_SAME_UID))
91 uid = (uid_t)read_int(f);
d16b5fd6 92@@ -611,6 +631,9 @@ static struct file_struct *receive_file_
31611b92
WD
93 file->modtime = modtime;
94 file->length = file_length;
95 file->mode = mode;
96+#ifdef SUPPORT_FLAGS
97+ file->fileflags = fileflags;
98+#endif
99 file->uid = uid;
100 file->gid = gid;
101
d16b5fd6 102@@ -871,6 +894,9 @@ struct file_struct *make_file(char *fnam
31611b92
WD
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;
108+#endif
109 file->uid = st.st_uid;
110 file->gid = st.st_gid;
111
112--- old/generator.c
113+++ new/generator.c
f813befd 114@@ -99,6 +99,12 @@ int non_perishable_cnt = 0;
87d0091c 115 static int deletion_count = 0; /* used to implement --max-delete */
f813befd 116 static FILE *delete_delay_fp = NULL;
f90a882e
WD
117
118+#ifdef SUPPORT_FLAGS
119+#define FILEFLAGS(ff) ff
120+#else
121+#define FILEFLAGS(ff) 0
122+#endif
123+
d16b5fd6 124 /* For calling delete_item() and delete_dir_contents(). */
87d0091c 125 #define DEL_RECURSE (1<<1) /* recurse */
d16b5fd6 126 #define DEL_DIR_IS_EMPTY (1<<2) /* internal delete_FUNCTIONS use only */
f813befd 127@@ -114,7 +120,6 @@ enum delret {
87d0091c
WD
128 /* Forward declaration for delete_item(). */
129 static enum delret delete_dir_contents(char *fname, int flags);
f90a882e 130
87d0091c
WD
131-
132 static int is_backup_file(char *fn)
133 {
134 int k = strlen(fn) - backup_suffix_len;
f813befd
WD
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
f90a882e
WD
137 * a directory! (The buffer is used for recursion, but returned unchanged.)
138 */
f813befd
WD
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)
f90a882e 141 {
87d0091c
WD
142 enum delret ret;
143 char *what;
144 int ok;
145
146 if (verbose > 2) {
147- rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
f813befd 148- fbuf, mode, flags);
87d0091c 149+ rprintf(FINFO, "delete_item(%s) mode=%o fileflags=%o flags=%d\n",
f813befd 150+ fbuf, mode, fileflags, flags);
87d0091c
WD
151 }
152
31611b92 153+#ifdef SUPPORT_FLAGS
f813befd 154+ make_mutable(fbuf, mode, fileflags);
31611b92 155+#endif
d16b5fd6
WD
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. */
f813befd 159@@ -249,7 +257,7 @@ static enum delret delete_dir_contents(c
d16b5fd6
WD
160 if (S_ISDIR(fp->mode)
161 && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
162 ret = DR_NOT_EMPTY;
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)
165 ret = DR_NOT_EMPTY;
f90a882e 166 }
d16b5fd6 167
f813befd 168@@ -401,10 +409,10 @@ static void delete_in_dir(struct file_li
87d0091c 169 }
f90a882e
WD
170 if (flist_find(flist, fp) < 0) {
171 f_name(fp, delbuf);
f813befd
WD
172- if (delete_delay_fp)
173+ if (delete_delay_fp) /* XXX need to output fileflags value here too */
5bafda30 174 fprintf(delete_delay_fp, "%o %s%c", (short)fp->mode, delbuf, '\0');
f813befd
WD
175 else
176- delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
177+ delete_item(delbuf, fp->mode, FILEFLAGS(fp->fileflags), NULL, DEL_RECURSE);
f90a882e
WD
178 }
179 }
180
f813befd 181@@ -1103,7 +1111,7 @@ static void recv_generator(char *fname,
0461b869 182 * we need to delete it. If it doesn't exist, then
f90a882e
WD
183 * (perhaps recursively) create it. */
184 if (statret == 0 && !S_ISDIR(st.st_mode)) {
87d0091c
WD
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)
f90a882e
WD
187 return;
188 statret = -1;
189 }
f813befd 190@@ -1197,7 +1205,7 @@ static void recv_generator(char *fname,
f90a882e
WD
191 }
192 /* Not the right symlink (or not a symlink), so
193 * delete it. */
87d0091c
WD
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)
f90a882e 196 return;
05031682
WD
197 } else if (basis_dir[0] != NULL) {
198 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
f813befd 199@@ -1268,7 +1276,7 @@ static void recv_generator(char *fname,
05031682
WD
200 goto return_with_success;
201 return;
202 }
87d0091c
WD
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)
f90a882e 205 return;
05031682
WD
206 } else if (basis_dir[0] != NULL) {
207 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
f813befd 208@@ -1354,7 +1362,7 @@ static void recv_generator(char *fname,
f90a882e
WD
209 fnamecmp_type = FNAMECMP_FNAME;
210
211 if (statret == 0 && !S_ISREG(st.st_mode)) {
87d0091c
WD
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)
f90a882e
WD
214 return;
215 statret = -1;
216 stat_errno = ENOENT;
31611b92
WD
217--- old/options.c
218+++ new/options.c
1898c899 219@@ -48,6 +48,7 @@ int copy_links = 0;
31611b92
WD
220 int preserve_links = 0;
221 int preserve_hard_links = 0;
222 int preserve_perms = 0;
223+int preserve_flags = 0;
224 int preserve_executability = 0;
225 int preserve_devices = 0;
226 int preserve_specials = 0;
03019e41 227@@ -201,6 +202,7 @@ static void print_rsync_version(enum log
31611b92
WD
228 char const *hardlinks = "no ";
229 char const *links = "no ";
230 char const *ipv6 = "no ";
231+ char const *flags = "no ";
232 STRUCT_STAT *dumstat;
233
234 #ifdef HAVE_SOCKETPAIR
03019e41 235@@ -223,6 +225,10 @@ static void print_rsync_version(enum log
31611b92
WD
236 ipv6 = "";
237 #endif
238
239+#ifdef SUPPORT_FLAGS
240+ flags = "";
241+#endif
242+
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");
03019e41 246@@ -235,9 +241,9 @@ static void print_rsync_version(enum log
31611b92
WD
247 /* Note that this field may not have type ino_t. It depends
248 * on the complicated interaction between largefile feature
249 * macros. */
250- rprintf(f, " %sinplace, %sIPv6, "
251+ rprintf(f, " %sinplace, %sIPv6, %sfile flags, "
252 "%d-bit system inums, %d-bit internal inums\n",
253- have_inplace, ipv6,
254+ have_inplace, ipv6, flags,
255 (int) (sizeof dumstat->st_ino * 8),
256 (int) (sizeof (int64) * 8));
257 #ifdef MAINTAINER_MODE
03019e41 258@@ -304,6 +310,7 @@ void usage(enum logcode F)
31611b92
WD
259 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
260 rprintf(F," -H, --hard-links preserve hard links\n");
261 rprintf(F," -p, --perms preserve permissions\n");
262+ rprintf(F," --flags preserve file flags\n");
263 rprintf(F," -E, --executability preserve the file's executability\n");
063cf77b 264 rprintf(F," --chmod=CHMOD affect file and/or directory permissions\n");
31611b92 265 rprintf(F," -o, --owner preserve owner (super-user only)\n");
f813befd 266@@ -425,6 +432,8 @@ static struct poptOption long_options[]
31611b92
WD
267 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
268 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
269 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
270+ {"flags", 0, POPT_ARG_VAL, &preserve_flags, 1, 0, 0 },
271+ {"no-flags", 0, POPT_ARG_VAL, &preserve_flags, 0, 0, 0 },
272 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
273 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
274 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
f813befd 275@@ -1130,6 +1139,15 @@ int parse_arguments(int *argc, const cha
31611b92
WD
276 }
277 #endif
278
279+#ifndef SUPPORT_FLAGS
280+ if (preserve_flags) {
281+ snprintf(err_buf, sizeof err_buf,
282+ "file flags are not supported on this %s\n",
283+ am_server ? "server" : "client");
284+ return 0;
285+ }
286+#endif
287+
288 if (write_batch && read_batch) {
289 snprintf(err_buf, sizeof err_buf,
290 "--write-batch and --read-batch can not be used together\n");
f813befd 291@@ -1584,6 +1602,9 @@ void server_options(char **args,int *arg
31611b92
WD
292 if (xfer_dirs && !recurse && delete_mode && am_sender)
293 args[ac++] = "--no-r";
294
295+ if (preserve_flags)
296+ args[ac++] = "--flags";
297+
298 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
299 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
300 goto oom;
301--- old/rsync.c
302+++ new/rsync.c
f90a882e
WD
303@@ -30,9 +30,12 @@
304 #include <langinfo.h>
305 #endif
306
307+#define NOCHANGE_FLAGS (UF_IMMUTABLE|UF_APPEND|UF_NOUNLINK|SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK)
308+
ff318e90 309 extern int verbose;
31611b92 310 extern int dry_run;
31611b92
WD
311 extern int preserve_perms;
312+extern int preserve_flags;
313 extern int preserve_executability;
314 extern int preserve_times;
315 extern int omit_dir_times;
f90a882e
WD
316@@ -123,6 +126,41 @@ mode_t dest_mode(mode_t flist_mode, mode
317 return new_mode;
318 }
319
320+#ifdef SUPPORT_FLAGS
321+/* Set a file's st_flags. */
322+static int set_fileflags(const char *fname, uint32 fileflags)
323+{
324+ if (do_chflags(fname, fileflags) != 0) {
325+ rsyserr(FERROR, errno,
326+ "failed to set file flags on %s",
327+ full_fname(fname));
328+ return 0;
329+ }
330+
331+ return 1;
332+}
333+
334+/* Remove immutable flags from an object, so it can be altered/removed. */
335+void make_mutable(char *fname, mode_t mode, uint32 fileflags)
336+{
337+ if (!preserve_flags && S_ISLNK(mode))
338+ return;
339+
340+ if (fileflags & NOCHANGE_FLAGS)
341+ set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
342+}
343+
344+/* Undo a prior make_mutable() call. */
345+void undo_make_mutable(char *fname, mode_t mode, uint32 fileflags)
346+{
347+ if (!preserve_flags && S_ISLNK(mode))
348+ return;
349+
350+ if (fileflags & NOCHANGE_FLAGS)
351+ set_fileflags(fname, fileflags);
352+}
353+#endif
354+
355 int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
356 int flags)
357 {
358@@ -221,6 +259,15 @@ int set_file_attrs(char *fname, struct f
31611b92
WD
359 }
360 #endif
361
362+#ifdef SUPPORT_FLAGS
363+ if (preserve_flags && !S_ISLNK(st->st_mode)
364+ && st->st_flags != file->fileflags) {
f90a882e 365+ if (!set_fileflags(fname, file->fileflags))
31611b92 366+ return 0;
31611b92
WD
367+ updated = 1;
368+ }
369+#endif
370+
371 if (verbose > 1 && flags & ATTRS_REPORT) {
ff318e90
WD
372 if (updated)
373 rprintf(FCLIENT, "%s\n", fname);
f90a882e
WD
374@@ -268,6 +315,9 @@ void finish_transfer(char *fname, char *
375 set_file_attrs(fnametmp, file, NULL,
376 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
31611b92
WD
377
378+#ifdef SUPPORT_FLAGS
9f62ddf8 379+ make_mutable(fnametmp, file->mode, file->fileflags);
31611b92 380+#endif
f90a882e
WD
381 /* move tmp file over real file */
382 if (verbose > 2)
383 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
384@@ -282,6 +332,9 @@ void finish_transfer(char *fname, char *
385 }
386 if (ret == 0) {
387 /* The file was moved into place (not copied), so it's done. */
31611b92 388+#ifdef SUPPORT_FLAGS
f90a882e 389+ undo_make_mutable(fname, file->mode, file->fileflags);
31611b92 390+#endif
f90a882e
WD
391 return;
392 }
393 /* The file was copied, so tweak the perms of the copied file. If it
31611b92
WD
394--- old/rsync.h
395+++ new/rsync.h
396@@ -54,6 +54,7 @@
397 #define XMIT_HAS_IDEV_DATA (1<<9)
398 #define XMIT_SAME_DEV (1<<10)
399 #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
400+#define XMIT_SAME_FLAGS (1<<12)
401
402 /* These flags are used in the live flist data. */
403
05031682 404@@ -345,6 +346,10 @@ enum msgcode {
6e6f514c
WD
405 #define schar char
406 #endif
407
408+#ifdef HAVE_CHFLAGS
409+#define SUPPORT_FLAGS 1
410+#endif
411+
412 /* Find a variable that is either exactly 32-bits or longer.
413 * If some code depends on 32-bit truncation, it will need to
414 * take special action in a "#if SIZEOF_INT32 > 4" section. */
05031682 415@@ -528,6 +533,9 @@ struct file_struct {
31611b92
WD
416 struct hlink *links;
417 } link_u;
418 time_t modtime;
419+#ifdef SUPPORT_FLAGS
420+ uint32 fileflags;
421+#endif
422 uid_t uid;
423 gid_t gid;
424 mode_t mode;
31611b92
WD
425--- old/rsync.yo
426+++ new/rsync.yo
03019e41 427@@ -321,6 +321,7 @@ to the detailed description below for a
1ed0b5c9
WD
428 -K, --keep-dirlinks treat symlinked dir on receiver as dir
429 -H, --hard-links preserve hard links
31611b92 430 -p, --perms preserve permissions
31611b92 431+ --flags preserve file flags
1ed0b5c9 432 -E, --executability preserve executability
063cf77b 433 --chmod=CHMOD affect file and/or directory permissions
31611b92 434 -o, --owner preserve owner (super-user only)
f813befd 435@@ -510,7 +511,9 @@ specified, in which case bf(-r) is not i
31611b92
WD
436
437 Note that bf(-a) bf(does not preserve hardlinks), because
438 finding multiply-linked files is expensive. You must separately
439-specify bf(-H).
440+specify bf(-H). Note also that for compatibility, bf(-a)
441+currently bf(does not include --flags) (see there) to include preserving
442+change file flags (if supported by the OS).
443
444 dit(--no-OPTION) You may turn off one or more implied options by prefixing
445 the option name with "no-". Not all options may be prefixed with a "no-":
f813befd 446@@ -805,6 +808,13 @@ quote(itemization(
31611b92
WD
447
448 If bf(--perms) is enabled, this option is ignored.
449
450+dit(bf(--flags)) This option causes rsync to update the change file flags
f90a882e 451+to be the same as the source file, if your OS supports the bf(chflags)(2)
31611b92
WD
452+system call. In any case, an attempt is made to remove flags that would
453+prevent a file to be altered. Some flags can only be altered by the
454+super-user and can only be unset below a certain secure-level (usually
455+single-user mode).
456+
457 dit(bf(--chmod)) This option tells rsync to apply one or more
458 comma-separated "chmod" strings to the permission of the files in the
459 transfer. The resulting value is treated as though it was the permissions
460--- old/syscall.c
461+++ new/syscall.c
f813befd 462@@ -153,6 +153,15 @@ int do_chmod(const char *path, mode_t mo
31611b92
WD
463 }
464 #endif
465
466+#ifdef SUPPORT_FLAGS
467+int do_chflags(const char *path, u_long flags)
468+{
469+ if (dry_run) return 0;
470+ RETURN_ERROR_IF_RO_OR_LO;
471+ return chflags(path, flags);
472+}
473+#endif
474+
475 int do_rename(const char *fname1, const char *fname2)
476 {
477 if (dry_run) return 0;