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