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