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