-+#ifdef SUPPORT_XATTRS
-+ xattrs = "";
-+#endif
- #ifdef SUPPORT_LINKS
- links = "";
- #endif
-@@ -229,9 +233,9 @@ static void print_rsync_version(enum log
- rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
- rprintf(f, "<http://rsync.samba.org/>\n");
- rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
-- "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
-+ "%shard links, %sACLs, %sxattrs, %ssymlinks, batchfiles,\n",
- (int) (sizeof (OFF_T) * 8),
-- got_socketpair, hardlinks, acls, links);
-+ got_socketpair, hardlinks, acls, xattrs, links);
-
- /* Note that this field may not have type ino_t. It depends
- * on the complicated interaction between largefile feature
-@@ -303,6 +307,9 @@ void usage(enum logcode F)
- #ifdef SUPPORT_ACLS
- rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
- #endif
-+#ifdef SUPPORT_XATTRS
-+ rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
-+#endif
- rprintf(F," --chmod=CHMOD change destination permissions\n");
- rprintf(F," -o, --owner preserve owner (super-user only)\n");
- rprintf(F," -g, --group preserve group\n");
-@@ -423,6 +430,9 @@ static struct poptOption long_options[]
- {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
- {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
- {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
-+ {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
-+ {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
-+ {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
- {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
- {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
- {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
-@@ -1088,6 +1098,17 @@ int parse_arguments(int *argc, const cha
- return 0;
- #endif
-
-+ case 'X':
-+#ifdef SUPPORT_XATTRS
-+ preserve_xattrs = 1;
-+ preserve_perms = 1;
-+ break;
-+#else
-+ snprintf(err_buf,sizeof(err_buf),
-+ "extended attributes are not supported on this %s\n",
-+ am_server ? "server" : "client");
-+ return 0;
-+#endif /* SUPPORT_XATTRS */
-
- default:
- /* A large opt value means that set_refuse_options()
-@@ -1532,6 +1553,10 @@ void server_options(char **args,int *arg
- if (preserve_acls)
- argstr[x++] = 'A';
- #endif
-+#ifdef SUPPORT_XATTRS
-+ if (preserve_xattrs)
-+ argstr[x++] = 'X';
-+#endif
- if (preserve_uid)
- argstr[x++] = 'o';
- if (preserve_gid)
---- old/rsync.c
-+++ new/rsync.c
-@@ -215,12 +215,15 @@ int set_file_attrs(char *fname, struct f
- }
- #endif
-
-- /* If this is a directory, SET_ACL() will be called on the cleanup
-- * receive_generator() pass (if we called it here, we might clobber
-- * writability on the directory). Everything else is OK to do now. */
-+ /* If this is a directory, SET_ACL() and/or SET_XATTR() will be called
-+ * on the cleanup receive_generator() pass -- if we called it here, we
-+ * might clobber writability on the dir (SELinux security contexts are
-+ * stored in xattrs). Everything else is OK to do now. */
- if (!S_ISDIR(st->st_mode)) {
- if (SET_ACL(fname, file) == 0)
- updated = 1;
-+ if (SET_XATTR(fname, file) == 0)
-+ updated = 1;
- }
-
- if (verbose > 1 && flags & ATTRS_REPORT) {
---- old/rsync.h
-+++ new/rsync.h
-@@ -695,6 +695,38 @@ struct chmod_mode_struct;
- #endif /* SUPPORT_ACLS */
- #include "smb_acls.h"
-
-+#ifdef HAVE_LINUX_XATTRS
-+#define SUPPORT_XATTRS 1
-+#endif
-+
-+#ifdef SUPPORT_XATTRS
-+#ifdef HAVE_ATTR_XATTR_H
-+#include <attr/xattr.h>
-+#endif
-+#define MAKE_XATTR(file, fname) make_xattr(file, fname)
-+#define SEND_XATTR(file, f) send_xattr(file, f)
-+#define RECEIVE_XATTR(file, f) receive_xattr(file, f)
-+#define SORT_FILE_XATTR_INDEX_LISTS() sort_file_xattr_index_lists()
-+#define SET_XATTR(fname, file) set_xattr(fname, file)
-+#define NEXT_XATTR_UID() next_xattr_uid()
-+#define XATTR_UID_MAP(uid) xattr_uid_map(uid)
-+#define PUSH_KEEP_BACKUP_XATTR(file, orig, dest) \
-+ push_keep_backup_xattr(file, orig, dest)
-+#define CLEANUP_KEEP_BACKUP_XATTR() cleanup_keep_backup_xattr()
-+#define DUP_XATTR(orig, dest) dup_xattr(orig, dest)
-+#else /* SUPPORT_XATTRS */
-+#define MAKE_XATTR(file, fname) 1 /* checked return value */
-+#define SEND_XATTR(file, f)
-+#define RECEIVE_XATTR(file, f)
-+#define SORT_FILE_XATTR_INDEX_LISTS()
-+#define SET_XATTR(fname, file) 1 /* checked return value */
-+#define NEXT_XATTR_UID()
-+#define XATTR_UID_MAP(uid)
-+#define PUSH_KEEP_BACKUP_XATTR(file, orig, dest)
-+#define CLEANUP_KEEP_BACKUP_XATTR()
-+#define DUP_XATTR(src, orig) 1 /* checked return value */
-+#endif /* SUPPORT_XATTRS */
-+
- #include "proto.h"
-
- /* We have replacement versions of these if they're missing. */
---- old/rsync.yo
-+++ new/rsync.yo
-@@ -318,6 +318,7 @@ to the detailed description below for a
- -p, --perms preserve permissions
- -E, --executability preserve executability
- -A, --acls preserve ACLs (implies -p) [non-standard]
-+ -X, --xattrs preserve extended attrs (implies -p) [n.s.]
- --chmod=CHMOD change destination permissions
- -o, --owner preserve owner (super-user only)
- -g, --group preserve group
-@@ -752,6 +753,11 @@ dit(bf(-A, --acls)) This option causes r
- ACLs to be the same as the source ACLs. This nonstandard option only
- works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
-
-+dit(bf(-X, --xattrs)) This option causes rsync to update the remote
-+extended attributes to be the same as the local ones. This will work
-+only if the remote machine's rsync supports this option also. This is
-+a non-standard option.
-+
- dit(bf(--chmod)) This option tells rsync to apply one or more
- comma-separated "chmod" strings to the permission of the files in the
- transfer. The resulting value is treated as though it was the permissions
---- old/xattr.c
-+++ new/xattr.c
-@@ -0,0 +1,540 @@
-+/* Extended Attribute support for rsync */
-+/* Copyright (C) 2004 Red Hat, Inc */
-+/* Written by Jay Fenlason, vaguely based on the ACLs patch */
-+
-+/* This program is free software; you can redistribute it and/or modify
-+ it under the terms of the GNU General Public License as published by
-+ the Free Software Foundation; either version 2 of the License, or
-+ (at your option) any later version.
-+
-+ This program is distributed in the hope that it will be useful,
-+ but WITHOUT ANY WARRANTY; without even the implied warranty of
-+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-+ GNU General Public License for more details.
-+
-+ You should have received a copy of the GNU General Public License
-+ along with this program; if not, write to the Free Software
-+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-+*/
-+
-+#include "rsync.h"
-+#include "lib/sysxattr.h"
-+
-+#ifdef SUPPORT_XATTRS
-+
-+extern int preserve_xattrs;
-+extern int dry_run;
-+
-+#define RSYNC_XAL_INITIAL 5
-+#define RSYNC_XAL_LIST_INITIAL 100
-+
-+typedef struct {
-+ size_t name_len;
-+ char *name;
-+ size_t datum_len;
-+ char *datum;
-+} rsync_xa;
-+
-+typedef struct {
-+ size_t count;
-+ size_t alloc;
-+ rsync_xa *rxas;
-+} rsync_xal;
-+
-+typedef struct {
-+ size_t count;
-+ size_t alloc;
-+ rsync_xal *rxals;
-+} rsync_xal_list;
-+
-+static size_t namebuf_len = 0;
-+static char *namebuf = NULL;
-+
-+static size_t datumbuf_len = 0;
-+static char *datumbuf = NULL;
-+
-+static rsync_xal curr_rsync_xal = { 0, 0, NULL };
-+static rsync_xal_list rsync_xal_l = { 0, 0, NULL };
-+
-+
-+/* ------------------------------------------------------------------------- */
-+
-+/* the below stuff is only used by the receiver */
-+
-+/* structure to hold index to rsync_xal_l member corresponding to
-+ * flist->files[i] */
-+
-+typedef struct {
-+ const struct file_struct *file;
-+ int xalidx;
-+} file_xal_index;
-+
-+typedef struct {
-+ size_t count;
-+ size_t alloc;
-+ file_xal_index *filexalidxs;
-+} file_xal_index_list;
-+
-+static file_xal_index_list fxil = {0, 0, NULL };
-+
-+/* stuff for redirecting calls to set_acl() from set_perms()
-+ * for keep_backup() */
-+static const struct file_struct *backup_orig_file = NULL;
-+static const char null_string[] = "";
-+static const char *backup_orig_fname = null_string;
-+static const char *backup_dest_fname = null_string;
-+static rsync_xal backup_xal;
-+
-+/* ------------------------------------------------------------------------- */
-+
-+static void rsync_xal_free(rsync_xal *x)
-+{
-+ size_t i;
-+
-+ for (i = 0; i < x->count; i++) {
-+ free(x->rxas[i].name);
-+ /* free(x->rxas[i].value); */
-+ }
-+ x->count = 0;
-+}
-+
-+static int rsync_xal_compare_names(const void *x1, const void *x2)
-+{
-+ const rsync_xa *xa1;
-+ const rsync_xa *xa2;
-+
-+ xa1 = x1;
-+ xa2 = x2;
-+ return strcmp(xa1->name, xa2->name);
-+}
-+
-+static int rsync_xal_get(const char *fname, rsync_xal *x)
-+{
-+ ssize_t name_size;
-+ ssize_t datum_size;
-+ ssize_t left;
-+ char *name;
-+ size_t len;
-+ char *ptr;
-+
-+ if (!namebuf) {
-+ namebuf_len = 100;
-+ namebuf = new_array(char, namebuf_len);
-+ datumbuf_len = 100;
-+ datumbuf = new_array(char, datumbuf_len);
-+ if (!namebuf || !datumbuf)
-+ out_of_memory("rsync_xal_get");
-+ }
-+
-+ name_size = sys_llistxattr(fname, namebuf, namebuf_len);
-+ if (name_size > (ssize_t)namebuf_len) {
-+ name_size = -1;
-+ errno = ERANGE;
-+ }
-+ if (name_size < 0) {
-+ if (errno == ENOTSUP)
-+ return -1;
-+ if (errno == ERANGE) {
-+ name_size = sys_llistxattr(fname, NULL, 0);
-+ if (name_size < 0) {
-+ rprintf(FERROR, "%s: rsync_xal_get: llistxattr: %s\n",
-+ fname, strerror(errno));
-+ return -1;
-+ }
-+ namebuf = realloc_array(namebuf, char, name_size + 1);
-+ if (!namebuf)
-+ out_of_memory("rsync_xal_get");
-+ namebuf_len = name_size;
-+ name_size = sys_llistxattr(fname, namebuf, namebuf_len);
-+ if (name_size < 0) {
-+ rprintf(FERROR,
-+ "%s: rsync_xal_get: re-llistxattr failed: %s\n",
-+ fname, strerror(errno));
-+ return -1;
-+ }
-+ } else {
-+ rprintf(FERROR,
-+ "%s: rsync_xal_get: llistxattr failed: %s\n",
-+ fname, strerror(errno));
-+ return -1;