Latest patches.
[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 @@ -1565,6 +1565,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 @@ -1782,6 +1783,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 @@ -1949,6 +1958,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 @@ -108,6 +113,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 @@ -567,6 +573,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 @@ -625,6 +668,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 @@ -685,7 +731,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 @@ -705,6 +753,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 @@ -712,6 +713,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 @@ -2008,6 +2019,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 @@ -2603,6 +2623,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 @@ -579,8 +579,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 @@ -591,6 +597,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          && (sxp->st.st_mtime != file->modtime
338  #ifdef ST_MTIME_NSEC
339           || (NSEC_BUMP(file) && (uint32)sxp->st.ST_MTIME_NSEC != F_MOD_NSEC(file))
340 @@ -653,6 +662,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 @@ -555,6 +555,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 @@ -1176,6 +1178,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 @@ -42,6 +43,10 @@ extern int saw_xattr_filter;
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 @@ -74,6 +79,17 @@ extern int saw_xattr_filter;
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 @@ -185,8 +201,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 @@ -195,7 +210,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 @@ -203,6 +218,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 @@ -231,7 +255,31 @@ 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(-1, checksum_seed);
541 +               sum_update(datum, datum_len);
542 +               sum_end(sum);
543 +       }
544 +}
545 +
546 +$$$ERROR$$$ the old patch needs reworking since rsync_xal_get() has totally changed!
547 +
548 +static int rsync_xal_get(const char *fname, stat_x *sxp)
549  {
550         ssize_t list_len, name_len;
551         size_t datum_len, name_offset;
552 @@ -240,7 +288,8 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
553         int user_only = am_sender ? 0 : !am_root;
554  #endif
555         rsync_xa *rxa;
556 -       int count;
557 +       int count, flags;
558 +       item_list *xalp = sxp->xattr;
559  
560         /* This puts the name list into the "namebuf" buffer. */
561         if ((list_len = get_xattr_names(fname)) < 0)
562 @@ -271,11 +320,15 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
563                 }
564  
565                 datum_len = name_len; /* Pass extra size to get_xattr_data() */
566 -               if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
567 +               flags = GXD_OMIT_COMPRESSED;
568 +               if (preserve_hfs_compression && sxp->st.st_flags & UF_COMPRESSED)
569 +                       flags |= GXD_FILE_IS_COMPRESSED;
570 +               if (!(ptr = get_xattr_data(fname, name, &datum_len, flags)))
571                         return -1;
572  
573                 if (datum_len > MAX_FULL_DATUM) {
574                         /* For large datums, we store a flag and a checksum. */
575 +                       char *datum = ptr;
576                         name_offset = 1 + MAX_DIGEST_LEN;
577                         sum_init(-1, checksum_seed);
578                         sum_update(ptr, datum_len);
579 @@ -284,7 +337,9 @@ static int rsync_xal_get(const char *fname, item_list *xalp)
580                         if (!(ptr = new_array(char, name_offset + name_len)))
581                                 out_of_memory("rsync_xal_get");
582                         *ptr = XSTATE_ABBREV;
583 -                       sum_end(ptr + 1);
584 +                       checksum_xattr_data(ptr+1, datum, datum_len, sxp);
585 +                       if (datum != UNREAD_DATA)
586 +                               free(datum);
587                 } else
588                         name_offset = datum_len;
589  
590 @@ -330,7 +385,7 @@ int get_xattr(const char *fname, stat_x *sxp)
591         } else if (IS_MISSING_FILE(sxp->st))
592                 return 0;
593  
594 -       if (rsync_xal_get(fname, sxp->xattr) < 0) {
595 +       if (rsync_xal_get(fname, sxp) < 0) {
596                 free_xattr(sxp);
597                 return -1;
598         }
599 @@ -367,6 +422,8 @@ int copy_xattrs(const char *source, const char *dest)
600                 datum_len = 0;
601                 if (!(ptr = get_xattr_data(source, name, &datum_len, 0)))
602                         return -1;
603 +               if (ptr == UNREAD_DATA)
604 +                       continue; /* XXX Is this right? */
605                 if (sys_lsetxattr(dest, name, ptr, datum_len) < 0) {
606                         int save_errno = errno ? errno : EINVAL;
607                         rsyserr(FERROR_XFER, errno,
608 @@ -457,6 +514,7 @@ static int find_matching_xattr(const item_list *xalp)
609         }
610  
611         return -1;
612 +#endif
613  }
614  
615  /* Store *xalp on the end of rsync_xal_l */
616 @@ -678,11 +736,13 @@ void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
617  
618                         /* Re-read the long datum. */
619                         if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0))) {
620 -                               rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
621 +                               if (errno != ENOTSUP && errno != ENOATTR)
622 +                                       rprintf(FERROR_XFER, "failed to re-read xattr %s for %s\n", rxa->name, fname);
623                                 write_varint(f_out, 0);
624                                 continue;
625                         }
626  
627 +                       assert(ptr != UNREAD_DATA);
628                         write_varint(f_out, len); /* length might have changed! */
629                         write_bigbuf(f_out, ptr, len);
630                         free(ptr);
631 @@ -969,7 +1029,7 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
632         int user_only = am_root <= 0;
633  #endif
634         size_t name_len;
635 -       int ret = 0;
636 +       int flags, ret = 0;
637  
638         /* This puts the current name list into the "namebuf" buffer. */
639         if ((list_len = get_xattr_names(fname)) < 0)
640 @@ -982,7 +1042,10 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
641                         int sum_len;
642                         /* See if the fnamecmp version is identical. */
643                         len = name_len = rxas[i].name_len;
644 -                       if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
645 +                       flags = GXD_OMIT_COMPRESSED | GXD_NO_MISSING_ERROR;
646 +                       if (preserve_hfs_compression && sxp->st.st_flags & UF_COMPRESSED)
647 +                               flags |= GXD_FILE_IS_COMPRESSED;
648 +                       if ((ptr = get_xattr_data(fnamecmp, name, &len, flags)) == NULL) {
649                           still_abbrev:
650                                 if (am_generator)
651                                         continue;
652 @@ -991,6 +1054,8 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
653                                 ret = -1;
654                                 continue;
655                         }
656 +                       if (ptr == UNREAD_DATA)
657 +                               continue; /* XXX Is this right? */
658                         if (len != rxas[i].datum_len) {
659                                 free(ptr);
660                                 goto still_abbrev;
661 @@ -1068,6 +1133,10 @@ static int rsync_xal_set(const char *fname, item_list *xalp,
662                 }
663         }
664  
665 +#ifdef HAVE_OSX_XATTRS
666 +       rsync_xal_free(xalp); /* Free this because we aren't using find_matching_xattr(). */
667 +#endif
668 +
669         return ret;
670  }
671  
672 @@ -1117,7 +1186,7 @@ char *get_xattr_acl(const char *fname, int is_access_acl, size_t *len_p)
673  {
674         const char *name = is_access_acl ? XACC_ACL_ATTR : XDEF_ACL_ATTR;
675         *len_p = 0; /* no extra data alloc needed from get_xattr_data() */
676 -       return get_xattr_data(fname, name, len_p, 1);
677 +       return get_xattr_data(fname, name, len_p, GXD_NO_MISSING_ERROR);
678  }
679  
680  int set_xattr_acl(const char *fname, int is_access_acl, const char *buf, size_t buf_len)
681 @@ -1260,11 +1329,33 @@ int set_stat_xattr(const char *fname, struct file_struct *file, mode_t new_mode)
682         return 0;
683  }
684  
685 +#ifdef SUPPORT_HFS_COMPRESSION
686 +static inline void hfs_compress_tweaks(STRUCT_STAT *fst)
687 +{
688 +       if (fst->st_flags & UF_COMPRESSED) {
689 +               if (preserve_hfs_compression) {
690 +                       /* We're sending the compression xattr, not the decompressed data fork.
691 +                        * Setting rsync's idea of the file size to 0 effectively prevents the
692 +                        * transfer of the data fork. */
693 +                       fst->st_size = 0;
694 +               } else {
695 +                       /* If the sender's filesystem supports compression, then we'll be able
696 +                        * to send the decompressed data fork and the decmpfs xattr will be
697 +                        * hidden (not sent). As such, we need to strip the compression flag. */
698 +                       fst->st_flags &= ~UF_COMPRESSED;
699 +               }
700 +       }
701 +}
702 +#endif
703 +
704  int x_stat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
705  {
706         int ret = do_stat(fname, fst);
707         if ((ret < 0 || get_stat_xattr(fname, -1, fst, xst) < 0) && xst)
708                 xst->st_mode = 0;
709 +#ifdef SUPPORT_HFS_COMPRESSION
710 +       hfs_compress_tweaks(fst);
711 +#endif
712         return ret;
713  }
714  
715 @@ -1273,6 +1364,9 @@ int x_lstat(const char *fname, STRUCT_STAT *fst, STRUCT_STAT *xst)
716         int ret = do_lstat(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 @@ -1281,6 +1375,9 @@ int x_fstat(int fd, STRUCT_STAT *fst, STRUCT_STAT *xst)
726         int ret = do_fstat(fd, fst);
727         if ((ret < 0 || get_stat_xattr(NULL, fd, 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