The OS X version now transmits a unique prefix ("user.0S%.") for
[rsync/rsync-patches.git] / xattrs.diff
1 Depends-On-Patch: acls.diff
2
3 After applying this patch, run these commands for a successful build:
4
5     ./prepare-source
6     ./configure --enable-acl-support --enable-xattr-support
7     make
8
9 TODO:
10
11  - This patch needs to be rewritten to more efficiently handle large xattrs.
12
13  - We need to affect the itemized output to know when xattrs are being updated.
14
15  - We need to affect the --link-dest option to avoid hard-linking two files
16    that differ in their xattrs (when --xattrs was specified).
17
18 --- old/Makefile.in
19 +++ new/Makefile.in
20 @@ -28,13 +28,13 @@ VERSION=@VERSION@
21  
22  HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
23  LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
24 -       lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
25 +       lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattr.o @LIBOBJS@
26  ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
27         zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
28  OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
29         main.o checksum.o match.o syscall.o log.o backup.o
30  OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
31 -       fileio.o batch.o clientname.o chmod.o acls.o
32 +       fileio.o batch.o clientname.o chmod.o acls.o xattr.o
33  OBJS3=progress.o pipe.o
34  DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
35  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
36 --- old/acls.c
37 +++ new/acls.c
38 @@ -29,6 +29,7 @@ extern int am_root;
39  extern int dry_run;
40  extern int orig_umask;
41  extern int preserve_acls;
42 +extern int preserve_xattrs;
43  extern unsigned int file_struct_len;
44  
45  /* === ACL structures === */
46 @@ -740,6 +741,10 @@ void receive_acl(struct file_struct *fil
47         type = SMB_ACL_TYPE_ACCESS;
48         racl_list = &access_acl_list;
49         ndx_ptr = (char*)file + file_struct_len;
50 +#ifdef SUPPORT_XATTRS
51 +       if (preserve_xattrs)
52 +               ndx_ptr += 4;
53 +#endif
54         do {
55                 char tag = read_byte(f);
56                 int ndx;
57 @@ -799,6 +804,10 @@ void cache_acl(struct file_struct *file,
58         racl = sxp->acc_acl;
59         racl_list = &access_acl_list;
60         ndx_ptr = (char*)file + file_struct_len;
61 +#ifdef SUPPORT_XATTRS
62 +       if (preserve_xattrs)
63 +               ndx_ptr += 4;
64 +#endif
65         do {
66                 if (!racl)
67                         ndx = -1;
68 @@ -914,6 +923,10 @@ int set_acl(const char *fname, const str
69  
70         type = SMB_ACL_TYPE_ACCESS;
71         ndx_ptr = (char*)file + file_struct_len;
72 +#ifdef SUPPORT_XATTRS
73 +       if (preserve_xattrs)
74 +               ndx_ptr += 4;
75 +#endif
76         do {
77                 acl_duo *duo_item;
78                 BOOL eq;
79 --- old/backup.c
80 +++ new/backup.c
81 @@ -30,6 +30,7 @@ extern char *backup_dir;
82  
83  extern int am_root;
84  extern int preserve_acls;
85 +extern int preserve_xattrs;
86  extern int preserve_devices;
87  extern int preserve_specials;
88  extern int preserve_links;
89 @@ -136,6 +137,9 @@ static int make_bak_dir(char *fullpath)
90  #ifdef SUPPORT_ACLS
91                                 sx.acc_acl = sx.def_acl = NULL;
92  #endif
93 +#ifdef SUPPORT_XATTRS
94 +                               sx.xattr = NULL;
95 +#endif
96                                 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
97                                         continue;
98  #ifdef SUPPORT_ACLS
99 @@ -144,6 +148,12 @@ static int make_bak_dir(char *fullpath)
100                                         cache_acl(file, &sx);
101                                 }
102  #endif
103 +#ifdef SUPPORT_XATTRS
104 +                               if (preserve_xattrs) {
105 +                                       get_xattr(rel, &sx);
106 +                                       cache_xattr(file, &sx);
107 +                               }
108 +#endif
109                                 set_file_attrs(fullpath, file, NULL, 0);
110                                 free(file);
111                         }
112 @@ -195,6 +205,9 @@ static int keep_backup(char *fname)
113  #ifdef SUPPORT_ACLS
114         sx.acc_acl = sx.def_acl = NULL;
115  #endif
116 +#ifdef SUPPORT_XATTRS
117 +       sx.xattr = NULL;
118 +#endif
119  
120         if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
121                 return 1; /* the file could have disappeared */
122 @@ -208,6 +221,12 @@ static int keep_backup(char *fname)
123                 cache_acl(file, &sx);
124         }
125  #endif
126 +#ifdef SUPPORT_XATTRS
127 +       if (preserve_xattrs) {
128 +               get_xattr(fname, &sx);
129 +               cache_xattr(file, &sx);
130 +       }
131 +#endif
132  
133         /* Check to see if this is a device file, or link */
134         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
135 --- old/configure.in
136 +++ new/configure.in
137 @@ -856,6 +856,35 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
138    AC_MSG_RESULT(no)
139  )
140  
141 +AC_CHECK_HEADERS(attr/xattr.h)
142 +AC_CHECK_HEADERS(sys/xattr.h)
143 +AC_MSG_CHECKING(whether to support extended attributes)
144 +AC_ARG_ENABLE(xattr-support,
145 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
146 +[ case "$enableval" in
147 +  yes)
148 +      case "$host_os" in
149 +      *linux*)
150 +            AC_MSG_RESULT(Using Linux xattrs)
151 +            AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
152 +            ;;
153 +      darwin*)
154 +            AC_MSG_RESULT(Using OS X xattrs)
155 +            AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
156 +            ;;
157 +      *)
158 +            AC_MSG_RESULT(Xattrs requested but not Linux or OS X.  Good luck...)
159 +            ;;
160 +      esac
161 +      ;;
162 +  *)
163 +      AC_MSG_RESULT(no)
164 +      AC_DEFINE(HAVE_NA_XATTRS, 1, [True if you don't have extended attributes])
165 +  esac ],
166 +  AC_MSG_RESULT(no)
167 +  AC_DEFINE(HAVE_NO_XATTRL, 1, [True if you don't have extended attributes])
168 +)
169 +
170  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
171  AC_OUTPUT
172  
173 --- old/flist.c
174 +++ new/flist.c
175 @@ -41,6 +41,7 @@ extern int one_file_system;
176  extern int copy_dirlinks;
177  extern int keep_dirlinks;
178  extern int preserve_acls;
179 +extern int preserve_xattrs;
180  extern int preserve_links;
181  extern int preserve_hard_links;
182  extern int preserve_devices;
183 @@ -498,7 +499,7 @@ static struct file_struct *receive_file_
184         char thisname[MAXPATHLEN];
185         unsigned int l1 = 0, l2 = 0;
186         int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
187 -#ifdef SUPPORT_ACLS
188 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
189         int xtra_len;
190  #endif
191         OFF_T file_length;
192 @@ -610,10 +611,16 @@ static struct file_struct *receive_file_
193                 xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4;
194         else
195                 xtra_len = 0;
196 +#elif defined SUPPORT_XATTRS
197 +       xtra_len = 0;
198 +#endif
199 +#ifdef SUPPORT_XATTRS
200 +       if (preserve_xattrs)
201 +               xtra_len += 4;
202  #endif
203  
204         alloc_len = file_struct_len + dirname_len + basename_len
205 -#ifdef SUPPORT_ACLS
206 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
207                   + xtra_len
208  #endif
209                   + linkname_len + sum_len;
210 @@ -622,7 +629,7 @@ static struct file_struct *receive_file_
211         file = (struct file_struct *)bp;
212         memset(bp, 0, file_struct_len);
213         bp += file_struct_len;
214 -#ifdef SUPPORT_ACLS
215 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
216         bp += xtra_len;
217  #endif
218  
219 @@ -723,6 +730,10 @@ static struct file_struct *receive_file_
220         if (preserve_acls)
221                 receive_acl(file, f);
222  #endif
223 +#ifdef SUPPORT_XATTRS
224 +       if (preserve_xattrs)
225 +               receive_xattr(file, f );
226 +#endif
227  
228         return file;
229  }
230 @@ -994,6 +1005,13 @@ static struct file_struct *send_file_nam
231                         return NULL;
232         }
233  #endif
234 +#ifdef SUPPORT_XATTRS
235 +       if (preserve_xattrs) {
236 +               sx.xattr = NULL;
237 +               if (get_xattr(fname, &sx) < 0)
238 +                       return NULL;
239 +       }
240 +#endif
241  
242         maybe_emit_filelist_progress(flist->count + flist_count_offset);
243  
244 @@ -1006,11 +1024,19 @@ static struct file_struct *send_file_nam
245                 if (preserve_acls)
246                         send_acl(&sx, f);
247  #endif
248 +#ifdef SUPPORT_XATTRS
249 +               if (preserve_xattrs)
250 +                       send_xattr(&sx, f);
251 +#endif
252         } else {
253  #ifdef SUPPORT_ACLS
254                 if (preserve_acls)
255                         free_acl(&sx);
256  #endif
257 +#ifdef SUPPORT_XATTRS
258 +               if (preserve_xattrs)
259 +                       free_xattr(&sx);
260 +#endif
261         }
262         return file;
263  }
264 --- old/lib/sysxattr.c
265 +++ new/lib/sysxattr.c
266 @@ -0,0 +1,61 @@
267 +/*
268 + * Extended attribute support for rsync.
269 + *
270 + * Copyright (C) 2004 Red Hat, Inc.
271 + * Written by Jay Fenlason.
272 + *
273 + * This program is free software; you can redistribute it and/or modify
274 + * it under the terms of the GNU General Public License as published by
275 + * the Free Software Foundation; either version 2 of the License, or
276 + * (at your option) any later version.
277 + *
278 + * This program is distributed in the hope that it will be useful,
279 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
280 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
281 + * GNU General Public License for more details.
282 + *
283 + * You should have received a copy of the GNU General Public License along
284 + * with this program; if not, write to the Free Software Foundation, Inc.,
285 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
286 + */
287 +
288 +#include "rsync.h"
289 +#include "sysxattr.h"
290 +
291 +#if defined HAVE_LINUX_XATTRS
292 +
293 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
294 +{
295 +       return lgetxattr(path, name, value, size);
296 +}
297 +
298 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
299 +{
300 +       return lsetxattr(path, name, value, size, flags);
301 +}
302 +
303 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
304 +{
305 +       return llistxattr(path, list, size);
306 +}
307 +
308 +#elif HAVE_OSX_XATTRS
309 +
310 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
311 +{
312 +       return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
313 +}
314 +
315 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
316 +{
317 +       return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW | flags);
318 +}
319 +
320 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
321 +{
322 +       return listxattr(path, list, size, XATTR_NOFOLLOW);
323 +}
324 +
325 +#else
326 +
327 +#endif /* No xattrs */
328 --- old/lib/sysxattr.h
329 +++ new/lib/sysxattr.h
330 @@ -0,0 +1,19 @@
331 +#ifdef SUPPORT_XATTRS
332 +#if defined HAVE_ATTR_XATTR_H
333 +#include <attr/xattr.h>
334 +#elif defined HAVE_SYS_XATTR_H
335 +#include <sys/xattr.h>
336 +#endif
337 +#endif
338 +
339 +#if defined HAVE_LINUX_XATTRS || defined HAVE_OSX_XATTRS
340 +
341 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
342 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags);
343 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
344 +
345 +#else
346 +
347 +/* No xattrs */
348 +
349 +#endif
350 --- old/options.c
351 +++ new/options.c
352 @@ -48,6 +48,7 @@ int copy_links = 0;
353  int preserve_links = 0;
354  int preserve_hard_links = 0;
355  int preserve_acls = 0;
356 +int preserve_xattrs = 0;
357  int preserve_perms = 0;
358  int preserve_executability = 0;
359  int preserve_devices = 0;
360 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
361         char const *have_inplace = "no ";
362         char const *hardlinks = "no ";
363         char const *acls = "no ";
364 +       char const *xattrs = "no ";
365         char const *links = "no ";
366         char const *ipv6 = "no ";
367         STRUCT_STAT *dumstat;
368 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
369  #ifdef SUPPORT_ACLS
370         acls = "";
371  #endif
372 -
373 +#ifdef SUPPORT_XATTRS
374 +       xattrs = "";
375 +#endif
376  #ifdef SUPPORT_LINKS
377         links = "";
378  #endif
379 @@ -234,9 +238,9 @@ static void print_rsync_version(enum log
380         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
381         rprintf(f, "<http://rsync.samba.org/>\n");
382         rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
383 -               "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
384 +               "%shard links, %sACLs, %sxattrs, %ssymlinks, batchfiles,\n",
385                 (int) (sizeof (OFF_T) * 8),
386 -               got_socketpair, hardlinks, acls, links);
387 +               got_socketpair, hardlinks, acls, xattrs, links);
388  
389         /* Note that this field may not have type ino_t.  It depends
390          * on the complicated interaction between largefile feature
391 @@ -290,7 +294,7 @@ void usage(enum logcode F)
392    rprintf(F," -q, --quiet                 suppress non-error messages\n");
393    rprintf(F,"     --no-motd               suppress daemon-mode MOTD (see manpage caveat)\n");
394    rprintf(F," -c, --checksum              skip based on checksum, not mod-time & size\n");
395 -  rprintf(F," -a, --archive               archive mode; same as -rlptgoD (no -H, -A)\n");
396 +  rprintf(F," -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)\n");
397    rprintf(F,"     --no-OPTION             turn off an implied OPTION (e.g. --no-D)\n");
398    rprintf(F," -r, --recursive             recurse into directories\n");
399    rprintf(F," -R, --relative              use relative path names\n");
400 @@ -315,6 +319,9 @@ void usage(enum logcode F)
401  #ifdef SUPPORT_ACLS
402    rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
403  #endif
404 +#ifdef SUPPORT_XATTRS
405 +  rprintf(F," -X, --xattrs                preserve extended attributes (implies --perms)\n");
406 +#endif
407    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
408    rprintf(F," -g, --group                 preserve group\n");
409    rprintf(F,"     --devices               preserve device files (super-user only)\n");
410 @@ -437,6 +444,9 @@ static struct poptOption long_options[] 
411    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
412    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
413    {"no-A",             0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
414 +  {"xattrs",          'X', POPT_ARG_NONE,   0, 'X', 0, 0 },
415 +  {"no-xattrs",        0,  POPT_ARG_VAL,    &preserve_xattrs, 0, 0, 0 },
416 +  {"no-X",             0,  POPT_ARG_VAL,    &preserve_xattrs, 0, 0, 0 },
417    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
418    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
419    {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
420 @@ -1118,6 +1128,17 @@ int parse_arguments(int *argc, const cha
421                         return 0;
422  #endif
423  
424 +               case 'X':
425 +#ifdef SUPPORT_XATTRS
426 +                       preserve_xattrs = 1;
427 +                       preserve_perms = 1;
428 +                       break;
429 +#else
430 +                       snprintf(err_buf,sizeof(err_buf),
431 +                                "extended attributes are not supported on this %s\n",
432 +                                am_server ? "server" : "client");
433 +                       return 0;
434 +#endif /* SUPPORT_XATTRS */
435  
436                 default:
437                         /* A large opt value means that set_refuse_options()
438 @@ -1564,6 +1585,10 @@ void server_options(char **args,int *arg
439         if (preserve_acls)
440                 argstr[x++] = 'A';
441  #endif
442 +#ifdef SUPPORT_XATTRS
443 +       if (preserve_xattrs)
444 +               argstr[x++] = 'X';
445 +#endif
446         if (preserve_uid)
447                 argstr[x++] = 'o';
448         if (preserve_gid)
449 --- old/rsync.c
450 +++ new/rsync.c
451 @@ -33,6 +33,7 @@
452  extern int verbose;
453  extern int dry_run;
454  extern int preserve_acls;
455 +extern int preserve_xattrs;
456  extern int preserve_perms;
457  extern int preserve_executability;
458  extern int preserve_times;
459 @@ -229,6 +230,10 @@ int set_file_attrs(char *fname, struct f
460         if (preserve_acls && set_acl(fname, file, sxp) == 0)
461                 updated = 1;
462  #endif
463 +#ifdef SUPPORT_XATTRS
464 +       if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
465 +               updated = 1;
466 +#endif
467  
468  #ifdef HAVE_CHMOD
469         if ((sxp->st.st_mode & CHMOD_BITS) != (new_mode & CHMOD_BITS)) {
470 --- old/rsync.h
471 +++ new/rsync.h
472 @@ -501,6 +501,10 @@ struct idev {
473  #define ACLS_NEED_MASK 1
474  #endif
475  
476 +#if defined HAVE_LINUX_XATTRS || defined HAVE_OSX_XATTRS
477 +#define SUPPORT_XATTRS 1
478 +#endif
479 +
480  #define GID_NONE ((gid_t)-1)
481  
482  #define HL_CHECK_MASTER        0
483 @@ -695,6 +699,9 @@ typedef struct {
484      struct rsync_acl *acc_acl; /* access ACL */
485      struct rsync_acl *def_acl; /* default ACL */
486  #endif
487 +#ifdef SUPPORT_XATTRS
488 +    item_list *xattr;
489 +#endif
490  } statx;
491  
492  #define ACL_READY(sx) ((sx).acc_acl != NULL)
493 --- old/rsync.yo
494 +++ new/rsync.yo
495 @@ -301,7 +301,7 @@ to the detailed description below for a 
496   -q, --quiet                 suppress non-error messages
497       --no-motd               suppress daemon-mode MOTD (see caveat)
498   -c, --checksum              skip based on checksum, not mod-time & size
499 - -a, --archive               archive mode; same as -rlptgoD (no -H, -A)
500 + -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
501       --no-OPTION             turn off an implied OPTION (e.g. --no-D)
502   -r, --recursive             recurse into directories
503   -R, --relative              use relative path names
504 @@ -324,6 +324,7 @@ to the detailed description below for a 
505   -E, --executability         preserve executability
506       --chmod=CHMOD           affect file and/or directory permissions
507   -A, --acls                  preserve ACLs (implies -p) [non-standard]
508 + -X, --xattrs                preserve extended attrs (implies -p) [n.s.]
509   -o, --owner                 preserve owner (super-user only)
510   -g, --group                 preserve group
511       --devices               preserve device files (super-user only)
512 @@ -818,6 +819,11 @@ version makes it incompatible with sendi
513  rsync unless you double the bf(--acls) option (e.g. bf(-AA)).  This
514  doubling is not needed when pulling files from an older rsync.
515  
516 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
517 +extended attributes to be the same as the local ones.  This will work
518 +only if the remote machine's rsync supports this option also. This is
519 +a non-standard option.
520 +
521  dit(bf(--chmod)) This option tells rsync to apply one or more
522  comma-separated "chmod" strings to the permission of the files in the
523  transfer.  The resulting value is treated as though it was the permissions
524 --- old/xattr.c
525 +++ new/xattr.c
526 @@ -0,0 +1,367 @@
527 +/*
528 + * Extended Attribute support for rsync.
529 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
530 + *
531 + * Copyright (C) 2004 Red Hat, Inc.
532 + * Copyright (C) 2006 Wayne Davison
533 + *
534 + * This program is free software; you can redistribute it and/or modify
535 + * it under the terms of the GNU General Public License as published by
536 + * the Free Software Foundation; either version 2 of the License, or
537 + * (at your option) any later version.
538 + *
539 + * This program is distributed in the hope that it will be useful,
540 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
541 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
542 + * GNU General Public License for more details.
543 + *
544 + * You should have received a copy of the GNU General Public License along
545 + * with this program; if not, write to the Free Software Foundation, Inc.,
546 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
547 + */
548 +
549 +#include "rsync.h"
550 +#include "lib/sysxattr.h"
551 +
552 +#ifdef SUPPORT_XATTRS
553 +
554 +extern int dry_run;
555 +extern unsigned int file_struct_len;
556 +
557 +#define RSYNC_XAL_INITIAL 5
558 +#define RSYNC_XAL_LIST_INITIAL 100
559 +
560 +typedef struct {
561 +       char *name;
562 +       char *datum;
563 +       size_t name_len;
564 +       size_t datum_len;
565 +} rsync_xa;
566 +
567 +static size_t namebuf_len = 0;
568 +static char *namebuf = NULL;
569 +
570 +static item_list empty_xattr = EMPTY_ITEM_LIST;
571 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
572 +
573 +#ifdef HAVE_OSX_XATTRS
574 +static char unique_prefix[] = "user.0S%."; /* OSX */
575 +static int upre_len = sizeof unique_prefix - 1;
576 +#endif
577 +
578 +/* ------------------------------------------------------------------------- */
579 +
580 +static void rsync_xal_free(item_list *xalp)
581 +{
582 +       size_t i;
583 +       rsync_xa *rxas = xalp->items;
584 +
585 +       for (i = 0; i < xalp->count; i++) {
586 +               free(rxas[i].name);
587 +               /* free(rxas[i].value); */
588 +       }
589 +       xalp->count = 0;
590 +}
591 +
592 +void free_xattr(statx *sxp)
593 +{
594 +       rsync_xal_free(sxp->xattr);
595 +       free(sxp->xattr);
596 +       sxp->xattr = NULL;
597 +}
598 +
599 +static int rsync_xal_compare_names(const void *x1, const void *x2)
600 +{
601 +       const rsync_xa *xa1 = x1;
602 +       const rsync_xa *xa2 = x2;
603 +       return strcmp(xa1->name, xa2->name);
604 +}
605 +
606 +static int rsync_xal_get(const char *fname, item_list *xalp)
607 +{
608 +       ssize_t name_size;
609 +       ssize_t datum_size;
610 +       ssize_t left;
611 +       char *name;
612 +       size_t len;
613 +       char *ptr;
614 +
615 +       if (!namebuf) {
616 +               namebuf_len = 1024;
617 +               namebuf = new_array(char, namebuf_len);
618 +               if (!namebuf)
619 +                       out_of_memory("rsync_xal_get");
620 +       }
621 +
622 +       /* The length returned includes all the '\0' terminators. */
623 +       name_size = sys_llistxattr(fname, namebuf, namebuf_len);
624 +       if (name_size > (ssize_t)namebuf_len) {
625 +               name_size = -1;
626 +               errno = ERANGE;
627 +       }
628 +       if (name_size < 0) {
629 +               if (errno == ENOTSUP)
630 +                       return 0;
631 +               if (errno == ERANGE) {
632 +                       name_size = sys_llistxattr(fname, NULL, 0);
633 +                       if (name_size < 0) {
634 +                               rsyserr(FERROR, errno, "%s: rsync_xal_get: llistxattr",
635 +                                       fname);
636 +                               return -1;
637 +                       }
638 +                       namebuf = realloc_array(namebuf, char, name_size + 1024);
639 +                       if (!namebuf)
640 +                               out_of_memory("rsync_xal_get");
641 +                       namebuf_len = name_size + 1024;
642 +                       name_size = sys_llistxattr(fname, namebuf, namebuf_len);
643 +                       if (name_size < 0) {
644 +                               rsyserr(FERROR, errno,
645 +                                   "%s: rsync_xal_get: re-llistxattr failed",
646 +                                   fname);
647 +                               return -1;
648 +                       }
649 +               } else {
650 +                       rsyserr(FERROR, errno,
651 +                           "%s: rsync_xal_get: llistxattr failed:",
652 +                           fname);
653 +                       return -1;
654 +               }
655 +       }
656 +       if (name_size == 0)
657 +               return 0;
658 +       for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
659 +               rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
660 +
661 +               len = strlen(name) + 1;
662 +               datum_size = sys_lgetxattr(fname, name, NULL, 0);
663 +               if (datum_size < 0) {
664 +                       if (errno == ENOTSUP)
665 +                               return -1;
666 +                       rsyserr(FERROR, errno,
667 +                           "%s: rsync_xal_get: lgetxattr %s failed",
668 +                           fname, name);
669 +                       return -1;
670 +               }
671 +               ptr = new_array(char, len + datum_size);
672 +               if (!ptr)
673 +                       out_of_memory("rsync_xal_get");
674 +               memcpy(ptr, name, len);
675 +               rxas->name_len = len;
676 +               rxas->name = ptr;
677 +               rxas->datum_len = datum_size;
678 +               rxas->datum = ptr + len;
679 +               if (datum_size) {
680 +                       ssize_t size = sys_lgetxattr(fname, name, rxas->datum, datum_size);
681 +                       if (size != datum_size) {
682 +                               if (size < 0) {
683 +                                       rsyserr(FERROR, errno,
684 +                                           "rsync_xal_get: lgetxattr(%s,%s)"
685 +                                           " failed", fname, name);
686 +                               } else {
687 +                                       rprintf(FERROR,
688 +                                           "rsync_xal_get: lgetxattr(%s,%s)"
689 +                                           " returned %d instead of %d\n",
690 +                                           fname, name, size, datum_size);
691 +                               }
692 +                               return -1;
693 +                       }
694 +               }
695 +       }
696 +       if (xalp->count > 1)
697 +               qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
698 +       return 0;
699 +}
700 +
701 +/* Read the xattr(s) for this filename. */
702 +int get_xattr(const char *fname, statx *sxp)
703 +{
704 +       sxp->xattr = new(item_list);
705 +       *sxp->xattr = empty_xattr;
706 +       if (rsync_xal_get(fname, sxp->xattr) < 0) {
707 +               free_xattr(sxp);
708 +               return -1;
709 +       }
710 +       return 0;
711 +}
712 +
713 +static int find_matching_xattr(item_list *xalp)
714 +{
715 +       size_t i, j;
716 +       item_list *lst = rsync_xal_l.items;
717 +
718 +       for (i = 0; i < rsync_xal_l.count; i++) {
719 +               rsync_xa *rxas1 = lst[i].items;
720 +               rsync_xa *rxas2 = xalp->items;
721 +
722 +               /* Wrong number of elements? */
723 +               if (lst[i].count != xalp->count)
724 +                       continue;
725 +               /* any elements different? */
726 +               for (j = 0; j < xalp->count; j++) {
727 +                       if (rxas1[j].name_len != rxas2[j].name_len
728 +                        || rxas1[j].datum_len != rxas2[j].datum_len
729 +                        || strcmp(rxas1[j].name, rxas2[j].name)
730 +                        || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
731 +                               break;
732 +               }
733 +               /* no differences found.  This is The One! */
734 +               if (j == xalp->count)
735 +                       return i;
736 +       }
737 +
738 +       return -1;
739 +}
740 +
741 +/* Store *xalp on the end of rsync_xal_l */
742 +static void rsync_xal_store(item_list *xalp)
743 +{
744 +       item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
745 +       /* Since the following call starts a new list, we know it will hold the
746 +        * entire initial-count, not just enough space for one new item. */
747 +       *new_lst = empty_xattr;
748 +       (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
749 +       memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
750 +       new_lst->count = xalp->count;
751 +       xalp->count = 0;
752 +}
753 +
754 +/* Send the make_xattr()-generated xattr list for this flist entry. */
755 +void send_xattr(statx *sxp, int f)
756 +{
757 +       int ndx = find_matching_xattr(sxp->xattr);
758 +       if (ndx != -1) {
759 +               write_byte(f, 'x');
760 +               write_int(f, ndx);
761 +               rsync_xal_free(sxp->xattr);
762 +       } else {
763 +               rsync_xa *rxa;
764 +               int count = sxp->xattr->count;
765 +               write_byte(f, 'X');
766 +               write_int(f, count);
767 +               for (rxa = sxp->xattr->items; count--; rxa++) {
768 +#ifdef HAVE_OSX_XATTRS
769 +                       if (strncmp(rxa->name, "user.", 5) != 0
770 +                        && strncmp(rxa->name, "system.", 7) != 0) {
771 +                               write_int(f, rxa->name_len + upre_len);
772 +                               write_int(f, rxa->datum_len);
773 +                               write_buf(f, unique_prefix, upre_len);
774 +                       } else
775 +#endif
776 +                       {
777 +                               write_int(f, rxa->name_len);
778 +                               write_int(f, rxa->datum_len);
779 +                       }
780 +                       write_buf(f, rxa->name, rxa->name_len);
781 +                       write_buf(f, rxa->datum, rxa->datum_len);
782 +               }
783 +               rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
784 +       }
785 +       free_xattr(sxp);
786 +}
787 +
788 +/* ------------------------------------------------------------------------- */
789 +
790 +/* receive and build the rsync_xattr_lists */
791 +void receive_xattr(struct file_struct *file, int f)
792 +{
793 +       static item_list temp_xattr = EMPTY_ITEM_LIST;
794 +       int tag = read_byte(f);
795 +       char *ndx_ptr = (char*)file + file_struct_len;
796 +       int ndx;
797 +
798 +       if (tag == 'X') {
799 +               int i, count = read_int(f);
800 +               for (i = 0; i < count; i++) {
801 +                       char *ptr;
802 +                       rsync_xa *rxa;
803 +                       size_t name_len = read_int(f);
804 +                       size_t datum_len = read_int(f);
805 +                       if (name_len + datum_len < name_len)
806 +                               out_of_memory("receive_xattr"); /* overflow */
807 +                       rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
808 +                       ptr = new_array(char, name_len + datum_len);
809 +                       if (!ptr)
810 +                               out_of_memory("receive_xattr");
811 +                       read_buf(f, ptr, name_len);
812 +                       read_buf(f, ptr + name_len, datum_len);
813 +                       rxa->name_len = name_len;
814 +                       rxa->datum_len = datum_len;
815 +                       rxa->name = ptr;
816 +                       rxa->datum = ptr + name_len;
817 +#ifdef HAVE_OSX_XATTRS
818 +                       if (strncmp(rxa->name, unique_prefix, upre_len) == 0) {
819 +                               rxa->name_len -= upre_len;
820 +                               memmove(rxa->name, rxa->name + upre_len, rxa->name_len);
821 +                       }
822 +#endif
823 +               }
824 +               ndx = rsync_xal_l.count; /* pre-incremented count */
825 +               rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
826 +       } else if (tag == 'x') {
827 +               ndx = read_int(f);
828 +               if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
829 +                       rprintf(FERROR, "%s: receive_xattr: xa index %d out of range\n",
830 +                               f_name(file, NULL), ndx);
831 +                       exit_cleanup(RERR_STREAMIO);
832 +               }
833 +       } else {
834 +               rprintf(FERROR,
835 +                   "%s: receive_xattr: unknown extended attribute type tag: %c\n",
836 +                   f_name(file, NULL), tag);
837 +               exit_cleanup(RERR_STREAMIO);
838 +               ndx = 0; /* silence a compiler warning... */
839 +       }
840 +
841 +       SIVAL(ndx_ptr, 0, ndx);
842 +}
843 +
844 +/* Turn the xattr data in statx into cached xattr data, setting the index
845 + * values in the file struct. */
846 +void cache_xattr(struct file_struct *file, statx *sxp)
847 +{
848 +       char *ndx_ptr = (char*)file + file_struct_len;
849 +       int ndx;
850 +
851 +       if (!sxp->xattr)
852 +               return;
853 +
854 +       ndx = find_matching_xattr(sxp->xattr);
855 +       if (ndx == -1)
856 +               rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
857 +       free_xattr(sxp);
858 +
859 +       SIVAL(ndx_ptr, 0, ndx);
860 +}
861 +
862 +static int rsync_xal_set(const char *fname, item_list *xalp)
863 +{
864 +       rsync_xa *rxas = xalp->items;
865 +       size_t i;
866 +       int ret = 0;
867 +
868 +       for (i = 0; i < xalp->count; i++) {
869 +               int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len, 0);
870 +               if (status < 0) {
871 +                       rsyserr(FERROR, errno, "%s: rsync_xal_set: lsetxattr %s failed",
872 +                               fname, rxas[i].name);
873 +                       ret = -1;
874 +               }
875 +       }
876 +       return ret;
877 +}
878 +
879 +/* Set extended attributes on indicated filename. */
880 +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
881 +{
882 +       int ndx;
883 +       char *ndx_ptr = (char*)file + file_struct_len;
884 +       item_list *lst = rsync_xal_l.items;
885 +
886 +       if (dry_run)
887 +               return 1; /* FIXME: --dry-run needs to compute this value */
888 +
889 +       ndx = IVAL(ndx_ptr, 0);
890 +       return rsync_xal_set(fname, lst + ndx); /* TODO:  This needs to return 1 if no xattrs changed! */
891 +}
892 +
893 +#endif /* SUPPORT_XATTRS */