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