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,40 @@ 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_CHECK_HEADERS(sys/extattr.h)
155 +AC_MSG_CHECKING(whether to support extended attributes)
156 +AC_ARG_ENABLE(xattr-support,
157 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
158 +[ case "$enableval" in
162 + AC_MSG_RESULT(Using Linux xattrs)
163 + AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
166 + AC_MSG_RESULT(Using OS X xattrs)
167 + AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
170 + AC_MSG_RESULT(Using FreeBSD extattrs)
171 + AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs])
174 + AC_MSG_RESULT(Xattrs requested but not Linux or OS X. Good luck...)
180 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
183 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
186 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
191 @@ -41,6 +41,7 @@ extern int one_file_system;
192 extern int copy_dirlinks;
193 extern int keep_dirlinks;
194 extern int preserve_acls;
195 +extern int preserve_xattrs;
196 extern int preserve_links;
197 extern int preserve_hard_links;
198 extern int preserve_devices;
199 @@ -498,7 +499,7 @@ static struct file_struct *receive_file_
200 char thisname[MAXPATHLEN];
201 unsigned int l1 = 0, l2 = 0;
202 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
204 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
208 @@ -610,10 +611,16 @@ static struct file_struct *receive_file_
209 xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4;
212 +#elif defined SUPPORT_XATTRS
215 +#ifdef SUPPORT_XATTRS
216 + if (preserve_xattrs)
220 alloc_len = file_struct_len + dirname_len + basename_len
222 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
225 + linkname_len + sum_len;
226 @@ -622,7 +629,7 @@ static struct file_struct *receive_file_
227 file = (struct file_struct *)bp;
228 memset(bp, 0, file_struct_len);
229 bp += file_struct_len;
231 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
235 @@ -723,6 +730,10 @@ static struct file_struct *receive_file_
237 receive_acl(file, f);
239 +#ifdef SUPPORT_XATTRS
240 + if (preserve_xattrs)
241 + receive_xattr(file, f );
246 @@ -974,7 +985,7 @@ static struct file_struct *send_file_nam
247 unsigned short flags)
249 struct file_struct *file;
251 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
255 @@ -994,6 +1005,13 @@ static struct file_struct *send_file_nam
259 +#ifdef SUPPORT_XATTRS
260 + if (preserve_xattrs) {
262 + if (get_xattr(fname, &sx) < 0)
267 maybe_emit_filelist_progress(flist->count + flist_count_offset);
269 @@ -1006,11 +1024,19 @@ static struct file_struct *send_file_nam
273 +#ifdef SUPPORT_XATTRS
274 + if (preserve_xattrs)
275 + send_xattr(&sx, f);
282 +#ifdef SUPPORT_XATTRS
283 + if (preserve_xattrs)
289 --- old/lib/sysxattr.c
290 +++ new/lib/sysxattr.c
293 + * Extended attribute support for rsync.
295 + * Copyright (C) 2004 Red Hat, Inc.
296 + * Written by Jay Fenlason.
298 + * This program is free software; you can redistribute it and/or modify
299 + * it under the terms of the GNU General Public License as published by
300 + * the Free Software Foundation; either version 2 of the License, or
301 + * (at your option) any later version.
303 + * This program is distributed in the hope that it will be useful,
304 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
305 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
306 + * GNU General Public License for more details.
308 + * You should have received a copy of the GNU General Public License along
309 + * with this program; if not, write to the Free Software Foundation, Inc.,
310 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
314 +#include "sysxattr.h"
316 +#ifdef SUPPORT_XATTRS
318 +#if defined HAVE_LINUX_XATTRS
320 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
322 + return lgetxattr(path, name, value, size);
325 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
327 + return fgetxattr(filedes, name, value, size);
330 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
332 + return lsetxattr(path, name, value, size, 0);
335 +int sys_lremovexattr(const char *path, const char *name)
337 + return lremovexattr(path, name);
340 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
342 + return llistxattr(path, list, size);
345 +#elif HAVE_OSX_XATTRS
347 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
349 + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
352 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
354 + return fgetxattr(filedes, name, value, size, 0, 0);
357 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
359 + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
362 +int sys_lremovexattr(const char *path, const char *name)
364 + return removexattr(path, name, XATTR_NOFOLLOW);
367 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
369 + return listxattr(path, list, size, XATTR_NOFOLLOW);
372 +#elif HAVE_FREEBSD_XATTRS
374 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
376 + return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
379 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
381 + return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
384 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
386 + return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
389 +int sys_lremovexattr(const char *path, const char *name)
391 + return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
394 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
396 + unsigned char keylen;
397 + ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
399 + if (len <= 0 || (size_t)len > size)
402 + /* FreeBSD puts a single-byte length before each string, with no '\0'
403 + * terminator. We need to change this into a series of null-terminted
404 + * strings. Since the size is the same, we can simply transform the
405 + * output in place. */
406 + for (off = 0; off < len; ) {
407 + keylen = ((unsigned char*)list)[off];
408 + if (off + keylen >= len) {
409 + /* Should be impossible, but kernel bugs happen! */
413 + memmove(list+off, list+off+1, keylen);
415 + list[off++] = '\0';
423 +#error You need to create xattr compatibility functions.
427 +#endif /* SUPPORT_XATTRS */
428 --- old/lib/sysxattr.h
429 +++ new/lib/sysxattr.h
431 +#ifdef SUPPORT_XATTRS
433 +#if defined HAVE_ATTR_XATTR_H
434 +#include <attr/xattr.h>
435 +#elif defined HAVE_SYS_XATTR_H
436 +#include <sys/xattr.h>
437 +#elif defined HAVE_SYS_EXTATTR_H
438 +#include <sys/extattr.h>
441 +/* Linux 2.4 does not define this as a distinct errno value: */
443 +#define ENOATTR ENODATA
446 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
447 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
448 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
449 +int sys_lremovexattr(const char *path, const char *name);
450 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
454 +/* No xattrs available */
459 @@ -48,6 +48,7 @@ int copy_links = 0;
460 int preserve_links = 0;
461 int preserve_hard_links = 0;
462 int preserve_acls = 0;
463 +int preserve_xattrs = 0;
464 int preserve_perms = 0;
465 int preserve_executability = 0;
466 int preserve_devices = 0;
467 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
468 char const *have_inplace = "no ";
469 char const *hardlinks = "no ";
470 char const *acls = "no ";
471 + char const *xattrs = "no ";
472 char const *links = "no ";
473 char const *ipv6 = "no ";
474 STRUCT_STAT *dumstat;
475 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
480 +#ifdef SUPPORT_XATTRS
486 @@ -236,8 +240,8 @@ static void print_rsync_version(enum log
487 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, %shard links, %ssymlinks,\n",
488 (int) (sizeof (OFF_T) * 8), got_socketpair, hardlinks, links);
490 - rprintf(f, " batchfiles, %sinplace, %sIPv6, %sACLs,\n",
491 - have_inplace, ipv6, acls);
492 + rprintf(f, " batchfiles, %sinplace, %sIPv6, %sACLs, %sxattrs,\n",
493 + have_inplace, ipv6, acls, xattrs);
495 /* Note that this field may not have type ino_t. It depends
496 * on the complicated interaction between largefile feature
497 @@ -289,7 +293,7 @@ void usage(enum logcode F)
498 rprintf(F," -q, --quiet suppress non-error messages\n");
499 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
500 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
501 - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
502 + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
503 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
504 rprintf(F," -r, --recursive recurse into directories\n");
505 rprintf(F," -R, --relative use relative path names\n");
506 @@ -314,6 +318,9 @@ void usage(enum logcode F)
508 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
510 +#ifdef SUPPORT_XATTRS
511 + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
513 rprintf(F," -o, --owner preserve owner (super-user only)\n");
514 rprintf(F," -g, --group preserve group\n");
515 rprintf(F," --devices preserve device files (super-user only)\n");
516 @@ -436,6 +443,9 @@ static struct poptOption long_options[]
517 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
518 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
519 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
520 + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
521 + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
522 + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
523 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
524 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
525 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
526 @@ -1117,6 +1127,17 @@ int parse_arguments(int *argc, const cha
531 +#ifdef SUPPORT_XATTRS
532 + preserve_xattrs = 1;
533 + preserve_perms = 1;
536 + snprintf(err_buf,sizeof(err_buf),
537 + "extended attributes are not supported on this %s\n",
538 + am_server ? "server" : "client");
543 /* A large opt value means that set_refuse_options()
544 @@ -1563,6 +1584,10 @@ void server_options(char **args,int *arg
548 +#ifdef SUPPORT_XATTRS
549 + if (preserve_xattrs)
560 extern int preserve_acls;
561 +extern int preserve_xattrs;
562 extern int preserve_perms;
563 extern int preserve_executability;
564 extern int preserve_times;
565 @@ -218,6 +219,10 @@ int set_file_attrs(char *fname, struct f
566 if (daemon_chmod_modes && !S_ISLNK(new_mode))
567 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
569 +#ifdef SUPPORT_XATTRS
570 + if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
574 /* It's OK to call set_acl() now, even for a dir, as the generator
575 * will enable owner-writability using chmod, if necessary.
578 @@ -500,6 +500,10 @@ struct idev {
579 #define ACLS_NEED_MASK 1
582 +#ifndef HAVE_NO_XATTRS
583 +#define SUPPORT_XATTRS 1
586 #define GID_NONE ((gid_t)-1)
588 #define HL_CHECK_MASTER 0
589 @@ -694,6 +698,9 @@ typedef struct {
590 struct rsync_acl *acc_acl; /* access ACL */
591 struct rsync_acl *def_acl; /* default ACL */
593 +#ifdef SUPPORT_XATTRS
598 #define ACL_READY(sx) ((sx).acc_acl != NULL)
601 @@ -301,7 +301,7 @@ to the detailed description below for a
602 -q, --quiet suppress non-error messages
603 --no-motd suppress daemon-mode MOTD (see caveat)
604 -c, --checksum skip based on checksum, not mod-time & size
605 - -a, --archive archive mode; same as -rlptgoD (no -H, -A)
606 + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
607 --no-OPTION turn off an implied OPTION (e.g. --no-D)
608 -r, --recursive recurse into directories
609 -R, --relative use relative path names
610 @@ -324,6 +324,7 @@ to the detailed description below for a
611 -E, --executability preserve executability
612 --chmod=CHMOD affect file and/or directory permissions
613 -A, --acls preserve ACLs (implies -p) [non-standard]
614 + -X, --xattrs preserve extended attrs (implies -p) [n.s.]
615 -o, --owner preserve owner (super-user only)
616 -g, --group preserve group
617 --devices preserve device files (super-user only)
618 @@ -818,6 +819,11 @@ version makes it incompatible with sendi
619 rsync unless you double the bf(--acls) option (e.g. bf(-AA)). This
620 doubling is not needed when pulling files from an older rsync.
622 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
623 +extended attributes to be the same as the local ones. This will work
624 +only if the remote machine's rsync supports this option also. This is
625 +a non-standard option.
627 dit(bf(--chmod)) This option tells rsync to apply one or more
628 comma-separated "chmod" strings to the permission of the files in the
629 transfer. The resulting value is treated as though it was the permissions
634 + * Extended Attribute support for rsync.
635 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
637 + * Copyright (C) 2004 Red Hat, Inc.
638 + * Copyright (C) 2006 Wayne Davison
640 + * This program is free software; you can redistribute it and/or modify
641 + * it under the terms of the GNU General Public License as published by
642 + * the Free Software Foundation; either version 2 of the License, or
643 + * (at your option) any later version.
645 + * This program is distributed in the hope that it will be useful,
646 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
647 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
648 + * GNU General Public License for more details.
650 + * You should have received a copy of the GNU General Public License along
651 + * with this program; if not, write to the Free Software Foundation, Inc.,
652 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
656 +#include "lib/sysxattr.h"
658 +#ifdef SUPPORT_XATTRS
662 +extern int read_only;
663 +extern int list_only;
664 +extern unsigned int file_struct_len;
666 +#define RSYNC_XAL_INITIAL 5
667 +#define RSYNC_XAL_LIST_INITIAL 100
669 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
670 + && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
672 +#define USER_PREFIX "user."
673 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
674 +#define SYSTEM_PREFIX "system."
675 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
677 +#ifdef HAVE_LINUX_XATTRS
680 +#define RSYNC_PREFIX "rsync."
681 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
685 + char *datum, *name;
686 + size_t datum_len, name_len;
689 +static size_t namebuf_len = 0;
690 +static char *namebuf = NULL;
692 +static item_list empty_xattr = EMPTY_ITEM_LIST;
693 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
695 +/* ------------------------------------------------------------------------- */
697 +static void rsync_xal_free(item_list *xalp)
700 + rsync_xa *rxas = xalp->items;
702 + for (i = 0; i < xalp->count; i++) {
703 + free(rxas[i].datum);
704 + /*free(rxas[i].name);*/
709 +void free_xattr(statx *sxp)
711 + rsync_xal_free(sxp->xattr);
716 +static int rsync_xal_compare_names(const void *x1, const void *x2)
718 + const rsync_xa *xa1 = x1;
719 + const rsync_xa *xa2 = x2;
720 + return strcmp(xa1->name, xa2->name);
723 +static int rsync_xal_get(const char *fname, item_list *xalp)
725 + ssize_t list_len, name_len, datum_len;
729 + namebuf_len = 1024;
730 + namebuf = new_array(char, namebuf_len);
732 + out_of_memory("rsync_xal_get");
735 + /* The length returned includes all the '\0' terminators. */
736 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
737 + if (list_len > (ssize_t)namebuf_len) {
741 + if (list_len < 0) {
742 + if (errno == ENOTSUP)
744 + if (errno == ERANGE) {
745 + list_len = sys_llistxattr(fname, NULL, 0);
746 + if (list_len < 0) {
747 + rsyserr(FERROR, errno,
748 + "rsync_xal_get: llistxattr(\"%s\",0) failed",
752 + namebuf = realloc_array(namebuf, char, list_len + 1024);
754 + out_of_memory("rsync_xal_get");
755 + namebuf_len = list_len + 1024;
756 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
757 + if (list_len < 0) {
758 + rsyserr(FERROR, errno,
759 + "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
760 + fname, (long)namebuf_len);
764 + rsyserr(FERROR, errno,
765 + "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
766 + fname, (long)namebuf_len);
771 + for (name = namebuf; list_len > 0; name += name_len) {
774 + name_len = strlen(name) + 1;
775 + list_len -= name_len;
777 +#ifdef HAVE_LINUX_XATTRS
778 + /* We don't send the system namespace. */
779 + if (HAS_PREFIX(name, SYSTEM_PREFIX))
783 + datum_len = sys_lgetxattr(fname, name, NULL, 0);
784 + if (datum_len < 0) {
785 + if (errno == ENOTSUP)
787 + rsyserr(FERROR, errno,
788 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",0) failed",
792 + ptr = new_array(char, name_len + datum_len);
794 + out_of_memory("rsync_xal_get");
796 + ssize_t len = sys_lgetxattr(fname, name, ptr, datum_len);
797 + if (len != datum_len) {
799 + rsyserr(FERROR, errno,
800 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
801 + " failed", fname, name, (long)datum_len);
804 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
805 + " returned %ld\n", fname, name,
806 + (long)datum_len, (long)len);
812 + rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
813 + rxas->name = ptr + datum_len;
815 + rxas->name_len = name_len;
816 + rxas->datum_len = datum_len;
817 + memcpy(rxas->name, name, name_len);
819 + if (xalp->count > 1)
820 + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
824 +/* Read the xattr(s) for this filename. */
825 +int get_xattr(const char *fname, statx *sxp)
827 + sxp->xattr = new(item_list);
828 + *sxp->xattr = empty_xattr;
829 + if (rsync_xal_get(fname, sxp->xattr) < 0) {
836 +static int find_matching_xattr(item_list *xalp)
839 + item_list *lst = rsync_xal_l.items;
841 + for (i = 0; i < rsync_xal_l.count; i++) {
842 + rsync_xa *rxas1 = lst[i].items;
843 + rsync_xa *rxas2 = xalp->items;
845 + /* Wrong number of elements? */
846 + if (lst[i].count != xalp->count)
848 + /* any elements different? */
849 + for (j = 0; j < xalp->count; j++) {
850 + if (rxas1[j].name_len != rxas2[j].name_len
851 + || rxas1[j].datum_len != rxas2[j].datum_len
852 + || strcmp(rxas1[j].name, rxas2[j].name)
853 + || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
856 + /* no differences found. This is The One! */
857 + if (j == xalp->count)
864 +/* Store *xalp on the end of rsync_xal_l */
865 +static void rsync_xal_store(item_list *xalp)
867 + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
868 + /* Since the following call starts a new list, we know it will hold the
869 + * entire initial-count, not just enough space for one new item. */
870 + *new_lst = empty_xattr;
871 + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
872 + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
873 + new_lst->count = xalp->count;
877 +/* Send the make_xattr()-generated xattr list for this flist entry. */
878 +void send_xattr(statx *sxp, int f)
880 + int ndx = find_matching_xattr(sxp->xattr);
882 + write_byte(f, 'x');
884 + rsync_xal_free(sxp->xattr);
887 + int count = sxp->xattr->count;
888 + write_byte(f, 'X');
889 + write_int(f, count);
890 + for (rxa = sxp->xattr->items; count--; rxa++) {
891 +#ifdef HAVE_LINUX_XATTRS
892 + write_int(f, rxa->name_len);
893 + write_int(f, rxa->datum_len);
894 + write_buf(f, rxa->name, rxa->name_len);
896 + /* We strip the rsync prefix from disguised namespaces
897 + * and put everything else in the user namespace. */
898 + if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
899 + && rxa->name[RPRE_LEN] != '%') {
900 + write_int(f, rxa->name_len - RPRE_LEN);
901 + write_int(f, rxa->datum_len);
902 + write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
904 + write_int(f, rxa->name_len + UPRE_LEN);
905 + write_int(f, rxa->datum_len);
906 + write_buf(f, USER_PREFIX, UPRE_LEN);
907 + write_buf(f, rxa->name, rxa->name_len);
910 + write_buf(f, rxa->datum, rxa->datum_len);
912 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
917 +/* ------------------------------------------------------------------------- */
919 +/* receive and build the rsync_xattr_lists */
920 +void receive_xattr(struct file_struct *file, int f)
922 + static item_list temp_xattr = EMPTY_ITEM_LIST;
923 + char *ndx_ptr = (char*)file + file_struct_len;
924 + int ndx, tag = read_byte(f);
927 + int i, count = read_int(f);
928 + for (i = 0; i < count; i++) {
931 + size_t name_len = read_int(f);
932 + size_t datum_len = read_int(f);
933 + size_t extra_len = am_root < 0 ? RPRE_LEN : 0;
934 +#ifndef HAVE_LINUX_XATTRS
935 + if (datum_len + extra_len < datum_len)
936 + out_of_memory("receive_xattr"); /* overflow */
938 + if (name_len + datum_len + extra_len < name_len)
939 + out_of_memory("receive_xattr"); /* overflow */
940 + ptr = new_array(char, name_len + datum_len + extra_len);
942 + out_of_memory("receive_xattr");
943 + name = ptr + datum_len + extra_len;
944 + read_buf(f, name, name_len);
945 + read_buf(f, ptr, datum_len);
946 +#ifdef HAVE_LINUX_XATTRS
947 + /* Non-root can only save the user namespace. */
948 + if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
953 + /* This OS only has a user namespace, so we either
954 + * strip the user prefix, or we put a non-user
955 + * namespace inside our rsync hierarchy. */
956 + if (HAS_PREFIX(name, USER_PREFIX)) {
958 + name_len -= UPRE_LEN;
959 + } else if (am_root) {
961 + name_len += RPRE_LEN;
962 + memcpy(name, RSYNC_PREFIX, RPRE_LEN);
968 + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
971 + rxa->name_len = name_len;
972 + rxa->datum_len = datum_len;
974 + ndx = rsync_xal_l.count; /* pre-incremented count */
975 + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
976 + } else if (tag == 'x') {
978 + if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
979 + rprintf(FERROR, "receive_xattr: xa index %d out of"
980 + " range for %s\n", ndx, f_name(file, NULL));
981 + exit_cleanup(RERR_STREAMIO);
984 + rprintf(FERROR, "receive_xattr: unknown extended attribute"
985 + " type tag (%c) for %s\n", tag, f_name(file, NULL));
986 + exit_cleanup(RERR_STREAMIO);
989 + SIVAL(ndx_ptr, 0, ndx);
992 +/* Turn the xattr data in statx into cached xattr data, setting the index
993 + * values in the file struct. */
994 +void cache_xattr(struct file_struct *file, statx *sxp)
996 + char *ndx_ptr = (char*)file + file_struct_len;
1002 + ndx = find_matching_xattr(sxp->xattr);
1004 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1007 + SIVAL(ndx_ptr, 0, ndx);
1010 +static int rsync_xal_set(const char *fname, item_list *xalp)
1012 + rsync_xa *rxas = xalp->items;
1016 + for (i = 0; i < xalp->count; i++) {
1017 + int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len);
1019 + rsyserr(FERROR, errno,
1020 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1021 + fname, rxas[i].name);
1028 +/* Set extended attributes on indicated filename. */
1029 +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
1032 + char *ndx_ptr = (char*)file + file_struct_len;
1033 + item_list *lst = rsync_xal_l.items;
1036 + return 1; /* FIXME: --dry-run needs to compute this value */
1038 + if (read_only || list_only) {
1043 + ndx = IVAL(ndx_ptr, 0);
1044 + return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
1047 +#endif /* SUPPORT_XATTRS */