A new version of an old patch by Rolf Grossmann.
[rsync/rsync-patches.git] / flags.diff
CommitLineData
31611b92
WD
1This patch provides --flags, which preserves the st_flags field.
2Modified from a patch that was written by Rolf Grossmann:
3
4 http://www.progtech.net/rsync.flags-patch
5
6--- old/configure.in
7+++ new/configure.in
8@@ -494,7 +494,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strd
9 memmove lchown vsnprintf snprintf vasprintf asprintf setsid glob strpbrk \
10 strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
11 setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
12- strerror putenv iconv_open locale_charset nl_langinfo \
13+ chflags strerror putenv iconv_open locale_charset nl_langinfo \
14 sigaction sigprocmask)
15
16 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
17--- old/flist.c
18+++ new/flist.c
19@@ -48,6 +48,7 @@ extern int preserve_links;
20 extern int preserve_hard_links;
21 extern int preserve_devices;
22 extern int preserve_specials;
23+extern int preserve_flags;
24 extern int preserve_uid;
25 extern int preserve_gid;
26 extern int relative_paths;
27@@ -312,6 +313,9 @@ static void send_file_entry(struct file_
28 unsigned short flags;
29 static time_t modtime;
30 static mode_t mode;
31+#ifdef SUPPORT_FLAGS
32+ static uint32 fileflags;
33+#endif
34 static int64 dev;
35 static dev_t rdev;
36 static uint32 rdev_major;
37@@ -344,6 +348,12 @@ static void send_file_entry(struct file_
38 flags |= XMIT_SAME_MODE;
39 else
40 mode = file->mode;
41+#ifdef SUPPORT_FLAGS
42+ if (file->fileflags == fileflags)
43+ flags |= XMIT_SAME_FLAGS;
44+ else
45+ fileflags = file->fileflags;
46+#endif
47 if ((preserve_devices && IS_DEVICE(mode))
48 || (preserve_specials && IS_SPECIAL(mode))) {
49 if (protocol_version < 28) {
50@@ -427,6 +437,10 @@ static void send_file_entry(struct file_
51 write_int(f, modtime);
52 if (!(flags & XMIT_SAME_MODE))
53 write_int(f, to_wire_mode(mode));
54+#ifdef SUPPORT_FLAGS
55+ if (preserve_flags && !(flags & XMIT_SAME_FLAGS))
56+ write_int(f, (int)fileflags);
57+#endif
58 if (preserve_uid && !(flags & XMIT_SAME_UID)) {
59 if (!numeric_ids)
60 add_uid(uid);
61@@ -496,6 +510,9 @@ static struct file_struct *receive_file_
62 {
63 static time_t modtime;
64 static mode_t mode;
65+#ifdef SUPPORT_FLAGS
66+ static uint32 fileflags;
67+#endif
68 static int64 dev;
69 static dev_t rdev;
70 static uint32 rdev_major;
71@@ -569,9 +586,12 @@ static struct file_struct *receive_file_
72 modtime = (time_t)read_int(f);
73 if (!(flags & XMIT_SAME_MODE))
74 mode = from_wire_mode(read_int(f));
75-
76 if (chmod_modes && !S_ISLNK(mode))
77 mode = tweak_mode(mode, chmod_modes);
78+#ifdef SUPPORT_FLAGS
79+ if (preserve_flags && !(flags & XMIT_SAME_FLAGS))
80+ fileflags = (uint32)read_int(f);
81+#endif
82
83 if (preserve_uid && !(flags & XMIT_SAME_UID))
84 uid = (uid_t)read_int(f);
85@@ -622,6 +642,9 @@ static struct file_struct *receive_file_
86 file->modtime = modtime;
87 file->length = file_length;
88 file->mode = mode;
89+#ifdef SUPPORT_FLAGS
90+ file->fileflags = fileflags;
91+#endif
92 file->uid = uid;
93 file->gid = gid;
94
95@@ -874,6 +897,9 @@ struct file_struct *make_file(char *fnam
96 file->modtime = st.st_mtime;
97 file->length = st.st_size;
98 file->mode = st.st_mode;
99+#ifdef SUPPORT_FLAGS
100+ file->fileflags = st.st_flags;
101+#endif
102 file->uid = st.st_uid;
103 file->gid = st.st_gid;
104
105--- old/generator.c
106+++ new/generator.c
107@@ -126,6 +126,9 @@ static int delete_item(char *fname, int
108 if (!S_ISDIR(mode)) {
109 if (max_delete && ++deletion_count > max_delete)
110 return 0;
111+#ifdef SUPPORT_FLAGS
112+ make_mutable(fname);
113+#endif
114 if (make_backups && (backup_dir || !is_backup_file(fname)))
115 ok = make_backup(fname);
116 else
117@@ -150,10 +153,17 @@ static int delete_item(char *fname, int
118 ok = 0;
119 errno = ENOTEMPTY;
120 } else if (make_backups && !backup_dir && !is_backup_file(fname)
121- && !(flags & DEL_FORCE_RECURSE))
122+ && !(flags & DEL_FORCE_RECURSE)) {
123+#ifdef SUPPORT_FLAGS
124+ make_mutable(fname);
125+#endif
126 ok = make_backup(fname);
127- else
128+ } else {
129+#ifdef SUPPORT_FLAGS
130+ make_mutable(fname);
131+#endif
132 ok = do_rmdir(fname) == 0;
133+ }
134 if (ok) {
135 if (!(flags & DEL_TERSE))
136 log_delete(fname, mode);
137--- old/options.c
138+++ new/options.c
139@@ -46,6 +46,7 @@ int copy_links = 0;
140 int preserve_links = 0;
141 int preserve_hard_links = 0;
142 int preserve_perms = 0;
143+int preserve_flags = 0;
144 int preserve_executability = 0;
145 int preserve_devices = 0;
146 int preserve_specials = 0;
147@@ -196,6 +197,7 @@ static void print_rsync_version(enum log
148 char const *hardlinks = "no ";
149 char const *links = "no ";
150 char const *ipv6 = "no ";
151+ char const *flags = "no ";
152 STRUCT_STAT *dumstat;
153
154 #ifdef HAVE_SOCKETPAIR
155@@ -218,6 +220,10 @@ static void print_rsync_version(enum log
156 ipv6 = "";
157 #endif
158
159+#ifdef SUPPORT_FLAGS
160+ flags = "";
161+#endif
162+
163 rprintf(f, "%s version %s protocol version %d\n",
164 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
165 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
166@@ -230,9 +236,9 @@ static void print_rsync_version(enum log
167 /* Note that this field may not have type ino_t. It depends
168 * on the complicated interaction between largefile feature
169 * macros. */
170- rprintf(f, " %sinplace, %sIPv6, "
171+ rprintf(f, " %sinplace, %sIPv6, %sfile flags, "
172 "%d-bit system inums, %d-bit internal inums\n",
173- have_inplace, ipv6,
174+ have_inplace, ipv6, flags,
175 (int) (sizeof dumstat->st_ino * 8),
176 (int) (sizeof (int64) * 8));
177 #ifdef MAINTAINER_MODE
178@@ -294,6 +300,7 @@ void usage(enum logcode F)
179 rprintf(F," -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
180 rprintf(F," -H, --hard-links preserve hard links\n");
181 rprintf(F," -p, --perms preserve permissions\n");
182+ rprintf(F," --flags preserve file flags\n");
183 rprintf(F," -E, --executability preserve the file's executability\n");
184 rprintf(F," --chmod=CHMOD change destination permissions\n");
185 rprintf(F," -o, --owner preserve owner (super-user only)\n");
186@@ -409,6 +416,8 @@ static struct poptOption long_options[]
187 {"perms", 'p', POPT_ARG_VAL, &preserve_perms, 1, 0, 0 },
188 {"no-perms", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
189 {"no-p", 0, POPT_ARG_VAL, &preserve_perms, 0, 0, 0 },
190+ {"flags", 0, POPT_ARG_VAL, &preserve_flags, 1, 0, 0 },
191+ {"no-flags", 0, POPT_ARG_VAL, &preserve_flags, 0, 0, 0 },
192 {"executability", 'E', POPT_ARG_NONE, &preserve_executability, 0, 0, 0 },
193 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
194 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
195@@ -1107,6 +1116,15 @@ int parse_arguments(int *argc, const cha
196 }
197 #endif
198
199+#ifndef SUPPORT_FLAGS
200+ if (preserve_flags) {
201+ snprintf(err_buf, sizeof err_buf,
202+ "file flags are not supported on this %s\n",
203+ am_server ? "server" : "client");
204+ return 0;
205+ }
206+#endif
207+
208 if (write_batch && read_batch) {
209 snprintf(err_buf, sizeof err_buf,
210 "--write-batch and --read-batch can not be used together\n");
211@@ -1562,6 +1580,9 @@ void server_options(char **args,int *arg
212 if (xfer_dirs && !recurse && delete_mode && am_sender)
213 args[ac++] = "--no-r";
214
215+ if (preserve_flags)
216+ args[ac++] = "--flags";
217+
218 if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
219 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
220 goto oom;
221--- old/rsync.c
222+++ new/rsync.c
223@@ -34,6 +34,7 @@ extern int verbose;
224 extern int dry_run;
225 extern int daemon_log_format_has_i;
226 extern int preserve_perms;
227+extern int preserve_flags;
228 extern int preserve_executability;
229 extern int preserve_times;
230 extern int omit_dir_times;
231@@ -217,6 +218,19 @@ int set_file_attrs(char *fname, struct f
232 }
233 #endif
234
235+#ifdef SUPPORT_FLAGS
236+ if (preserve_flags && !S_ISLNK(st->st_mode)
237+ && st->st_flags != file->fileflags) {
238+ if (do_chflags(fname, file->fileflags) != 0) {
239+ rsyserr(FERROR, errno,
240+ "failed to set file flags on %s",
241+ full_fname(fname));
242+ return 0;
243+ }
244+ updated = 1;
245+ }
246+#endif
247+
248 if (verbose > 1 && flags & ATTRS_REPORT) {
249 enum logcode code = daemon_log_format_has_i || dry_run
250 ? FCLIENT : FINFO;
251@@ -252,6 +266,10 @@ void finish_transfer(char *fname, char *
252 {
253 int ret;
254
255+#ifdef SUPPORT_FLAGS
256+ make_mutable(fname); /* XXX */
257+#endif
258+
259 if (inplace) {
260 if (verbose > 2)
261 rprintf(FINFO, "finishing %s\n", fname);
262@@ -305,3 +323,27 @@ const char *who_am_i(void)
263 return am_server ? "server" : "client";
264 return am_sender ? "sender" : am_generator ? "generator" : "receiver";
265 }
266+
267+#ifdef SUPPORT_FLAGS
268+/* remove immutable flags from an object, so it can be altered/removed. */
269+void make_mutable(char *fname)
270+{
271+#define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | UF_NOUNLINK | SF_IMMUTABLE | SF_APPEND | SF_NOUNLINK)
272+ STRUCT_STAT stb;
273+ int failed;
274+
275+ if (dry_run)
276+ return;
277+
278+ /* XXX get rid of this extra stat() */
279+#ifdef SUPPORT_LINKS
280+ failed = do_lstat(fname, &stb);
281+#else
282+ failed = do_stat(fname, &stb);
283+#endif
284+ if (failed)
285+ return;
286+ if (stb.st_flags & NOCHANGEBITS)
287+ do_chflags(fname, stb.st_flags & ~NOCHANGEBITS);
288+}
289+#endif
290--- old/rsync.h
291+++ new/rsync.h
292@@ -54,6 +54,7 @@
293 #define XMIT_HAS_IDEV_DATA (1<<9)
294 #define XMIT_SAME_DEV (1<<10)
295 #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
296+#define XMIT_SAME_FLAGS (1<<12)
297
298 /* These flags are used in the live flist data. */
299
300@@ -519,6 +520,9 @@ struct file_struct {
301 struct hlink *links;
302 } link_u;
303 time_t modtime;
304+#ifdef SUPPORT_FLAGS
305+ uint32 fileflags;
306+#endif
307 uid_t uid;
308 gid_t gid;
309 mode_t mode;
310@@ -702,6 +706,9 @@ extern int errno;
311 #ifdef HAVE_LINK
312 #define SUPPORT_HARD_LINKS 1
313 #endif
314+#ifdef HAVE_CHFLAGS
315+#define SUPPORT_FLAGS 1
316+#endif
317
318 #ifdef HAVE_SIGACTION
319 #define SIGACTION(n,h) sigact.sa_handler=(h), sigaction((n),&sigact,NULL)
320--- old/rsync.yo
321+++ new/rsync.yo
322@@ -322,6 +322,7 @@ to the detailed description below for a
323 -p, --perms preserve permissions
324 -E, --executability preserve executability
325 --chmod=CHMOD change destination permissions
326+ --flags preserve file flags
327 -o, --owner preserve owner (super-user only)
328 -g, --group preserve group
329 --devices preserve device files (super-user only)
330@@ -498,7 +499,9 @@ specified, in which case bf(-r) is not i
331
332 Note that bf(-a) bf(does not preserve hardlinks), because
333 finding multiply-linked files is expensive. You must separately
334-specify bf(-H).
335+specify bf(-H). Note also that for compatibility, bf(-a)
336+currently bf(does not include --flags) (see there) to include preserving
337+change file flags (if supported by the OS).
338
339 dit(--no-OPTION) You may turn off one or more implied options by prefixing
340 the option name with "no-". Not all options may be prefixed with a "no-":
341@@ -793,6 +796,13 @@ quote(itemize(
342
343 If bf(--perms) is enabled, this option is ignored.
344
345+dit(bf(--flags)) This option causes rsync to update the change file flags
346+to be the same as the source file, if your OS supports the chflags(2)
347+system call. In any case, an attempt is made to remove flags that would
348+prevent a file to be altered. Some flags can only be altered by the
349+super-user and can only be unset below a certain secure-level (usually
350+single-user mode).
351+
352 dit(bf(--chmod)) This option tells rsync to apply one or more
353 comma-separated "chmod" strings to the permission of the files in the
354 transfer. The resulting value is treated as though it was the permissions
355--- old/syscall.c
356+++ new/syscall.c
357@@ -155,6 +155,15 @@ int do_chmod(const char *path, mode_t mo
358 }
359 #endif
360
361+#ifdef SUPPORT_FLAGS
362+int do_chflags(const char *path, u_long flags)
363+{
364+ if (dry_run) return 0;
365+ RETURN_ERROR_IF_RO_OR_LO;
366+ return chflags(path, flags);
367+}
368+#endif
369+
370 int do_rename(const char *fname1, const char *fname2)
371 {
372 if (dry_run) return 0;