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 - We need to affect the itemized output to know when xattrs are being updated.
23 - We need to affect the --link-dest option to avoid hard-linking two files
24 that differ in their xattrs (when --xattrs was specified).
28 @@ -28,13 +28,13 @@ VERSION=@VERSION@
30 HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
31 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
32 - lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
33 + lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattr.o @LIBOBJS@
34 ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
35 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
36 OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
37 util.o main.o checksum.o match.o syscall.o log.o backup.o
38 OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o \
39 - fileio.o batch.o clientname.o chmod.o acls.o
40 + fileio.o batch.o clientname.o chmod.o acls.o xattr.o
41 OBJS3=progress.o pipe.o
42 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
43 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
49 extern int preserve_acls;
50 +extern int preserve_xattrs;
51 extern int preserve_devices;
52 extern int preserve_specials;
53 extern int preserve_links;
54 @@ -134,6 +135,9 @@ static int make_bak_dir(char *fullpath)
56 sx.acc_acl = sx.def_acl = NULL;
58 +#ifdef SUPPORT_XATTRS
61 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
64 @@ -142,6 +146,12 @@ static int make_bak_dir(char *fullpath)
68 +#ifdef SUPPORT_XATTRS
69 + if (preserve_xattrs) {
70 + get_xattr(rel, &sx);
71 + cache_xattr(file, &sx);
74 set_file_attrs(fullpath, file, NULL, 0);
77 @@ -193,6 +203,9 @@ static int keep_backup(const char *fname
79 sx.acc_acl = sx.def_acl = NULL;
81 +#ifdef SUPPORT_XATTRS
85 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
86 return 1; /* the file could have disappeared */
87 @@ -208,6 +221,12 @@ static int keep_backup(const char *fname
91 +#ifdef SUPPORT_XATTRS
92 + if (preserve_xattrs) {
93 + get_xattr(fname, &sx);
94 + cache_xattr(file, &sx);
98 /* Check to see if this is a device file, or link */
99 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
102 @@ -64,6 +64,8 @@ void setup_protocol(int f_out,int f_in)
103 preserve_gid = ++file_extra_cnt;
104 if (preserve_acls && !am_sender)
105 preserve_acls = ++file_extra_cnt;
106 + if (preserve_xattrs && !am_sender)
107 + preserve_xattrs = ++file_extra_cnt;
109 if (remote_protocol == 0) {
113 @@ -883,6 +883,40 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
117 +AC_CHECK_HEADERS(attr/xattr.h)
118 +AC_CHECK_HEADERS(sys/xattr.h)
119 +AC_CHECK_HEADERS(sys/extattr.h)
120 +AC_MSG_CHECKING(whether to support extended attributes)
121 +AC_ARG_ENABLE(xattr-support,
122 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
123 +[ case "$enableval" in
127 + AC_MSG_RESULT(Using Linux xattrs)
128 + AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
131 + AC_MSG_RESULT(Using OS X xattrs)
132 + AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
135 + AC_MSG_RESULT(Using FreeBSD extattrs)
136 + AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs])
139 + AC_MSG_RESULT(Xattrs requested but not Linux or OS X. Good luck...)
145 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
148 + AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
151 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
156 @@ -42,6 +42,7 @@ extern int one_file_system;
157 extern int copy_dirlinks;
158 extern int keep_dirlinks;
159 extern int preserve_acls;
160 +extern int preserve_xattrs;
161 extern int preserve_links;
162 extern int preserve_hard_links;
163 extern int preserve_devices;
164 @@ -864,6 +865,10 @@ static struct file_struct *recv_file_ent
166 receive_acl(file, f);
168 +#ifdef SUPPORT_XATTRS
169 + if (preserve_xattrs)
170 + receive_xattr(file, f );
173 if (S_ISREG(mode) || S_ISLNK(mode))
174 stats.total_size += file_length;
175 @@ -1136,7 +1141,7 @@ static struct file_struct *send_file_nam
176 int flags, int filter_flags)
178 struct file_struct *file;
180 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
184 @@ -1155,6 +1160,13 @@ static struct file_struct *send_file_nam
188 +#ifdef SUPPORT_XATTRS
189 + if (preserve_xattrs && f >= 0) {
191 + if (get_xattr(fname, &sx) < 0)
196 maybe_emit_filelist_progress(flist->count + flist_count_offset);
198 @@ -1166,6 +1178,10 @@ static struct file_struct *send_file_nam
202 +#ifdef SUPPORT_XATTRS
203 + if (preserve_xattrs)
204 + send_xattr(&sx, f);
209 --- old/lib/sysxattr.c
210 +++ new/lib/sysxattr.c
213 + * Extended attribute support for rsync.
215 + * Copyright (C) 2004 Red Hat, Inc.
216 + * Written by Jay Fenlason.
218 + * This program is free software; you can redistribute it and/or modify
219 + * it under the terms of the GNU General Public License as published by
220 + * the Free Software Foundation; either version 2 of the License, or
221 + * (at your option) any later version.
223 + * This program is distributed in the hope that it will be useful,
224 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
225 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
226 + * GNU General Public License for more details.
228 + * You should have received a copy of the GNU General Public License along
229 + * with this program; if not, write to the Free Software Foundation, Inc.,
230 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
234 +#include "sysxattr.h"
236 +#ifdef SUPPORT_XATTRS
238 +#if defined HAVE_LINUX_XATTRS
240 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
242 + return lgetxattr(path, name, value, size);
245 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
247 + return fgetxattr(filedes, name, value, size);
250 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
252 + return lsetxattr(path, name, value, size, 0);
255 +int sys_lremovexattr(const char *path, const char *name)
257 + return lremovexattr(path, name);
260 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
262 + return llistxattr(path, list, size);
265 +#elif HAVE_OSX_XATTRS
267 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
269 + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
272 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
274 + return fgetxattr(filedes, name, value, size, 0, 0);
277 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
279 + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
282 +int sys_lremovexattr(const char *path, const char *name)
284 + return removexattr(path, name, XATTR_NOFOLLOW);
287 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
289 + return listxattr(path, list, size, XATTR_NOFOLLOW);
292 +#elif HAVE_FREEBSD_XATTRS
294 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
296 + return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
299 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
301 + return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
304 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
306 + return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
309 +int sys_lremovexattr(const char *path, const char *name)
311 + return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
314 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
316 + unsigned char keylen;
317 + ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
319 + if (len <= 0 || (size_t)len > size)
322 + /* FreeBSD puts a single-byte length before each string, with no '\0'
323 + * terminator. We need to change this into a series of null-terminted
324 + * strings. Since the size is the same, we can simply transform the
325 + * output in place. */
326 + for (off = 0; off < len; off += keylen + 1) {
327 + keylen = ((unsigned char*)list)[off];
328 + if (off + keylen >= len) {
329 + /* Should be impossible, but kernel bugs happen! */
333 + memmove(list+off, list+off+1, keylen);
334 + list[off+keylen] = '\0';
342 +#error You need to create xattr compatibility functions.
346 +#endif /* SUPPORT_XATTRS */
347 --- old/lib/sysxattr.h
348 +++ new/lib/sysxattr.h
350 +#ifdef SUPPORT_XATTRS
352 +#if defined HAVE_ATTR_XATTR_H
353 +#include <attr/xattr.h>
354 +#elif defined HAVE_SYS_XATTR_H
355 +#include <sys/xattr.h>
356 +#elif defined HAVE_SYS_EXTATTR_H
357 +#include <sys/extattr.h>
360 +/* Linux 2.4 does not define this as a distinct errno value: */
362 +#define ENOATTR ENODATA
365 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
366 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
367 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
368 +int sys_lremovexattr(const char *path, const char *name);
369 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
373 +/* No xattrs available */
378 @@ -47,6 +47,7 @@ int copy_links = 0;
379 int preserve_links = 0;
380 int preserve_hard_links = 0;
381 int preserve_acls = 0;
382 +int preserve_xattrs = 0;
383 int preserve_perms = 0;
384 int preserve_executability = 0;
385 int preserve_devices = 0;
386 @@ -200,6 +201,7 @@ static void print_rsync_version(enum log
387 char const *have_inplace = "no ";
388 char const *hardlinks = "no ";
389 char const *acls = "no ";
390 + char const *xattrs = "no ";
391 char const *links = "no ";
392 char const *ipv6 = "no ";
393 STRUCT_STAT *dumstat;
394 @@ -219,7 +221,9 @@ static void print_rsync_version(enum log
399 +#ifdef SUPPORT_XATTRS
405 @@ -238,8 +242,8 @@ static void print_rsync_version(enum log
406 (int)(sizeof (int64) * 8));
407 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
408 got_socketpair, hardlinks, links, ipv6, have_inplace);
409 - rprintf(f, " %sappend, %sACLs\n",
410 - have_inplace, acls);
411 + rprintf(f, " %sappend, %sACLs, %sxattrs\n",
412 + have_inplace, acls, xattrs);
414 #ifdef MAINTAINER_MODE
415 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
416 @@ -285,7 +289,7 @@ void usage(enum logcode F)
417 rprintf(F," -q, --quiet suppress non-error messages\n");
418 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
419 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
420 - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
421 + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
422 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
423 rprintf(F," -r, --recursive recurse into directories\n");
424 rprintf(F," -R, --relative use relative path names\n");
425 @@ -310,6 +314,9 @@ void usage(enum logcode F)
427 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
429 +#ifdef SUPPORT_XATTRS
430 + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
432 rprintf(F," -o, --owner preserve owner (super-user only)\n");
433 rprintf(F," -g, --group preserve group\n");
434 rprintf(F," --devices preserve device files (super-user only)\n");
435 @@ -433,6 +440,9 @@ static struct poptOption long_options[]
436 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
437 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
438 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
439 + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
440 + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
441 + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
442 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
443 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
444 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
445 @@ -1121,6 +1131,17 @@ int parse_arguments(int *argc, const cha
450 +#ifdef SUPPORT_XATTRS
451 + preserve_xattrs = 1;
452 + preserve_perms = 1;
455 + snprintf(err_buf,sizeof(err_buf),
456 + "extended attributes are not supported on this %s\n",
457 + am_server ? "server" : "client");
462 /* A large opt value means that set_refuse_options()
463 @@ -1585,6 +1606,10 @@ void server_options(char **args,int *arg
467 +#ifdef SUPPORT_XATTRS
468 + if (preserve_xattrs)
479 extern int preserve_acls;
480 +extern int preserve_xattrs;
481 extern int preserve_perms;
482 extern int preserve_executability;
483 extern int preserve_times;
484 @@ -321,6 +322,10 @@ int set_file_attrs(char *fname, struct f
485 if (daemon_chmod_modes && !S_ISLNK(new_mode))
486 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
488 +#ifdef SUPPORT_XATTRS
489 + if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
493 /* It's OK to call set_acl() now, even for a dir, as the generator
494 * will enable owner-writability using chmod, if necessary.
497 @@ -555,6 +555,10 @@ struct idev_node {
498 #define ACLS_NEED_MASK 1
501 +#ifndef HAVE_NO_XATTRS
502 +#define SUPPORT_XATTRS 1
505 #define GID_NONE ((gid_t)-1)
508 @@ -575,6 +579,7 @@ extern int file_extra_cnt;
509 extern int preserve_uid;
510 extern int preserve_gid;
511 extern int preserve_acls;
512 +extern int preserve_xattrs;
514 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
515 #define EXTRA_LEN (sizeof (union file_extras))
516 @@ -608,6 +613,7 @@ extern int preserve_acls;
517 #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
518 #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
519 #define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
520 +#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->unum
522 /* These items are per-entry optional and mutally exclusive: */
523 #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
524 @@ -799,6 +805,9 @@ typedef struct {
525 struct rsync_acl *acc_acl; /* access ACL */
526 struct rsync_acl *def_acl; /* default ACL */
528 +#ifdef SUPPORT_XATTRS
533 #define ACL_READY(sx) ((sx).acc_acl != NULL)
536 @@ -301,7 +301,7 @@ to the detailed description below for a
537 -q, --quiet suppress non-error messages
538 --no-motd suppress daemon-mode MOTD (see caveat)
539 -c, --checksum skip based on checksum, not mod-time & size
540 - -a, --archive archive mode; same as -rlptgoD (no -H, -A)
541 + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
542 --no-OPTION turn off an implied OPTION (e.g. --no-D)
543 -r, --recursive recurse into directories
544 -R, --relative use relative path names
545 @@ -324,6 +324,7 @@ to the detailed description below for a
546 -E, --executability preserve executability
547 --chmod=CHMOD affect file and/or directory permissions
548 -A, --acls preserve ACLs (implies -p) [non-standard]
549 + -X, --xattrs preserve extended attrs (implies -p) [n.s.]
550 -o, --owner preserve owner (super-user only)
551 -g, --group preserve group
552 --devices preserve device files (super-user only)
553 @@ -835,6 +836,11 @@ The ACL-sending protocol used by this ve
554 the patch that was shipped with 2.6.8. Sending ACLs to an older version
555 of the ACL patch is not supported.
557 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
558 +extended attributes to be the same as the local ones. This will work
559 +only if the remote machine's rsync supports this option also. This is
560 +a non-standard option.
562 dit(bf(--chmod)) This option tells rsync to apply one or more
563 comma-separated "chmod" strings to the permission of the files in the
564 transfer. The resulting value is treated as though it was the permissions
569 + * Extended Attribute support for rsync.
570 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
572 + * Copyright (C) 2004 Red Hat, Inc.
573 + * Copyright (C) 2006 Wayne Davison
575 + * This program is free software; you can redistribute it and/or modify
576 + * it under the terms of the GNU General Public License as published by
577 + * the Free Software Foundation; either version 2 of the License, or
578 + * (at your option) any later version.
580 + * This program is distributed in the hope that it will be useful,
581 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
582 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
583 + * GNU General Public License for more details.
585 + * You should have received a copy of the GNU General Public License along
586 + * with this program; if not, write to the Free Software Foundation, Inc.,
587 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
591 +#include "lib/sysxattr.h"
593 +#ifdef SUPPORT_XATTRS
597 +extern int read_only;
598 +extern int list_only;
600 +#define RSYNC_XAL_INITIAL 5
601 +#define RSYNC_XAL_LIST_INITIAL 100
603 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
604 + && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
606 +#define USER_PREFIX "user."
607 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
608 +#define SYSTEM_PREFIX "system."
609 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
611 +#ifdef HAVE_LINUX_XATTRS
614 +#define RSYNC_PREFIX "rsync."
615 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
619 + char *datum, *name;
620 + size_t datum_len, name_len;
623 +static size_t namebuf_len = 0;
624 +static char *namebuf = NULL;
626 +static item_list empty_xattr = EMPTY_ITEM_LIST;
627 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
629 +/* ------------------------------------------------------------------------- */
631 +static void rsync_xal_free(item_list *xalp)
634 + rsync_xa *rxas = xalp->items;
636 + for (i = 0; i < xalp->count; i++) {
637 + free(rxas[i].datum);
638 + /*free(rxas[i].name);*/
643 +void free_xattr(statx *sxp)
645 + rsync_xal_free(sxp->xattr);
650 +static int rsync_xal_compare_names(const void *x1, const void *x2)
652 + const rsync_xa *xa1 = x1;
653 + const rsync_xa *xa2 = x2;
654 + return strcmp(xa1->name, xa2->name);
657 +static ssize_t rsync_xal_list(const char *fname)
662 + namebuf_len = 1024;
663 + namebuf = new_array(char, namebuf_len);
665 + out_of_memory("rsync_xal_list");
668 + /* The length returned includes all the '\0' terminators. */
669 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
670 + if (list_len > (ssize_t)namebuf_len) {
674 + if (list_len < 0) {
675 + if (errno == ENOTSUP)
677 + if (errno == ERANGE) {
678 + list_len = sys_llistxattr(fname, NULL, 0);
679 + if (list_len < 0) {
680 + rsyserr(FERROR, errno,
681 + "rsync_xal_list: llistxattr(\"%s\",0) failed",
685 + namebuf = realloc_array(namebuf, char, list_len + 1024);
687 + out_of_memory("rsync_xal_list");
688 + namebuf_len = list_len + 1024;
689 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
690 + if (list_len < 0) {
691 + rsyserr(FERROR, errno,
692 + "rsync_xal_list: llistxattr(\"%s\",%ld) failed",
693 + fname, (long)namebuf_len);
697 + rsyserr(FERROR, errno,
698 + "rsync_xal_list: llistxattr(\"%s\",%ld) failed",
699 + fname, (long)namebuf_len);
707 +static int rsync_xal_get(const char *fname, item_list *xalp)
709 + ssize_t list_len, name_len, datum_len;
712 + /* This puts the name list into the "namebuf" buffer. */
713 + if ((list_len = rsync_xal_list(fname)) < 0)
716 + for (name = namebuf; list_len > 0; name += name_len) {
719 + name_len = strlen(name) + 1;
720 + list_len -= name_len;
722 +#ifdef HAVE_LINUX_XATTRS
723 + /* We don't send the system namespace. */
724 + if (HAS_PREFIX(name, SYSTEM_PREFIX))
728 + datum_len = sys_lgetxattr(fname, name, NULL, 0);
729 + if (datum_len < 0) {
730 + if (errno == ENOTSUP)
732 + rsyserr(FERROR, errno,
733 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",0) failed",
737 + ptr = new_array(char, name_len + datum_len);
739 + out_of_memory("rsync_xal_get");
741 + ssize_t len = sys_lgetxattr(fname, name, ptr, datum_len);
742 + if (len != datum_len) {
744 + rsyserr(FERROR, errno,
745 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
746 + " failed", fname, name, (long)datum_len);
749 + "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
750 + " returned %ld\n", fname, name,
751 + (long)datum_len, (long)len);
757 + rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
758 + rxas->name = ptr + datum_len;
760 + rxas->name_len = name_len;
761 + rxas->datum_len = datum_len;
762 + memcpy(rxas->name, name, name_len);
764 + if (xalp->count > 1)
765 + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
769 +/* Read the xattr(s) for this filename. */
770 +int get_xattr(const char *fname, statx *sxp)
772 + sxp->xattr = new(item_list);
773 + *sxp->xattr = empty_xattr;
774 + if (rsync_xal_get(fname, sxp->xattr) < 0) {
781 +static int find_matching_xattr(item_list *xalp)
784 + item_list *lst = rsync_xal_l.items;
786 + for (i = 0; i < rsync_xal_l.count; i++) {
787 + rsync_xa *rxas1 = lst[i].items;
788 + rsync_xa *rxas2 = xalp->items;
790 + /* Wrong number of elements? */
791 + if (lst[i].count != xalp->count)
793 + /* any elements different? */
794 + for (j = 0; j < xalp->count; j++) {
795 + if (rxas1[j].name_len != rxas2[j].name_len
796 + || rxas1[j].datum_len != rxas2[j].datum_len
797 + || strcmp(rxas1[j].name, rxas2[j].name)
798 + || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
801 + /* no differences found. This is The One! */
802 + if (j == xalp->count)
809 +/* Store *xalp on the end of rsync_xal_l */
810 +static void rsync_xal_store(item_list *xalp)
812 + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
813 + /* Since the following call starts a new list, we know it will hold the
814 + * entire initial-count, not just enough space for one new item. */
815 + *new_lst = empty_xattr;
816 + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
817 + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
818 + new_lst->count = xalp->count;
822 +/* Send the make_xattr()-generated xattr list for this flist entry. */
823 +void send_xattr(statx *sxp, int f)
825 + int ndx = find_matching_xattr(sxp->xattr);
827 + write_byte(f, 'x');
829 + rsync_xal_free(sxp->xattr);
832 + int count = sxp->xattr->count;
833 + write_byte(f, 'X');
834 + write_int(f, count);
835 + for (rxa = sxp->xattr->items; count--; rxa++) {
836 +#ifdef HAVE_LINUX_XATTRS
837 + write_int(f, rxa->name_len);
838 + write_int(f, rxa->datum_len);
839 + write_buf(f, rxa->name, rxa->name_len);
841 + /* We strip the rsync prefix from disguised namespaces
842 + * and put everything else in the user namespace. */
843 + if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
844 + && rxa->name[RPRE_LEN] != '%') {
845 + write_int(f, rxa->name_len - RPRE_LEN);
846 + write_int(f, rxa->datum_len);
847 + write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
849 + write_int(f, rxa->name_len + UPRE_LEN);
850 + write_int(f, rxa->datum_len);
851 + write_buf(f, USER_PREFIX, UPRE_LEN);
852 + write_buf(f, rxa->name, rxa->name_len);
855 + write_buf(f, rxa->datum, rxa->datum_len);
857 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
862 +/* ------------------------------------------------------------------------- */
864 +/* receive and build the rsync_xattr_lists */
865 +void receive_xattr(struct file_struct *file, int f)
867 + static item_list temp_xattr = EMPTY_ITEM_LIST;
868 + int ndx, tag = read_byte(f);
871 + int i, count = read_int(f);
872 + for (i = 0; i < count; i++) {
875 + size_t name_len = read_int(f);
876 + size_t datum_len = read_int(f);
877 +#ifdef HAVE_LINUX_XATTRS
878 + size_t extra_len = 0;
880 + size_t extra_len = am_root ? RPRE_LEN : 0;
881 + if (datum_len + extra_len < datum_len)
882 + out_of_memory("receive_xattr"); /* overflow */
884 + if (name_len + datum_len + extra_len < name_len)
885 + out_of_memory("receive_xattr"); /* overflow */
886 + ptr = new_array(char, name_len + datum_len + extra_len);
888 + out_of_memory("receive_xattr");
889 + name = ptr + datum_len + extra_len;
890 + read_buf(f, name, name_len);
891 + read_buf(f, ptr, datum_len);
892 +#ifdef HAVE_LINUX_XATTRS
893 + /* Non-root can only save the user namespace. */
894 + if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
899 + /* This OS only has a user namespace, so we either
900 + * strip the user prefix, or we put a non-user
901 + * namespace inside our rsync hierarchy. */
902 + if (HAS_PREFIX(name, USER_PREFIX)) {
904 + name_len -= UPRE_LEN;
905 + } else if (am_root) {
907 + name_len += RPRE_LEN;
908 + memcpy(name, RSYNC_PREFIX, RPRE_LEN);
914 + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
917 + rxa->name_len = name_len;
918 + rxa->datum_len = datum_len;
920 + ndx = rsync_xal_l.count; /* pre-incremented count */
921 + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
922 + } else if (tag == 'x') {
924 + if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
925 + rprintf(FERROR, "receive_xattr: xa index %d out of"
926 + " range for %s\n", ndx, f_name(file, NULL));
927 + exit_cleanup(RERR_STREAMIO);
930 + rprintf(FERROR, "receive_xattr: unknown extended attribute"
931 + " type tag (%c) for %s\n", tag, f_name(file, NULL));
932 + exit_cleanup(RERR_STREAMIO);
935 + F_XATTR(file) = ndx;
938 +/* Turn the xattr data in statx into cached xattr data, setting the index
939 + * values in the file struct. */
940 +void cache_xattr(struct file_struct *file, statx *sxp)
947 + ndx = find_matching_xattr(sxp->xattr);
949 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
952 + F_XATTR(file) = ndx;
955 +static int rsync_xal_set(const char *fname, item_list *xalp)
957 + rsync_xa *rxas = xalp->items;
961 + int name_len, ret = 0;
963 + /* This puts the current name list into the "namebuf" buffer. */
964 + if ((list_len = rsync_xal_list(fname)) < 0)
967 + for (i = 0; i < xalp->count; i++) {
968 + int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len);
970 + rsyserr(FERROR, errno,
971 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
972 + fname, rxas[i].name);
977 + /* Remove any extraneous names. */
978 + for (name = namebuf; list_len > 0; name += name_len) {
979 + name_len = strlen(name) + 1;
980 + list_len -= name_len;
982 +#ifdef HAVE_LINUX_XATTRS
983 + /* We always ignore the system namespace, and non-root
984 + * ignores everything but the user namespace. */
985 + if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
986 + : !HAS_PREFIX(name, USER_PREFIX))
990 + for (i = 0; i < xalp->count; i++) {
991 + if (strcmp(name, rxas[i].name) == 0)
994 + if (i == xalp->count) {
995 + int status = sys_lremovexattr(fname, name);
997 + rsyserr(FERROR, errno,
998 + "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
1008 +/* Set extended attributes on indicated filename. */
1009 +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
1012 + item_list *lst = rsync_xal_l.items;
1015 + return 1; /* FIXME: --dry-run needs to compute this value */
1017 + if (read_only || list_only) {
1022 + ndx = F_XATTR(file);
1023 + return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
1026 +#endif /* SUPPORT_XATTRS */