1 This patch adds basic support for extended attributes. It works, but there
2 are some things that still needs to be improved (see TODO list below).
4 To use this patch, run these commands for a successful build:
6 patch -p1 <patches/acls.diff
7 patch -p1 <patches/xattrs.diff
9 ./configure --enable-acl-support --enable-xattr-support
12 Alternately, if you don't want ACL support, configure it this way:
14 ./configure --enable-xattr-support
18 - This patch needs to more efficiently handle large xattrs, especially when
21 - Extraneous xattr values need to be removed from files that are not being
24 - We need to affect the itemized output to know when xattrs are being updated.
26 - We need to affect the --link-dest option to avoid hard-linking two files
27 that differ in their xattrs (when --xattrs was specified).
31 @@ -28,13 +28,13 @@ VERSION=@VERSION@
33 HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
34 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
35 - lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
36 + lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattr.o @LIBOBJS@
37 ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
38 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
39 OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
40 main.o checksum.o match.o syscall.o log.o backup.o
41 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
42 - fileio.o batch.o clientname.o chmod.o acls.o
43 + fileio.o batch.o clientname.o chmod.o acls.o xattr.o
44 OBJS3=progress.o pipe.o
45 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
46 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
49 @@ -30,6 +30,7 @@ extern int read_only;
51 extern int orig_umask;
52 extern int preserve_acls;
53 +extern int preserve_xattrs;
54 extern unsigned int file_struct_len;
56 /* === ACL structures === */
57 @@ -741,6 +742,10 @@ void receive_acl(struct file_struct *fil
58 type = SMB_ACL_TYPE_ACCESS;
59 racl_list = &access_acl_list;
60 ndx_ptr = (char*)file + file_struct_len;
61 +#ifdef SUPPORT_XATTRS
62 + if (preserve_xattrs)
66 char tag = read_byte(f);
68 @@ -800,6 +805,10 @@ void cache_acl(struct file_struct *file,
70 racl_list = &access_acl_list;
71 ndx_ptr = (char*)file + file_struct_len;
72 +#ifdef SUPPORT_XATTRS
73 + if (preserve_xattrs)
79 @@ -920,6 +929,10 @@ int set_acl(const char *fname, const str
81 type = SMB_ACL_TYPE_ACCESS;
82 ndx_ptr = (char*)file + file_struct_len;
83 +#ifdef SUPPORT_XATTRS
84 + if (preserve_xattrs)
92 @@ -30,6 +30,7 @@ extern char *backup_dir;
95 extern int preserve_acls;
96 +extern int preserve_xattrs;
97 extern int preserve_devices;
98 extern int preserve_specials;
99 extern int preserve_links;
100 @@ -136,6 +137,9 @@ static int make_bak_dir(char *fullpath)
102 sx.acc_acl = sx.def_acl = NULL;
104 +#ifdef SUPPORT_XATTRS
107 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
110 @@ -144,6 +148,12 @@ static int make_bak_dir(char *fullpath)
111 cache_acl(file, &sx);
114 +#ifdef SUPPORT_XATTRS
115 + if (preserve_xattrs) {
116 + get_xattr(rel, &sx);
117 + cache_xattr(file, &sx);
120 set_file_attrs(fullpath, file, NULL, 0);
123 @@ -195,6 +205,9 @@ static int keep_backup(char *fname)
125 sx.acc_acl = sx.def_acl = NULL;
127 +#ifdef SUPPORT_XATTRS
131 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
132 return 1; /* the file could have disappeared */
133 @@ -208,6 +221,12 @@ static int keep_backup(char *fname)
134 cache_acl(file, &sx);
137 +#ifdef SUPPORT_XATTRS
138 + if (preserve_xattrs) {
139 + get_xattr(fname, &sx);
140 + cache_xattr(file, &sx);
144 /* Check to see if this is a device file, or link */
145 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
148 @@ -856,6 +856,35 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
152 +AC_CHECK_HEADERS(attr/xattr.h)
153 +AC_CHECK_HEADERS(sys/xattr.h)
154 +AC_MSG_CHECKING(whether to support extended attributes)
155 +AC_ARG_ENABLE(xattr-support,
156 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
157 +[ case "$enableval" in
161 + AC_MSG_RESULT(Using Linux xattrs)
162 + AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
165 + AC_MSG_RESULT(Using OS X xattrs)
166 + AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
169 + AC_MSG_RESULT(Xattrs requested but not Linux or OS X. Good luck...)
175 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
178 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
181 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
186 @@ -41,6 +41,7 @@ extern int one_file_system;
187 extern int copy_dirlinks;
188 extern int keep_dirlinks;
189 extern int preserve_acls;
190 +extern int preserve_xattrs;
191 extern int preserve_links;
192 extern int preserve_hard_links;
193 extern int preserve_devices;
194 @@ -498,7 +499,7 @@ static struct file_struct *receive_file_
195 char thisname[MAXPATHLEN];
196 unsigned int l1 = 0, l2 = 0;
197 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
199 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
203 @@ -610,10 +611,16 @@ static struct file_struct *receive_file_
204 xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4;
207 +#elif defined SUPPORT_XATTRS
210 +#ifdef SUPPORT_XATTRS
211 + if (preserve_xattrs)
215 alloc_len = file_struct_len + dirname_len + basename_len
217 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
220 + linkname_len + sum_len;
221 @@ -622,7 +629,7 @@ static struct file_struct *receive_file_
222 file = (struct file_struct *)bp;
223 memset(bp, 0, file_struct_len);
224 bp += file_struct_len;
226 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
230 @@ -723,6 +730,10 @@ static struct file_struct *receive_file_
232 receive_acl(file, f);
234 +#ifdef SUPPORT_XATTRS
235 + if (preserve_xattrs)
236 + receive_xattr(file, f );
241 @@ -974,7 +985,7 @@ static struct file_struct *send_file_nam
242 unsigned short flags)
244 struct file_struct *file;
246 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
250 @@ -994,6 +1005,13 @@ static struct file_struct *send_file_nam
254 +#ifdef SUPPORT_XATTRS
255 + if (preserve_xattrs) {
257 + if (get_xattr(fname, &sx) < 0)
262 maybe_emit_filelist_progress(flist->count + flist_count_offset);
264 @@ -1006,11 +1024,19 @@ static struct file_struct *send_file_nam
268 +#ifdef SUPPORT_XATTRS
269 + if (preserve_xattrs)
270 + send_xattr(&sx, f);
277 +#ifdef SUPPORT_XATTRS
278 + if (preserve_xattrs)
284 --- old/lib/sysxattr.c
285 +++ new/lib/sysxattr.c
288 + * Extended attribute support for rsync.
290 + * Copyright (C) 2004 Red Hat, Inc.
291 + * Written by Jay Fenlason.
293 + * This program is free software; you can redistribute it and/or modify
294 + * it under the terms of the GNU General Public License as published by
295 + * the Free Software Foundation; either version 2 of the License, or
296 + * (at your option) any later version.
298 + * This program is distributed in the hope that it will be useful,
299 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
300 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
301 + * GNU General Public License for more details.
303 + * You should have received a copy of the GNU General Public License along
304 + * with this program; if not, write to the Free Software Foundation, Inc.,
305 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
309 +#include "sysxattr.h"
311 +#ifdef SUPPORT_XATTRS
313 +#if defined HAVE_LINUX_XATTRS
315 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
317 + return lgetxattr(path, name, value, size);
320 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
322 + return fgetxattr(filedes, name, value, size);
325 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
327 + return lsetxattr(path, name, value, size, flags);
330 +int sys_lremovexattr(const char *path, const char *name)
332 + return lremovexattr(path, name);
335 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
337 + return llistxattr(path, list, size);
340 +#elif HAVE_OSX_XATTRS
342 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
344 + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
347 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
349 + return fgetxattr(filedes, name, value, size, 0, 0);
352 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
354 + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW | flags);
357 +int sys_lremovexattr(const char *path, const char *name)
359 + return removexattr(path, name, XATTR_NOFOLLOW);
362 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
364 + return listxattr(path, list, size, XATTR_NOFOLLOW);
369 +#error You need to create xattr compatibility functions.
373 +#endif /* SUPPORT_XATTRS */
374 --- old/lib/sysxattr.h
375 +++ new/lib/sysxattr.h
377 +#ifdef SUPPORT_XATTRS
379 +#if defined HAVE_ATTR_XATTR_H
380 +#include <attr/xattr.h>
381 +#elif defined HAVE_SYS_XATTR_H
382 +#include <sys/xattr.h>
385 +/* Linux 2.4 does not define this as a distinct errno value: */
387 +#define ENOATTR ENODATA
390 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
391 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
392 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags);
393 +int sys_lremovexattr(const char *path, const char *name);
394 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
398 +/* No xattrs available */
403 @@ -48,6 +48,7 @@ int copy_links = 0;
404 int preserve_links = 0;
405 int preserve_hard_links = 0;
406 int preserve_acls = 0;
407 +int preserve_xattrs = 0;
408 int preserve_perms = 0;
409 int preserve_executability = 0;
410 int preserve_devices = 0;
411 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
412 char const *have_inplace = "no ";
413 char const *hardlinks = "no ";
414 char const *acls = "no ";
415 + char const *xattrs = "no ";
416 char const *links = "no ";
417 char const *ipv6 = "no ";
418 STRUCT_STAT *dumstat;
419 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
424 +#ifdef SUPPORT_XATTRS
430 @@ -236,8 +240,8 @@ static void print_rsync_version(enum log
431 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, %shard links, %ssymlinks,\n",
432 (int) (sizeof (OFF_T) * 8), got_socketpair, hardlinks, links);
434 - rprintf(f, " batchfiles, %sinplace, %sIPv6, %sACLs,\n",
435 - have_inplace, ipv6, acls);
436 + rprintf(f, " batchfiles, %sinplace, %sIPv6, %sACLs, %sxattrs,\n",
437 + have_inplace, ipv6, acls, xattrs);
439 /* Note that this field may not have type ino_t. It depends
440 * on the complicated interaction between largefile feature
441 @@ -289,7 +293,7 @@ void usage(enum logcode F)
442 rprintf(F," -q, --quiet suppress non-error messages\n");
443 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
444 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
445 - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
446 + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
447 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
448 rprintf(F," -r, --recursive recurse into directories\n");
449 rprintf(F," -R, --relative use relative path names\n");
450 @@ -314,6 +318,9 @@ void usage(enum logcode F)
452 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
454 +#ifdef SUPPORT_XATTRS
455 + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
457 rprintf(F," -o, --owner preserve owner (super-user only)\n");
458 rprintf(F," -g, --group preserve group\n");
459 rprintf(F," --devices preserve device files (super-user only)\n");
460 @@ -436,6 +443,9 @@ static struct poptOption long_options[]
461 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
462 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
463 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
464 + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
465 + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
466 + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
467 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
468 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
469 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
470 @@ -1117,6 +1127,17 @@ int parse_arguments(int *argc, const cha
475 +#ifdef SUPPORT_XATTRS
476 + preserve_xattrs = 1;
477 + preserve_perms = 1;
480 + snprintf(err_buf,sizeof(err_buf),
481 + "extended attributes are not supported on this %s\n",
482 + am_server ? "server" : "client");
487 /* A large opt value means that set_refuse_options()
488 @@ -1563,6 +1584,10 @@ void server_options(char **args,int *arg
492 +#ifdef SUPPORT_XATTRS
493 + if (preserve_xattrs)
504 extern int preserve_acls;
505 +extern int preserve_xattrs;
506 extern int preserve_perms;
507 extern int preserve_executability;
508 extern int preserve_times;
509 @@ -218,6 +219,10 @@ int set_file_attrs(char *fname, struct f
510 if (daemon_chmod_modes && !S_ISLNK(new_mode))
511 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
513 +#ifdef SUPPORT_XATTRS
514 + if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
518 /* It's OK to call set_acl() now, even for a dir, as the generator
519 * will enable owner-writability using chmod, if necessary.
522 @@ -500,6 +500,10 @@ struct idev {
523 #define ACLS_NEED_MASK 1
526 +#ifndef HAVE_NO_XATTRS
527 +#define SUPPORT_XATTRS 1
530 #define GID_NONE ((gid_t)-1)
532 #define HL_CHECK_MASTER 0
533 @@ -694,6 +698,9 @@ typedef struct {
534 struct rsync_acl *acc_acl; /* access ACL */
535 struct rsync_acl *def_acl; /* default ACL */
537 +#ifdef SUPPORT_XATTRS
542 #define ACL_READY(sx) ((sx).acc_acl != NULL)
545 @@ -301,7 +301,7 @@ to the detailed description below for a
546 -q, --quiet suppress non-error messages
547 --no-motd suppress daemon-mode MOTD (see caveat)
548 -c, --checksum skip based on checksum, not mod-time & size
549 - -a, --archive archive mode; same as -rlptgoD (no -H, -A)
550 + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
551 --no-OPTION turn off an implied OPTION (e.g. --no-D)
552 -r, --recursive recurse into directories
553 -R, --relative use relative path names
554 @@ -324,6 +324,7 @@ to the detailed description below for a
555 -E, --executability preserve executability
556 --chmod=CHMOD affect file and/or directory permissions
557 -A, --acls preserve ACLs (implies -p) [non-standard]
558 + -X, --xattrs preserve extended attrs (implies -p) [n.s.]
559 -o, --owner preserve owner (super-user only)
560 -g, --group preserve group
561 --devices preserve device files (super-user only)
562 @@ -818,6 +819,11 @@ version makes it incompatible with sendi
563 rsync unless you double the bf(--acls) option (e.g. bf(-AA)). This
564 doubling is not needed when pulling files from an older rsync.
566 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
567 +extended attributes to be the same as the local ones. This will work
568 +only if the remote machine's rsync supports this option also. This is
569 +a non-standard option.
571 dit(bf(--chmod)) This option tells rsync to apply one or more
572 comma-separated "chmod" strings to the permission of the files in the
573 transfer. The resulting value is treated as though it was the permissions
578 + * Extended Attribute support for rsync.
579 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
581 + * Copyright (C) 2004 Red Hat, Inc.
582 + * Copyright (C) 2006 Wayne Davison
584 + * This program is free software; you can redistribute it and/or modify
585 + * it under the terms of the GNU General Public License as published by
586 + * the Free Software Foundation; either version 2 of the License, or
587 + * (at your option) any later version.
589 + * This program is distributed in the hope that it will be useful,
590 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
591 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
592 + * GNU General Public License for more details.
594 + * You should have received a copy of the GNU General Public License along
595 + * with this program; if not, write to the Free Software Foundation, Inc.,
596 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
600 +#include "lib/sysxattr.h"
602 +#ifdef SUPPORT_XATTRS
605 +extern int read_only;
606 +extern int list_only;
607 +extern unsigned int file_struct_len;
609 +#define RSYNC_XAL_INITIAL 5
610 +#define RSYNC_XAL_LIST_INITIAL 100
619 +static size_t namebuf_len = 0;
620 +static char *namebuf = NULL;
622 +static item_list empty_xattr = EMPTY_ITEM_LIST;
623 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
625 +#ifdef HAVE_OSX_XATTRS
626 +#define UNIQUE_PREFIX "user.0S%." /* OSX */
627 +#define UPRE_LEN (sizeof UNIQUE_PREFIX - 1)
630 +/* ------------------------------------------------------------------------- */
632 +static void rsync_xal_free(item_list *xalp)
635 + rsync_xa *rxas = xalp->items;
637 + for (i = 0; i < xalp->count; i++) {
638 + free(rxas[i].name);
639 + /* free(rxas[i].value); */
644 +void free_xattr(statx *sxp)
646 + rsync_xal_free(sxp->xattr);
651 +static int rsync_xal_compare_names(const void *x1, const void *x2)
653 + const rsync_xa *xa1 = x1;
654 + const rsync_xa *xa2 = x2;
655 + return strcmp(xa1->name, xa2->name);
658 +static int rsync_xal_get(const char *fname, item_list *xalp)
661 + ssize_t datum_size;
668 + namebuf_len = 1024;
669 + namebuf = new_array(char, namebuf_len);
671 + out_of_memory("rsync_xal_get");
674 + /* The length returned includes all the '\0' terminators. */
675 + name_size = sys_llistxattr(fname, namebuf, namebuf_len);
676 + if (name_size > (ssize_t)namebuf_len) {
680 + if (name_size < 0) {
681 + if (errno == ENOTSUP)
683 + if (errno == ERANGE) {
684 + name_size = sys_llistxattr(fname, NULL, 0);
685 + if (name_size < 0) {
686 + rsyserr(FERROR, errno, "%s: rsync_xal_get: llistxattr",
690 + namebuf = realloc_array(namebuf, char, name_size + 1024);
692 + out_of_memory("rsync_xal_get");
693 + namebuf_len = name_size + 1024;
694 + name_size = sys_llistxattr(fname, namebuf, namebuf_len);
695 + if (name_size < 0) {
696 + rsyserr(FERROR, errno,
697 + "%s: rsync_xal_get: re-llistxattr failed",
702 + rsyserr(FERROR, errno,
703 + "%s: rsync_xal_get: llistxattr failed:",
708 + if (name_size == 0)
710 + for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
711 + rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
713 + len = strlen(name) + 1;
714 + datum_size = sys_lgetxattr(fname, name, NULL, 0);
715 + if (datum_size < 0) {
716 + if (errno == ENOTSUP)
718 + rsyserr(FERROR, errno,
719 + "%s: rsync_xal_get: lgetxattr %s failed",
723 + ptr = new_array(char, len + datum_size);
725 + out_of_memory("rsync_xal_get");
726 + memcpy(ptr, name, len);
727 + rxas->name_len = len;
729 + rxas->datum_len = datum_size;
730 + rxas->datum = ptr + len;
732 + ssize_t size = sys_lgetxattr(fname, name, rxas->datum, datum_size);
733 + if (size != datum_size) {
735 + rsyserr(FERROR, errno,
736 + "rsync_xal_get: lgetxattr(%s,%s)"
737 + " failed", fname, name);
740 + "rsync_xal_get: lgetxattr(%s,%s)"
741 + " returned %ld instead of %ld\n",
743 + (long)size, (long)datum_size);
749 + if (xalp->count > 1)
750 + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
754 +/* Read the xattr(s) for this filename. */
755 +int get_xattr(const char *fname, statx *sxp)
757 + sxp->xattr = new(item_list);
758 + *sxp->xattr = empty_xattr;
759 + if (rsync_xal_get(fname, sxp->xattr) < 0) {
766 +static int find_matching_xattr(item_list *xalp)
769 + item_list *lst = rsync_xal_l.items;
771 + for (i = 0; i < rsync_xal_l.count; i++) {
772 + rsync_xa *rxas1 = lst[i].items;
773 + rsync_xa *rxas2 = xalp->items;
775 + /* Wrong number of elements? */
776 + if (lst[i].count != xalp->count)
778 + /* any elements different? */
779 + for (j = 0; j < xalp->count; j++) {
780 + if (rxas1[j].name_len != rxas2[j].name_len
781 + || rxas1[j].datum_len != rxas2[j].datum_len
782 + || strcmp(rxas1[j].name, rxas2[j].name)
783 + || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
786 + /* no differences found. This is The One! */
787 + if (j == xalp->count)
794 +/* Store *xalp on the end of rsync_xal_l */
795 +static void rsync_xal_store(item_list *xalp)
797 + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
798 + /* Since the following call starts a new list, we know it will hold the
799 + * entire initial-count, not just enough space for one new item. */
800 + *new_lst = empty_xattr;
801 + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
802 + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
803 + new_lst->count = xalp->count;
807 +/* Send the make_xattr()-generated xattr list for this flist entry. */
808 +void send_xattr(statx *sxp, int f)
810 + int ndx = find_matching_xattr(sxp->xattr);
812 + write_byte(f, 'x');
814 + rsync_xal_free(sxp->xattr);
817 + int count = sxp->xattr->count;
818 + write_byte(f, 'X');
819 + write_int(f, count);
820 + for (rxa = sxp->xattr->items; count--; rxa++) {
821 +#ifdef HAVE_OSX_XATTRS
822 + if (strncmp(rxa->name, "user.", 5) != 0
823 + && strncmp(rxa->name, "system.", 7) != 0) {
824 + write_int(f, rxa->name_len + UPRE_LEN);
825 + write_int(f, rxa->datum_len);
826 + write_buf(f, UNIQUE_PREFIX, UPRE_LEN);
830 + write_int(f, rxa->name_len);
831 + write_int(f, rxa->datum_len);
833 + write_buf(f, rxa->name, rxa->name_len);
834 + write_buf(f, rxa->datum, rxa->datum_len);
836 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
841 +/* ------------------------------------------------------------------------- */
843 +/* receive and build the rsync_xattr_lists */
844 +void receive_xattr(struct file_struct *file, int f)
846 + static item_list temp_xattr = EMPTY_ITEM_LIST;
847 + int tag = read_byte(f);
848 + char *ndx_ptr = (char*)file + file_struct_len;
852 + int i, count = read_int(f);
853 + for (i = 0; i < count; i++) {
856 + size_t name_len = read_int(f);
857 + size_t datum_len = read_int(f);
858 + if (name_len + datum_len < name_len)
859 + out_of_memory("receive_xattr"); /* overflow */
860 + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
861 + ptr = new_array(char, name_len + datum_len);
863 + out_of_memory("receive_xattr");
864 + read_buf(f, ptr, name_len);
865 + read_buf(f, ptr + name_len, datum_len);
866 + rxa->name_len = name_len;
867 + rxa->datum_len = datum_len;
869 + rxa->datum = ptr + name_len;
870 +#ifdef HAVE_OSX_XATTRS
871 + if (strncmp(rxa->name, UNIQUE_PREFIX, UPRE_LEN) == 0) {
872 + rxa->name_len -= UPRE_LEN;
873 + memmove(rxa->name, rxa->name + UPRE_LEN, rxa->name_len);
877 + ndx = rsync_xal_l.count; /* pre-incremented count */
878 + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
879 + } else if (tag == 'x') {
881 + if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
882 + rprintf(FERROR, "%s: receive_xattr: xa index %d out of range\n",
883 + f_name(file, NULL), ndx);
884 + exit_cleanup(RERR_STREAMIO);
888 + "%s: receive_xattr: unknown extended attribute type tag: %c\n",
889 + f_name(file, NULL), tag);
890 + exit_cleanup(RERR_STREAMIO);
891 + ndx = 0; /* silence a compiler warning... */
894 + SIVAL(ndx_ptr, 0, ndx);
897 +/* Turn the xattr data in statx into cached xattr data, setting the index
898 + * values in the file struct. */
899 +void cache_xattr(struct file_struct *file, statx *sxp)
901 + char *ndx_ptr = (char*)file + file_struct_len;
907 + ndx = find_matching_xattr(sxp->xattr);
909 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
912 + SIVAL(ndx_ptr, 0, ndx);
915 +static int rsync_xal_set(const char *fname, item_list *xalp)
917 + rsync_xa *rxas = xalp->items;
921 + for (i = 0; i < xalp->count; i++) {
922 + int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len, 0);
924 + rsyserr(FERROR, errno, "%s: rsync_xal_set: lsetxattr %s failed",
925 + fname, rxas[i].name);
932 +/* Set extended attributes on indicated filename. */
933 +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
936 + char *ndx_ptr = (char*)file + file_struct_len;
937 + item_list *lst = rsync_xal_l.items;
940 + return 1; /* FIXME: --dry-run needs to compute this value */
942 + if (read_only || list_only) {
947 + ndx = IVAL(ndx_ptr, 0);
948 + return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
951 +#endif /* SUPPORT_XATTRS */