The patches for 3.2.4pre4.
[rsync.git/patches.git] / hfs-compression.diff
1 This patch adds support for HFS+ compression.
2
3 Written by Mike Bombich.  Taken from http://www.bombich.com/rsync.html
4
5 Modified by Wayne to fix some issues and tweak the implementation a bit.
6 This compiles on OS X and passes the testsuite, but otherwise UNTESTED!
7
8 To use this patch, run these commands for a successful build:
9
10     patch -p1 <patches/fileflags.diff
11     patch -p1 <patches/hfs-compression.diff
12     ./prepare-source
13     ./configure
14     make
15
16 TODO:
17  - Should rsync try to treat the compressed data as file data and use the
18    rsync algorithm on the data transfer?
19
20 based-on: patch/master/fileflags
21 diff --git a/flist.c b/flist.c
22 --- a/flist.c
23 +++ b/flist.c
24 @@ -1656,6 +1656,11 @@ static struct file_struct *send_file_name(int f, struct file_list *flist,
25  #ifdef SUPPORT_XATTRS
26                 if (preserve_xattrs) {
27                         sx.st.st_mode = file->mode;
28 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
29 +                       if (preserve_fileflags)
30 +                               sx.st.st_flags = F_FFLAGS(file);
31 +#endif
32 +                       sx.st.st_mtime = file->modtime; /* get_xattr needs mtime for decmpfs xattrs */
33                         if (get_xattr(fname, &sx) < 0) {
34                                 io_error |= IOERR_GENERAL;
35                                 return NULL;
36 diff --git a/generator.c b/generator.c
37 --- a/generator.c
38 +++ b/generator.c
39 @@ -38,6 +38,7 @@ extern int keep_dirlinks;
40  extern int write_devices;
41  extern int preserve_acls;
42  extern int preserve_xattrs;
43 +extern int preserve_hfs_compression;
44  extern int preserve_links;
45  extern int preserve_devices;
46  extern int preserve_specials;
47 @@ -1798,6 +1799,14 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
48                                         fname, fnamecmpbuf);
49                         }
50                         sx.st.st_size = F_LENGTH(fuzzy_file);
51 +#ifdef SUPPORT_HFS_COMPRESSION
52 +                       if (sx.st.st_flags & UF_COMPRESSED) {
53 +                               if (preserve_hfs_compression)
54 +                                       sx.st.st_size = 0;
55 +                               else
56 +                                       sx.st.st_flags &= ~UF_COMPRESSED;
57 +                       }
58 +#endif
59                         statret = 0;
60                         fnamecmp = fnamecmpbuf;
61                 }
62 @@ -1967,6 +1976,18 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
63         if (read_batch)
64                 goto cleanup;
65  
66 +#ifdef SUPPORT_HFS_COMPRESSION
67 +       if (F_FFLAGS(file) & UF_COMPRESSED) {
68 +               /* At this point the attrs have already been copied, we don't need to transfer a data fork
69 +                * If my filesystem doesn't support HFS compression, the existing file's content
70 +                * will not be automatically truncated, so we'll do that manually here */
71 +               if (preserve_hfs_compression && sx.st.st_size > 0) {
72 +                       if (ftruncate(fd, 0) == 0)
73 +                               sx.st.st_size = 0;
74 +               }
75 +       }
76 +#endif
77 +
78         if (statret != 0 || whole_file)
79                 write_sum_head(f_out, NULL);
80         else if (sx.st.st_size <= 0) {
81 diff --git a/lib/sysxattrs.c b/lib/sysxattrs.c
82 --- a/lib/sysxattrs.c
83 +++ b/lib/sysxattrs.c
84 @@ -22,10 +22,17 @@
85  #include "rsync.h"
86  #include "sysxattrs.h"
87  
88 +extern int preserve_hfs_compression;
89 +
90  #ifdef SUPPORT_XATTRS
91  
92  #ifdef HAVE_OSX_XATTRS
93 +#ifndef XATTR_SHOWCOMPRESSION
94 +#define XATTR_SHOWCOMPRESSION 0x0020
95 +#endif
96  #define GETXATTR_FETCH_LIMIT (64*1024*1024)
97 +
98 +int xattr_options = XATTR_NOFOLLOW;
99  #endif
100  
101  #if defined HAVE_LINUX_XATTRS
102 @@ -59,7 +66,12 @@ ssize_t sys_llistxattr(const char *path, char *list, size_t size)
103  
104  ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
105  {
106 -       ssize_t len = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
107 +       ssize_t len;
108 +
109 +       if (preserve_hfs_compression)
110 +               xattr_options |= XATTR_SHOWCOMPRESSION;
111 +
112 +       len = getxattr(path, name, value, size, 0, xattr_options);
113  
114         /* If we're retrieving data, handle resource forks > 64MB specially */
115         if (value != NULL && len == GETXATTR_FETCH_LIMIT && (size_t)len < size) {
116 @@ -67,7 +79,7 @@ ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t si
117                 u_int32_t offset = len;
118                 size_t data_retrieved = len;
119                 while (data_retrieved < size) {
120 -                       len = getxattr(path, name, (char*)value + offset, size - data_retrieved, offset, XATTR_NOFOLLOW);
121 +                       len = getxattr(path, name, (char*)value + offset, size - data_retrieved, offset, xattr_options);
122                         if (len <= 0)
123                                 break;
124                         data_retrieved += len;
125 @@ -91,12 +103,16 @@ int sys_lsetxattr(const char *path, const char *name, const void *value, size_t
126  
127  int sys_lremovexattr(const char *path, const char *name)
128  {
129 -       return removexattr(path, name, XATTR_NOFOLLOW);
130 +       if (preserve_hfs_compression)
131 +               xattr_options |= XATTR_SHOWCOMPRESSION;
132 +       return removexattr(path, name, xattr_options);
133  }
134  
135  ssize_t sys_llistxattr(const char *path, char *list, size_t size)
136  {
137 -       return listxattr(path, list, size, XATTR_NOFOLLOW);
138 +       if (preserve_hfs_compression)
139 +               xattr_options |= XATTR_SHOWCOMPRESSION;
140 +       return listxattr(path, list, size, xattr_options);
141  }
142  
143  #elif HAVE_FREEBSD_XATTRS
144 diff --git a/main.c b/main.c
145 --- a/main.c
146 +++ b/main.c
147 @@ -34,6 +34,10 @@
148  #ifdef SUPPORT_FORCE_CHANGE
149  #include <sys/sysctl.h>
150  #endif
151 +#ifdef SUPPORT_HFS_COMPRESSION
152 +#include <sys/attr.h> /* For getattrlist() */
153 +#include <sys/mount.h> /* For statfs() */
154 +#endif
155  
156  extern int dry_run;
157  extern int list_only;
158 @@ -61,6 +65,7 @@ extern int copy_dirlinks;
159  extern int copy_unsafe_links;
160  extern int keep_dirlinks;
161  extern int preserve_hard_links;
162 +extern int preserve_hfs_compression;
163  extern int protocol_version;
164  extern int mkpath_dest_arg;
165  extern int file_total;
166 @@ -119,6 +124,7 @@ int daemon_connection = 0; /* 0 = no daemon, 1 = daemon via remote shell, -1 = d
167  mode_t orig_umask = 0;
168  int batch_gen_fd = -1;
169  int sender_keeps_checksum = 0;
170 +int fs_supports_hfs_compression = 0;
171  int raw_argc, cooked_argc;
172  char **raw_argv, **cooked_argv;
173  
174 @@ -664,6 +670,43 @@ static pid_t do_cmd(char *cmd, char *machine, char *user, char **remote_argv, in
175         return pid;
176  }
177  
178 +#ifdef SUPPORT_HFS_COMPRESSION
179 +static void hfs_receiver_check(void)
180 +{
181 +       struct statfs fsb;
182 +       struct attrlist attrs;
183 +       struct {
184 +               int32_t len;
185 +               vol_capabilities_set_t caps;
186 +       } attrData;
187 +
188 +       if (preserve_hfs_compression != 1)
189 +               return; /* Nothing to check if --hfs-compression option isn't enabled. */
190 +
191 +       if (statfs(".", &fsb) < 0) {
192 +               rsyserr(FERROR, errno, "statfs %s failed", curr_dir);
193 +               exit_cleanup(RERR_FILESELECT);
194 +       }
195 +
196 +       bzero(&attrs, sizeof attrs);
197 +       attrs.bitmapcount = ATTR_BIT_MAP_COUNT;
198 +       attrs.volattr = ATTR_VOL_CAPABILITIES;
199 +
200 +       bzero(&attrData, sizeof attrData);
201 +       attrData.len = sizeof attrData;
202 +
203 +       if (getattrlist(fsb.f_mntonname, &attrs, &attrData, sizeof attrData, 0) < 0) {
204 +               rsyserr(FERROR, errno, "getattrlist %s failed", curr_dir);
205 +               exit_cleanup(RERR_FILESELECT);
206 +       }
207 +
208 +       if (!(attrData.caps[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_DECMPFS_COMPRESSION)) {
209 +               rprintf(FERROR, "The destination filesystem does not support HFS+ compression.\n");
210 +               exit_cleanup(RERR_UNSUPPORTED);
211 +       }
212 +}
213 +#endif
214 +
215  /* The receiving side operates in one of two modes:
216   *
217   * 1. it receives any number of files into a destination directory,
218 @@ -744,6 +787,9 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
219                                 exit_cleanup(RERR_FILESELECT);
220                         }
221                         filesystem_dev = st.st_dev; /* ensures --force works right w/-x */
222 +#ifdef SUPPORT_HFS_COMPRESSION
223 +                       hfs_receiver_check();
224 +#endif
225                         return NULL;
226                 }
227                 if (file_total > 1) {
228 @@ -801,7 +847,9 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
229                                 full_fname(dest_path));
230                         exit_cleanup(RERR_FILESELECT);
231                 }
232 -
233 +#ifdef SUPPORT_HFS_COMPRESSION
234 +               hfs_receiver_check();
235 +#endif
236                 return NULL;
237         }
238  
239 @@ -821,6 +869,9 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
240                         full_fname(dest_path));
241                 exit_cleanup(RERR_FILESELECT);
242         }
243 +#ifdef SUPPORT_HFS_COMPRESSION
244 +       hfs_receiver_check();
245 +#endif
246         *cp = '/';
247  
248         return cp + 1;
249 diff --git a/options.c b/options.c
250 --- a/options.c
251 +++ b/options.c
252 @@ -53,6 +53,7 @@ int preserve_links = 0;
253  int preserve_hard_links = 0;
254  int preserve_acls = 0;
255  int preserve_xattrs = 0;
256 +int preserve_hfs_compression = 0;
257  int preserve_perms = 0;
258  int preserve_fileflags = 0;
259  int preserve_executability = 0;
260 @@ -727,6 +728,10 @@ static struct poptOption long_options[] = {
261    {"no-force-change",  0,  POPT_ARG_VAL,    &force_change, 0, 0, 0 },
262    {"force-uchange",    0,  POPT_ARG_VAL,    &force_change, USR_IMMUTABLE, 0, 0 },
263    {"force-schange",    0,  POPT_ARG_VAL,    &force_change, SYS_IMMUTABLE, 0, 0 },
264 +  {"hfs-compression",  0,  POPT_ARG_VAL,    &preserve_hfs_compression, 1, 0, 0 },
265 +  {"no-hfs-compression",0, POPT_ARG_VAL,    &preserve_hfs_compression, 0, 0, 0 },
266 +  {"protect-decmpfs",  0,  POPT_ARG_VAL,    &preserve_hfs_compression, 2, 0, 0 },
267 +  {"no-protect-decmpfs",0, POPT_ARG_VAL,    &preserve_hfs_compression, 0, 0, 0 },
268    {"ignore-errors",    0,  POPT_ARG_VAL,    &ignore_errors, 1, 0, 0 },
269    {"no-ignore-errors", 0,  POPT_ARG_VAL,    &ignore_errors, 0, 0, 0 },
270    {"max-delete",       0,  POPT_ARG_INT,    &max_delete, 0, 0, 0 },
271 @@ -1027,6 +1032,10 @@ static void set_refuse_options(void)
272         parse_one_refuse_match(0, "force-uchange", list_end);
273         parse_one_refuse_match(0, "force-schange", list_end);
274  #endif
275 +#ifndef SUPPORT_HFS_COMPRESSION
276 +       parse_one_refuse_match(0, "hfs-compression", list_end);
277 +       parse_one_refuse_match(0, "protect-decmpfs", list_end);
278 +#endif
279  
280         /* Now we use the descrip values to actually mark the options for refusal. */
281         for (op = long_options; op != list_end; op++) {
282 @@ -2111,6 +2120,15 @@ int parse_arguments(int *argc_p, const char ***argv_p)
283         }
284  #endif
285  
286 +#ifdef SUPPORT_HFS_COMPRESSION
287 +       if (preserve_hfs_compression) {
288 +               if (!preserve_xattrs)
289 +                       preserve_xattrs = 1;
290 +               if (!preserve_fileflags)
291 +                       preserve_fileflags = 1;
292 +       }
293 +#endif
294 +
295         if (write_batch && read_batch) {
296                 snprintf(err_buf, sizeof err_buf,
297                         "--write-batch and --read-batch can not be used together\n");
298 @@ -2710,6 +2728,11 @@ void server_options(char **args, int *argc_p)
299         if (preserve_fileflags)
300                 args[ac++] = "--fileflags";
301  
302 +#ifdef SUPPORT_HFS_COMPRESSION
303 +       if (preserve_hfs_compression)
304 +               args[ac++] = preserve_hfs_compression == 1 ? "--hfs-compression" : "--protect-decmpfs";
305 +#endif
306 +
307         if (do_compression && do_compression_level != CLVL_NOT_SPECIFIED) {
308                 if (asprintf(&arg, "--compress-level=%d", do_compression_level) < 0)
309                         goto oom;
310 diff --git a/rsync.1.md b/rsync.1.md
311 --- a/rsync.1.md
312 +++ b/rsync.1.md
313 @@ -371,6 +371,8 @@ has its own detailed description later in this manpage.
314  --chmod=CHMOD            affect file and/or directory permissions
315  --acls, -A               preserve ACLs (implies --perms)
316  --xattrs, -X             preserve extended attributes
317 +--hfs-compression        preserve HFS compression if supported
318 +--protect-decmpfs        preserve HFS compression as xattrs
319  --owner, -o              preserve owner (super-user only)
320  --group, -g              preserve group
321  --devices                preserve device files (super-user only)
322 @@ -1430,6 +1432,47 @@ your home directory (remove the '=' for that).
323      does not try to affect user flags.  This option overrides
324      [`--force-change`](#opt) and [`--force-uchange`](#opt).
325  
326 +0.  `--hfs-compression`
327 +
328 +    This option causes rsync to preserve HFS+ compression if the destination
329 +    filesystem supports it.  If the destination does not support it, rsync will
330 +    exit with an error.
331 +
332 +    Filesystem compression was introduced to HFS+ in Mac OS 10.6. A file that
333 +    is compressed has no data in its data fork. Rather, the compressed data is
334 +    stored in an extended attribute named com.apple.decmpfs and a file flag is
335 +    set to indicate that the file is compressed (UF_COMPRESSED). HFS+
336 +    decompresses this data "on-the-fly" and presents it to the operating system
337 +    as a normal file.  Normal attempts to copy compressed files (e.g. in the
338 +    Finder, via cp, ditto, etc.) will copy the file's decompressed contents,
339 +    remove the UF_COMPRESSED file flag, and discard the com.apple.decmpfs
340 +    extended attribute. This option will preserve the data in the
341 +    com.apple.decmpfs extended attribute and ignore the synthesized data in the
342 +    file contents.
343 +
344 +    This option implies both [`--fileflags`](#opt) and [`--xattrs`](#opt).
345 +
346 +0.  `--protect-decmpfs`
347 +
348 +    The com.apple.decmpfs extended attribute is hidden by default from list/get
349 +    xattr calls, therefore normal attempts to copy compressed files will
350 +    functionally decompress those files. While this is desirable behavior when
351 +    copying files to filesystems that do not support HFS+ compression, it has
352 +    serious performance and capacity impacts when backing up or restoring the
353 +    Mac OS X filesystem.
354 +
355 +    This option will transfer the com.apple.decmpfs extended attribute
356 +    regardless of support on the destination. If a source file is compressed
357 +    and an existing file on the destination is not compressed, the data fork of
358 +    the destination file will be truncated and the com.apple.decmpfs xattr will
359 +    be transferred instead. Note that compressed files will not be readable to
360 +    the operating system of the destination if that operating system does not
361 +    support HFS+ compression. Once restored (with or without this option) to an
362 +    operating system that supports HFS+ compression, however, these files will
363 +    be accessible as usual.
364 +
365 +    This option implies [`--fileflags`](#opt) and [`--xattrs`](#opt).
366 +
367  0.  `--chmod=CHMOD`
368  
369      This option tells rsync to apply one or more comma-separated "chmod" modes
370 diff --git a/rsync.c b/rsync.c
371 --- a/rsync.c
372 +++ b/rsync.c
373 @@ -607,8 +607,18 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
374  #ifdef SUPPORT_XATTRS
375         if (am_root < 0)
376                 set_stat_xattr(fname, file, new_mode);
377 -       if (preserve_xattrs && fnamecmp)
378 +       if (preserve_xattrs && fnamecmp) {
379 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
380 +               uint32 tmpflags = sxp->st.st_flags;
381 +               sxp->st.st_flags = F_FFLAGS(file); /* set_xattr() needs to check UF_COMPRESSED */
382 +#endif
383                 set_xattr(fname, file, fnamecmp, sxp);
384 +#if defined SUPPORT_FILEFLAGS || defined SUPPORT_FORCE_CHANGE
385 +               sxp->st.st_flags = tmpflags;
386 +#endif
387 +               if (S_ISDIR(sxp->st.st_mode))
388 +                       link_stat(fname, &sx2.st, 0);
389 +       }
390  #endif
391  
392         if ((omit_dir_times && S_ISDIR(sxp->st.st_mode))
393 @@ -625,7 +635,11 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
394         }
395         if (sxp != &sx2)
396                 memcpy(&sx2.st, &sxp->st, sizeof sx2.st);
397 -       if (!(flags & ATTRS_SKIP_MTIME) && !same_mtime(file, &sxp->st, flags & ATTRS_ACCURATE_TIME)) {
398 +       if (!(flags & ATTRS_SKIP_MTIME)
399 +#ifdef SUPPORT_HFS_COMPRESSION
400 +        && !(sxp->st.st_flags & UF_COMPRESSED) /* setting this alters mtime, so defer to after set_fileflags */
401 +#endif
402 +        && !same_mtime(file, &sxp->st, flags & ATTRS_ACCURATE_TIME)) {
403                 sx2.st.st_mtime = file->modtime;
404  #ifdef ST_MTIME_NSEC
405                 sx2.st.ST_MTIME_NSEC = F_MOD_NSEC_or_0(file);
406 @@ -710,6 +724,16 @@ int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
407                  && !set_fileflags(fname, fileflags))
408                         goto cleanup;
409                 updated = 1;
410 +#ifdef SUPPORT_HFS_COMPRESSION
411 +               int ret = set_modtime(fname, file->modtime, F_MOD_NSEC(file), new_mode, fileflags);
412 +               if (ret < 0) {
413 +                       rsyserr(FERROR_XFER, errno, "failed to set times on %s",
414 +                               full_fname(fname));
415 +                       goto cleanup;
416 +               }
417 +               if (ret != 0)
418 +                       file->flags |= FLAG_TIME_FAILED;
419 +#endif
420         }
421  #endif
422  
423 diff --git a/rsync.h b/rsync.h
424 --- a/rsync.h
425 +++ b/rsync.h
426 @@ -584,6 +584,17 @@ typedef unsigned int size_t;
427  #endif
428  #endif
429  
430 +#ifndef UF_COMPRESSED
431 +#define UF_COMPRESSED 0x00000020
432 +#endif
433 +#ifndef VOL_CAP_FMT_DECMPFS_COMPRESSION
434 +#define VOL_CAP_FMT_DECMPFS_COMPRESSION 0x00010000
435 +#endif
436 +
437 +#if defined SUPPORT_XATTRS && defined SUPPORT_FILEFLAGS
438 +#define SUPPORT_HFS_COMPRESSION 1
439 +#endif
440 +
441  #if !defined __APPLE__ || defined HAVE_GETATTRLIST
442  #define SUPPORT_ATIMES 1
443  #endif
444 diff --git a/t_stub.c b/t_stub.c
445 --- a/t_stub.c
446 +++ b/t_stub.c
447 @@ -35,6 +35,7 @@ int preserve_mtimes = 0;
448  int preserve_xattrs = 0;
449  int preserve_perms = 0;
450  int preserve_executability = 0;
451 +int preserve_hfs_compression = 0;
452  int omit_link_times = 0;
453  int open_noatime = 0;
454  size_t max_alloc = 0; /* max_alloc is needed when combined with util2.o */
455 diff --git a/xattrs.c b/xattrs.c
456 --- a/xattrs.c
457 +++ b/xattrs.c
458 @@ -33,6 +33,7 @@ extern int am_generator;
459  extern int read_only;
460  extern int list_only;
461  extern int preserve_xattrs;
462 +extern int preserve_hfs_compression;
463  extern int preserve_links;
464  extern int preserve_devices;
465  extern int preserve_specials;
466 @@ -42,6 +43,10 @@ extern int saw_xattr_filter;
467  #define RSYNC_XAL_INITIAL 5
468  #define RSYNC_XAL_LIST_INITIAL 100
469  
470 +#define GXD_NO_MISSING_ERROR (1<<0)
471 +#define GXD_OMIT_COMPRESSED (1<<1)
472 +#define GXD_FILE_IS_COMPRESSED (1<<2)
473 +
474  #define MAX_FULL_DATUM 32
475  
476  #define HAS_PREFIX(str, prfx) (*(str) == *(prfx) && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
477 @@ -73,6 +78,17 @@ extern int saw_xattr_filter;
478  #define XDEF_ACL_SUFFIX "dacl"
479  #define XDEF_ACL_ATTR RSYNC_PREFIX "%" XDEF_ACL_SUFFIX
480  
481 +#define APPLE_PREFIX "com.apple."
482 +#define APLPRE_LEN ((int)sizeof APPLE_PREFIX - 1)
483 +#define DECMPFS_SUFFIX "decmpfs"
484 +#define RESOURCEFORK_SUFFIX "ResourceFork"
485 +
486 +#define UNREAD_DATA ((char *)1)
487 +
488 +#if MAX_DIGEST_LEN < SIZEOF_TIME_T
489 +#error MAX_DIGEST_LEN is too small to hold an mtime
490 +#endif
491 +
492  typedef struct {
493         char *datum, *name;
494         size_t datum_len, name_len;
495 @@ -180,7 +196,7 @@ static ssize_t get_xattr_names(const char *fname)
496  /* On entry, the *len_ptr parameter contains the size of the extra space we
497   * should allocate when we create a buffer for the data.  On exit, it contains
498   * the length of the datum. */
499 -static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr, int no_missing_error)
500 +static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr, int flags)
501  {
502         size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
503         size_t extra_len = *len_ptr;
504 @@ -189,7 +205,7 @@ static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr
505         *len_ptr = datum_len;
506  
507         if (datum_len == (size_t)-1) {
508 -               if (errno == ENOTSUP || no_missing_error)
509 +               if (errno == ENOTSUP || flags & GXD_NO_MISSING_ERROR)
510                         return NULL;
511                 rsyserr(FERROR_XFER, errno,
512                         "get_xattr_data: lgetxattr(%s,\"%s\",0) failed",
513 @@ -197,6 +213,15 @@ static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr
514                 return NULL;
515         }
516  
517 +       if (flags & GXD_OMIT_COMPRESSED && datum_len > MAX_FULL_DATUM
518 +        && HAS_PREFIX(name, APPLE_PREFIX)
519 +        && (strcmp(name+APLPRE_LEN, DECMPFS_SUFFIX) == 0
520 +         || (flags & GXD_FILE_IS_COMPRESSED && strcmp(name+APLPRE_LEN, RESOURCEFORK_SUFFIX) == 0))) {
521 +               /* If we are omitting compress-file-related data, we don't want to
522 +                * actually read this data. */
523 +               return UNREAD_DATA;
524 +       }
525 +
526         if (!datum_len && !extra_len)
527                 extra_len = 1; /* request non-zero amount of memory */
528         if (SIZE_MAX - datum_len < extra_len)
529 @@ -224,7 +249,31 @@ static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr
530         return ptr;
531  }
532  
533 -static int rsync_xal_get(const char *fname, item_list *xalp)
534 +static void checksum_xattr_data(char *sum, const char *datum, size_t datum_len, stat_x *sxp)
535 +{
536 +       if (datum == UNREAD_DATA) {
537 +               /* For abbreviated compressed data, we store the file's mtime as the checksum. */
538 +               SIVAL(sum, 0, sxp->st.st_mtime);
539 +#if SIZEOF_TIME_T > 4
540 +               SIVAL(sum, 4, sxp->st.st_mtime >> 32);
541 +#if MAX_DIGEST_LEN > 8
542 +               memset(sum + 8, 0, MAX_DIGEST_LEN - 8);
543 +#endif
544 +#else
545 +#if MAX_DIGEST_LEN > 4
546 +               memset(sum + 4, 0, MAX_DIGEST_LEN - 4);
547 +#endif
548 +#endif
549 +       } else {
550 +               sum_init(-1, checksum_seed);
551 +               sum_update(datum, datum_len);
552 +               sum_end(sum);
553 +       }
554 +}
555 +
556 +$$$ERROR$$$ the old patch needs reworking since rsync_xal_get() has totally changed!
557 +
558 +static int rsync_xal_get(const char *fname, stat_x *sxp)
559  {
560         ssize_t list_len, name_len;
561         size_t datum_len, name_offset;
562 @@ -233,7 +282,8 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
563         int user_only = am_sender ? 0 : !am_root;
564  #endif
565         rsync_xa *rxa;
566 -       int count;
567 +       int count, flags;
568 +       item_list *xalp = sxp->xattr;
569  
570         /* This puts the name list into the "namebuf" buffer. */
571         if ((list_len = get_xattr_names(fname)) < 0)
572 @@ -264,11 +314,15 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
573                 }
574  
575                 datum_len = name_len; /* Pass extra size to get_xattr_data() */
576 -               if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
577 +               flags = GXD_OMIT_COMPRESSED;
578 +               if (preserve_hfs_compression && sxp->st.st_flags & UF_COMPRESSED)
579 +                       flags |= GXD_FILE_IS_COMPRESSED;
580 +               if (!(ptr = get_xattr_data(fname, name, &datum_len, flags)))
581                         return -1;
582  
583                 if (datum_len > MAX_FULL_DATUM) {
584                         /* For large datums, we store a flag and a checksum. */
585 +                       char *datum = ptr;
586                         name_offset = 1 + MAX_DIGEST_LEN;
587                         sum_init(-1, checksum_seed);
588                         sum_update(ptr, datum_len);
589 @@ -276,7 +330,9 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
590  
591                         ptr = new_array(char, name_offset + name_len);
592                         *ptr = XSTATE_ABBREV;
593 -                       sum_end(ptr + 1);
594 +                       checksum_xattr_data(ptr+1, datum, datum_len, sxp);
595 +                       if (datum != UNREAD_DATA)
596 +                               free(datum);
597                 } else
598                         name_offset = datum_len;
599  
600 @@ -322,7 +378,7 @@ int get_xattr(const char *fname, stat_x *sxp)
601         } else if (IS_MISSING_FILE(sxp->st))
602                 return 0;
603  
604 -       if (rsync_xal_get(fname, sxp->xattr) < 0) {
605 +       if (rsync_xal_get(fname, sxp) < 0) {
606                 free_xattr(sxp);
607                 return -1;
608         }
609 @@ -359,6 +415,8 @@ int copy_xattrs(const char *source, const char *dest)
610                 datum_len = 0;
611                 if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
612                         return -1;
613 +               if (ptr == UNREAD_DATA)
614 +                       continue; /* XXX Is this right? */
615                 if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
616                         int save_errno = errno ? errno : EINVAL;
617                         rsyserr(FERROR_XFER, errno,
618 @@ -449,6 +507,7 @@ static int find_matching_xattr(const item_list *xalp)
619         }
620  
621         return -1;
622 +#endif
623  }
624  
625  /* Store *xalp on the end of rsync_xal_l */
626 @@ -663,11 +722,13 @@ void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
627  
628                         /* Re-read the long datum. */
629                         if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
630 -                               rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
631 +                               if (errno != ENOTSUP && errno != ENOATTR)
632 +                                       rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
633                                 write_varint(f_out, 0);
634                                 continue;
635                         }
636  
637 +                       assert(ptr != UNREAD_DATA);
638                         write_varint(f_out, len); /* length might have changed! */
639                         write_bigbuf(f_out, ptr, len);
640                         free(ptr);
641 @@ -948,7 +1009,7 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
642         int user_only = am_root <= 0;
643  #endif
644         size_t name_len;
645 -       int ret = 0;
646 +       int flags, ret = 0;
647  
648         /* This puts the current name list into the "namebuf" buffer. */
649         if ((list_len = get_xattr_names(fname)) < 0)
650 @@ -961,7 +1022,10 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
651                         int sum_len;
652                         /* See if the fnamecmp version is identical. */
653                         len = name_len = rxas[i].name_len;
654 -                       if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
655 +                       flags = GXD_OMIT_COMPRESSED | GXD_NO_MISSING_ERROR;
656 +                       if (preserve_hfs_compression && sxp->st.st_flags & UF_COMPRESSED)
657 +                               flags |= GXD_FILE_IS_COMPRESSED;
658 +                       if ((ptr = get_xattr_data(fnamecmp, name, &len, flags)) == NULL) {
659                           still_abbrev:
660                                 if (am_generator)
661                                         continue;
662 @@ -970,6 +1034,8 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
663                                 ret = -1;
664                                 continue;
665                         }
666 +                       if (ptr == UNREAD_DATA)
667 +                               continue; /* XXX Is this right? */
668                         if (len != rxas[i].datum_len) {
669                                 free(ptr);
670                                 goto still_abbrev;
671 @@ -1047,6 +1113,10 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
672                 }
673         }
674  
675 +#ifdef HAVE_OSX_XATTRS
676 +       rsync_xal_free(xalp); /* Free this because we aren't using find_matching_xattr(). */
677 +#endif
678 +
679         return ret;
680  }
681  
682 @@ -1108,7 +1178,7 @@ char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
683  {
684         const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
685         *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
686 -       return get_xattr_data(fname, name, len_p, 1);
687 +       return get_xattr_data(fname, name, len_p, GXD_NO_MISSING_ERROR);
688  }
689  
690  int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
691 @@ -1252,11 +1322,33 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
692         return 0;
693  }
694  
695 +#ifdef SUPPORT_HFS_COMPRESSION
696 +static inline void hfs_compress_tweaks(STRUCT_STAT *fst)
697 +{
698 +       if (fst->st_flags & UF_COMPRESSED) {
699 +               if (preserve_hfs_compression) {
700 +                       /* We're sending the compression xattr, not the decompressed data fork.
701 +                        * Setting rsync's idea of the file size to 0 effectively prevents the
702 +                        * transfer of the data fork. */
703 +                       fst->st_size = 0;
704 +               } else {
705 +                       /* If the sender's filesystem supports compression, then we'll be able
706 +                        * to send the decompressed data fork and the decmpfs xattr will be
707 +                        * hidden (not sent). As such, we need to strip the compression flag. */
708 +                       fst->st_flags &= ~UF_COMPRESSED;
709 +               }
710 +       }
711 +}
712 +#endif
713 +
714  int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
715  {
716         int ret = do_stat(fname, fst);
717         if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
718                 xst->st_mode = 0;
719 +#ifdef SUPPORT_HFS_COMPRESSION
720 +       hfs_compress_tweaks(fst);
721 +#endif
722         return ret;
723  }
724  
725 @@ -1265,6 +1357,9 @@ int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
726         int ret = do_lstat(fname, fst);
727         if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
728                 xst->st_mode = 0;
729 +#ifdef SUPPORT_HFS_COMPRESSION
730 +       hfs_compress_tweaks(fst);
731 +#endif
732         return ret;
733  }
734  
735 @@ -1273,6 +1368,9 @@ int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
736         int ret = do_fstat(fd, fst);
737         if ((ret < 0 || get_stat_xattr(NULL, fd, fst, xst) < 0) && xst)
738                 xst->st_mode = 0;
739 +#ifdef SUPPORT_HFS_COMPRESSION
740 +       hfs_compress_tweaks(fst);
741 +#endif
742         return ret;
743  }
744