- temp_copy_name = partialptr;
-- finish_transfer(fname, fnametmp, temp_copy_name,
-- file, recv_ok, 1);
-+ finish_transfer(fname, fnametmp, fnamecmp,
-+ temp_copy_name, file, recv_ok, 1);
- if (fnamecmp == partialptr) {
- do_unlink(partialptr);
- handle_partial_dir(partialptr, PDIR_DELETE);
- }
- } else if (keep_partial && partialptr
- && handle_partial_dir(partialptr, PDIR_CREATE)) {
-- finish_transfer(partialptr, fnametmp, NULL,
-+ finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
- file, recv_ok, !partial_dir);
- if (delay_updates && recv_ok) {
- bitbag_set_bit(delayed_bits, ndx);
---- old/rsync.c
-+++ new/rsync.c
-@@ -32,6 +32,7 @@
- extern int verbose;
- extern int dry_run;
- extern int preserve_acls;
-+extern int preserve_xattrs;
- extern int preserve_perms;
- extern int preserve_executability;
- extern int preserve_times;
-@@ -91,10 +92,8 @@ void setup_iconv()
- }
- #endif
-
--/* This is used by sender.c with a valid f_out, and by receive.c with
-- * f_out = -1. */
--int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr,
-- uchar *type_ptr, char *buf, int *len_ptr)
-+int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
-+ char *buf, int *len_ptr)
- {
- int len, iflags = 0;
- struct file_list *flist;
-@@ -181,11 +180,6 @@ int read_ndx_and_attrs(int f_in, int f_o
- ndx, who_am_i());
- exit_cleanup(RERR_PROTOCOL);
- }
-- } else if (f_out >= 0) {
-- if (inc_recurse)
-- send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
-- write_ndx_and_attrs(f_out, ndx, iflags,
-- fnamecmp_type, buf, len);
- }
-
- *iflag_ptr = iflags;
-@@ -228,8 +222,8 @@ mode_t dest_mode(mode_t flist_mode, mode
- return new_mode;
- }
-
--int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
-- int flags)
-+int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp,
-+ const char *fnamecmp, int flags)
- {
- int updated = 0;
- statx sx2;
-@@ -247,6 +241,9 @@ int set_file_attrs(char *fname, struct f
- #ifdef SUPPORT_ACLS
- sx2.acc_acl = sx2.def_acl = NULL;
- #endif
-+#ifdef SUPPORT_XATTRS
-+ sx2.xattr = NULL;
-+#endif
- if (!preserve_perms && S_ISDIR(new_mode)
- && sx2.st.st_mode & S_ISGID) {
- /* We just created this directory and its setgid
-@@ -321,6 +318,10 @@ int set_file_attrs(char *fname, struct f
- if (daemon_chmod_modes && !S_ISLNK(new_mode))
- new_mode = tweak_mode(new_mode, daemon_chmod_modes);
-
-+#ifdef SUPPORT_XATTRS
-+ if (preserve_xattrs && fnamecmp)
-+ set_xattr(fname, file, fnamecmp);
-+#endif
- #ifdef SUPPORT_ACLS
- /* It's OK to call set_acl() now, even for a dir, as the generator
- * will enable owner-writability using chmod, if necessary.
-@@ -353,10 +354,16 @@ int set_file_attrs(char *fname, struct f
- rprintf(FCLIENT, "%s is uptodate\n", fname);
- }
- cleanup:
-+ if (sxp == &sx2) {
- #ifdef SUPPORT_ACLS
-- if (preserve_acls && sxp == &sx2)
-- free_acl(&sx2);
-+ if (preserve_acls)
-+ free_acl(&sx2);
-+#endif
-+#ifdef SUPPORT_XATTRS
-+ if (preserve_xattrs)
-+ free_xattr(&sx2);
- #endif
-+ }
- return updated;
- }
-
-@@ -378,7 +385,8 @@ RETSIGTYPE sig_int(UNUSED(int val))
- * attributes (e.g. permissions, ownership, etc.). If partialptr is not
- * NULL and the robust_rename() call is forced to copy the temp file, we
- * stage the file into the partial-dir and then rename it into place. */
--void finish_transfer(char *fname, char *fnametmp, char *partialptr,
-+void finish_transfer(const char *fname, const char *fnametmp,
-+ const char *fnamecmp, const char *partialptr,
- struct file_struct *file, int ok_to_set_time,
- int overwriting_basis)
- {
-@@ -395,7 +403,7 @@ void finish_transfer(char *fname, char *
- return;
-
- /* Change permissions before putting the file into place. */
-- set_file_attrs(fnametmp, file, NULL,
-+ set_file_attrs(fnametmp, file, NULL, fnamecmp,
- ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
-
- /* move tmp file over real file */
-@@ -419,7 +427,7 @@ void finish_transfer(char *fname, char *
- fnametmp = partialptr ? partialptr : fname;
-
- do_set_file_attrs:
-- set_file_attrs(fnametmp, file, NULL,
-+ set_file_attrs(fnametmp, file, NULL, fnamecmp,
- ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
-
- if (partialptr) {
---- old/rsync.h
-+++ new/rsync.h
-@@ -569,6 +569,7 @@ extern int file_extra_cnt;
- extern int preserve_uid;
- extern int preserve_gid;
- extern int preserve_acls;
-+extern int preserve_xattrs;
-
- #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
- #define EXTRA_LEN (sizeof (union file_extras))
-@@ -601,7 +602,8 @@ extern int preserve_acls;
- /* When the associated option is on, all entries will have these present: */
- #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
- #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
--#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
-+#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->num
-+#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->num
-
- /* These items are per-entry optional and mutally exclusive: */
- #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
-@@ -793,9 +795,13 @@ typedef struct {
- struct rsync_acl *acc_acl; /* access ACL */
- struct rsync_acl *def_acl; /* default ACL */
- #endif
-+#ifdef SUPPORT_XATTRS
-+ item_list *xattr;
-+#endif
- } statx;
-
- #define ACL_READY(sx) ((sx).acc_acl != NULL)
-+#define XATTR_READY(sx) ((sx).xattr != NULL)
-
- #include "proto.h"
-
---- old/rsync.yo
-+++ new/rsync.yo
-@@ -301,7 +301,7 @@ to the detailed description below for a
- -q, --quiet suppress non-error messages
- --no-motd suppress daemon-mode MOTD (see caveat)
- -c, --checksum skip based on checksum, not mod-time & size
-- -a, --archive archive mode; same as -rlptgoD (no -H, -A)
-+ -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
- --no-OPTION turn off an implied OPTION (e.g. --no-D)
- -r, --recursive recurse into directories
- -R, --relative use relative path names
-@@ -324,6 +324,7 @@ to the detailed description below for a
- -E, --executability preserve executability
- --chmod=CHMOD affect file and/or directory permissions
- -A, --acls preserve ACLs (implies -p)
-+ -X, --xattrs preserve extended attrs (implies -p)
- -o, --owner preserve owner (super-user only)
- -g, --group preserve group
- --devices preserve device files (super-user only)
-@@ -831,6 +832,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/sender.c
-+++ new/sender.c
-@@ -22,6 +22,7 @@
- #include "rsync.h"
-
- extern int verbose;
-+extern int dry_run;
- extern int do_xfers;
- extern int am_server;
- extern int am_daemon;
-@@ -144,8 +145,9 @@ void successful_send(int ndx)
- rsyserr(FERROR, errno, "sender failed to remove %s", fname);
- }
-
--void write_ndx_and_attrs(int f_out, int ndx, int iflags,
-- uchar fnamecmp_type, char *buf, int len)
-+static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
-+ const char *fname, struct file_struct *file,
-+ uchar fnamecmp_type, char *buf, int len)
- {
- write_ndx(f_out, ndx);
- if (protocol_version < 29)
-@@ -155,6 +157,10 @@ void write_ndx_and_attrs(int f_out, int
- write_byte(f_out, fnamecmp_type);
- if (iflags & ITEM_XNAME_FOLLOWS)
- write_vstring(f_out, buf, len);
-+#ifdef SUPPORT_XATTRS
-+ if (iflags & ITEM_REPORT_XATTR && !dry_run)
-+ send_xattr_request(fname, file, f_out);
-+#endif
- }
-
- void send_files(int f_in, int f_out)
-@@ -183,8 +189,8 @@ void send_files(int f_in, int f_out)
- send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
-
- /* This call also sets cur_flist. */
-- ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
-- &fnamecmp_type, xname, &xlen);
-+ ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
-+ xname, &xlen);
- if (ndx == NDX_DONE) {
- if (inc_recurse && first_flist) {
- flist_free(first_flist);
-@@ -201,6 +207,9 @@ void send_files(int f_in, int f_out)
- continue;
- }
-
-+ if (inc_recurse)
-+ send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
-+
- file = cur_flist->files[ndx - cur_flist->ndx_start];
- if (F_ROOTDIR(file)) {
- path = F_ROOTDIR(file);
-@@ -215,8 +224,13 @@ void send_files(int f_in, int f_out)
- if (verbose > 2)
- rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
-
-+ if (iflags & ITEM_REPORT_XATTR)
-+ recv_xattr_request(file, f_in);
-+
- if (!(iflags & ITEM_TRANSFER)) {
- maybe_log_item(file, iflags, itemizing, xname);
-+ write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
-+ fnamecmp_type, xname, xlen);
- continue;
- }
- if (phase == 2) {
-@@ -251,8 +265,8 @@ void send_files(int f_in, int f_out)
-
- if (!do_xfers) { /* log the transfer */
- log_item(FCLIENT, file, &stats, iflags, NULL);
-- write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
-- xname, xlen);
-+ write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
-+ fnamecmp_type, xname, xlen);
- continue;
- }
-
-@@ -305,8 +319,8 @@ void send_files(int f_in, int f_out)
- path,slash,fname, (double)st.st_size);
- }
-
-- write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
-- xname, xlen);
-+ write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
-+ fnamecmp_type, xname, xlen);
- write_sum_head(f_xfer, s);
-
- if (verbose > 2)
---- old/testsuite/xattrs.test
-+++ new/testsuite/xattrs.test
-@@ -0,0 +1,76 @@
-+#! /bin/sh
-+
-+# This program is distributable under the terms of the GNU GPL (see
-+# COPYING).
-+
-+# Test that rsync handles basic xattr preservation.
-+
-+. $srcdir/testsuite/rsync.fns
-+
-+$RSYNC --version | grep ", xattrs" >/dev/null || test_skipped "Rsync is configured without xattr support"
-+
-+case "$RSYNC" in
-+*protocol=29*) test_skipped "xattr support requires protocol 30" ;;
-+esac
-+
-+makepath "$fromdir/foo"
-+echo something >"$fromdir/file1"
-+echo else >"$fromdir/file2"
-+echo last >"$fromdir/foo/file3"
-+
-+makepath "$chkdir/foo"
-+echo wow >"$chkdir/file1"
-+cp -p "$fromdir/foo/file3" "$chkdir/foo"
-+
-+files='foo file1 file2 foo/file3'
-+
-+cd "$fromdir"
-+
-+setfattr -n user.short -v 'this is short' file1 2>/dev/null || test_skipped "Unable to set an xattr"
-+setfattr -n user.long -v 'this is a long attribute that will be truncated in the initial data send' file1
-+setfattr -n user.good -v 'this is good' file1
-+setfattr -n user.nice -v 'this is nice' file1
-+
-+setfattr -n user.foo -v foo file2
-+setfattr -n user.bar -v bar file2
-+
-+setfattr -n user.foo -v 'new foo' foo/file3
-+setfattr -n user.bar -v 'new bar' foo/file3
-+setfattr -n user.long -v 'this is also a long attribute that will be truncated in the initial data send' foo/file3
-+setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' foo/file3
-+
-+setfattr -n user.short -v 'old short' "$chkdir/file1"
-+setfattr -n user.extra -v 'remove me' "$chkdir/file1"
-+
-+setfattr -n user.foo -v 'old foo' "$chkdir/foo/file3"
-+setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' "$chkdir/foo/file3"
-+
-+getfattr -d $files >"$scratchdir/xattrs.txt"
-+
-+# OK, let's try a simple xattr copy.
-+checkit "$RSYNC -avX . \"$chkdir/\"" "$fromdir" "$chkdir"
-+
-+cd "$chkdir"
-+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
-+
-+cd "$fromdir"
-+
-+checkit "$RSYNC -aiX --copy-dest=../chk . ../to" "$fromdir" "$todir"
-+
-+cd "$todir"
-+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
-+
-+cd "$fromdir"
-+setfattr -n user.nice -v 'this is nice, but different' file1
-+
-+getfattr -d $files >"$scratchdir/xattrs.txt"
-+
-+rm -rf "$todir"
-+
-+checkit "$RSYNC -aiX --link-dest=../chk . ../to" "$chkdir" "$todir"
-+
-+cd "$todir"
-+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
-+
-+# The script would have aborted on error, so getting here means we've won.
-+exit 0
---- old/xattrs.c
-+++ new/xattrs.c
-@@ -0,0 +1,769 @@
-+/*
-+ * Extended Attribute support for rsync.
-+ * Written by Jay Fenlason, vaguely based on the ACLs patch.
-+ *
-+ * Copyright (C) 2004 Red Hat, Inc.
-+ * Copyright (C) 2006, 2007 Wayne Davison
-+ *
-+ * This program is free software; you can redistribute it and/or modify
-+ * it under the terms of the GNU General Public License version 2 as
-+ * published by the Free Software Foundation.
-+ *
-+ * 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.,
-+ * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
-+ */
-+
-+#include "rsync.h"
-+#include "lib/sysxattrs.h"
-+
-+#ifdef SUPPORT_XATTRS
-+
-+extern int dry_run;
-+extern int am_root;
-+extern int am_sender;
-+extern int am_generator;
-+extern int read_only;
-+extern int list_only;
-+extern int checksum_seed;
-+
-+#define RSYNC_XAL_INITIAL 5
-+#define RSYNC_XAL_LIST_INITIAL 100
-+
-+#define MAX_FULL_DATUM 32
-+
-+#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
-+ && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
-+
-+#define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
-+
-+#define XSTATE_ABBREV 0
-+#define XSTATE_DONE 1
-+#define XSTATE_TODO 2
-+#define XSTATE_LOCAL 3
-+
-+#define USER_PREFIX "user."
-+#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
-+#define SYSTEM_PREFIX "system."
-+#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
-+
-+#ifdef HAVE_LINUX_XATTRS
-+#define RPRE_LEN 0
-+#else
-+#define RSYNC_PREFIX "rsync."
-+#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
-+#endif
-+
-+typedef struct {
-+ char *datum, *name;
-+ size_t datum_len, name_len;
-+} rsync_xa;
-+
-+static size_t namebuf_len = 0;
-+static char *namebuf = NULL;
-+
-+static item_list empty_xattr = EMPTY_ITEM_LIST;
-+static item_list rsync_xal_l = EMPTY_ITEM_LIST;
-+
-+/* ------------------------------------------------------------------------- */
-+
-+static void rsync_xal_free(item_list *xalp)
-+{
-+ size_t i;
-+ rsync_xa *rxas = xalp->items;
-+
-+ for (i = 0; i < xalp->count; i++) {
-+ free(rxas[i].datum);
-+ /*free(rxas[i].name);*/
-+ }
-+ xalp->count = 0;
-+}
-+
-+void free_xattr(statx *sxp)
-+{
-+ if (!sxp->xattr)