0b12de66787318177edeff847d59b51dcc34fbc3
[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 @@ -27,13 +27,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 @@ -743,6 +744,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 @@ -801,6 +806,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 @@ -916,6 +925,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 @@ -823,6 +823,30 @@ 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_MSG_CHECKING(whether to support extended attributes)
143 +AC_ARG_ENABLE(xattr-support,
144 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
145 +[ case "$enableval" in
146 +  yes)
147 +      case "$host_os" in
148 +      *linux*)
149 +            AC_MSG_RESULT(Using Linux xattrs)
150 +            AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
151 +            ;;
152 +      *)
153 +            AC_MSG_RESULT(Xattrs requested but not linux.  Good luck)
154 +            ;;
155 +      esac
156 +      ;;
157 +  *)
158 +      AC_MSG_RESULT(no)
159 +      AC_DEFINE(HAVE_NA_XATTRS, 1, [True if you don't have extended attributes])
160 +  esac ],
161 +  AC_MSG_RESULT(no)
162 +  AC_DEFINE(HAVE_NO_XATTRL, 1, [True if you don't have extended attributes])
163 +)
164 +
165  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
166  AC_OUTPUT
167  
168 --- old/flist.c
169 +++ new/flist.c
170 @@ -41,6 +41,7 @@ extern int one_file_system;
171  extern int copy_dirlinks;
172  extern int keep_dirlinks;
173  extern int preserve_acls;
174 +extern int preserve_xattrs;
175  extern int preserve_links;
176  extern int preserve_hard_links;
177  extern int preserve_devices;
178 @@ -494,7 +495,7 @@ static struct file_struct *receive_file_
179         char thisname[MAXPATHLEN];
180         unsigned int l1 = 0, l2 = 0;
181         int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
182 -#ifdef SUPPORT_ACLS
183 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
184         int xtra_len;
185  #endif
186         OFF_T file_length;
187 @@ -606,10 +607,16 @@ static struct file_struct *receive_file_
188                 xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4;
189         else
190                 xtra_len = 0;
191 +#elif defined SUPPORT_XATTRS
192 +       xtra_len = 0;
193 +#endif
194 +#ifdef SUPPORT_XATTRS
195 +       if (preserve_xattrs)
196 +               xtra_len += 4;
197  #endif
198  
199         alloc_len = file_struct_len + dirname_len + basename_len
200 -#ifdef SUPPORT_ACLS
201 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
202                   + xtra_len
203  #endif
204                   + linkname_len + sum_len;
205 @@ -618,7 +625,7 @@ static struct file_struct *receive_file_
206         file = (struct file_struct *)bp;
207         memset(bp, 0, file_struct_len);
208         bp += file_struct_len;
209 -#ifdef SUPPORT_ACLS
210 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
211         bp += xtra_len;
212  #endif
213  
214 @@ -717,6 +724,10 @@ static struct file_struct *receive_file_
215         if (preserve_acls)
216                 receive_acl(file, f);
217  #endif
218 +#ifdef SUPPORT_XATTRS
219 +       if (preserve_xattrs)
220 +               receive_xattr(file, f );
221 +#endif
222  
223         return file;
224  }
225 @@ -987,6 +998,13 @@ static struct file_struct *send_file_nam
226                         return NULL;
227         }
228  #endif
229 +#ifdef SUPPORT_XATTRS
230 +       if (preserve_xattrs) {
231 +               sx.xattr = NULL;
232 +               if (get_xattr(fname, &sx) < 0)
233 +                       return NULL;
234 +       }
235 +#endif
236  
237         maybe_emit_filelist_progress(flist->count + flist_count_offset);
238  
239 @@ -999,11 +1017,19 @@ static struct file_struct *send_file_nam
240                 if (preserve_acls)
241                         send_acl(&sx, f);
242  #endif
243 +#ifdef SUPPORT_XATTRS
244 +               if (preserve_xattrs)
245 +                       send_xattr(&sx, f);
246 +#endif
247         } else {
248  #ifdef SUPPORT_ACLS
249                 if (preserve_acls)
250                         free_acl(&sx);
251  #endif
252 +#ifdef SUPPORT_XATTRS
253 +               if (preserve_xattrs)
254 +                       free_xattr(&sx);
255 +#endif
256         }
257         return file;
258  }
259 --- old/lib/sysxattr.c
260 +++ new/lib/sysxattr.c
261 @@ -0,0 +1,44 @@
262 +/*
263 + * Extended attribute support for rsync.
264 + *
265 + * Copyright (C) 2004 Red Hat, Inc.
266 + * Written by Jay Fenlason.
267 + *
268 + * This program is free software; you can redistribute it and/or modify
269 + * it under the terms of the GNU General Public License as published by
270 + * the Free Software Foundation; either version 2 of the License, or
271 + * (at your option) any later version.
272 + *
273 + * This program is distributed in the hope that it will be useful,
274 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
275 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
276 + * GNU General Public License for more details.
277 + *
278 + * You should have received a copy of the GNU General Public License along
279 + * with this program; if not, write to the Free Software Foundation, Inc.,
280 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
281 + */
282 +
283 +#include "rsync.h"
284 +#include "sysxattr.h"
285 +
286 +#if defined HAVE_LINUX_XATTRS
287 +
288 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
289 +{
290 +       return lgetxattr(path, name, value, size);
291 +}
292 +
293 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
294 +{
295 +       return lsetxattr(path, name, value, size, flags);
296 +}
297 +
298 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
299 +{
300 +       return llistxattr(path, list, size);
301 +}
302 +
303 +#else
304 +
305 +#endif /* No xattrs */
306 --- old/lib/sysxattr.h
307 +++ new/lib/sysxattr.h
308 @@ -0,0 +1,13 @@
309 +#if defined SUPPORT_XATTRS && defined HAVE_ATTR_XATTR_H
310 +#include <attr/xattr.h>
311 +#endif
312 +
313 +#if defined HAVE_LINUX_XATTRS
314 +
315 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
316 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags);
317 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
318 +
319 +#else
320 +
321 +#endif /* No xattrs */
322 --- old/options.c
323 +++ new/options.c
324 @@ -48,6 +48,7 @@ int copy_links = 0;
325  int preserve_links = 0;
326  int preserve_hard_links = 0;
327  int preserve_acls = 0;
328 +int preserve_xattrs = 0;
329  int preserve_perms = 0;
330  int preserve_executability = 0;
331  int preserve_devices = 0;
332 @@ -200,6 +201,7 @@ static void print_rsync_version(enum log
333         char const *have_inplace = "no ";
334         char const *hardlinks = "no ";
335         char const *acls = "no ";
336 +       char const *xattrs = "no ";
337         char const *links = "no ";
338         char const *ipv6 = "no ";
339         STRUCT_STAT *dumstat;
340 @@ -219,7 +221,9 @@ static void print_rsync_version(enum log
341  #ifdef SUPPORT_ACLS
342         acls = "";
343  #endif
344 -
345 +#ifdef SUPPORT_XATTRS
346 +       xattrs = "";
347 +#endif
348  #ifdef SUPPORT_LINKS
349         links = "";
350  #endif
351 @@ -233,9 +237,9 @@ static void print_rsync_version(enum log
352         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
353         rprintf(f, "<http://rsync.samba.org/>\n");
354         rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
355 -               "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
356 +               "%shard links, %sACLs, %sxattrs, %ssymlinks, batchfiles,\n",
357                 (int) (sizeof (OFF_T) * 8),
358 -               got_socketpair, hardlinks, acls, links);
359 +               got_socketpair, hardlinks, acls, xattrs, links);
360  
361         /* Note that this field may not have type ino_t.  It depends
362          * on the complicated interaction between largefile feature
363 @@ -308,6 +312,9 @@ void usage(enum logcode F)
364  #ifdef SUPPORT_ACLS
365    rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
366  #endif
367 +#ifdef SUPPORT_XATTRS
368 +  rprintf(F," -X, --xattrs                preserve extended attributes (implies --perms)\n");
369 +#endif
370    rprintf(F,"     --chmod=CHMOD           change destination permissions\n");
371    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
372    rprintf(F," -g, --group                 preserve group\n");
373 @@ -427,6 +434,9 @@ static struct poptOption long_options[] 
374    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
375    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
376    {"no-A",             0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
377 +  {"xattrs",          'X', POPT_ARG_NONE,   0, 'X', 0, 0 },
378 +  {"no-xattrs",        0,  POPT_ARG_VAL,    &preserve_xattrs, 0, 0, 0 },
379 +  {"no-X",             0,  POPT_ARG_VAL,    &preserve_xattrs, 0, 0, 0 },
380    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
381    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
382    {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
383 @@ -1109,6 +1119,17 @@ int parse_arguments(int *argc, const cha
384                         return 0;
385  #endif
386  
387 +               case 'X':
388 +#ifdef SUPPORT_XATTRS
389 +                       preserve_xattrs = 1;
390 +                       preserve_perms = 1;
391 +                       break;
392 +#else
393 +                       snprintf(err_buf,sizeof(err_buf),
394 +                                "extended attributes are not supported on this %s\n",
395 +                                am_server ? "server" : "client");
396 +                       return 0;
397 +#endif /* SUPPORT_XATTRS */
398  
399                 default:
400                         /* A large opt value means that set_refuse_options()
401 @@ -1560,6 +1581,10 @@ void server_options(char **args,int *arg
402         if (preserve_acls)
403                 argstr[x++] = 'A';
404  #endif
405 +#ifdef SUPPORT_XATTRS
406 +       if (preserve_xattrs)
407 +               argstr[x++] = 'X';
408 +#endif
409         if (preserve_uid)
410                 argstr[x++] = 'o';
411         if (preserve_gid)
412 --- old/rsync.c
413 +++ new/rsync.c
414 @@ -34,6 +34,7 @@ extern int verbose;
415  extern int dry_run;
416  extern int logfile_format_has_i;
417  extern int preserve_acls;
418 +extern int preserve_xattrs;
419  extern int preserve_perms;
420  extern int preserve_executability;
421  extern int preserve_times;
422 @@ -223,6 +224,10 @@ int set_file_attrs(char *fname, struct f
423         if (preserve_acls && set_acl(fname, file, sxp) == 0)
424                 updated = 1;
425  #endif
426 +#ifdef SUPPORT_XATTRS
427 +       if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
428 +               updated = 1;
429 +#endif
430  
431  #ifdef HAVE_CHMOD
432         if ((sxp->st.st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
433 --- old/rsync.h
434 +++ new/rsync.h
435 @@ -495,6 +495,10 @@ struct idev {
436  #define ACLS_NEED_MASK 1
437  #endif
438  
439 +#ifdef HAVE_LINUX_XATTRS
440 +#define SUPPORT_XATTRS 1
441 +#endif
442 +
443  #define GID_NONE ((gid_t)-1)
444  
445  #define HL_CHECK_MASTER        0
446 @@ -687,6 +691,9 @@ typedef struct {
447      struct rsync_acl *acc_acl; /* access ACL */
448      struct rsync_acl *def_acl; /* default ACL */
449  #endif
450 +#ifdef SUPPORT_XATTRS
451 +    item_list *xattr;
452 +#endif
453  } statx;
454  
455  #define ACL_READY(sx) ((sx).acc_acl != NULL)
456 --- old/rsync.yo
457 +++ new/rsync.yo
458 @@ -322,6 +322,7 @@ to the detailed description below for a 
459   -p, --perms                 preserve permissions
460   -E, --executability         preserve executability
461   -A, --acls                  preserve ACLs (implies -p) [non-standard]
462 + -X, --xattrs                preserve extended attrs (implies -p) [n.s.]
463       --chmod=CHMOD           change destination permissions
464   -o, --owner                 preserve owner (super-user only)
465   -g, --group                 preserve group
466 @@ -809,6 +810,11 @@ version makes it incompatible with sendi
467  rsync unless you double the bf(--acls) option (e.g. bf(-AA)).  This
468  doubling is not needed when pulling files from an older rsync.
469  
470 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
471 +extended attributes to be the same as the local ones.  This will work
472 +only if the remote machine's rsync supports this option also. This is
473 +a non-standard option.
474 +
475  dit(bf(--chmod)) This option tells rsync to apply one or more
476  comma-separated "chmod" strings to the permission of the files in the
477  transfer.  The resulting value is treated as though it was the permissions
478 --- old/xattr.c
479 +++ new/xattr.c
480 @@ -0,0 +1,358 @@
481 +/*
482 + * Extended Attribute support for rsync.
483 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
484 + *
485 + * Copyright (C) 2004 Red Hat, Inc.
486 + * Copyright (C) 2006 Wayne Davison
487 + *
488 + * This program is free software; you can redistribute it and/or modify
489 + * it under the terms of the GNU General Public License as published by
490 + * the Free Software Foundation; either version 2 of the License, or
491 + * (at your option) any later version.
492 + *
493 + * This program is distributed in the hope that it will be useful,
494 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
495 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
496 + * GNU General Public License for more details.
497 + *
498 + * You should have received a copy of the GNU General Public License along
499 + * with this program; if not, write to the Free Software Foundation, Inc.,
500 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
501 + */
502 +
503 +#include "rsync.h"
504 +#include "lib/sysxattr.h"
505 +
506 +#ifdef SUPPORT_XATTRS
507 +
508 +extern int dry_run;
509 +extern unsigned int file_struct_len;
510 +
511 +#define RSYNC_XAL_INITIAL 5
512 +#define RSYNC_XAL_LIST_INITIAL 100
513 +
514 +typedef struct {
515 +       char *name;
516 +       char *datum;
517 +       size_t name_len;
518 +       size_t datum_len;
519 +} rsync_xa;
520 +
521 +static size_t namebuf_len = 0;
522 +static char *namebuf = NULL;
523 +
524 +static size_t datumbuf_len = 0;
525 +static char *datumbuf = NULL;
526 +
527 +static item_list empty_xattr = EMPTY_ITEM_LIST;
528 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
529 +
530 +/* ------------------------------------------------------------------------- */
531 +
532 +static void rsync_xal_free(item_list *xalp)
533 +{
534 +       size_t i;
535 +       rsync_xa *rxas = xalp->items;
536 +
537 +       for (i = 0; i < xalp->count; i++) {
538 +               free(rxas[i].name);
539 +               /* free(rxas[i].value); */
540 +       }
541 +       xalp->count = 0;
542 +}
543 +
544 +void free_xattr(statx *sxp)
545 +{
546 +       rsync_xal_free(sxp->xattr);
547 +       free(sxp->xattr);
548 +       sxp->xattr = NULL;
549 +}
550 +
551 +static int rsync_xal_compare_names(const void *x1, const void *x2)
552 +{
553 +       const rsync_xa *xa1 = x1;
554 +       const rsync_xa *xa2 = x2;
555 +       return strcmp(xa1->name, xa2->name);
556 +}
557 +
558 +static int rsync_xal_get(const char *fname, item_list *xalp)
559 +{
560 +       ssize_t name_size;
561 +       ssize_t datum_size;
562 +       ssize_t left;
563 +       char *name;
564 +       size_t len;
565 +       char *ptr;
566 +
567 +       if (!namebuf) {
568 +               namebuf_len = 100;
569 +               namebuf = new_array(char, namebuf_len);
570 +               datumbuf_len = 100;
571 +               datumbuf = new_array(char, datumbuf_len);
572 +               if (!namebuf || !datumbuf)
573 +                       out_of_memory("rsync_xal_get");
574 +       }
575 +
576 +       name_size = sys_llistxattr(fname, namebuf, namebuf_len);
577 +       if (name_size > (ssize_t)namebuf_len) {
578 +               name_size = -1;
579 +               errno = ERANGE;
580 +       }
581 +       if (name_size < 0) {
582 +               if (errno == ENOTSUP)
583 +                       return 0;
584 +               if (errno == ERANGE) {
585 +                       name_size = sys_llistxattr(fname, NULL, 0);
586 +                       if (name_size < 0) {
587 +                               rsyserr(FERROR, errno, "%s: rsync_xal_get: llistxattr",
588 +                                       fname);
589 +                               return -1;
590 +                       }
591 +                       namebuf = realloc_array(namebuf, char, name_size + 1);
592 +                       if (!namebuf)
593 +                               out_of_memory("rsync_xal_get");
594 +                       namebuf_len = name_size;
595 +                       name_size = sys_llistxattr(fname, namebuf, namebuf_len);
596 +                       if (name_size < 0) {
597 +                               rsyserr(FERROR, errno,
598 +                                   "%s: rsync_xal_get: re-llistxattr failed",
599 +                                   fname);
600 +                               return -1;
601 +                       }
602 +               } else {
603 +                       rsyserr(FERROR, errno,
604 +                           "%s: rsync_xal_get: llistxattr failed:",
605 +                           fname);
606 +                       return -1;
607 +               }
608 +       }
609 +       if (name_size == 0)
610 +               return 0;
611 +       for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
612 +               rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
613 +
614 +               len = strlen(name) + 1;
615 +               datum_size = sys_lgetxattr(fname, name, datumbuf, datumbuf_len);
616 +               if (datum_size > (ssize_t)datumbuf_len) {
617 +                       datum_size = -1;
618 +                       errno = ERANGE;
619 +               }
620 +               if (datum_size < 0) {
621 +                       if (errno == ENOTSUP)
622 +                               return -1;
623 +                       if (errno == ERANGE) {
624 +                               datum_size = sys_lgetxattr(fname, name, NULL, 0);
625 +                               if (datum_size < 0) {
626 +                                       rsyserr(FERROR, errno,
627 +                                           "%s: rsync_xal_get: lgetxattr %s failed",
628 +                                           fname, name);
629 +                                       return -1;
630 +                               }
631 +                               datumbuf = realloc_array(datumbuf, char, datum_size + 1);
632 +                               if (!datumbuf)
633 +                                       out_of_memory("rsync_xal_get");
634 +                               datumbuf_len = datum_size;
635 +                               datum_size = sys_lgetxattr(fname, name, datumbuf, datumbuf_len);
636 +                               if (datum_size < 0) {
637 +                                       rsyserr(FERROR, errno,
638 +                                           "%s: rsync_xal_get: re-lgetxattr of %s failed",
639 +                                           name, fname);
640 +                                       return -1;
641 +                               }
642 +                       } else {
643 +                               rsyserr(FERROR, errno,
644 +                                   "%s: rsync_xal_get: lgetxattr %s failed",
645 +                                   fname, name);
646 +                               return -1;
647 +                       }
648 +               }
649 +               ptr = new_array(char, len + datum_size);
650 +               if (!ptr)
651 +                       out_of_memory("rsync_xal_get");
652 +               memcpy(ptr, name, len);
653 +               if (datum_size)
654 +                       memcpy(ptr + len, datumbuf, datum_size);
655 +               rxas->name_len = len;
656 +               rxas->name = ptr;
657 +               rxas->datum_len = datum_size;
658 +               rxas->datum = ptr + len;
659 +       }
660 +       if (xalp->count > 1)
661 +               qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
662 +       return 0;
663 +}
664 +
665 +/* Read the xattr(s) for this filename. */
666 +int get_xattr(const char *fname, statx *sxp)
667 +{
668 +       sxp->xattr = new(item_list);
669 +       *sxp->xattr = empty_xattr;
670 +       if (rsync_xal_get(fname, sxp->xattr) < 0) {
671 +               free_xattr(sxp);
672 +               return -1;
673 +       }
674 +       return 0;
675 +}
676 +
677 +static int find_matching_xattr(item_list *xalp)
678 +{
679 +       size_t i, j;
680 +       item_list *lst = rsync_xal_l.items;
681 +
682 +       for (i = 0; i < rsync_xal_l.count; i++) {
683 +               rsync_xa *rxas1 = lst[i].items;
684 +               rsync_xa *rxas2 = xalp->items;
685 +
686 +               /* Wrong number of elements? */
687 +               if (lst[i].count != xalp->count)
688 +                       continue;
689 +               /* any elements different? */
690 +               for (j = 0; j < xalp->count; j++) {
691 +                       if (rxas1[j].name_len != rxas2[j].name_len
692 +                        || rxas1[j].datum_len != rxas2[j].datum_len
693 +                        || strcmp(rxas1[j].name, rxas2[j].name)
694 +                        || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
695 +                               break;
696 +               }
697 +               /* no differences found.  This is The One! */
698 +               if (j == xalp->count)
699 +                       return i;
700 +       }
701 +
702 +       return -1;
703 +}
704 +
705 +/* Store *xalp on the end of rsync_xal_l */
706 +static void rsync_xal_store(item_list *xalp)
707 +{
708 +       item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
709 +       EXPAND_ITEM_LIST(new_lst, item_list, xalp->count);
710 +       memcpy(new_lst->items, xalp->items, xalp->count * sizeof (item_list));
711 +       new_lst->count = xalp->count;
712 +       xalp->count = 0;
713 +}
714 +
715 +/* Send the make_xattr()-generated xattr list for this flist entry. */
716 +void send_xattr(statx *sxp, int f)
717 +{
718 +       int ndx = find_matching_xattr(sxp->xattr);
719 +       if (ndx != -1) {
720 +               write_byte(f, 'x');
721 +               write_int(f, ndx);
722 +               rsync_xal_free(sxp->xattr);
723 +       } else {
724 +               rsync_xa *rxa;
725 +               int count = sxp->xattr->count;
726 +               write_byte(f, 'X');
727 +               write_int(f, count);
728 +               for (rxa = sxp->xattr->items; count--; rxa++) {
729 +                       write_int(f, rxa->name_len);
730 +                       write_int(f, rxa->datum_len);
731 +                       write_buf(f, rxa->name, rxa->name_len);
732 +                       write_buf(f, rxa->datum, rxa->datum_len);
733 +               }
734 +               rsync_xal_store(sxp->xattr);
735 +       }
736 +       free_xattr(sxp);
737 +}
738 +
739 +/* ------------------------------------------------------------------------- */
740 +
741 +/* receive and build the rsync_xattr_lists */
742 +void receive_xattr(struct file_struct *file, int f)
743 +{
744 +       static item_list temp_xattr = EMPTY_ITEM_LIST;
745 +       int tag = read_byte(f);
746 +       char *ndx_ptr = (char*)file + file_struct_len;
747 +       int ndx;
748 +
749 +       if (tag == 'X') {
750 +               int i, count = read_int(f);
751 +               for (i = 0; i < count; i++) {
752 +                       char *ptr;
753 +                       rsync_xa *rxa;
754 +                       size_t name_len = read_int(f);
755 +                       size_t datum_len = read_int(f);
756 +                       if (name_len + datum_len < name_len)
757 +                               out_of_memory("receive_xattr"); /* overflow */
758 +                       rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
759 +                       ptr = new_array(char, name_len + datum_len);
760 +                       if (!ptr)
761 +                               out_of_memory("receive_xattr");
762 +                       read_buf(f, ptr, name_len);
763 +                       read_buf(f, ptr + name_len, datum_len);
764 +                       rxa->name_len = name_len;
765 +                       rxa->datum_len = datum_len;
766 +                       rxa->name = ptr;
767 +                       rxa->datum = ptr + name_len;
768 +               }
769 +               ndx = rsync_xal_l.count;
770 +               rsync_xal_store(&temp_xattr);
771 +       } else if (tag == 'x') {
772 +               ndx = read_int(f);
773 +               if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
774 +                       rprintf(FERROR, "%s: receive_xattr: xa index %d out of range\n",
775 +                               f_name(file, NULL), ndx);
776 +                       exit_cleanup(RERR_STREAMIO);
777 +               }
778 +       } else {
779 +               rprintf(FERROR,
780 +                   "%s: receive_xattr: unknown extended attribute type tag: %c\n",
781 +                   f_name(file, NULL), tag);
782 +               exit_cleanup(RERR_STREAMIO);
783 +               ndx = 0; /* silence a compiler warning... */
784 +       }
785 +
786 +       SIVAL(ndx_ptr, 0, ndx);
787 +}
788 +
789 +/* Turn the xattr data in statx into cached xattr data, setting the index
790 + * values in the file struct. */
791 +void cache_xattr(struct file_struct *file, statx *sxp)
792 +{
793 +       char *ndx_ptr = (char*)file + file_struct_len;
794 +       int ndx;
795 +
796 +       if (!sxp->xattr)
797 +               return;
798 +
799 +       ndx = find_matching_xattr(sxp->xattr);
800 +       if (ndx == -1)
801 +               rsync_xal_store(sxp->xattr);
802 +       free_xattr(sxp);
803 +
804 +       SIVAL(ndx_ptr, 0, ndx);
805 +}
806 +
807 +static int rsync_xal_set(const char *fname, item_list *xalp)
808 +{
809 +       rsync_xa *rxas = xalp->items;
810 +       size_t i;
811 +       int ret = 0;
812 +
813 +       for (i = 0; i < xalp->count; i++) {
814 +               int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len, 0);
815 +               if (status < 0) {
816 +                       rsyserr(FERROR, errno, "%s: rsync_xal_set: lsetxattr %s failed",
817 +                               fname, rxas[i].name);
818 +                       ret = -1;
819 +               }
820 +       }
821 +       return ret;
822 +}
823 +
824 +/* Set extended attributes on indicated filename. */
825 +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
826 +{
827 +       int ndx;
828 +       char *ndx_ptr = (char*)file + file_struct_len;
829 +       item_list *lst = rsync_xal_l.items;
830 +
831 +       if (dry_run)
832 +               return 1; /* FIXME: --dry-run needs to compute this value */
833 +
834 +       ndx = IVAL(ndx_ptr, 0);
835 +       return rsync_xal_set(fname, lst + ndx); /* TODO:  This needs to return 1 if no xattrs changed! */
836 +}
837 +
838 +#endif /* SUPPORT_XATTRS */