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