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 @@ -52,6 +52,8 @@ void setup_protocol(int f_out,int f_in)
106 preserve_gid = ++file_extra_cnt;
107 if (preserve_acls && !am_sender)
108 preserve_acls = ++file_extra_cnt;
109 + if (preserve_xattrs && !am_sender)
110 + preserve_xattrs = ++file_extra_cnt;
112 if (remote_protocol == 0) {
116 @@ -883,6 +883,40 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
120 +AC_CHECK_HEADERS(attr/xattr.h)
121 +AC_CHECK_HEADERS(sys/xattr.h)
122 +AC_CHECK_HEADERS(sys/extattr.h)
123 +AC_MSG_CHECKING(whether to support extended attributes)
124 +AC_ARG_ENABLE(xattr-support,
125 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
126 +[ case "$enableval" in
130 + AC_MSG_RESULT(Using Linux xattrs)
131 + AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
134 + AC_MSG_RESULT(Using OS X xattrs)
135 + AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
138 + AC_MSG_RESULT(Using FreeBSD extattrs)
139 + AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs])
142 + AC_MSG_RESULT(Xattrs requested but not Linux or OS X. Good luck...)
148 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
151 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
154 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
159 @@ -42,6 +42,7 @@ extern int one_file_system;
160 extern int copy_dirlinks;
161 extern int keep_dirlinks;
162 extern int preserve_acls;
163 +extern int preserve_xattrs;
164 extern int preserve_links;
165 extern int preserve_hard_links;
166 extern int preserve_devices;
167 @@ -812,6 +813,10 @@ static struct file_struct *recv_file_ent
169 receive_acl(file, f);
171 +#ifdef SUPPORT_XATTRS
172 + if (preserve_xattrs)
173 + receive_xattr(file, f );
178 @@ -1082,7 +1087,7 @@ static struct file_struct *send_file_nam
181 struct file_struct *file;
183 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
187 @@ -1102,6 +1107,13 @@ static struct file_struct *send_file_nam
191 +#ifdef SUPPORT_XATTRS
192 + if (preserve_xattrs && f >= 0) {
194 + if (get_xattr(fname, &sx) < 0)
199 maybe_emit_filelist_progress(flist->count + flist_count_offset);
201 @@ -1113,6 +1125,10 @@ static struct file_struct *send_file_nam
205 +#ifdef SUPPORT_XATTRS
206 + if (preserve_xattrs)
207 + send_xattr(&sx, f);
212 --- old/lib/sysxattr.c
213 +++ new/lib/sysxattr.c
216 + * Extended attribute support for rsync.
218 + * Copyright (C) 2004 Red Hat, Inc.
219 + * Written by Jay Fenlason.
221 + * This program is free software; you can redistribute it and/or modify
222 + * it under the terms of the GNU General Public License as published by
223 + * the Free Software Foundation; either version 2 of the License, or
224 + * (at your option) any later version.
226 + * This program is distributed in the hope that it will be useful,
227 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
228 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
229 + * GNU General Public License for more details.
231 + * You should have received a copy of the GNU General Public License along
232 + * with this program; if not, write to the Free Software Foundation, Inc.,
233 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
237 +#include "sysxattr.h"
239 +#ifdef SUPPORT_XATTRS
241 +#if defined HAVE_LINUX_XATTRS
243 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
245 + return lgetxattr(path, name, value, size);
248 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
250 + return fgetxattr(filedes, name, value, size);
253 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
255 + return lsetxattr(path, name, value, size, 0);
258 +int sys_lremovexattr(const char *path, const char *name)
260 + return lremovexattr(path, name);
263 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
265 + return llistxattr(path, list, size);
268 +#elif HAVE_OSX_XATTRS
270 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
272 + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
275 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
277 + return fgetxattr(filedes, name, value, size, 0, 0);
280 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
282 + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
285 +int sys_lremovexattr(const char *path, const char *name)
287 + return removexattr(path, name, XATTR_NOFOLLOW);
290 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
292 + return listxattr(path, list, size, XATTR_NOFOLLOW);
295 +#elif HAVE_FREEBSD_XATTRS
297 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
299 + return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
302 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
304 + return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
307 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
309 + return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
312 +int sys_lremovexattr(const char *path, const char *name)
314 + return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
317 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
319 + unsigned char keylen;
320 + ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
322 + if (len <= 0 || (size_t)len > size)
325 + /* FreeBSD puts a single-byte length before each string, with no '\0'
326 + * terminator. We need to change this into a series of null-terminted
327 + * strings. Since the size is the same, we can simply transform the
328 + * output in place. */
329 + for (off = 0; off < len; off += keylen + 1) {
330 + keylen = ((unsigned char*)list)[off];
331 + if (off + keylen >= len) {
332 + /* Should be impossible, but kernel bugs happen! */
336 + memmove(list+off, list+off+1, keylen);
337 + list[off+keylen] = '\0';
345 +#error You need to create xattr compatibility functions.
349 +#endif /* SUPPORT_XATTRS */
350 --- old/lib/sysxattr.h
351 +++ new/lib/sysxattr.h
353 +#ifdef SUPPORT_XATTRS
355 +#if defined HAVE_ATTR_XATTR_H
356 +#include <attr/xattr.h>
357 +#elif defined HAVE_SYS_XATTR_H
358 +#include <sys/xattr.h>
359 +#elif defined HAVE_SYS_EXTATTR_H
360 +#include <sys/extattr.h>
363 +/* Linux 2.4 does not define this as a distinct errno value: */
365 +#define ENOATTR ENODATA
368 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
369 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
370 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
371 +int sys_lremovexattr(const char *path, const char *name);
372 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
376 +/* No xattrs available */
381 @@ -48,6 +48,7 @@ int copy_links = 0;
382 int preserve_links = 0;
383 int preserve_hard_links = 0;
384 int preserve_acls = 0;
385 +int preserve_xattrs = 0;
386 int preserve_perms = 0;
387 int preserve_executability = 0;
388 int preserve_devices = 0;
389 @@ -200,6 +201,7 @@ static void print_rsync_version(enum log
390 char const *have_inplace = "no ";
391 char const *hardlinks = "no ";
392 char const *acls = "no ";
393 + char const *xattrs = "no ";
394 char const *links = "no ";
395 char const *ipv6 = "no ";
396 STRUCT_STAT *dumstat;
397 @@ -219,7 +221,9 @@ static void print_rsync_version(enum log
402 +#ifdef SUPPORT_XATTRS
408 @@ -238,8 +242,8 @@ static void print_rsync_version(enum log
409 (int)(sizeof (int64) * 8));
410 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
411 got_socketpair, hardlinks, links, ipv6, have_inplace);
412 - rprintf(f, " %sappend, %sACLs\n",
413 - have_inplace, acls);
414 + rprintf(f, " %sappend, %sACLs, %sxattrs\n",
415 + have_inplace, acls, xattrs);
417 #ifdef MAINTAINER_MODE
418 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
419 @@ -285,7 +289,7 @@ void usage(enum logcode F)
420 rprintf(F," -q, --quiet suppress non-error messages\n");
421 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
422 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
423 - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
424 + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
425 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
426 rprintf(F," -r, --recursive recurse into directories\n");
427 rprintf(F," -R, --relative use relative path names\n");
428 @@ -310,6 +314,9 @@ void usage(enum logcode F)
430 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
432 +#ifdef SUPPORT_XATTRS
433 + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
435 rprintf(F," -o, --owner preserve owner (super-user only)\n");
436 rprintf(F," -g, --group preserve group\n");
437 rprintf(F," --devices preserve device files (super-user only)\n");
438 @@ -433,6 +440,9 @@ static struct poptOption long_options[]
439 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
440 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
441 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
442 + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
443 + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
444 + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
445 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
446 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
447 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
448 @@ -1115,6 +1125,17 @@ int parse_arguments(int *argc, const cha
453 +#ifdef SUPPORT_XATTRS
454 + preserve_xattrs = 1;
455 + preserve_perms = 1;
458 + snprintf(err_buf,sizeof(err_buf),
459 + "extended attributes are not supported on this %s\n",
460 + am_server ? "server" : "client");
465 /* A large opt value means that set_refuse_options()
466 @@ -1562,6 +1583,10 @@ void server_options(char **args,int *arg
470 +#ifdef SUPPORT_XATTRS
471 + if (preserve_xattrs)
482 extern int preserve_acls;
483 +extern int preserve_xattrs;
484 extern int preserve_perms;
485 extern int preserve_executability;
486 extern int preserve_times;
487 @@ -271,6 +272,10 @@ int set_file_attrs(char *fname, struct f
488 if (daemon_chmod_modes && !S_ISLNK(new_mode))
489 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
491 +#ifdef SUPPORT_XATTRS
492 + if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
496 /* It's OK to call set_acl() now, even for a dir, as the generator
497 * will enable owner-writability using chmod, if necessary.
500 @@ -540,6 +540,10 @@ struct idev_node {
501 #define ACLS_NEED_MASK 1
504 +#ifndef HAVE_NO_XATTRS
505 +#define SUPPORT_XATTRS 1
508 #define GID_NONE ((gid_t)-1)
511 @@ -560,6 +564,7 @@ extern int file_extra_cnt;
512 extern int preserve_uid;
513 extern int preserve_gid;
514 extern int preserve_acls;
515 +extern int preserve_xattrs;
517 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
518 #define EXTRA_LEN (sizeof (union file_extras))
519 @@ -593,6 +598,7 @@ extern int preserve_acls;
520 #define F_UID(f) REQ_EXTRA(f, preserve_uid)->unum
521 #define F_GID(f) REQ_EXTRA(f, preserve_gid)->unum
522 #define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
523 +#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->unum
525 /* These items are per-entry optional and mutally exclusive: */
526 #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
527 @@ -768,6 +774,9 @@ typedef struct {
528 struct rsync_acl *acc_acl; /* access ACL */
529 struct rsync_acl *def_acl; /* default ACL */
531 +#ifdef SUPPORT_XATTRS
536 #define ACL_READY(sx) ((sx).acc_acl != NULL)
539 @@ -301,7 +301,7 @@ to the detailed description below for a
540 -q, --quiet suppress non-error messages
541 --no-motd suppress daemon-mode MOTD (see caveat)
542 -c, --checksum skip based on checksum, not mod-time & size
543 - -a, --archive archive mode; same as -rlptgoD (no -H, -A)
544 + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
545 --no-OPTION turn off an implied OPTION (e.g. --no-D)
546 -r, --recursive recurse into directories
547 -R, --relative use relative path names
548 @@ -324,6 +324,7 @@ to the detailed description below for a
549 -E, --executability preserve executability
550 --chmod=CHMOD affect file and/or directory permissions
551 -A, --acls preserve ACLs (implies -p) [non-standard]
552 + -X, --xattrs preserve extended attrs (implies -p) [n.s.]
553 -o, --owner preserve owner (super-user only)
554 -g, --group preserve group
555 --devices preserve device files (super-user only)
556 @@ -818,6 +819,11 @@ The ACL-sending protocol used by this ve
557 the patch that was shipped with 2.6.8. Sending ACLs to an older version
558 of the ACL patch is not supported.
560 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
561 +extended attributes to be the same as the local ones. This will work
562 +only if the remote machine's rsync supports this option also. This is
563 +a non-standard option.
565 dit(bf(--chmod)) This option tells rsync to apply one or more
566 comma-separated "chmod" strings to the permission of the files in the
567 transfer. The resulting value is treated as though it was the permissions
572 + * Extended Attribute support for rsync.
573 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
575 + * Copyright (C) 2004 Red Hat, Inc.
576 + * Copyright (C) 2006 Wayne Davison
578 + * This program is free software; you can redistribute it and/or modify
579 + * it under the terms of the GNU General Public License as published by
580 + * the Free Software Foundation; either version 2 of the License, or
581 + * (at your option) any later version.
583 + * This program is distributed in the hope that it will be useful,
584 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
585 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
586 + * GNU General Public License for more details.
588 + * You should have received a copy of the GNU General Public License along
589 + * with this program; if not, write to the Free Software Foundation, Inc.,
590 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
594 +#include "lib/sysxattr.h"
596 +#ifdef SUPPORT_XATTRS
600 +extern int read_only;
601 +extern int list_only;
603 +#define RSYNC_XAL_INITIAL 5
604 +#define RSYNC_XAL_LIST_INITIAL 100
606 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
607 + && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
609 +#define USER_PREFIX "user."
610 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
611 +#define SYSTEM_PREFIX "system."
612 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
614 +#ifdef HAVE_LINUX_XATTRS
617 +#define RSYNC_PREFIX "rsync."
618 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
622 + char *datum, *name;
623 + size_t datum_len, name_len;
626 +static size_t namebuf_len = 0;
627 +static char *namebuf = NULL;
629 +static item_list empty_xattr = EMPTY_ITEM_LIST;
630 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
632 +/* ------------------------------------------------------------------------- */
634 +static void rsync_xal_free(item_list *xalp)
637 + rsync_xa *rxas = xalp->items;
639 + for (i = 0; i < xalp->count; i++) {
640 + free(rxas[i].datum);
641 + /*free(rxas[i].name);*/
646 +void free_xattr(statx *sxp)
648 + rsync_xal_free(sxp->xattr);
653 +static int rsync_xal_compare_names(const void *x1, const void *x2)
655 + const rsync_xa *xa1 = x1;
656 + const rsync_xa *xa2 = x2;
657 + return strcmp(xa1->name, xa2->name);
660 +static int rsync_xal_get(const char *fname, item_list *xalp)
662 + ssize_t list_len, name_len, datum_len;
666 + namebuf_len = 1024;
667 + namebuf = new_array(char, namebuf_len);
669 + out_of_memory("rsync_xal_get");
672 + /* The length returned includes all the '\0' terminators. */
673 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
674 + if (list_len > (ssize_t)namebuf_len) {
678 + if (list_len < 0) {
679 + if (errno == ENOTSUP)
681 + if (errno == ERANGE) {
682 + list_len = sys_llistxattr(fname, NULL, 0);
683 + if (list_len < 0) {
684 + rsyserr(FERROR, errno,
685 + "rsync_xal_get: llistxattr(\"%s\",0) failed",
689 + namebuf = realloc_array(namebuf, char, list_len + 1024);
691 + out_of_memory("rsync_xal_get");
692 + namebuf_len = list_len + 1024;
693 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
694 + if (list_len < 0) {
695 + rsyserr(FERROR, errno,
696 + "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
697 + fname, (long)namebuf_len);
701 + rsyserr(FERROR, errno,
702 + "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
703 + fname, (long)namebuf_len);
708 + for (name = namebuf; list_len > 0; name += name_len) {
711 + name_len = strlen(name) + 1;
712 + list_len -= name_len;
714 +#ifdef HAVE_LINUX_XATTRS
715 + /* We don't send the system namespace. */
716 + if (HAS_PREFIX(name, SYSTEM_PREFIX))
720 + datum_len = sys_lgetxattr(fname, name, NULL, 0);
721 + if (datum_len < 0) {
722 + if (errno == ENOTSUP)
724 + rsyserr(FERROR, errno,
725 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",0) failed",
729 + ptr = new_array(char, name_len + datum_len);
731 + out_of_memory("rsync_xal_get");
733 + ssize_t len = sys_lgetxattr(fname, name, ptr, datum_len);
734 + if (len != datum_len) {
736 + rsyserr(FERROR, errno,
737 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
738 + " failed", fname, name, (long)datum_len);
741 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
742 + " returned %ld\n", fname, name,
743 + (long)datum_len, (long)len);
749 + rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
750 + rxas->name = ptr + datum_len;
752 + rxas->name_len = name_len;
753 + rxas->datum_len = datum_len;
754 + memcpy(rxas->name, name, name_len);
756 + if (xalp->count > 1)
757 + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
761 +/* Read the xattr(s) for this filename. */
762 +int get_xattr(const char *fname, statx *sxp)
764 + sxp->xattr = new(item_list);
765 + *sxp->xattr = empty_xattr;
766 + if (rsync_xal_get(fname, sxp->xattr) < 0) {
773 +static int find_matching_xattr(item_list *xalp)
776 + item_list *lst = rsync_xal_l.items;
778 + for (i = 0; i < rsync_xal_l.count; i++) {
779 + rsync_xa *rxas1 = lst[i].items;
780 + rsync_xa *rxas2 = xalp->items;
782 + /* Wrong number of elements? */
783 + if (lst[i].count != xalp->count)
785 + /* any elements different? */
786 + for (j = 0; j < xalp->count; j++) {
787 + if (rxas1[j].name_len != rxas2[j].name_len
788 + || rxas1[j].datum_len != rxas2[j].datum_len
789 + || strcmp(rxas1[j].name, rxas2[j].name)
790 + || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
793 + /* no differences found. This is The One! */
794 + if (j == xalp->count)
801 +/* Store *xalp on the end of rsync_xal_l */
802 +static void rsync_xal_store(item_list *xalp)
804 + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
805 + /* Since the following call starts a new list, we know it will hold the
806 + * entire initial-count, not just enough space for one new item. */
807 + *new_lst = empty_xattr;
808 + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
809 + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
810 + new_lst->count = xalp->count;
814 +/* Send the make_xattr()-generated xattr list for this flist entry. */
815 +void send_xattr(statx *sxp, int f)
817 + int ndx = find_matching_xattr(sxp->xattr);
819 + write_byte(f, 'x');
821 + rsync_xal_free(sxp->xattr);
824 + int count = sxp->xattr->count;
825 + write_byte(f, 'X');
826 + write_int(f, count);
827 + for (rxa = sxp->xattr->items; count--; rxa++) {
828 +#ifdef HAVE_LINUX_XATTRS
829 + write_int(f, rxa->name_len);
830 + write_int(f, rxa->datum_len);
831 + write_buf(f, rxa->name, rxa->name_len);
833 + /* We strip the rsync prefix from disguised namespaces
834 + * and put everything else in the user namespace. */
835 + if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
836 + && rxa->name[RPRE_LEN] != '%') {
837 + write_int(f, rxa->name_len - RPRE_LEN);
838 + write_int(f, rxa->datum_len);
839 + write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
841 + write_int(f, rxa->name_len + UPRE_LEN);
842 + write_int(f, rxa->datum_len);
843 + write_buf(f, USER_PREFIX, UPRE_LEN);
844 + write_buf(f, rxa->name, rxa->name_len);
847 + write_buf(f, rxa->datum, rxa->datum_len);
849 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
854 +/* ------------------------------------------------------------------------- */
856 +/* receive and build the rsync_xattr_lists */
857 +void receive_xattr(struct file_struct *file, int f)
859 + static item_list temp_xattr = EMPTY_ITEM_LIST;
860 + int ndx, tag = read_byte(f);
863 + int i, count = read_int(f);
864 + for (i = 0; i < count; i++) {
867 + size_t name_len = read_int(f);
868 + size_t datum_len = read_int(f);
869 +#ifdef HAVE_LINUX_XATTRS
870 + size_t extra_len = 0;
872 + size_t extra_len = am_root ? RPRE_LEN : 0;
873 + if (datum_len + extra_len < datum_len)
874 + out_of_memory("receive_xattr"); /* overflow */
876 + if (name_len + datum_len + extra_len < name_len)
877 + out_of_memory("receive_xattr"); /* overflow */
878 + ptr = new_array(char, name_len + datum_len + extra_len);
880 + out_of_memory("receive_xattr");
881 + name = ptr + datum_len + extra_len;
882 + read_buf(f, name, name_len);
883 + read_buf(f, ptr, datum_len);
884 +#ifdef HAVE_LINUX_XATTRS
885 + /* Non-root can only save the user namespace. */
886 + if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
891 + /* This OS only has a user namespace, so we either
892 + * strip the user prefix, or we put a non-user
893 + * namespace inside our rsync hierarchy. */
894 + if (HAS_PREFIX(name, USER_PREFIX)) {
896 + name_len -= UPRE_LEN;
897 + } else if (am_root) {
899 + name_len += RPRE_LEN;
900 + memcpy(name, RSYNC_PREFIX, RPRE_LEN);
906 + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
909 + rxa->name_len = name_len;
910 + rxa->datum_len = datum_len;
912 + ndx = rsync_xal_l.count; /* pre-incremented count */
913 + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
914 + } else if (tag == 'x') {
916 + if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
917 + rprintf(FERROR, "receive_xattr: xa index %d out of"
918 + " range for %s\n", ndx, f_name(file, NULL));
919 + exit_cleanup(RERR_STREAMIO);
922 + rprintf(FERROR, "receive_xattr: unknown extended attribute"
923 + " type tag (%c) for %s\n", tag, f_name(file, NULL));
924 + exit_cleanup(RERR_STREAMIO);
927 + F_XATTR(file) = ndx;
930 +/* Turn the xattr data in statx into cached xattr data, setting the index
931 + * values in the file struct. */
932 +void cache_xattr(struct file_struct *file, statx *sxp)
939 + ndx = find_matching_xattr(sxp->xattr);
941 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
944 + F_XATTR(file) = ndx;
947 +static int rsync_xal_set(const char *fname, item_list *xalp)
949 + rsync_xa *rxas = xalp->items;
953 + for (i = 0; i < xalp->count; i++) {
954 + int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len);
956 + rsyserr(FERROR, errno,
957 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
958 + fname, rxas[i].name);
965 +/* Set extended attributes on indicated filename. */
966 +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
969 + item_list *lst = rsync_xal_l.items;
972 + return 1; /* FIXME: --dry-run needs to compute this value */
974 + if (read_only || list_only) {
979 + ndx = F_XATTR(file);
980 + return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
983 +#endif /* SUPPORT_XATTRS */