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 @@ -312,6 +313,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 @@ -342,6 +346,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 @@ -428,6 +438,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 @@ -496,6 +510,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 @@ -570,9 +587,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 @@ -650,6 +670,10 @@ static struct file_struct *recv_file_ent
93                 OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
94         }
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 @@ -927,6 +951,10 @@ struct file_struct *make_file(const char
104                 OPT_EXTRA(file, 0)->unum = (uint32)(st.st_size >> 32);
105         }
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 @@ -102,6 +102,12 @@ static char *deldelay_buf = NULL;
117  static int deldelay_fd = -1;
118  static BOOL solo_file = 0;
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 @@ -117,7 +123,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 @@ -130,17 +135,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 @@ -252,7 +260,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 @@ -308,8 +316,9 @@ static int remember_delete(struct file_s
171         
172         while (1) {
173                 len = snprintf(deldelay_buf + deldelay_cnt,
174 -                              deldelay_size - deldelay_cnt,
175 -                              "%x %s%c", (int)file->mode, fname, '\0');
176 +                              deldelay_size - deldelay_cnt, "%x %x %s%c",
177 +                              (int)file->mode, (int)FILEFLAGS(file->fileflags),
178 +                              fname, '\0');
179                 if ((deldelay_cnt += len) <= deldelay_size)
180                         break;
181                 if (deldelay_fd < 0 && !start_delete_delay_temp())
182 @@ -322,7 +331,7 @@ static int remember_delete(struct file_s
183         return 1;
184  }
185  
186 -static int read_delay_line(char *buf)
187 +static int read_delay_line(char *buf, int *fileflags_p)
188  {
189         static int read_pos = 0;
190         int j, len, mode;
191 @@ -364,7 +373,7 @@ static int read_delay_line(char *buf)
192  
193         bp = deldelay_buf + read_pos;
194  
195 -       if (sscanf(bp, "%x ", &mode) != 1) {
196 +       if (sscanf(bp, "%x %x ", &mode, fileflags_p) != 2) {
197           invalid_data:
198                 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
199                 return -1;
200 @@ -387,15 +396,15 @@ static int read_delay_line(char *buf)
201  
202  static void do_delayed_deletions(char *delbuf)
203  {
204 -       int mode;
205 +       int mode, fileflags;
206  
207         if (deldelay_fd >= 0) {
208                 if (deldelay_cnt && !flush_delete_delay())
209                         return;
210                 lseek(deldelay_fd, 0, 0);
211         }
212 -       while ((mode = read_delay_line(delbuf)) >= 0)
213 -               delete_item(delbuf, mode, NULL, DEL_RECURSE);
214 +       while ((mode = read_delay_line(delbuf, &fileflags)) >= 0)
215 +               delete_item(delbuf, mode, fileflags, NULL, DEL_RECURSE);
216         if (deldelay_fd >= 0)
217                 close(deldelay_fd);
218  }
219 @@ -476,7 +485,7 @@ static void delete_in_dir(struct file_li
220                                 if (!remember_delete(fp, delbuf))
221                                         break;
222                         } else
223 -                               delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
224 +                               delete_item(delbuf, fp->mode, FILEFLAGS(fp->fileflags), NULL, DEL_RECURSE);
225                 }
226         }
227  
228 @@ -1177,7 +1186,7 @@ static void recv_generator(char *fname, 
229                  * we need to delete it.  If it doesn't exist, then
230                  * (perhaps recursively) create it. */
231                 if (statret == 0 && !S_ISDIR(st.st_mode)) {
232 -                       if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
233 +                       if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "directory", del_opts) != 0)
234                                 return;
235                         statret = -1;
236                 }
237 @@ -1275,7 +1284,7 @@ static void recv_generator(char *fname, 
238                         }
239                         /* Not the right symlink (or not a symlink), so
240                          * delete it. */
241 -                       if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
242 +                       if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "symlink", del_opts) != 0)
243                                 return;
244                 } else if (basis_dir[0] != NULL) {
245                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
246 @@ -1354,7 +1363,7 @@ static void recv_generator(char *fname, 
247                                         goto return_with_success;
248                                 return;
249                         }
250 -                       if (delete_item(fname, st.st_mode, t, del_opts) != 0)
251 +                       if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), t, del_opts) != 0)
252                                 return;
253                 } else if (basis_dir[0] != NULL) {
254                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
255 @@ -1445,7 +1454,7 @@ static void recv_generator(char *fname, 
256         fnamecmp_type = FNAMECMP_FNAME;
257  
258         if (statret == 0 && !S_ISREG(st.st_mode)) {
259 -               if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
260 +               if (delete_item(fname, st.st_mode, FILEFLAGS(st.st_flags), "regular file", del_opts) != 0)
261                         return;
262                 statret = -1;
263                 stat_errno = ENOENT;
264 --- old/options.c
265 +++ new/options.c
266 @@ -48,6 +48,7 @@ int copy_links = 0;
267  int preserve_links = 0;
268  int preserve_hard_links = 0;
269  int preserve_perms = 0;
270 +int preserve_fileflags = 0;
271  int preserve_executability = 0;
272  int preserve_devices = 0;
273  int preserve_specials = 0;
274 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
275         char const *hardlinks = "no ";
276         char const *links = "no ";
277         char const *ipv6 = "no ";
278 +       char const *fileflags = "no ";
279         STRUCT_STAT *dumstat;
280  
281  #ifdef HAVE_SOCKETPAIR
282 @@ -223,6 +225,10 @@ static void print_rsync_version(enum log
283         ipv6 = "";
284  #endif
285  
286 +#ifdef SUPPORT_FLAGS
287 +       fileflags = "";
288 +#endif
289 +
290         rprintf(f, "%s  version %s  protocol version %d\n",
291                 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
292         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
293 @@ -233,8 +239,8 @@ static void print_rsync_version(enum log
294                 (int)(sizeof (int64) * 8));
295         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
296                 got_socketpair, hardlinks, links, ipv6, have_inplace);
297 -       rprintf(f, "    %sappend\n",
298 -               have_inplace);
299 +       rprintf(f, "    %sappend, %sfile-flags\n",
300 +               have_inplace, fileflags);
301  
302  #ifdef MAINTAINER_MODE
303         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
304 @@ -300,6 +306,7 @@ void usage(enum logcode F)
305    rprintf(F," -K, --keep-dirlinks         treat symlinked dir on receiver as dir\n");
306    rprintf(F," -H, --hard-links            preserve hard links\n");
307    rprintf(F," -p, --perms                 preserve permissions\n");
308 +  rprintf(F,"     --fileflags             preserve file-flags\n");
309    rprintf(F," -E, --executability         preserve the file's executability\n");
310    rprintf(F,"     --chmod=CHMOD           affect file and/or directory permissions\n");
311    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
312 @@ -421,6 +428,8 @@ static struct poptOption long_options[] 
313    {"perms",           'p', POPT_ARG_VAL,    &preserve_perms, 1, 0, 0 },
314    {"no-perms",         0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
315    {"no-p",             0,  POPT_ARG_VAL,    &preserve_perms, 0, 0, 0 },
316 +  {"fileflags",        0,  POPT_ARG_VAL,    &preserve_fileflags, 1, 0, 0 },
317 +  {"no-fileflags",     0,  POPT_ARG_VAL,    &preserve_fileflags, 0, 0, 0 },
318    {"executability",   'E', POPT_ARG_NONE,   &preserve_executability, 0, 0, 0 },
319    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
320    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
321 @@ -1126,6 +1135,15 @@ int parse_arguments(int *argc, const cha
322         }
323  #endif
324  
325 +#ifndef SUPPORT_FLAGS
326 +       if (preserve_fileflags) {
327 +               snprintf(err_buf, sizeof err_buf,
328 +                        "file flags are not supported on this %s\n",
329 +                        am_server ? "server" : "client");
330 +               return 0;
331 +       }
332 +#endif
333 +
334         if (write_batch && read_batch) {
335                 snprintf(err_buf, sizeof err_buf,
336                         "--write-batch and --read-batch can not be used together\n");
337 @@ -1223,6 +1241,8 @@ int parse_arguments(int *argc, const cha
338                 preserve_uid = ++flist_extra_cnt;
339         if (preserve_gid)
340                 preserve_gid = ++flist_extra_cnt;
341 +       if (preserve_fileflags)
342 +               preserve_fileflags = ++flist_extra_cnt;
343  
344         *argv = poptGetArgs(pc);
345         *argc = count_args(*argv);
346 @@ -1585,6 +1605,9 @@ void server_options(char **args,int *arg
347         if (xfer_dirs && !recurse && delete_mode && am_sender)
348                 args[ac++] = "--no-r";
349  
350 +       if (preserve_fileflags)
351 +               args[ac++] = "--flags";
352 +
353         if (do_compression && def_compress_level != Z_DEFAULT_COMPRESSION) {
354                 if (asprintf(&arg, "--compress-level=%d", def_compress_level) < 0)
355                         goto oom;
356 --- old/rsync.c
357 +++ new/rsync.c
358 @@ -30,9 +30,12 @@
359  #include <langinfo.h>
360  #endif
361  
362 +#define NOCHANGE_FLAGS (UF_IMMUTABLE|UF_APPEND|UF_NOUNLINK|SF_IMMUTABLE|SF_APPEND|SF_NOUNLINK)
363 +
364  extern int verbose;
365  extern int dry_run;
366  extern int preserve_perms;
367 +extern int preserve_fileflags;
368  extern int preserve_executability;
369  extern int preserve_times;
370  extern int omit_dir_times;
371 @@ -176,6 +179,41 @@ mode_t dest_mode(mode_t flist_mode, mode
372         return new_mode;
373  }
374  
375 +#ifdef SUPPORT_FLAGS
376 +/* Set a file's st_flags. */
377 +static int set_fileflags(const char *fname, uint32 fileflags)
378 +{
379 +       if (do_chflags(fname, fileflags) != 0) {
380 +               rsyserr(FERROR, errno,
381 +                       "failed to set file flags on %s",
382 +                       full_fname(fname));
383 +               return 0;
384 +       }
385 +
386 +       return 1;
387 +}
388 +
389 +/* Remove immutable flags from an object, so it can be altered/removed. */
390 +void make_mutable(char *fname, mode_t mode, uint32 fileflags)
391 +{
392 +       if (!preserve_fileflags && S_ISLNK(mode))
393 +               return;
394 +
395 +       if (fileflags & NOCHANGE_FLAGS)
396 +               set_fileflags(fname, fileflags & ~NOCHANGE_FLAGS);
397 +}
398 +
399 +/* Undo a prior make_mutable() call. */
400 +void undo_make_mutable(char *fname, mode_t mode, uint32 fileflags)
401 +{
402 +       if (!preserve_fileflags && S_ISLNK(mode))
403 +               return;
404 +
405 +       if (fileflags & NOCHANGE_FLAGS)
406 +               set_fileflags(fname, fileflags);
407 +}
408 +#endif
409 +
410  int set_file_attrs(char *fname, struct file_struct *file, STRUCT_STAT *st,
411                    int flags)
412  {
413 @@ -274,6 +312,15 @@ int set_file_attrs(char *fname, struct f
414         }
415  #endif
416  
417 +#ifdef SUPPORT_FLAGS
418 +       if (preserve_fileflags && !S_ISLNK(st->st_mode)
419 +        && st->st_flags != file->fileflags) {
420 +               if (!set_fileflags(fname, file->fileflags))
421 +                       return 0;
422 +               updated = 1;
423 +       }
424 +#endif
425 +
426         if (verbose > 1 && flags & ATTRS_REPORT) {
427                 if (updated)
428                         rprintf(FCLIENT, "%s\n", fname);
429 @@ -321,6 +368,9 @@ void finish_transfer(char *fname, char *
430         set_file_attrs(fnametmp, file, NULL,
431                        ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
432  
433 +#ifdef SUPPORT_FLAGS
434 +       make_mutable(fnametmp, file->mode, file->fileflags);
435 +#endif
436         /* move tmp file over real file */
437         if (verbose > 2)
438                 rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
439 @@ -335,6 +385,9 @@ void finish_transfer(char *fname, char *
440         }
441         if (ret == 0) {
442                 /* The file was moved into place (not copied), so it's done. */
443 +#ifdef SUPPORT_FLAGS
444 +               undo_make_mutable(fname, file->mode, file->fileflags);
445 +#endif
446                 return;
447         }
448         /* The file was copied, so tweak the perms of the copied file.  If it
449 --- old/rsync.h
450 +++ new/rsync.h
451 @@ -54,6 +54,7 @@
452  #define XMIT_HAS_IDEV_DATA (1<<9)
453  #define XMIT_SAME_DEV (1<<10)
454  #define XMIT_RDEV_MINOR_IS_SMALL (1<<11)
455 +#define XMIT_SAME_FLAGS (1<<12)
456  
457  /* These flags are used in the live flist data. */
458  
459 @@ -348,6 +349,10 @@ enum msgcode {
460  #define schar char
461  #endif
462  
463 +#ifdef HAVE_CHFLAGS
464 +#define SUPPORT_FLAGS 1
465 +#endif
466 +
467  /* Find a variable that is either exactly 32-bits or longer.
468   * If some code depends on 32-bit truncation, it will need to
469   * take special action in a "#if SIZEOF_INT32 > 4" section. */
470 @@ -553,6 +558,7 @@ extern int preserve_gid;
471  /* When the associated option is on, all entries will have these present: */
472  #define F_UID(f) REQ_EXTRA(f, preserve_uid)->uid
473  #define F_GID(f) REQ_EXTRA(f, preserve_gid)->gid
474 +#define F_FFLAGS(f) REQ_EXTRA(f, preserve_fileflags).num
475  
476  /* These items are per-entry optional and mutally exclusive: */
477  #define F_HL_IDEV(f) OPT_EXTRA(f, LEN64_BUMP(f))->idev
478 --- old/rsync.yo
479 +++ new/rsync.yo
480 @@ -321,6 +321,7 @@ to the detailed description below for a 
481   -K, --keep-dirlinks         treat symlinked dir on receiver as dir
482   -H, --hard-links            preserve hard links
483   -p, --perms                 preserve permissions
484 +     --flags                 preserve file flags
485   -E, --executability         preserve executability
486       --chmod=CHMOD           affect file and/or directory permissions
487   -o, --owner                 preserve owner (super-user only)
488 @@ -510,7 +511,9 @@ specified, in which case bf(-r) is not i
489  
490  Note that bf(-a) bf(does not preserve hardlinks), because
491  finding multiply-linked files is expensive.  You must separately
492 -specify bf(-H).
493 +specify bf(-H).  Note also that for compatibility, bf(-a)
494 +currently bf(does not include --flags) (see there) to include preserving
495 +change file flags (if supported by the OS).
496  
497  dit(--no-OPTION) You may turn off one or more implied options by prefixing
498  the option name with "no-".  Not all options may be prefixed with a "no-":
499 @@ -805,6 +808,13 @@ quote(itemization(
500  
501  If bf(--perms) is enabled, this option is ignored.
502  
503 +dit(bf(--flags)) This option causes rsync to update the change file flags
504 +to be the same as the source file, if your OS supports the bf(chflags)(2)
505 +system call.  In any case, an attempt is made to remove flags that would
506 +prevent a file to be altered.  Some flags can only be altered by the
507 +super-user and can only be unset below a certain secure-level (usually
508 +single-user mode).
509 +
510  dit(bf(--chmod)) This option tells rsync to apply one or more
511  comma-separated "chmod" strings to the permission of the files in the
512  transfer.  The resulting value is treated as though it was the permissions
513 --- old/syscall.c
514 +++ new/syscall.c
515 @@ -153,6 +153,15 @@ int do_chmod(const char *path, mode_t mo
516  }
517  #endif
518  
519 +#ifdef SUPPORT_FLAGS
520 +int do_chflags(const char *path, u_long flags)
521 +{
522 +       if (dry_run) return 0;
523 +       RETURN_ERROR_IF_RO_OR_LO;
524 +       return chflags(path, flags);
525 +}
526 +#endif
527 +
528  int do_rename(const char *fname1, const char *fname2)
529  {
530         if (dry_run) return 0;