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=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
40 util.o main.o checksum.o match.o syscall.o log.o backup.o
41 OBJS2=options.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 \
52 extern int preserve_acls;
53 +extern int preserve_xattrs;
54 extern int preserve_devices;
55 extern int preserve_specials;
56 extern int preserve_links;
57 @@ -135,6 +136,9 @@ static int make_bak_dir(char *fullpath)
59 sx.acc_acl = sx.def_acl = NULL;
61 +#ifdef SUPPORT_XATTRS
64 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
67 @@ -143,6 +147,12 @@ static int make_bak_dir(char *fullpath)
71 +#ifdef SUPPORT_XATTRS
72 + if (preserve_xattrs) {
73 + get_xattr(rel, &sx);
74 + cache_xattr(file, &sx);
77 set_file_attrs(fullpath, file, NULL, 0);
80 @@ -194,6 +204,9 @@ static int keep_backup(const char *fname
82 sx.acc_acl = sx.def_acl = NULL;
84 +#ifdef SUPPORT_XATTRS
88 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
89 return 1; /* the file could have disappeared */
90 @@ -209,6 +222,12 @@ static int keep_backup(const char *fname
94 +#ifdef SUPPORT_XATTRS
95 + if (preserve_xattrs) {
96 + get_xattr(fname, &sx);
97 + cache_xattr(file, &sx);
101 /* Check to see if this is a device file, or link */
102 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
105 @@ -878,6 +878,40 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
109 +AC_CHECK_HEADERS(attr/xattr.h)
110 +AC_CHECK_HEADERS(sys/xattr.h)
111 +AC_CHECK_HEADERS(sys/extattr.h)
112 +AC_MSG_CHECKING(whether to support extended attributes)
113 +AC_ARG_ENABLE(xattr-support,
114 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
115 +[ case "$enableval" in
119 + AC_MSG_RESULT(Using Linux xattrs)
120 + AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
123 + AC_MSG_RESULT(Using OS X xattrs)
124 + AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
127 + AC_MSG_RESULT(Using FreeBSD extattrs)
128 + AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs])
131 + AC_MSG_RESULT(Xattrs requested but not Linux or OS X. Good luck...)
137 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
140 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
143 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
148 @@ -42,6 +42,7 @@ extern int one_file_system;
149 extern int copy_dirlinks;
150 extern int keep_dirlinks;
151 extern int preserve_acls;
152 +extern int preserve_xattrs;
153 extern int preserve_links;
154 extern int preserve_hard_links;
155 extern int preserve_devices;
156 @@ -806,6 +807,10 @@ static struct file_struct *recv_file_ent
158 receive_acl(file, f);
160 +#ifdef SUPPORT_XATTRS
161 + if (preserve_xattrs)
162 + receive_xattr(file, f );
167 @@ -1076,7 +1081,7 @@ static struct file_struct *send_file_nam
168 unsigned short flags)
170 struct file_struct *file;
172 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
176 @@ -1096,6 +1101,13 @@ static struct file_struct *send_file_nam
180 +#ifdef SUPPORT_XATTRS
181 + if (preserve_xattrs && f >= 0) {
183 + if (get_xattr(fname, &sx) < 0)
188 maybe_emit_filelist_progress(flist->count + flist_count_offset);
190 @@ -1107,6 +1119,10 @@ static struct file_struct *send_file_nam
194 +#ifdef SUPPORT_XATTRS
195 + if (preserve_xattrs)
196 + send_xattr(&sx, f);
201 --- old/lib/sysxattr.c
202 +++ new/lib/sysxattr.c
205 + * Extended attribute support for rsync.
207 + * Copyright (C) 2004 Red Hat, Inc.
208 + * Written by Jay Fenlason.
210 + * This program is free software; you can redistribute it and/or modify
211 + * it under the terms of the GNU General Public License as published by
212 + * the Free Software Foundation; either version 2 of the License, or
213 + * (at your option) any later version.
215 + * This program is distributed in the hope that it will be useful,
216 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
217 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
218 + * GNU General Public License for more details.
220 + * You should have received a copy of the GNU General Public License along
221 + * with this program; if not, write to the Free Software Foundation, Inc.,
222 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
226 +#include "sysxattr.h"
228 +#ifdef SUPPORT_XATTRS
230 +#if defined HAVE_LINUX_XATTRS
232 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
234 + return lgetxattr(path, name, value, size);
237 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
239 + return fgetxattr(filedes, name, value, size);
242 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
244 + return lsetxattr(path, name, value, size, 0);
247 +int sys_lremovexattr(const char *path, const char *name)
249 + return lremovexattr(path, name);
252 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
254 + return llistxattr(path, list, size);
257 +#elif HAVE_OSX_XATTRS
259 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
261 + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
264 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
266 + return fgetxattr(filedes, name, value, size, 0, 0);
269 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
271 + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
274 +int sys_lremovexattr(const char *path, const char *name)
276 + return removexattr(path, name, XATTR_NOFOLLOW);
279 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
281 + return listxattr(path, list, size, XATTR_NOFOLLOW);
284 +#elif HAVE_FREEBSD_XATTRS
286 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
288 + return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
291 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
293 + return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
296 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
298 + return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
301 +int sys_lremovexattr(const char *path, const char *name)
303 + return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
306 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
308 + unsigned char keylen;
309 + ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
311 + if (len <= 0 || (size_t)len > size)
314 + /* FreeBSD puts a single-byte length before each string, with no '\0'
315 + * terminator. We need to change this into a series of null-terminted
316 + * strings. Since the size is the same, we can simply transform the
317 + * output in place. */
318 + for (off = 0; off < len; off += keylen + 1) {
319 + keylen = ((unsigned char*)list)[off];
320 + if (off + keylen >= len) {
321 + /* Should be impossible, but kernel bugs happen! */
325 + memmove(list+off, list+off+1, keylen);
326 + list[off+keylen] = '\0';
334 +#error You need to create xattr compatibility functions.
338 +#endif /* SUPPORT_XATTRS */
339 --- old/lib/sysxattr.h
340 +++ new/lib/sysxattr.h
342 +#ifdef SUPPORT_XATTRS
344 +#if defined HAVE_ATTR_XATTR_H
345 +#include <attr/xattr.h>
346 +#elif defined HAVE_SYS_XATTR_H
347 +#include <sys/xattr.h>
348 +#elif defined HAVE_SYS_EXTATTR_H
349 +#include <sys/extattr.h>
352 +/* Linux 2.4 does not define this as a distinct errno value: */
354 +#define ENOATTR ENODATA
357 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
358 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
359 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
360 +int sys_lremovexattr(const char *path, const char *name);
361 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
365 +/* No xattrs available */
370 @@ -48,6 +48,7 @@ int copy_links = 0;
371 int preserve_links = 0;
372 int preserve_hard_links = 0;
373 int preserve_acls = 0;
374 +int preserve_xattrs = 0;
375 int preserve_perms = 0;
376 int preserve_executability = 0;
377 int preserve_devices = 0;
378 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
379 char const *have_inplace = "no ";
380 char const *hardlinks = "no ";
381 char const *acls = "no ";
382 + char const *xattrs = "no ";
383 char const *links = "no ";
384 char const *ipv6 = "no ";
385 STRUCT_STAT *dumstat;
386 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
391 +#ifdef SUPPORT_XATTRS
397 @@ -239,8 +243,8 @@ static void print_rsync_version(enum log
398 (int)(sizeof (int64) * 8));
399 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
400 got_socketpair, hardlinks, links, ipv6, have_inplace);
401 - rprintf(f, " %sappend, %sACLs\n",
402 - have_inplace, acls);
403 + rprintf(f, " %sappend, %sACLs, %sxattrs\n",
404 + have_inplace, acls, xattrs);
406 #ifdef MAINTAINER_MODE
407 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
408 @@ -286,7 +290,7 @@ void usage(enum logcode F)
409 rprintf(F," -q, --quiet suppress non-error messages\n");
410 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
411 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
412 - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
413 + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
414 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
415 rprintf(F," -r, --recursive recurse into directories\n");
416 rprintf(F," -R, --relative use relative path names\n");
417 @@ -311,6 +315,9 @@ void usage(enum logcode F)
419 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
421 +#ifdef SUPPORT_XATTRS
422 + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
424 rprintf(F," -o, --owner preserve owner (super-user only)\n");
425 rprintf(F," -g, --group preserve group\n");
426 rprintf(F," --devices preserve device files (super-user only)\n");
427 @@ -434,6 +441,9 @@ static struct poptOption long_options[]
428 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
429 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
430 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
431 + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
432 + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
433 + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
434 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
435 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
436 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
437 @@ -1116,6 +1126,17 @@ int parse_arguments(int *argc, const cha
442 +#ifdef SUPPORT_XATTRS
443 + preserve_xattrs = 1;
444 + preserve_perms = 1;
447 + snprintf(err_buf,sizeof(err_buf),
448 + "extended attributes are not supported on this %s\n",
449 + am_server ? "server" : "client");
454 /* A large opt value means that set_refuse_options()
455 @@ -1255,6 +1276,8 @@ int parse_arguments(int *argc, const cha
456 preserve_gid = ++flist_extra_cnt;
458 preserve_acls = ++flist_extra_cnt;
459 + if (preserve_xattrs)
460 + preserve_xattrs = ++flist_extra_cnt;
462 *argv = poptGetArgs(pc);
463 *argc = count_args(*argv);
464 @@ -1570,6 +1593,10 @@ void server_options(char **args,int *arg
468 +#ifdef SUPPORT_XATTRS
469 + if (preserve_xattrs)
480 extern int preserve_acls;
481 +extern int preserve_xattrs;
482 extern int preserve_perms;
483 extern int preserve_executability;
484 extern int preserve_times;
485 @@ -271,6 +272,10 @@ int set_file_attrs(char *fname, struct f
486 if (daemon_chmod_modes && !S_ISLNK(new_mode))
487 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
489 +#ifdef SUPPORT_XATTRS
490 + if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
494 /* It's OK to call set_acl() now, even for a dir, as the generator
495 * will enable owner-writability using chmod, if necessary.
498 @@ -516,6 +516,10 @@ struct idev_node {
499 #define ACLS_NEED_MASK 1
502 +#ifndef HAVE_NO_XATTRS
503 +#define SUPPORT_XATTRS 1
506 #define GID_NONE ((gid_t)-1)
509 @@ -568,6 +572,7 @@ extern int preserve_gid;
510 #define F_UID(f) REQ_EXTRA(f, preserve_uid)->uid
511 #define F_GID(f) REQ_EXTRA(f, preserve_gid)->gid
512 #define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
513 +#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->unum
515 /* These items are per-entry optional and mutally exclusive: */
516 #define F_HL_IDEV(f) OPT_EXTRA(f, LEN64_BUMP(f))->idev
517 @@ -744,6 +749,9 @@ typedef struct {
518 struct rsync_acl *acc_acl; /* access ACL */
519 struct rsync_acl *def_acl; /* default ACL */
521 +#ifdef SUPPORT_XATTRS
526 #define ACL_READY(sx) ((sx).acc_acl != NULL)
529 @@ -301,7 +301,7 @@ to the detailed description below for a
530 -q, --quiet suppress non-error messages
531 --no-motd suppress daemon-mode MOTD (see caveat)
532 -c, --checksum skip based on checksum, not mod-time & size
533 - -a, --archive archive mode; same as -rlptgoD (no -H, -A)
534 + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
535 --no-OPTION turn off an implied OPTION (e.g. --no-D)
536 -r, --recursive recurse into directories
537 -R, --relative use relative path names
538 @@ -324,6 +324,7 @@ to the detailed description below for a
539 -E, --executability preserve executability
540 --chmod=CHMOD affect file and/or directory permissions
541 -A, --acls preserve ACLs (implies -p) [non-standard]
542 + -X, --xattrs preserve extended attrs (implies -p) [n.s.]
543 -o, --owner preserve owner (super-user only)
544 -g, --group preserve group
545 --devices preserve device files (super-user only)
546 @@ -818,6 +819,11 @@ The ACL-sending protocol used by this ve
547 the patch that was shipped with 2.6.8. Sending ACLs to an older version
548 of the ACL patch is not supported.
550 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
551 +extended attributes to be the same as the local ones. This will work
552 +only if the remote machine's rsync supports this option also. This is
553 +a non-standard option.
555 dit(bf(--chmod)) This option tells rsync to apply one or more
556 comma-separated "chmod" strings to the permission of the files in the
557 transfer. The resulting value is treated as though it was the permissions
562 + * Extended Attribute support for rsync.
563 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
565 + * Copyright (C) 2004 Red Hat, Inc.
566 + * Copyright (C) 2006 Wayne Davison
568 + * This program is free software; you can redistribute it and/or modify
569 + * it under the terms of the GNU General Public License as published by
570 + * the Free Software Foundation; either version 2 of the License, or
571 + * (at your option) any later version.
573 + * This program is distributed in the hope that it will be useful,
574 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
575 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
576 + * GNU General Public License for more details.
578 + * You should have received a copy of the GNU General Public License along
579 + * with this program; if not, write to the Free Software Foundation, Inc.,
580 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
584 +#include "lib/sysxattr.h"
586 +#ifdef SUPPORT_XATTRS
590 +extern int read_only;
591 +extern int list_only;
592 +extern int preserve_xattrs;
593 +extern unsigned int file_struct_len;
595 +#define RSYNC_XAL_INITIAL 5
596 +#define RSYNC_XAL_LIST_INITIAL 100
598 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
599 + && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
601 +#define USER_PREFIX "user."
602 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
603 +#define SYSTEM_PREFIX "system."
604 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
606 +#ifdef HAVE_LINUX_XATTRS
609 +#define RSYNC_PREFIX "rsync."
610 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
614 + char *datum, *name;
615 + size_t datum_len, name_len;
618 +static size_t namebuf_len = 0;
619 +static char *namebuf = NULL;
621 +static item_list empty_xattr = EMPTY_ITEM_LIST;
622 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
624 +/* ------------------------------------------------------------------------- */
626 +static void rsync_xal_free(item_list *xalp)
629 + rsync_xa *rxas = xalp->items;
631 + for (i = 0; i < xalp->count; i++) {
632 + free(rxas[i].datum);
633 + /*free(rxas[i].name);*/
638 +void free_xattr(statx *sxp)
640 + rsync_xal_free(sxp->xattr);
645 +static int rsync_xal_compare_names(const void *x1, const void *x2)
647 + const rsync_xa *xa1 = x1;
648 + const rsync_xa *xa2 = x2;
649 + return strcmp(xa1->name, xa2->name);
652 +static int rsync_xal_get(const char *fname, item_list *xalp)
654 + ssize_t list_len, name_len, datum_len;
658 + namebuf_len = 1024;
659 + namebuf = new_array(char, namebuf_len);
661 + out_of_memory("rsync_xal_get");
664 + /* The length returned includes all the '\0' terminators. */
665 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
666 + if (list_len > (ssize_t)namebuf_len) {
670 + if (list_len < 0) {
671 + if (errno == ENOTSUP)
673 + if (errno == ERANGE) {
674 + list_len = sys_llistxattr(fname, NULL, 0);
675 + if (list_len < 0) {
676 + rsyserr(FERROR, errno,
677 + "rsync_xal_get: llistxattr(\"%s\",0) failed",
681 + namebuf = realloc_array(namebuf, char, list_len + 1024);
683 + out_of_memory("rsync_xal_get");
684 + namebuf_len = list_len + 1024;
685 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
686 + if (list_len < 0) {
687 + rsyserr(FERROR, errno,
688 + "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
689 + fname, (long)namebuf_len);
693 + rsyserr(FERROR, errno,
694 + "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
695 + fname, (long)namebuf_len);
700 + for (name = namebuf; list_len > 0; name += name_len) {
703 + name_len = strlen(name) + 1;
704 + list_len -= name_len;
706 +#ifdef HAVE_LINUX_XATTRS
707 + /* We don't send the system namespace. */
708 + if (HAS_PREFIX(name, SYSTEM_PREFIX))
712 + datum_len = sys_lgetxattr(fname, name, NULL, 0);
713 + if (datum_len < 0) {
714 + if (errno == ENOTSUP)
716 + rsyserr(FERROR, errno,
717 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",0) failed",
721 + ptr = new_array(char, name_len + datum_len);
723 + out_of_memory("rsync_xal_get");
725 + ssize_t len = sys_lgetxattr(fname, name, ptr, datum_len);
726 + if (len != datum_len) {
728 + rsyserr(FERROR, errno,
729 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
730 + " failed", fname, name, (long)datum_len);
733 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
734 + " returned %ld\n", fname, name,
735 + (long)datum_len, (long)len);
741 + rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
742 + rxas->name = ptr + datum_len;
744 + rxas->name_len = name_len;
745 + rxas->datum_len = datum_len;
746 + memcpy(rxas->name, name, name_len);
748 + if (xalp->count > 1)
749 + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
753 +/* Read the xattr(s) for this filename. */
754 +int get_xattr(const char *fname, statx *sxp)
756 + sxp->xattr = new(item_list);
757 + *sxp->xattr = empty_xattr;
758 + if (rsync_xal_get(fname, sxp->xattr) < 0) {
765 +static int find_matching_xattr(item_list *xalp)
768 + item_list *lst = rsync_xal_l.items;
770 + for (i = 0; i < rsync_xal_l.count; i++) {
771 + rsync_xa *rxas1 = lst[i].items;
772 + rsync_xa *rxas2 = xalp->items;
774 + /* Wrong number of elements? */
775 + if (lst[i].count != xalp->count)
777 + /* any elements different? */
778 + for (j = 0; j < xalp->count; j++) {
779 + if (rxas1[j].name_len != rxas2[j].name_len
780 + || rxas1[j].datum_len != rxas2[j].datum_len
781 + || strcmp(rxas1[j].name, rxas2[j].name)
782 + || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
785 + /* no differences found. This is The One! */
786 + if (j == xalp->count)
793 +/* Store *xalp on the end of rsync_xal_l */
794 +static void rsync_xal_store(item_list *xalp)
796 + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
797 + /* Since the following call starts a new list, we know it will hold the
798 + * entire initial-count, not just enough space for one new item. */
799 + *new_lst = empty_xattr;
800 + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
801 + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
802 + new_lst->count = xalp->count;
806 +/* Send the make_xattr()-generated xattr list for this flist entry. */
807 +void send_xattr(statx *sxp, int f)
809 + int ndx = find_matching_xattr(sxp->xattr);
811 + write_byte(f, 'x');
813 + rsync_xal_free(sxp->xattr);
816 + int count = sxp->xattr->count;
817 + write_byte(f, 'X');
818 + write_int(f, count);
819 + for (rxa = sxp->xattr->items; count--; rxa++) {
820 +#ifdef HAVE_LINUX_XATTRS
821 + write_int(f, rxa->name_len);
822 + write_int(f, rxa->datum_len);
823 + write_buf(f, rxa->name, rxa->name_len);
825 + /* We strip the rsync prefix from disguised namespaces
826 + * and put everything else in the user namespace. */
827 + if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
828 + && rxa->name[RPRE_LEN] != '%') {
829 + write_int(f, rxa->name_len - RPRE_LEN);
830 + write_int(f, rxa->datum_len);
831 + write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
833 + write_int(f, rxa->name_len + UPRE_LEN);
834 + write_int(f, rxa->datum_len);
835 + write_buf(f, USER_PREFIX, UPRE_LEN);
836 + write_buf(f, rxa->name, rxa->name_len);
839 + write_buf(f, rxa->datum, rxa->datum_len);
841 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
846 +/* ------------------------------------------------------------------------- */
848 +/* receive and build the rsync_xattr_lists */
849 +void receive_xattr(struct file_struct *file, int f)
851 + static item_list temp_xattr = EMPTY_ITEM_LIST;
852 + int ndx, tag = read_byte(f);
855 + int i, count = read_int(f);
856 + for (i = 0; i < count; i++) {
859 + size_t name_len = read_int(f);
860 + size_t datum_len = read_int(f);
861 +#ifdef HAVE_LINUX_XATTRS
862 + size_t extra_len = 0;
864 + size_t extra_len = am_root ? RPRE_LEN : 0;
865 + if (datum_len + extra_len < datum_len)
866 + out_of_memory("receive_xattr"); /* overflow */
868 + if (name_len + datum_len + extra_len < name_len)
869 + out_of_memory("receive_xattr"); /* overflow */
870 + ptr = new_array(char, name_len + datum_len + extra_len);
872 + out_of_memory("receive_xattr");
873 + name = ptr + datum_len + extra_len;
874 + read_buf(f, name, name_len);
875 + read_buf(f, ptr, datum_len);
876 +#ifdef HAVE_LINUX_XATTRS
877 + /* Non-root can only save the user namespace. */
878 + if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
883 + /* This OS only has a user namespace, so we either
884 + * strip the user prefix, or we put a non-user
885 + * namespace inside our rsync hierarchy. */
886 + if (HAS_PREFIX(name, USER_PREFIX)) {
888 + name_len -= UPRE_LEN;
889 + } else if (am_root) {
891 + name_len += RPRE_LEN;
892 + memcpy(name, RSYNC_PREFIX, RPRE_LEN);
898 + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
901 + rxa->name_len = name_len;
902 + rxa->datum_len = datum_len;
904 + ndx = rsync_xal_l.count; /* pre-incremented count */
905 + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
906 + } else if (tag == 'x') {
908 + if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
909 + rprintf(FERROR, "receive_xattr: xa index %d out of"
910 + " range for %s\n", ndx, f_name(file, NULL));
911 + exit_cleanup(RERR_STREAMIO);
914 + rprintf(FERROR, "receive_xattr: unknown extended attribute"
915 + " type tag (%c) for %s\n", tag, f_name(file, NULL));
916 + exit_cleanup(RERR_STREAMIO);
919 + F_XATTR(file) = ndx;
922 +/* Turn the xattr data in statx into cached xattr data, setting the index
923 + * values in the file struct. */
924 +void cache_xattr(struct file_struct *file, statx *sxp)
931 + ndx = find_matching_xattr(sxp->xattr);
933 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
936 + F_XATTR(file) = ndx;
939 +static int rsync_xal_set(const char *fname, item_list *xalp)
941 + rsync_xa *rxas = xalp->items;
945 + for (i = 0; i < xalp->count; i++) {
946 + int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len);
948 + rsyserr(FERROR, errno,
949 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
950 + fname, rxas[i].name);
957 +/* Set extended attributes on indicated filename. */
958 +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
961 + item_list *lst = rsync_xal_l.items;
964 + return 1; /* FIXME: --dry-run needs to compute this value */
966 + if (read_only || list_only) {
971 + ndx = F_XATTR(file);
972 + return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
975 +#endif /* SUPPORT_XATTRS */