Commit | Line | Data |
---|---|---|
31611b92 WD |
1 | This patch provides --flags, which preserves the st_flags field. |
2 | Modified 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 | |
1898c899 | 19 | @@ -44,6 +44,7 @@ extern int preserve_links; |
31611b92 WD |
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; | |
1898c899 | 27 | @@ -299,6 +300,9 @@ static void send_file_entry(struct file_ |
31611b92 WD |
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; | |
1898c899 | 37 | @@ -329,6 +333,12 @@ static void send_file_entry(struct file_ |
31611b92 WD |
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) { | |
1898c899 | 50 | @@ -412,6 +422,10 @@ static void send_file_entry(struct file_ |
31611b92 WD |
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); | |
1898c899 | 61 | @@ -479,6 +493,9 @@ static struct file_struct *receive_file_ |
31611b92 WD |
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; | |
1898c899 | 71 | @@ -552,9 +569,12 @@ static struct file_struct *receive_file_ |
31611b92 WD |
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); | |
1898c899 | 85 | @@ -605,6 +625,9 @@ static struct file_struct *receive_file_ |
31611b92 WD |
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 | ||
1898c899 | 95 | @@ -855,6 +878,9 @@ struct file_struct *make_file(char *fnam |
31611b92 WD |
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 | |
ff318e90 | 107 | @@ -123,6 +123,9 @@ static int delete_item(char *fname, int |
31611b92 WD |
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 | |
ff318e90 | 117 | @@ -147,10 +150,17 @@ static int delete_item(char *fname, int |
31611b92 WD |
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 | |
1898c899 | 139 | @@ -48,6 +48,7 @@ int copy_links = 0; |
31611b92 WD |
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; | |
1898c899 | 147 | @@ -200,6 +201,7 @@ static void print_rsync_version(enum log |
31611b92 WD |
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 | |
1898c899 | 155 | @@ -222,6 +224,10 @@ static void print_rsync_version(enum log |
31611b92 WD |
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"); | |
1898c899 | 166 | @@ -234,9 +240,9 @@ static void print_rsync_version(enum log |
31611b92 WD |
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 | |
ff318e90 | 178 | @@ -302,6 +308,7 @@ void usage(enum logcode F) |
31611b92 WD |
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"); | |
ff318e90 | 186 | @@ -420,6 +427,8 @@ static struct poptOption long_options[] |
31611b92 WD |
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 }, | |
ff318e90 | 195 | @@ -1122,6 +1131,15 @@ int parse_arguments(int *argc, const cha |
31611b92 WD |
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"); | |
ff318e90 | 211 | @@ -1579,6 +1597,9 @@ void server_options(char **args,int *arg |
31611b92 WD |
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 | |
ff318e90 WD |
223 | @@ -33,6 +33,7 @@ |
224 | extern int verbose; | |
31611b92 | 225 | extern int dry_run; |
31611b92 WD |
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; | |
ff318e90 | 231 | @@ -216,6 +217,19 @@ int set_file_attrs(char *fname, struct f |
31611b92 WD |
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) { | |
ff318e90 WD |
249 | if (updated) |
250 | rprintf(FCLIENT, "%s\n", fname); | |
251 | @@ -249,6 +263,10 @@ void finish_transfer(char *fname, char * | |
31611b92 WD |
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); | |
ff318e90 | 262 | @@ -302,3 +320,21 @@ const char *who_am_i(void) |
31611b92 WD |
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; | |
31611b92 WD |
273 | + |
274 | + if (dry_run) | |
275 | + return; | |
276 | + | |
277 | + /* XXX get rid of this extra stat() */ | |
21158bc6 | 278 | + if (do_lstat(fname, &stb) < 0) |
31611b92 WD |
279 | + return; |
280 | + if (stb.st_flags & NOCHANGEBITS) | |
281 | + do_chflags(fname, stb.st_flags & ~NOCHANGEBITS); | |
282 | +} | |
283 | +#endif | |
284 | --- old/rsync.h | |
285 | +++ new/rsync.h | |
286 | @@ -54,6 +54,7 @@ | |
287 | #define XMIT_HAS_IDEV_DATA (1<<9) | |
288 | #define XMIT_SAME_DEV (1<<10) | |
289 | #define XMIT_RDEV_MINOR_IS_SMALL (1<<11) | |
290 | +#define XMIT_SAME_FLAGS (1<<12) | |
291 | ||
292 | /* These flags are used in the live flist data. */ | |
293 | ||
1898c899 | 294 | @@ -338,6 +339,10 @@ enum msgcode { |
6e6f514c WD |
295 | #define schar char |
296 | #endif | |
297 | ||
298 | +#ifdef HAVE_CHFLAGS | |
299 | +#define SUPPORT_FLAGS 1 | |
300 | +#endif | |
301 | + | |
302 | /* Find a variable that is either exactly 32-bits or longer. | |
303 | * If some code depends on 32-bit truncation, it will need to | |
304 | * take special action in a "#if SIZEOF_INT32 > 4" section. */ | |
1898c899 | 305 | @@ -520,6 +525,9 @@ struct file_struct { |
31611b92 WD |
306 | struct hlink *links; |
307 | } link_u; | |
308 | time_t modtime; | |
309 | +#ifdef SUPPORT_FLAGS | |
310 | + uint32 fileflags; | |
311 | +#endif | |
312 | uid_t uid; | |
313 | gid_t gid; | |
314 | mode_t mode; | |
31611b92 WD |
315 | --- old/rsync.yo |
316 | +++ new/rsync.yo | |
317 | @@ -322,6 +322,7 @@ to the detailed description below for a | |
318 | -p, --perms preserve permissions | |
319 | -E, --executability preserve executability | |
320 | --chmod=CHMOD change destination permissions | |
321 | + --flags preserve file flags | |
322 | -o, --owner preserve owner (super-user only) | |
323 | -g, --group preserve group | |
324 | --devices preserve device files (super-user only) | |
ff318e90 | 325 | @@ -502,7 +503,9 @@ specified, in which case bf(-r) is not i |
31611b92 WD |
326 | |
327 | Note that bf(-a) bf(does not preserve hardlinks), because | |
328 | finding multiply-linked files is expensive. You must separately | |
329 | -specify bf(-H). | |
330 | +specify bf(-H). Note also that for compatibility, bf(-a) | |
331 | +currently bf(does not include --flags) (see there) to include preserving | |
332 | +change file flags (if supported by the OS). | |
333 | ||
334 | dit(--no-OPTION) You may turn off one or more implied options by prefixing | |
335 | the option name with "no-". Not all options may be prefixed with a "no-": | |
ff318e90 | 336 | @@ -797,6 +800,13 @@ quote(itemize( |
31611b92 WD |
337 | |
338 | If bf(--perms) is enabled, this option is ignored. | |
339 | ||
340 | +dit(bf(--flags)) This option causes rsync to update the change file flags | |
341 | +to be the same as the source file, if your OS supports the chflags(2) | |
342 | +system call. In any case, an attempt is made to remove flags that would | |
343 | +prevent a file to be altered. Some flags can only be altered by the | |
344 | +super-user and can only be unset below a certain secure-level (usually | |
345 | +single-user mode). | |
346 | + | |
347 | dit(bf(--chmod)) This option tells rsync to apply one or more | |
348 | comma-separated "chmod" strings to the permission of the files in the | |
349 | transfer. The resulting value is treated as though it was the permissions | |
350 | --- old/syscall.c | |
351 | +++ new/syscall.c | |
1898c899 | 352 | @@ -152,6 +152,15 @@ int do_chmod(const char *path, mode_t mo |
31611b92 WD |
353 | } |
354 | #endif | |
355 | ||
356 | +#ifdef SUPPORT_FLAGS | |
357 | +int do_chflags(const char *path, u_long flags) | |
358 | +{ | |
359 | + if (dry_run) return 0; | |
360 | + RETURN_ERROR_IF_RO_OR_LO; | |
361 | + return chflags(path, flags); | |
362 | +} | |
363 | +#endif | |
364 | + | |
365 | int do_rename(const char *fname1, const char *fname2) | |
366 | { | |
367 | if (dry_run) return 0; |