1 This patch adds support for extended attributes.
3 To use this patch, run these commands for a successful build:
5 patch -p1 <patches/xattrs.diff
10 CAUTION: this patch has been recently reworked, and needs more testing!
14 @@ -28,13 +28,13 @@ VERSION=@VERSION@
16 HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
17 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o lib/md5.o \
18 - lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
19 + lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattrs.o @LIBOBJS@
20 ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
21 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
22 OBJS1=flist.o rsync.o generator.o receiver.o cleanup.o sender.o exclude.o \
23 util.o main.o checksum.o match.o syscall.o log.o backup.o
24 OBJS2=options.o io.o compat.o hlink.o token.o uidlist.o socket.o \
25 - fileio.o batch.o clientname.o chmod.o acls.o
26 + fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
27 OBJS3=progress.o pipe.o
28 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
29 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
35 extern int preserve_acls;
36 +extern int preserve_xattrs;
37 extern int preserve_devices;
38 extern int preserve_specials;
39 extern int preserve_links;
40 @@ -134,6 +135,9 @@ static int make_bak_dir(char *fullpath)
42 sx.acc_acl = sx.def_acl = NULL;
44 +#ifdef SUPPORT_XATTRS
47 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
50 @@ -143,7 +147,14 @@ static int make_bak_dir(char *fullpath)
54 - set_file_attrs(fullpath, file, NULL, 0);
55 +#ifdef SUPPORT_XATTRS
56 + if (preserve_xattrs) {
57 + get_xattr(rel, &sx);
58 + cache_xattr(file, &sx);
62 + set_file_attrs(fullpath, file, NULL, NULL, 0);
66 @@ -194,6 +205,9 @@ static int keep_backup(const char *fname
68 sx.acc_acl = sx.def_acl = NULL;
70 +#ifdef SUPPORT_XATTRS
74 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
75 return 1; /* the file could have disappeared */
76 @@ -210,6 +224,13 @@ static int keep_backup(const char *fname
80 +#ifdef SUPPORT_XATTRS
81 + if (preserve_xattrs) {
82 + get_xattr(fname, &sx);
83 + cache_xattr(file, &sx);
88 /* Check to see if this is a device file, or link */
89 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
90 @@ -288,7 +309,7 @@ static int keep_backup(const char *fname
91 robust_unlink(fname); /* Just in case... */
94 - set_file_attrs(buf, file, NULL, 0);
95 + set_file_attrs(buf, file, NULL, fname, 0);
101 @@ -142,7 +142,7 @@ NORETURN void _exit_cleanup(int code, co
102 flush_write_file(cleanup_fd_w);
105 - finish_transfer(cleanup_new_fname, fname, NULL,
106 + finish_transfer(cleanup_new_fname, fname, NULL, NULL,
107 cleanup_file, 0, !partial_dir);
112 @@ -43,6 +43,7 @@ extern int protocol_version;
113 extern int preserve_uid;
114 extern int preserve_gid;
115 extern int preserve_acls;
116 +extern int preserve_xattrs;
117 extern int preserve_hard_links;
118 extern int need_messages_from_generator;
119 extern int delete_mode, delete_before, delete_during, delete_after;
120 @@ -65,6 +66,8 @@ void setup_protocol(int f_out,int f_in)
121 preserve_gid = ++file_extra_cnt;
122 if (preserve_acls && !am_sender)
123 preserve_acls = ++file_extra_cnt;
124 + if (preserve_xattrs)
125 + preserve_xattrs = ++file_extra_cnt;
127 if (remote_protocol == 0) {
129 @@ -119,6 +122,13 @@ void setup_protocol(int f_out,int f_in)
131 exit_cleanup(RERR_PROTOCOL);
133 + if (preserve_xattrs) {
135 + "--xattrs requires protocol 30 or higher"
136 + " (negotiated %d).\n",
138 + exit_cleanup(RERR_PROTOCOL);
142 if (delete_mode && !(delete_before+delete_during+delete_after)) {
145 @@ -891,6 +891,46 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
149 +AC_CHECK_HEADERS(attr/xattr.h)
150 +AC_CHECK_HEADERS(sys/xattr.h)
151 +AC_CHECK_HEADERS(sys/extattr.h)
153 +#################################################
154 +# check for extended attribute support
155 +AC_MSG_CHECKING(whether to support extended attributes)
156 +AC_ARG_ENABLE(xattr-support,
157 + AC_HELP_STRING([--disable-xattr-support],
158 + [Turn off extended attribute support]))
160 +if test x"$enable_xattr_support" = x"no"; then
165 + AC_MSG_RESULT(Using Linux xattrs)
166 + AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
167 + AC_DEFINE(SUPPORT_XATTRS, 1, [Define to 1 to add support for extended attributes])
170 + AC_MSG_RESULT(Using OS X xattrs)
171 + AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
172 + AC_DEFINE(SUPPORT_XATTRS, 1)
175 + AC_MSG_RESULT(Using FreeBSD extattrs)
176 + AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs])
177 + AC_DEFINE(SUPPORT_XATTRS, 1)
180 + if test x"$enable_xattr_support" = x"yes"; then
181 + AC_MSG_ERROR(Failed to find extended attribute support)
183 + AC_MSG_RESULT(No extended attribute support found)
189 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
194 @@ -43,6 +43,7 @@ extern int one_file_system;
195 extern int copy_dirlinks;
196 extern int keep_dirlinks;
197 extern int preserve_acls;
198 +extern int preserve_xattrs;
199 extern int preserve_links;
200 extern int preserve_hard_links;
201 extern int preserve_devices;
202 @@ -888,6 +889,10 @@ static struct file_struct *recv_file_ent
203 if (preserve_acls && !S_ISLNK(mode))
204 receive_acl(file, f);
206 +#ifdef SUPPORT_XATTRS
207 + if (preserve_xattrs)
208 + receive_xattr(file, f );
211 if (S_ISREG(mode) || S_ISLNK(mode))
212 stats.total_size += file_length;
213 @@ -1160,7 +1165,7 @@ static struct file_struct *send_file_nam
214 int flags, int filter_flags)
216 struct file_struct *file;
218 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
222 @@ -1179,6 +1184,13 @@ static struct file_struct *send_file_nam
226 +#ifdef SUPPORT_XATTRS
227 + if (preserve_xattrs && f >= 0) {
229 + if (get_xattr(fname, &sx) < 0)
234 maybe_emit_filelist_progress(flist->count + flist_count_offset);
236 @@ -1192,6 +1204,12 @@ static struct file_struct *send_file_nam
240 +#ifdef SUPPORT_XATTRS
241 + if (preserve_xattrs) {
242 + F_XATTR(file) = send_xattr(&sx, f);
251 @@ -36,6 +36,7 @@ extern int relative_paths;
252 extern int implied_dirs;
253 extern int keep_dirlinks;
254 extern int preserve_acls;
255 +extern int preserve_xattrs;
256 extern int preserve_links;
257 extern int preserve_devices;
258 extern int preserve_specials;
259 @@ -532,11 +533,19 @@ int unchanged_attrs(const char *fname, s
263 +#ifdef SUPPORT_XATTRS
264 + if (preserve_xattrs) {
265 + if (!XATTR_READY(*sxp))
266 + get_xattr(fname, sxp);
267 + if (xattr_diff(file, sxp, 0))
275 -void itemize(const char *fname, struct file_struct *file, int ndx, int statret,
276 +void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statret,
277 statx *sxp, int32 iflags, uchar fnamecmp_type,
280 @@ -562,16 +571,24 @@ void itemize(const char *fname, struct f
282 if (preserve_acls && !S_ISLNK(file->mode)) {
283 if (!ACL_READY(*sxp))
284 - get_acl(fname, sxp);
285 + get_acl(fnamecmp, sxp);
286 if (set_acl(NULL, file, sxp) == 0)
287 iflags |= ITEM_REPORT_ACL;
290 +#ifdef SUPPORT_XATTRS
291 + if (preserve_xattrs) {
292 + if (!XATTR_READY(*sxp))
293 + get_xattr(fnamecmp, sxp);
294 + if (xattr_diff(file, sxp, 1))
295 + iflags |= ITEM_REPORT_XATTR;
299 iflags |= ITEM_IS_NEW;
302 - if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
303 + if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
304 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
305 if (protocol_version >= 29) {
307 @@ -581,6 +598,10 @@ void itemize(const char *fname, struct f
308 write_byte(sock_f_out, fnamecmp_type);
309 if (iflags & ITEM_XNAME_FOLLOWS)
310 write_vstring(sock_f_out, xname, strlen(xname));
311 +#ifdef SUPPORT_XATTRS
312 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
313 + send_xattr_request(NULL, file, sock_f_out);
315 } else if (ndx >= 0) {
316 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
317 log_item(code, file, &stats, iflags, xname);
318 @@ -855,14 +876,14 @@ static int try_dests_reg(struct file_str
319 if (preserve_hard_links && F_IS_HLINKED(file))
320 finish_hard_link(file, fname, &sxp->st, itemizing, code, j);
321 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
322 - itemize(fname, file, ndx, 1, sxp,
323 + itemize(cmpbuf, file, ndx, 1, sxp,
324 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
330 - itemize(fname, file, ndx, 0, sxp, 0, 0, NULL);
331 + itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
332 if (verbose > 1 && maybe_ATTRS_REPORT)
333 rprintf(FCLIENT, "%s is uptodate\n", fname);
335 @@ -879,9 +900,13 @@ static int try_dests_reg(struct file_str
339 + set_file_attrs(fname, file, NULL, cmpbuf, 0);
341 - itemize(fname, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
342 - set_file_attrs(fname, file, NULL, 0);
343 + itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
344 +#ifdef SUPPORT_XATTRS
345 + if (preserve_xattrs)
346 + xattr_clear_locals(file);
348 if (maybe_ATTRS_REPORT
349 && ((!itemizing && verbose && match_level == 2)
350 || (verbose > 1 && match_level == 3))) {
351 @@ -1029,7 +1054,7 @@ static int try_dests_non(struct file_str
353 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
354 char *lp = match_level == 3 ? "" : NULL;
355 - itemize(fname, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
356 + itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
358 if (verbose > 1 && maybe_ATTRS_REPORT) {
359 rprintf(FCLIENT, "%s%s is uptodate\n",
360 @@ -1112,6 +1137,9 @@ static void recv_generator(char *fname,
362 sx.acc_acl = sx.def_acl = NULL;
364 +#ifdef SUPPORT_XATTRS
369 flist_free(fuzzy_dirlist);
370 @@ -1224,7 +1252,7 @@ static void recv_generator(char *fname,
374 - if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
375 + if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
376 && verbose && code != FNONE && f_out != -1)
377 rprintf(code, "%s/\n", fname);
378 if (real_ret != 0 && one_file_system)
379 @@ -1278,9 +1306,9 @@ static void recv_generator(char *fname,
380 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
381 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
382 /* The link is pointing to the right place. */
383 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
385 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
386 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
387 #ifdef SUPPORT_HARD_LINKS
388 if (preserve_hard_links && F_IS_HLINKED(file))
389 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
390 @@ -1317,7 +1345,7 @@ static void recv_generator(char *fname,
391 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
392 full_fname(fname), sl);
394 - set_file_attrs(fname, file, NULL, 0);
395 + set_file_attrs(fname, file, NULL, NULL, 0);
397 itemize(fname, file, ndx, statret, &sx,
398 ITEM_LOCAL_CHANGE, 0, NULL);
399 @@ -1357,9 +1385,9 @@ static void recv_generator(char *fname,
400 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
401 && sx.st.st_rdev == rdev) {
402 /* The device or special file is identical. */
403 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
405 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
406 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
407 #ifdef SUPPORT_HARD_LINKS
408 if (preserve_hard_links && F_IS_HLINKED(file))
409 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
410 @@ -1399,7 +1427,7 @@ static void recv_generator(char *fname,
411 rsyserr(FERROR, errno, "mknod %s failed",
414 - set_file_attrs(fname, file, NULL, 0);
415 + set_file_attrs(fname, file, NULL, NULL, 0);
417 itemize(fname, file, ndx, statret, &sx,
418 ITEM_LOCAL_CHANGE, 0, NULL);
419 @@ -1529,9 +1557,9 @@ static void recv_generator(char *fname,
420 do_unlink(partialptr);
421 handle_partial_dir(partialptr, PDIR_DELETE);
423 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
425 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
426 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
427 #ifdef SUPPORT_HARD_LINKS
428 if (preserve_hard_links && F_IS_HLINKED(file))
429 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
430 @@ -1636,6 +1664,10 @@ static void recv_generator(char *fname,
434 +#ifdef SUPPORT_XATTRS
435 + if (preserve_xattrs)
436 + free_xattr(&real_sx);
441 @@ -1657,7 +1689,7 @@ static void recv_generator(char *fname,
445 - set_file_attrs(backupptr, back_file, NULL, 0);
446 + set_file_attrs(backupptr, back_file, NULL, NULL, 0);
448 rprintf(FINFO, "backed up %s to %s\n",
450 @@ -1672,6 +1704,10 @@ static void recv_generator(char *fname,
454 +#ifdef SUPPORT_XATTRS
455 + if (preserve_xattrs)
461 --- old/lib/sysxattrs.c
462 +++ new/lib/sysxattrs.c
465 + * Extended attribute support for rsync.
467 + * Copyright (C) 2004 Red Hat, Inc.
468 + * Written by Jay Fenlason.
470 + * This program is free software; you can redistribute it and/or modify
471 + * it under the terms of the GNU General Public License as published by
472 + * the Free Software Foundation; either version 2 of the License, or
473 + * (at your option) any later version.
475 + * This program is distributed in the hope that it will be useful,
476 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
477 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
478 + * GNU General Public License for more details.
480 + * You should have received a copy of the GNU General Public License along
481 + * with this program; if not, write to the Free Software Foundation, Inc.,
482 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
486 +#include "sysxattrs.h"
488 +#ifdef SUPPORT_XATTRS
490 +#if defined HAVE_LINUX_XATTRS
492 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
494 + return lgetxattr(path, name, value, size);
497 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
499 + return fgetxattr(filedes, name, value, size);
502 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
504 + return lsetxattr(path, name, value, size, 0);
507 +int sys_lremovexattr(const char *path, const char *name)
509 + return lremovexattr(path, name);
512 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
514 + return llistxattr(path, list, size);
517 +#elif HAVE_OSX_XATTRS
519 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
521 + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
524 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
526 + return fgetxattr(filedes, name, value, size, 0, 0);
529 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
531 + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
534 +int sys_lremovexattr(const char *path, const char *name)
536 + return removexattr(path, name, XATTR_NOFOLLOW);
539 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
541 + return listxattr(path, list, size, XATTR_NOFOLLOW);
544 +#elif HAVE_FREEBSD_XATTRS
546 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
548 + return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
551 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
553 + return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
556 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
558 + return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
561 +int sys_lremovexattr(const char *path, const char *name)
563 + return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
566 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
568 + unsigned char keylen;
569 + ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
571 + if (len <= 0 || (size_t)len > size)
574 + /* FreeBSD puts a single-byte length before each string, with no '\0'
575 + * terminator. We need to change this into a series of null-terminted
576 + * strings. Since the size is the same, we can simply transform the
577 + * output in place. */
578 + for (off = 0; off < len; off += keylen + 1) {
579 + keylen = ((unsigned char*)list)[off];
580 + if (off + keylen >= len) {
581 + /* Should be impossible, but kernel bugs happen! */
585 + memmove(list+off, list+off+1, keylen);
586 + list[off+keylen] = '\0';
594 +#error You need to create xattr compatibility functions.
598 +#endif /* SUPPORT_XATTRS */
599 --- old/lib/sysxattrs.h
600 +++ new/lib/sysxattrs.h
602 +#ifdef SUPPORT_XATTRS
604 +#if defined HAVE_ATTR_XATTR_H
605 +#include <attr/xattr.h>
606 +#elif defined HAVE_SYS_XATTR_H
607 +#include <sys/xattr.h>
608 +#elif defined HAVE_SYS_EXTATTR_H
609 +#include <sys/extattr.h>
612 +/* Linux 2.4 does not define this as a distinct errno value: */
614 +#define ENOATTR ENODATA
617 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
618 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
619 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
620 +int sys_lremovexattr(const char *path, const char *name);
621 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
625 +/* No xattrs available */
630 @@ -608,7 +608,7 @@ static void fix_basis_dirs(void)
633 /* This is only called by the sender. */
634 -static void read_final_goodbye(int f_in, int f_out)
635 +static void read_final_goodbye(int f_in)
639 @@ -617,8 +617,8 @@ static void read_final_goodbye(int f_in,
640 if (protocol_version < 29)
643 - i = read_ndx_and_attrs(f_in, f_out, &iflags,
644 - &fnamecmp_type, xname, &xlen);
645 + i = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
650 @@ -677,7 +677,7 @@ static void do_server_sender(int f_in, i
651 io_flush(FULL_FLUSH);
653 if (protocol_version >= 24)
654 - read_final_goodbye(f_in, f_out);
655 + read_final_goodbye(f_in);
656 io_flush(FULL_FLUSH);
659 @@ -740,7 +740,7 @@ static int do_recv(int f_in, int f_out,
660 kluge_around_eof = -1;
662 /* This should only get stopped via a USR2 signal. */
663 - read_ndx_and_attrs(f_in, -1, &iflags, &fnamecmp_type,
664 + read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
667 rprintf(FERROR, "Invalid packet at end of run [%s]\n",
668 @@ -977,7 +977,7 @@ int client_run(int f_in, int f_out, pid_
669 io_flush(FULL_FLUSH);
671 if (protocol_version >= 24)
672 - read_final_goodbye(f_in, f_out);
673 + read_final_goodbye(f_in);
676 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
679 @@ -47,6 +47,7 @@ int copy_links = 0;
680 int preserve_links = 0;
681 int preserve_hard_links = 0;
682 int preserve_acls = 0;
683 +int preserve_xattrs = 0;
684 int preserve_perms = 0;
685 int preserve_executability = 0;
686 int preserve_devices = 0;
687 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
688 char const *have_inplace = "no ";
689 char const *hardlinks = "no ";
690 char const *acls = "no ";
691 + char const *xattrs = "no ";
692 char const *links = "no ";
693 char const *ipv6 = "no ";
694 STRUCT_STAT *dumstat;
695 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
700 +#ifdef SUPPORT_XATTRS
706 @@ -239,8 +243,8 @@ static void print_rsync_version(enum log
707 (int)(sizeof (int64) * 8));
708 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
709 got_socketpair, hardlinks, links, ipv6, have_inplace);
710 - rprintf(f, " %sappend, %sACLs\n",
711 - have_inplace, acls);
712 + rprintf(f, " %sappend, %sACLs, %sxattrs\n",
713 + have_inplace, acls, xattrs);
715 #ifdef MAINTAINER_MODE
716 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
717 @@ -286,7 +290,7 @@ void usage(enum logcode F)
718 rprintf(F," -q, --quiet suppress non-error messages\n");
719 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
720 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
721 - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
722 + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
723 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
724 rprintf(F," -r, --recursive recurse into directories\n");
725 rprintf(F," -R, --relative use relative path names\n");
726 @@ -311,6 +315,9 @@ void usage(enum logcode F)
728 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
730 +#ifdef SUPPORT_XATTRS
731 + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
733 rprintf(F," -o, --owner preserve owner (super-user only)\n");
734 rprintf(F," -g, --group preserve group\n");
735 rprintf(F," --devices preserve device files (super-user only)\n");
736 @@ -438,6 +445,9 @@ static struct poptOption long_options[]
737 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
738 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
739 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
740 + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
741 + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
742 + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
743 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
744 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
745 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
746 @@ -1126,6 +1136,17 @@ int parse_arguments(int *argc, const cha
751 +#ifdef SUPPORT_XATTRS
752 + preserve_xattrs = 1;
753 + preserve_perms = 1;
756 + snprintf(err_buf,sizeof(err_buf),
757 + "extended attributes are not supported on this %s\n",
758 + am_server ? "server" : "client");
763 /* A large opt value means that set_refuse_options()
764 @@ -1590,6 +1611,10 @@ void server_options(char **args,int *arg
768 +#ifdef SUPPORT_XATTRS
769 + if (preserve_xattrs)
783 extern int am_server;
784 extern int do_progress;
785 @@ -37,6 +38,7 @@ extern int protocol_version;
786 extern int relative_paths;
787 extern int preserve_hard_links;
788 extern int preserve_perms;
789 +extern int preserve_xattrs;
790 extern int basis_dir_cnt;
791 extern int make_backups;
792 extern int cleanup_got_literal;
793 @@ -366,8 +368,8 @@ int recv_files(int f_in, char *local_nam
796 /* This call also sets cur_flist. */
797 - ndx = read_ndx_and_attrs(f_in, -1, &iflags,
798 - &fnamecmp_type, xname, &xlen);
799 + ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
801 if (ndx == NDX_DONE) {
802 if (inc_recurse && first_flist) {
803 flist_free(first_flist);
804 @@ -397,8 +399,17 @@ int recv_files(int f_in, char *local_nam
806 rprintf(FINFO, "recv_files(%s)\n", fname);
808 +#ifdef SUPPORT_XATTRS
809 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
810 + recv_xattr_request(file, f_in);
813 if (!(iflags & ITEM_TRANSFER)) {
814 maybe_log_item(file, iflags, itemizing, xname);
815 +#ifdef SUPPORT_XATTRS
816 + if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
817 + set_file_attrs(fname, file, NULL, fname, 0);
822 @@ -655,15 +666,15 @@ int recv_files(int f_in, char *local_nam
823 temp_copy_name = NULL;
825 temp_copy_name = partialptr;
826 - finish_transfer(fname, fnametmp, temp_copy_name,
828 + finish_transfer(fname, fnametmp, fnamecmp,
829 + temp_copy_name, file, recv_ok, 1);
830 if (fnamecmp == partialptr) {
831 do_unlink(partialptr);
832 handle_partial_dir(partialptr, PDIR_DELETE);
834 } else if (keep_partial && partialptr
835 && handle_partial_dir(partialptr, PDIR_CREATE)) {
836 - finish_transfer(partialptr, fnametmp, NULL,
837 + finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
838 file, recv_ok, !partial_dir);
839 if (delay_updates && recv_ok) {
840 bitbag_set_bit(delayed_bits, ndx);
846 extern int preserve_acls;
847 +extern int preserve_xattrs;
848 extern int preserve_perms;
849 extern int preserve_executability;
850 extern int preserve_times;
851 @@ -91,10 +92,8 @@ void setup_iconv()
855 -/* This is used by sender.c with a valid f_out, and by receive.c with
857 -int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr,
858 - uchar *type_ptr, char *buf, int *len_ptr)
859 +int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
860 + char *buf, int *len_ptr)
863 struct file_list *flist;
864 @@ -181,11 +180,6 @@ int read_ndx_and_attrs(int f_in, int f_o
866 exit_cleanup(RERR_PROTOCOL);
868 - } else if (f_out >= 0) {
870 - send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
871 - write_ndx_and_attrs(f_out, ndx, iflags,
872 - fnamecmp_type, buf, len);
876 @@ -228,8 +222,8 @@ mode_t dest_mode(mode_t flist_mode, mode
880 -int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
882 +int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp,
883 + const char *fnamecmp, int flags)
887 @@ -247,6 +241,9 @@ int set_file_attrs(char *fname, struct f
889 sx2.acc_acl = sx2.def_acl = NULL;
891 +#ifdef SUPPORT_XATTRS
894 if (!preserve_perms && S_ISDIR(new_mode)
895 && sx2.st.st_mode & S_ISGID) {
896 /* We just created this directory and its setgid
897 @@ -321,6 +318,10 @@ int set_file_attrs(char *fname, struct f
898 if (daemon_chmod_modes && !S_ISLNK(new_mode))
899 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
901 +#ifdef SUPPORT_XATTRS
902 + if (preserve_xattrs && fnamecmp)
903 + set_xattr(fname, file, fnamecmp);
906 /* It's OK to call set_acl() now, even for a dir, as the generator
907 * will enable owner-writability using chmod, if necessary.
908 @@ -353,10 +354,16 @@ int set_file_attrs(char *fname, struct f
909 rprintf(FCLIENT, "%s is uptodate\n", fname);
914 - if (preserve_acls && sxp == &sx2)
919 +#ifdef SUPPORT_XATTRS
920 + if (preserve_xattrs)
927 @@ -378,7 +385,8 @@ RETSIGTYPE sig_int(UNUSED(int val))
928 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
929 * NULL and the robust_rename() call is forced to copy the temp file, we
930 * stage the file into the partial-dir and then rename it into place. */
931 -void finish_transfer(char *fname, char *fnametmp, char *partialptr,
932 +void finish_transfer(const char *fname, const char *fnametmp,
933 + const char *fnamecmp, const char *partialptr,
934 struct file_struct *file, int ok_to_set_time,
935 int overwriting_basis)
937 @@ -395,7 +403,7 @@ void finish_transfer(char *fname, char *
940 /* Change permissions before putting the file into place. */
941 - set_file_attrs(fnametmp, file, NULL,
942 + set_file_attrs(fnametmp, file, NULL, fnamecmp,
943 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
945 /* move tmp file over real file */
946 @@ -419,7 +427,7 @@ void finish_transfer(char *fname, char *
947 fnametmp = partialptr ? partialptr : fname;
950 - set_file_attrs(fnametmp, file, NULL,
951 + set_file_attrs(fnametmp, file, NULL, fnamecmp,
952 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
957 @@ -569,6 +569,7 @@ extern int file_extra_cnt;
958 extern int preserve_uid;
959 extern int preserve_gid;
960 extern int preserve_acls;
961 +extern int preserve_xattrs;
963 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
964 #define EXTRA_LEN (sizeof (union file_extras))
965 @@ -601,7 +602,8 @@ extern int preserve_acls;
966 /* When the associated option is on, all entries will have these present: */
967 #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
968 #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
969 -#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
970 +#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->num
971 +#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->num
973 /* These items are per-entry optional and mutally exclusive: */
974 #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
975 @@ -793,9 +795,13 @@ typedef struct {
976 struct rsync_acl *acc_acl; /* access ACL */
977 struct rsync_acl *def_acl; /* default ACL */
979 +#ifdef SUPPORT_XATTRS
984 #define ACL_READY(sx) ((sx).acc_acl != NULL)
985 +#define XATTR_READY(sx) ((sx).xattr != NULL)
991 @@ -301,7 +301,7 @@ to the detailed description below for a
992 -q, --quiet suppress non-error messages
993 --no-motd suppress daemon-mode MOTD (see caveat)
994 -c, --checksum skip based on checksum, not mod-time & size
995 - -a, --archive archive mode; same as -rlptgoD (no -H, -A)
996 + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
997 --no-OPTION turn off an implied OPTION (e.g. --no-D)
998 -r, --recursive recurse into directories
999 -R, --relative use relative path names
1000 @@ -324,6 +324,7 @@ to the detailed description below for a
1001 -E, --executability preserve executability
1002 --chmod=CHMOD affect file and/or directory permissions
1003 -A, --acls preserve ACLs (implies -p)
1004 + -X, --xattrs preserve extended attrs (implies -p)
1005 -o, --owner preserve owner (super-user only)
1006 -g, --group preserve group
1007 --devices preserve device files (super-user only)
1008 @@ -831,6 +832,11 @@ dit(bf(-A, --acls)) This option causes r
1009 ACLs to be the same as the source ACLs. This nonstandard option only
1010 works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
1012 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
1013 +extended attributes to be the same as the local ones. This will work
1014 +only if the remote machine's rsync supports this option also. This is
1015 +a non-standard option.
1017 dit(bf(--chmod)) This option tells rsync to apply one or more
1018 comma-separated "chmod" strings to the permission of the files in the
1019 transfer. The resulting value is treated as though it was the permissions
1026 +extern int dry_run;
1027 extern int do_xfers;
1028 extern int am_server;
1029 extern int am_daemon;
1030 @@ -144,8 +145,9 @@ void successful_send(int ndx)
1031 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
1034 -void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1035 - uchar fnamecmp_type, char *buf, int len)
1036 +static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1037 + const char *fname, struct file_struct *file,
1038 + uchar fnamecmp_type, char *buf, int len)
1040 write_ndx(f_out, ndx);
1041 if (protocol_version < 29)
1042 @@ -155,6 +157,10 @@ void write_ndx_and_attrs(int f_out, int
1043 write_byte(f_out, fnamecmp_type);
1044 if (iflags & ITEM_XNAME_FOLLOWS)
1045 write_vstring(f_out, buf, len);
1046 +#ifdef SUPPORT_XATTRS
1047 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
1048 + send_xattr_request(fname, file, f_out);
1052 void send_files(int f_in, int f_out)
1053 @@ -183,8 +189,8 @@ void send_files(int f_in, int f_out)
1054 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1056 /* This call also sets cur_flist. */
1057 - ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
1058 - &fnamecmp_type, xname, &xlen);
1059 + ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
1061 if (ndx == NDX_DONE) {
1062 if (inc_recurse && first_flist) {
1063 flist_free(first_flist);
1064 @@ -201,6 +207,9 @@ void send_files(int f_in, int f_out)
1069 + send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1071 file = cur_flist->files[ndx - cur_flist->ndx_start];
1072 if (F_ROOTDIR(file)) {
1073 path = F_ROOTDIR(file);
1074 @@ -215,8 +224,13 @@ void send_files(int f_in, int f_out)
1076 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
1078 + if (iflags & ITEM_REPORT_XATTR)
1079 + recv_xattr_request(file, f_in);
1081 if (!(iflags & ITEM_TRANSFER)) {
1082 maybe_log_item(file, iflags, itemizing, xname);
1083 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1084 + fnamecmp_type, xname, xlen);
1088 @@ -251,8 +265,8 @@ void send_files(int f_in, int f_out)
1090 if (!do_xfers) { /* log the transfer */
1091 log_item(FCLIENT, file, &stats, iflags, NULL);
1092 - write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1094 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1095 + fnamecmp_type, xname, xlen);
1099 @@ -305,8 +319,8 @@ void send_files(int f_in, int f_out)
1100 path,slash,fname, (double)st.st_size);
1103 - write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1105 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1106 + fnamecmp_type, xname, xlen);
1107 write_sum_head(f_xfer, s);
1110 --- old/testsuite/xattrs.test
1111 +++ new/testsuite/xattrs.test
1115 +# This program is distributable under the terms of the GNU GPL (see
1118 +# Test that rsync handles basic xattr preservation.
1120 +. $srcdir/testsuite/rsync.fns
1122 +$RSYNC --version | grep ", xattrs" >/dev/null || test_skipped "Rsync is configured without xattr support"
1125 +*protocol=29*) test_skipped "xattr support requires protocol 30" ;;
1128 +makepath "$fromdir/foo"
1129 +echo something >"$fromdir/file1"
1130 +echo else >"$fromdir/file2"
1131 +echo last >"$fromdir/foo/file3"
1133 +makepath "$chkdir/foo"
1134 +echo wow >"$chkdir/file1"
1135 +cp -p "$fromdir/foo/file3" "$chkdir/foo"
1137 +files='foo file1 file2 foo/file3'
1141 +setfattr -n user.short -v 'this is short' file1 2>/dev/null || test_skipped "Unable to set an xattr"
1142 +setfattr -n user.long -v 'this is a long attribute that will be truncated in the initial data send' file1
1143 +setfattr -n user.good -v 'this is good' file1
1144 +setfattr -n user.nice -v 'this is nice' file1
1146 +setfattr -n user.foo -v foo file2
1147 +setfattr -n user.bar -v bar file2
1149 +setfattr -n user.foo -v 'new foo' foo/file3
1150 +setfattr -n user.bar -v 'new bar' foo/file3
1151 +setfattr -n user.long -v 'this is also a long attribute that will be truncated in the initial data send' foo/file3
1152 +setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' foo/file3
1154 +setfattr -n user.short -v 'old short' "$chkdir/file1"
1155 +setfattr -n user.extra -v 'remove me' "$chkdir/file1"
1157 +setfattr -n user.foo -v 'old foo' "$chkdir/foo/file3"
1158 +setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' "$chkdir/foo/file3"
1160 +getfattr -d $files >"$scratchdir/xattrs.txt"
1162 +# OK, let's try a simple xattr copy.
1163 +checkit "$RSYNC -avX . \"$chkdir/\"" "$fromdir" "$chkdir"
1166 +getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
1170 +checkit "$RSYNC -aiX --copy-dest=../chk . ../to" "$fromdir" "$todir"
1173 +getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
1176 +setfattr -n user.nice -v 'this is nice, but different' file1
1178 +getfattr -d $files >"$scratchdir/xattrs.txt"
1182 +checkit "$RSYNC -aiX --link-dest=../chk . ../to" "$chkdir" "$todir"
1185 +getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
1187 +# The script would have aborted on error, so getting here means we've won.
1193 + * Extended Attribute support for rsync.
1194 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
1196 + * Copyright (C) 2004 Red Hat, Inc.
1197 + * Copyright (C) 2006, 2007 Wayne Davison
1199 + * This program is free software; you can redistribute it and/or modify
1200 + * it under the terms of the GNU General Public License version 2 as
1201 + * published by the Free Software Foundation.
1203 + * This program is distributed in the hope that it will be useful,
1204 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1205 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1206 + * GNU General Public License for more details.
1208 + * You should have received a copy of the GNU General Public License along
1209 + * with this program; if not, write to the Free Software Foundation, Inc.,
1210 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1214 +#include "lib/sysxattrs.h"
1216 +#ifdef SUPPORT_XATTRS
1218 +extern int dry_run;
1219 +extern int am_root;
1220 +extern int am_sender;
1221 +extern int am_generator;
1222 +extern int read_only;
1223 +extern int list_only;
1224 +extern int checksum_seed;
1226 +#define RSYNC_XAL_INITIAL 5
1227 +#define RSYNC_XAL_LIST_INITIAL 100
1229 +#define MAX_FULL_DATUM 32
1231 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
1232 + && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
1234 +#define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
1236 +#define XSTATE_ABBREV 0
1237 +#define XSTATE_DONE 1
1238 +#define XSTATE_TODO 2
1239 +#define XSTATE_LOCAL 3
1241 +#define USER_PREFIX "user."
1242 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
1243 +#define SYSTEM_PREFIX "system."
1244 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
1246 +#ifdef HAVE_LINUX_XATTRS
1249 +#define RSYNC_PREFIX "rsync."
1250 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
1254 + char *datum, *name;
1255 + size_t datum_len, name_len;
1258 +static size_t namebuf_len = 0;
1259 +static char *namebuf = NULL;
1261 +static item_list empty_xattr = EMPTY_ITEM_LIST;
1262 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
1264 +/* ------------------------------------------------------------------------- */
1266 +static void rsync_xal_free(item_list *xalp)
1269 + rsync_xa *rxas = xalp->items;
1271 + for (i = 0; i < xalp->count; i++) {
1272 + free(rxas[i].datum);
1273 + /*free(rxas[i].name);*/
1278 +void free_xattr(statx *sxp)
1282 + rsync_xal_free(sxp->xattr);
1284 + sxp->xattr = NULL;
1287 +static int rsync_xal_compare_names(const void *x1, const void *x2)
1289 + const rsync_xa *xa1 = x1;
1290 + const rsync_xa *xa2 = x2;
1291 + return strcmp(xa1->name, xa2->name);
1294 +static ssize_t get_xattr_names(const char *fname)
1299 + namebuf_len = 1024;
1300 + namebuf = new_array(char, namebuf_len);
1302 + out_of_memory("get_xattr_names");
1305 + /* The length returned includes all the '\0' terminators. */
1306 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1307 + if (list_len > (ssize_t)namebuf_len) {
1311 + if (list_len < 0) {
1312 + if (errno == ENOTSUP)
1314 + if (errno == ERANGE) {
1315 + list_len = sys_llistxattr(fname, NULL, 0);
1316 + if (list_len < 0) {
1317 + rsyserr(FERROR, errno,
1318 + "get_xattr_names: llistxattr(\"%s\",0) failed",
1324 + namebuf_len = list_len + 1024;
1325 + namebuf = new_array(char, namebuf_len);
1327 + out_of_memory("get_xattr_names");
1328 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1329 + if (list_len < 0) {
1330 + rsyserr(FERROR, errno,
1331 + "get_xattr_names: llistxattr(\"%s\",%ld) failed",
1332 + fname, (long)namebuf_len);
1336 + rsyserr(FERROR, errno,
1337 + "get_xattr_names: llistxattr(\"%s\",%ld) failed",
1338 + fname, (long)namebuf_len);
1346 +/* On entry, the *len_ptr parameter contains the size of the extra space we
1347 + * should allocate when we create a buffer for the data. On exit, it contains
1348 + * the length of the datum. */
1349 +static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
1350 + int no_missing_error)
1352 + size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
1355 + if (datum_len == (size_t)-1) {
1356 + if (errno == ENOTSUP || no_missing_error)
1358 + rsyserr(FERROR, errno,
1359 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
1364 + if (datum_len + *len_ptr < datum_len /* checks for overflow */
1365 + || !(ptr = new_array(char, datum_len + *len_ptr)))
1366 + out_of_memory("get_xattr_data");
1368 + *len_ptr = datum_len;
1371 + size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
1372 + if (len != datum_len) {
1373 + if (len == (size_t)-1) {
1374 + rsyserr(FERROR, errno,
1375 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1376 + " failed", fname, name, (long)datum_len);
1379 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1380 + " returned %ld\n", fname, name,
1381 + (long)datum_len, (long)len);
1391 +static int rsync_xal_get(const char *fname, item_list *xalp)
1393 + ssize_t list_len, name_len;
1394 + size_t datum_len, name_offset;
1396 + int user_only = am_sender ? 0 : !am_root;
1398 + /* This puts the name list into the "namebuf" buffer. */
1399 + if ((list_len = get_xattr_names(fname)) < 0)
1402 + for (name = namebuf; list_len > 0; name += name_len) {
1405 + name_len = strlen(name) + 1;
1406 + list_len -= name_len;
1408 +#ifdef HAVE_LINUX_XATTRS
1409 + /* We always ignore the system namespace, and non-root
1410 + * ignores everything but the user namespace. */
1411 + if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
1412 + : HAS_PREFIX(name, SYSTEM_PREFIX))
1416 + datum_len = name_len; /* Pass extra size to get_xattr_data() */
1417 + if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
1420 + if (datum_len > MAX_FULL_DATUM) {
1421 + /* For large datums, we store a flag and a checksum. */
1422 + name_offset = 1 + MAX_DIGEST_LEN;
1423 + sum_init(checksum_seed);
1424 + sum_update(ptr, datum_len);
1427 + if (!(ptr = new_array(char, name_offset + name_len)))
1428 + out_of_memory("rsync_xal_get");
1429 + *ptr = XSTATE_ABBREV;
1432 + name_offset = datum_len;
1434 + rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
1435 + rxas->name = ptr + name_offset;
1436 + memcpy(rxas->name, name, name_len);
1437 + rxas->datum = ptr;
1438 + rxas->name_len = name_len;
1439 + rxas->datum_len = datum_len;
1441 + if (xalp->count > 1)
1442 + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
1446 +/* Read the xattr(s) for this filename. */
1447 +int get_xattr(const char *fname, statx *sxp)
1449 + sxp->xattr = new(item_list);
1450 + *sxp->xattr = empty_xattr;
1451 + if (rsync_xal_get(fname, sxp->xattr) < 0) {
1458 +static int find_matching_xattr(item_list *xalp)
1461 + item_list *lst = rsync_xal_l.items;
1463 + for (i = 0; i < rsync_xal_l.count; i++) {
1464 + rsync_xa *rxas1 = lst[i].items;
1465 + rsync_xa *rxas2 = xalp->items;
1467 + /* Wrong number of elements? */
1468 + if (lst[i].count != xalp->count)
1470 + /* any elements different? */
1471 + for (j = 0; j < xalp->count; j++) {
1472 + if (rxas1[j].name_len != rxas2[j].name_len
1473 + || rxas1[j].datum_len != rxas2[j].datum_len
1474 + || strcmp(rxas1[j].name, rxas2[j].name))
1476 + if (rxas1[j].datum_len > MAX_FULL_DATUM) {
1477 + if (memcmp(rxas1[j].datum + 1,
1478 + rxas2[j].datum + 1,
1479 + MAX_DIGEST_LEN) != 0)
1482 + if (memcmp(rxas1[j].datum, rxas2[j].datum,
1483 + rxas2[j].datum_len))
1487 + /* no differences found. This is The One! */
1488 + if (j == xalp->count)
1495 +/* Store *xalp on the end of rsync_xal_l */
1496 +static void rsync_xal_store(item_list *xalp)
1498 + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
1499 + /* Since the following call starts a new list, we know it will hold the
1500 + * entire initial-count, not just enough space for one new item. */
1501 + *new_lst = empty_xattr;
1502 + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
1503 + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
1504 + new_lst->count = xalp->count;
1508 +/* Send the make_xattr()-generated xattr list for this flist entry. */
1509 +int send_xattr(statx *sxp, int f)
1511 + int ndx = find_matching_xattr(sxp->xattr);
1513 + /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
1514 + write_abbrevint(f, ndx + 1);
1518 + int count = sxp->xattr->count;
1519 + write_abbrevint(f, count);
1520 + for (rxa = sxp->xattr->items; count--; rxa++) {
1521 +#ifdef HAVE_LINUX_XATTRS
1522 + write_abbrevint(f, rxa->name_len);
1523 + write_abbrevint(f, rxa->datum_len);
1524 + write_buf(f, rxa->name, rxa->name_len);
1526 + /* We strip the rsync prefix from disguised namespaces
1527 + * and put everything else in the user namespace. */
1528 + if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
1529 + && rxa->name[RPRE_LEN] != '%') {
1530 + write_abbrevint(f, rxa->name_len - RPRE_LEN);
1531 + write_abbrevint(f, rxa->datum_len);
1532 + write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
1534 + write_abbrevint(f, rxa->name_len + UPRE_LEN);
1535 + write_abbrevint(f, rxa->datum_len);
1536 + write_buf(f, USER_PREFIX, UPRE_LEN);
1537 + write_buf(f, rxa->name, rxa->name_len);
1540 + if (rxa->datum_len > MAX_FULL_DATUM)
1541 + write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
1543 + write_buf(f, rxa->datum, rxa->datum_len);
1545 + ndx = rsync_xal_l.count; /* pre-incremented count */
1546 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1552 +/* Return a flag indicating if we need to change a file's xattrs. If
1553 + * "find_all" is specified, also mark any abbreviated xattrs that we
1554 + * need so that send_xattr_request() can tell the sender about them. */
1555 +int xattr_diff(struct file_struct *file, statx *sxp, int find_all)
1557 + item_list *lst = rsync_xal_l.items;
1558 + rsync_xa *snd_rxa, *rec_rxa;
1559 + int snd_cnt, rec_cnt;
1560 + int cmp, same, xattrs_equal = 1;
1562 + if (!XATTR_READY(*sxp)) {
1566 + rec_rxa = sxp->xattr->items;
1567 + rec_cnt = sxp->xattr->count;
1570 + if (F_XATTR(file) >= 0)
1571 + lst += F_XATTR(file);
1573 + lst = &empty_xattr;
1575 + snd_rxa = lst->items;
1576 + snd_cnt = lst->count;
1578 + /* If the count of the sender's xattrs is different from our
1579 + * (receiver's) xattrs, the lists are not the same. */
1580 + if (snd_cnt != rec_cnt) {
1587 + cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
1590 + else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
1591 + same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1592 + && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
1593 + MAX_DIGEST_LEN) == 0;
1594 + /* Flag unrequested items that we need. */
1595 + if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
1596 + snd_rxa->datum[0] = XSTATE_TODO;
1598 + same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1599 + && memcmp(snd_rxa->datum, rec_rxa->datum,
1600 + snd_rxa->datum_len) == 0;
1621 + return !xattrs_equal;
1624 +/* When called by the generator with a NULL fname, this tells the sender
1625 + * which abbreviated xattr values we need. When called by the sender
1626 + * (with a non-NULL fname), we send all the extra xattr data it needs. */
1627 +void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
1629 + item_list *lst = rsync_xal_l.items;
1630 + int j, cnt, prior_req = -1;
1633 + lst += F_XATTR(file);
1635 + for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) {
1636 + if (rxa->datum_len <= MAX_FULL_DATUM)
1638 + switch (rxa->datum[0]) {
1639 + case XSTATE_LOCAL:
1640 + /* Items set locally will get cached by receiver. */
1641 + rxa->datum[0] = XSTATE_DONE;
1649 + /* Flag that we handled this abbreviated item. */
1650 + rxa->datum[0] = XSTATE_DONE;
1652 + write_abbrevint(f_out, j - prior_req);
1659 + /* Re-read the long datum. */
1660 + if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0)))
1663 + write_abbrevint(f_out, len); /* length might have changed! */
1664 + write_buf(f_out, ptr, len);
1669 + write_byte(f_out, 0); /* end the list */
1672 +/* Any items set locally by the generator that the receiver doesn't
1673 + * get told about get changed back to XSTATE_ABBREV. */
1674 +void xattr_clear_locals(struct file_struct *file)
1676 + item_list *lst = rsync_xal_l.items;
1680 + if (F_XATTR(file) < 0)
1683 + lst += F_XATTR(file);
1685 + for (rxa = lst->items; cnt--; rxa++) {
1686 + if (rxa->datum_len <= MAX_FULL_DATUM)
1688 + if (rxa->datum[0] == XSTATE_LOCAL)
1689 + rxa->datum[0] = XSTATE_ABBREV;
1693 +/* When called by the sender, read the request from the generator and mark
1694 + * any needed xattrs with a flag that lets us know they need to be sent to
1695 + * the receiver. When called by the receiver, reads the sent data and
1696 + * stores it in place of its checksum. */
1697 +void recv_xattr_request(struct file_struct *file, int f_in)
1699 + item_list *lst = rsync_xal_l.items;
1700 + char *old_datum, *name;
1704 + if (F_XATTR(file) < 0)
1705 + exit_cleanup(RERR_STREAMIO); /* XXX */
1706 + lst += F_XATTR(file);
1711 + while ((rel_pos = read_abbrevint(f_in)) != 0) {
1714 + if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM
1715 + || rxa->datum[0] != XSTATE_ABBREV)
1716 + exit_cleanup(RERR_STREAMIO); /* XXX */
1719 + rxa->datum[0] = XSTATE_TODO;
1723 + old_datum = rxa->datum;
1724 + rxa->datum_len = read_abbrevint(f_in);
1726 + if (rxa->name_len + rxa->datum_len < rxa->name_len)
1727 + out_of_memory("recv_xattr_request"); /* overflow */
1728 + rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
1730 + out_of_memory("recv_xattr_request");
1731 + name = rxa->datum + rxa->datum_len;
1732 + memcpy(name, rxa->name, rxa->name_len);
1735 + read_buf(f_in, rxa->datum, rxa->datum_len);
1739 +/* ------------------------------------------------------------------------- */
1741 +/* receive and build the rsync_xattr_lists */
1742 +void receive_xattr(struct file_struct *file, int f)
1744 + static item_list temp_xattr = EMPTY_ITEM_LIST;
1746 + int ndx = read_abbrevint(f);
1748 + if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
1749 + rprintf(FERROR, "receive_xattr: xa index %d out of"
1750 + " range for %s\n", ndx, f_name(file, NULL));
1751 + exit_cleanup(RERR_STREAMIO);
1755 + F_XATTR(file) = ndx - 1;
1759 + if ((count = read_abbrevint(f)) != 0) {
1760 + (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
1761 + temp_xattr.count = 0;
1767 + size_t name_len = read_abbrevint(f);
1768 + size_t datum_len = read_abbrevint(f);
1769 + size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
1770 +#ifdef HAVE_LINUX_XATTRS
1771 + size_t extra_len = 0;
1773 + size_t extra_len = am_root ? RPRE_LEN : 0;
1774 + if (dget_len + extra_len < dget_len)
1775 + out_of_memory("receive_xattr"); /* overflow */
1777 + if (dget_len + extra_len + name_len < dget_len)
1778 + out_of_memory("receive_xattr"); /* overflow */
1779 + ptr = new_array(char, dget_len + extra_len + name_len);
1781 + out_of_memory("receive_xattr");
1782 + name = ptr + dget_len + extra_len;
1783 + read_buf(f, name, name_len);
1784 + if (dget_len == datum_len)
1785 + read_buf(f, ptr, dget_len);
1787 + *ptr = XSTATE_ABBREV;
1788 + read_buf(f, ptr + 1, MAX_DIGEST_LEN);
1790 +#ifdef HAVE_LINUX_XATTRS
1791 + /* Non-root can only save the user namespace. */
1792 + if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
1797 + /* This OS only has a user namespace, so we either
1798 + * strip the user prefix, or we put a non-user
1799 + * namespace inside our rsync hierarchy. */
1800 + if (HAS_PREFIX(name, USER_PREFIX)) {
1802 + name_len -= UPRE_LEN;
1803 + } else if (am_root) {
1805 + name_len += RPRE_LEN;
1806 + memcpy(name, RSYNC_PREFIX, RPRE_LEN);
1812 + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
1815 + rxa->name_len = name_len;
1816 + rxa->datum_len = datum_len;
1819 + ndx = rsync_xal_l.count; /* pre-incremented count */
1820 + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
1822 + F_XATTR(file) = ndx;
1825 +/* Turn the xattr data in statx into cached xattr data, setting the index
1826 + * values in the file struct. */
1827 +void cache_xattr(struct file_struct *file, statx *sxp)
1834 + ndx = find_matching_xattr(sxp->xattr);
1836 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1838 + F_XATTR(file) = ndx;
1841 +static int rsync_xal_set(const char *fname, item_list *xalp, const char *fnamecmp)
1843 + rsync_xa *rxas = xalp->items;
1846 + char *name, *ptr, sum[MAX_DIGEST_LEN];
1847 + int name_len, ret = 0;
1849 + /* This puts the current name list into the "namebuf" buffer. */
1850 + if ((list_len = get_xattr_names(fname)) < 0)
1853 + for (i = 0; i < xalp->count; i++) {
1854 + name = rxas[i].name;
1856 + if (XATTR_ABBREV(rxas[i])) {
1857 + /* See if the fnamecmp version is identical. */
1858 + len = name_len = rxas[i].name_len;
1859 + if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
1863 + rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
1864 + rxas[i].name, full_fname(fname));
1868 + if (len != rxas[i].datum_len) {
1870 + goto still_abbrev;
1873 + sum_init(checksum_seed);
1874 + sum_update(ptr, len);
1876 + if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
1878 + goto still_abbrev;
1881 + if (fname != fnamecmp /* value is already set */
1882 + && sys_lsetxattr(fname, name, ptr, len) < 0) {
1883 + rsyserr(FERROR, errno,
1884 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1889 + if (am_generator) { /* generator items stay abbreviated */
1890 + if (rxas[i].datum[0] == XSTATE_ABBREV)
1891 + rxas[i].datum[0] = XSTATE_LOCAL;
1896 + memcpy(ptr + len, name, name_len);
1897 + free(rxas[i].datum);
1899 + rxas[i].name = name = ptr + len;
1900 + rxas[i].datum = ptr;
1904 + if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
1905 + rsyserr(FERROR, errno,
1906 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1912 + /* Remove any extraneous names. */
1913 + for (name = namebuf; list_len > 0; name += name_len) {
1914 + name_len = strlen(name) + 1;
1915 + list_len -= name_len;
1917 +#ifdef HAVE_LINUX_XATTRS
1918 + /* We always ignore the system namespace, and non-root
1919 + * ignores everything but the user namespace. */
1920 + if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
1921 + : !HAS_PREFIX(name, USER_PREFIX))
1925 + for (i = 0; i < xalp->count; i++) {
1926 + if (strcmp(name, rxas[i].name) == 0)
1929 + if (i == xalp->count) {
1930 + if (sys_lremovexattr(fname, name) < 0) {
1931 + rsyserr(FERROR, errno,
1932 + "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
1942 +/* Set extended attributes on indicated filename. */
1943 +int set_xattr(const char *fname, const struct file_struct *file, const char *fnamecmp)
1946 + item_list *lst = rsync_xal_l.items;
1949 + return 1; /* FIXME: --dry-run needs to compute this value */
1951 + if (read_only || list_only) {
1956 + ndx = F_XATTR(file);
1957 + return rsync_xal_set(fname, lst + ndx, fnamecmp);
1960 +#endif /* SUPPORT_XATTRS */