Fixed failing hunks.
[rsync/rsync-patches.git] / flags.diff
1 This patch provides --flags, which preserves the st_flags field.
2 Modified from a patch that was written by Rolf Grossmann.
3
4 To 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 TODO: fix --delete-delay to work with --flags option.
12
13 --- old/configure.in
14 +++ new/configure.in
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)
22  
23  AC_CHECK_FUNCS(getpgrp tcgetpgrp)
24 --- old/flist.c
25 +++ new/flist.c
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 @@ -313,6 +314,9 @@ static void send_file_entry(struct file_
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;
44 @@ -343,6 +347,12 @@ static void send_file_entry(struct file_
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) {
57 @@ -429,6 +439,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));
61 +#ifdef SUPPORT_FLAGS
62 +       if (preserve_fileflags && !(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);
68 @@ -497,6 +511,9 @@ static struct file_struct *recv_file_ent
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;
78 @@ -571,9 +588,12 @@ static struct file_struct *recv_file_ent
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_fileflags && !(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);
92 @@ -639,6 +659,10 @@ static struct file_struct *recv_file_ent
93         file->modtime = modtime;
94         file->length = file_length;
95         file->mode = mode;
96 +#ifdef SUPPORT_FLAGS
97 +       if (preserve_fileflags)
98 +               F_FFLAGS(file) = fileflags;
99 +#endif
100         if (preserve_uid)
101                 F_UID(file) = uid;
102         if (preserve_gid)
103 @@ -902,6 +926,10 @@ struct file_struct *make_file(const char
104         file->modtime = st.st_mtime;
105         file->length = st.st_size;
106         file->mode = st.st_mode;
107 +#ifdef SUPPORT_FLAGS
108 +       if (preserve_fileflags)
109 +               F_FFLAGS(file) = st.st_flags;
110 +#endif
111         if (preserve_uid)
112                 F_UID(file) = st.st_uid;
113         if (preserve_gid)
114 --- old/generator.c
115 +++ new/generator.c
116 @@ -101,6 +101,12 @@ int non_perishable_cnt = 0;
117  static int deletion_count = 0; /* used to implement --max-delete */
118  static FILE *delete_delay_fp = NULL;
119  
120 +#ifdef SUPPORT_FLAGS
121 +#define FILEFLAGS(ff) ff
122 +#else
123 +#define FILEFLAGS(ff) 0
124 +#endif
125 +
126  /* For calling delete_item() and delete_dir_contents(). */
127  #define DEL_RECURSE            (1<<1) /* recurse */
128  #define DEL_DIR_IS_EMPTY       (1<<2) /* internal delete_FUNCTIONS use only */
129 @@ -116,7 +122,6 @@ enum delret {
130  /* Forward declaration for delete_item(). */
131  static enum delret delete_dir_contents(char *fname, int flags);
132  
133 -
134  static int is_backup_file(char *fn)
135  {
136         int k = strlen(fn) - backup_suffix_len;
137 @@ -129,17 +134,20 @@ static int is_backup_file(char *fn)
138   * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
139   * a directory! (The buffer is used for recursion, but returned unchanged.)
140   */
141 -static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
142 +static enum delret delete_item(char *fbuf, int mode, uint32 fileflags, char *replace, int flags)
143  {
144         enum delret ret;
145         char *what;
146         int ok;
147  
148         if (verbose > 2) {
149 -               rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
150 -                       fbuf, mode, flags);
151 +               rprintf(FINFO, "delete_item(%s) mode=%o fileflags=%o flags=%d\n",
152 +                       fbuf, mode, fileflags, flags);
153         }
154  
155 +#ifdef SUPPORT_FLAGS
156 +       make_mutable(fbuf, mode, fileflags);
157 +#endif
158         if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
159                 ignore_perishable = 1;
160                 /* If DEL_RECURSE is not set, this just reports emptiness. */
161 @@ -251,7 +259,7 @@ static enum delret delete_dir_contents(c
162                 if (S_ISDIR(fp->mode)
163                  && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
164                         ret = DR_NOT_EMPTY;
165 -               if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
166 +               if (delete_item(fname, fp->mode, FILEFLAGS(fp->fileflags), NULL, flags) != DR_SUCCESS)
167                         ret = DR_NOT_EMPTY;
168         }
169  
170 @@ -403,10 +411,10 @@ static void delete_in_dir(struct file_li
171                 }
172                 if (flist_find(flist, fp) < 0) {
173                         f_name(fp, delbuf);
174 -                       if (delete_delay_fp)
175 +                       if (delete_delay_fp) /* XXX need to output fileflags value here too */
176                                 fprintf(delete_delay_fp, "%o %s%c", (short)fp->mode, delbuf, '\0');
177                         else
178 -                               delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
179 +                               delete_item(delbuf, fp->mode, FILEFLAGS(fp->fileflags), NULL, DEL_RECURSE);
180                 }
181         }
182  
183 @@ -1101,7 +1109,7 @@ static void recv_generator(char *fname, 
184                  * we need to delete it.  If it doesn't exist, then
185                  * (perhaps recursively) create it. */
186                 if (statret == 0 && !S_ISDIR(st.st_mode)) {
187 -                       if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
188 +                       if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "directory", del_opts) != 0)
189                                 return;
190                         statret = -1;
191                 }
192 @@ -1195,7 +1203,7 @@ static void recv_generator(char *fname, 
193                         }
194                         /* Not the right symlink (or not a symlink), so
195                          * delete it. */
196 -                       if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
197 +                       if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "symlink", del_opts) != 0)
198                                 return;
199                 } else if (basis_dir[0] != NULL) {
200                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
201 @@ -1267,7 +1275,7 @@ static void recv_generator(char *fname, 
202                                         goto return_with_success;
203                                 return;
204                         }
205 -                       if (delete_item(fname, st.st_mode, t, del_opts) != 0)
206 +                       if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), t, del_opts) != 0)
207                                 return;
208                 } else if (basis_dir[0] != NULL) {
209                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
210 @@ -1353,7 +1361,7 @@ static void recv_generator(char *fname, 
211         fnamecmp_type = FNAMECMP_FNAME;
212  
213         if (statret == 0 && !S_ISREG(st.st_mode)) {
214 -               if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
215 +               if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "regular file", del_opts) != 0)
216                         return;
217                 statret = -1;
218                 stat_errno = ENOENT;
219 --- old/options.c
220 +++ new/options.c
221 @@ -48,6 +48,7 @@ int copy_links = 0;
222  int preserve_links = 0;
223  int preserve_hard_links = 0;
224  int preserve_perms = 0;
225 +int preserve_fileflags = 0;
226  int preserve_executability = 0;
227  int preserve_devices = 0;
228  int preserve_specials = 0;
229 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
230         char const *hardlinks = "no ";
231         char const *links = "no ";
232         char const *ipv6 = "no ";
233 +       char const *fileflags = "no ";
234         STRUCT_STAT *dumstat;
235  
236  #ifdef HAVE_SOCKETPAIR
237 @@ -223,6 +225,10 @@ static void print_rsync_version(enum log
238         ipv6 = "";
239  #endif
240  
241 +#ifdef SUPPORT_FLAGS
242 +       fileflags = "";
243 +#endif
244 +
245         rprintf(f, "%s  version %s  protocol version %d\n",
246                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
247         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
248 @@ -233,8 +239,8 @@ static void print_rsync_version(enum log
249                 (int)(sizeof (int64) * 8));
250         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
251                 got_socketpair, hardlinks, links, ipv6, have_inplace);
252 -       rprintf(f, "    %sappend\n",
253 -               have_inplace);
254 +       rprintf(f, "    %sappend, %sfile-flags\n",
255 +               have_inplace, fileflags);
256  
257  #ifdef MAINTAINER_MODE
258         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
259 @@ -300,6 +306,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,"     --fileflags             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 @@ -421,6 +428,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 +  {"fileflags",        0,  POPT_ARG_VAL,    &preserve_fileflags, 1, 0, 0 },
272 +  {"no-fileflags",     0,  POPT_ARG_VAL,    &preserve_fileflags, 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 @@ -1126,6 +1135,15 @@ int parse_arguments(int *argc, const cha
277         }
278  #endif
279  
280 +#ifndef SUPPORT_FLAGS
281 +       if (preserve_fileflags) {
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 @@ -1223,6 +1241,8 @@ int parse_arguments(int *argc, const cha
293                 preserve_uid = flist_extra_ndx++;
294         if (preserve_gid)
295                 preserve_gid = flist_extra_ndx++;
296 +       if (preserve_fileflags)
297 +               preserve_fileflags = flist_extra_ndx++;
298  
299         *argv = poptGetArgs(pc);
300         *argc = count_args(*argv);
301 @@ -1585,6 +1605,9 @@ void server_options(char **args,int *arg
302         if (xfer_dirs && !recurse && delete_mode && am_sender)
303                 args[ac++] = "--no-r";
304  
305 +       if (preserve_fileflags)
306 +               args[ac++] = "--flags";
307 +
308         if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
309                 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
310                         goto oom;
311 --- old/rsync.c
312 +++ new/rsync.c
313 @@ -30,9 +30,12 @@
314  #include <langinfo.h>
315  #endif
316  
317 +#define NOCHANGE_FLAGS (UF_IMMUTABLE|UF_APPEND|UF_NOUNLINK|SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK)
318 +
319  extern int verbose;
320  extern int dry_run;
321  extern int preserve_perms;
322 +extern int preserve_fileflags;
323  extern int preserve_executability;
324  extern int preserve_times;
325  extern int omit_dir_times;
326 @@ -176,6 +179,41 @@ mode_t dest_mode(mode_t flist_mode, mode
327         return new_mode;
328  }
329  
330 +#ifdef SUPPORT_FLAGS
331 +/* Set a file's st_flags. */
332 +static int set_fileflags(const char *fname, uint32 fileflags)
333 +{
334 +       if (do_chflags(fname, fileflags) != 0) {
335 +               rsyserr(FERROR, errno,
336 +                       "failed to set file flags on %s",
337 +                       full_fname(fname));
338 +               return 0;
339 +       }
340 +
341 +       return 1;
342 +}
343 +
344 +/* Remove immutable flags from an object, so it can be altered/removed. */
345 +void make_mutable(char *fname, mode_t mode, uint32 fileflags)
346 +{
347 +       if (!preserve_fileflags && S_ISLNK(mode))
348 +               return;
349 +
350 +       if (fileflags & NOCHANGE_FLAGS)
351 +               set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
352 +}
353 +
354 +/* Undo a prior make_mutable() call. */
355 +void undo_make_mutable(char *fname, mode_t mode, uint32 fileflags)
356 +{
357 +       if (!preserve_fileflags && S_ISLNK(mode))
358 +               return;
359 +
360 +       if (fileflags & NOCHANGE_FLAGS)
361 +               set_fileflags(fname, fileflags);
362 +}
363 +#endif
364 +
365  int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
366                    int flags)
367  {
368 @@ -274,6 +312,15 @@ int set_file_attrs(char *fname, struct f
369         }
370  #endif
371  
372 +#ifdef SUPPORT_FLAGS
373 +       if (preserve_fileflags && !S_ISLNK(st->st_mode)
374 +        && st->st_flags != file->fileflags) {
375 +               if (!set_fileflags(fname, file->fileflags))
376 +                       return 0;
377 +               updated = 1;
378 +       }
379 +#endif
380 +
381         if (verbose > 1 && flags & ATTRS_REPORT) {
382                 if (updated)
383                         rprintf(FCLIENT, "%s\n", fname);
384 @@ -321,6 +368,9 @@ void finish_transfer(char *fname, char *
385         set_file_attrs(fnametmp, file, NULL,
386                        ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
387  
388 +#ifdef SUPPORT_FLAGS
389 +       make_mutable(fnametmp, file->mode, file->fileflags);
390 +#endif
391         /* move tmp file over real file */
392         if (verbose > 2)
393                 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
394 @@ -335,6 +385,9 @@ void finish_transfer(char *fname, char *
395         }
396         if (ret == 0) {
397                 /* The file was moved into place (not copied), so it's done. */
398 +#ifdef SUPPORT_FLAGS
399 +               undo_make_mutable(fname, file->mode, file->fileflags);
400 +#endif
401                 return;
402         }
403         /* The file was copied, so tweak the perms of the copied file.  If it
404 --- old/rsync.h
405 +++ new/rsync.h
406 @@ -54,6 +54,7 @@
407  #define XMIT_HAS_IDEV_DATA (1<<9)
408  #define XMIT_SAME_DEV (1<<10)
409  #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
410 +#define XMIT_SAME_FLAGS (1<<12)
411  
412  /* These flags are used in the live flist data. */
413  
414 @@ -347,6 +348,10 @@ enum msgcode {
415  #define schar char
416  #endif
417  
418 +#ifdef HAVE_CHFLAGS
419 +#define SUPPORT_FLAGS 1
420 +#endif
421 +
422  /* Find a variable that is either exactly 32-bits or longer.
423   * If some code depends on 32-bit truncation, it will need to
424   * take special action in a "#if SIZEOF_INT32 > 4" section. */
425 @@ -540,6 +545,7 @@ union flist_extras {
426  /* When enabled, all entries have these: */
427  #define F_UID(f) FLIST_EXTRA(f, preserve_uid).uid
428  #define F_GID(f) FLIST_EXTRA(f, preserve_gid).gid
429 +#define F_FFLAGS(f) FLIST_EXTRA(f, preserve_fileflags).num
430  
431  /* These are per-entry optional and mutally exclusive: */
432  #define F_IDEV(f) FLIST_EXTRA(f, flist_extra_ndx).idev
433 --- old/rsync.yo
434 +++ new/rsync.yo
435 @@ -321,6 +321,7 @@ to the detailed description below for a 
436   -K, --keep-dirlinks         treat symlinked dir on receiver as dir
437   -H, --hard-links            preserve hard links
438   -p, --perms                 preserve permissions
439 +     --flags                 preserve file flags
440   -E, --executability         preserve executability
441       --chmod=CHMOD           affect file and/or directory permissions
442   -o, --owner                 preserve owner (super-user only)
443 @@ -510,7 +511,9 @@ specified, in which case bf(-r) is not i
444  
445  Note that bf(-a) bf(does not preserve hardlinks), because
446  finding multiply-linked files is expensive.  You must separately
447 -specify bf(-H).
448 +specify bf(-H).  Note also that for compatibility, bf(-a)
449 +currently bf(does not include --flags) (see there) to include preserving
450 +change file flags (if supported by the OS).
451  
452  dit(--no-OPTION) You may turn off one or more implied options by prefixing
453  the option name with "no-".  Not all options may be prefixed with a "no-":
454 @@ -805,6 +808,13 @@ quote(itemization(
455  
456  If bf(--perms) is enabled, this option is ignored.
457  
458 +dit(bf(--flags)) This option causes rsync to update the change file flags
459 +to be the same as the source file, if your OS supports the bf(chflags)(2)
460 +system call.  In any case, an attempt is made to remove flags that would
461 +prevent a file to be altered.  Some flags can only be altered by the
462 +super-user and can only be unset below a certain secure-level (usually
463 +single-user mode).
464 +
465  dit(bf(--chmod)) This option tells rsync to apply one or more
466  comma-separated "chmod" strings to the permission of the files in the
467  transfer.  The resulting value is treated as though it was the permissions
468 --- old/syscall.c
469 +++ new/syscall.c
470 @@ -153,6 +153,15 @@ int do_chmod(const char *path, mode_t mo
471  }
472  #endif
473  
474 +#ifdef SUPPORT_FLAGS
475 +int do_chflags(const char *path, u_long flags)
476 +{
477 +       if (dry_run) return 0;
478 +       RETURN_ERROR_IF_RO_OR_LO;
479 +       return chflags(path, flags);
480 +}
481 +#endif
482 +
483  int do_rename(const char *fname1, const char *fname2)
484  {
485         if (dry_run) return 0;