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