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,6 +533,14 @@ 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))
274 @@ -567,11 +576,19 @@ void itemize(const char *fname, struct f
275 iflags |= ITEM_REPORT_ACL;
278 +#ifdef SUPPORT_XATTRS
279 + if (preserve_xattrs) {
280 + if (!XATTR_READY(*sxp))
281 + get_xattr(fname, sxp);
282 + if (xattr_diff(file, sxp, 1))
283 + iflags |= ITEM_REPORT_XATTR;
287 iflags |= ITEM_IS_NEW;
290 - if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
291 + if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
292 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
293 if (protocol_version >= 29) {
295 @@ -581,6 +598,10 @@ void itemize(const char *fname, struct f
296 write_byte(sock_f_out, fnamecmp_type);
297 if (iflags & ITEM_XNAME_FOLLOWS)
298 write_vstring(sock_f_out, xname, strlen(xname));
299 +#ifdef SUPPORT_XATTRS
300 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
301 + send_xattr_request(NULL, file, sock_f_out);
303 } else if (ndx >= 0) {
304 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
305 log_item(code, file, &stats, iflags, xname);
306 @@ -881,7 +902,7 @@ static int try_dests_reg(struct file_str
309 itemize(fname, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
310 - set_file_attrs(fname, file, NULL, 0);
311 + set_file_attrs(fname, file, NULL, NULL, 0);
312 if (maybe_ATTRS_REPORT
313 && ((!itemizing && verbose && match_level == 2)
314 || (verbose > 1 && match_level == 3))) {
315 @@ -1112,6 +1133,9 @@ static void recv_generator(char *fname,
317 sx.acc_acl = sx.def_acl = NULL;
319 +#ifdef SUPPORT_XATTRS
324 flist_free(fuzzy_dirlist);
325 @@ -1224,7 +1248,7 @@ static void recv_generator(char *fname,
329 - if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
330 + if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
331 && verbose && code != FNONE && f_out != -1)
332 rprintf(code, "%s/\n", fname);
333 if (real_ret != 0 && one_file_system)
334 @@ -1280,7 +1304,7 @@ static void recv_generator(char *fname,
335 /* The link is pointing to the right place. */
337 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
338 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
339 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
340 #ifdef SUPPORT_HARD_LINKS
341 if (preserve_hard_links && F_IS_HLINKED(file))
342 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
343 @@ -1317,7 +1341,7 @@ static void recv_generator(char *fname,
344 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
345 full_fname(fname), sl);
347 - set_file_attrs(fname, file, NULL, 0);
348 + set_file_attrs(fname, file, NULL, NULL, 0);
350 itemize(fname, file, ndx, statret, &sx,
351 ITEM_LOCAL_CHANGE, 0, NULL);
352 @@ -1359,7 +1383,7 @@ static void recv_generator(char *fname,
353 /* The device or special file is identical. */
355 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
356 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
357 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
358 #ifdef SUPPORT_HARD_LINKS
359 if (preserve_hard_links && F_IS_HLINKED(file))
360 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
361 @@ -1399,7 +1423,7 @@ static void recv_generator(char *fname,
362 rsyserr(FERROR, errno, "mknod %s failed",
365 - set_file_attrs(fname, file, NULL, 0);
366 + set_file_attrs(fname, file, NULL, NULL, 0);
368 itemize(fname, file, ndx, statret, &sx,
369 ITEM_LOCAL_CHANGE, 0, NULL);
370 @@ -1531,7 +1555,7 @@ static void recv_generator(char *fname,
373 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
374 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
375 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
376 #ifdef SUPPORT_HARD_LINKS
377 if (preserve_hard_links && F_IS_HLINKED(file))
378 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
379 @@ -1636,6 +1660,10 @@ static void recv_generator(char *fname,
383 +#ifdef SUPPORT_XATTRS
384 + if (preserve_xattrs)
385 + free_xattr(&real_sx);
390 @@ -1657,7 +1685,7 @@ static void recv_generator(char *fname,
394 - set_file_attrs(backupptr, back_file, NULL, 0);
395 + set_file_attrs(backupptr, back_file, NULL, NULL, 0);
397 rprintf(FINFO, "backed up %s to %s\n",
399 @@ -1672,6 +1700,10 @@ static void recv_generator(char *fname,
403 +#ifdef SUPPORT_XATTRS
404 + if (preserve_xattrs)
410 --- old/lib/sysxattrs.c
411 +++ new/lib/sysxattrs.c
414 + * Extended attribute support for rsync.
416 + * Copyright (C) 2004 Red Hat, Inc.
417 + * Written by Jay Fenlason.
419 + * This program is free software; you can redistribute it and/or modify
420 + * it under the terms of the GNU General Public License as published by
421 + * the Free Software Foundation; either version 2 of the License, or
422 + * (at your option) any later version.
424 + * This program is distributed in the hope that it will be useful,
425 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
426 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
427 + * GNU General Public License for more details.
429 + * You should have received a copy of the GNU General Public License along
430 + * with this program; if not, write to the Free Software Foundation, Inc.,
431 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
435 +#include "sysxattrs.h"
437 +#ifdef SUPPORT_XATTRS
439 +#if defined HAVE_LINUX_XATTRS
441 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
443 + return lgetxattr(path, name, value, size);
446 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
448 + return fgetxattr(filedes, name, value, size);
451 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
453 + return lsetxattr(path, name, value, size, 0);
456 +int sys_lremovexattr(const char *path, const char *name)
458 + return lremovexattr(path, name);
461 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
463 + return llistxattr(path, list, size);
466 +#elif HAVE_OSX_XATTRS
468 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
470 + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
473 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
475 + return fgetxattr(filedes, name, value, size, 0, 0);
478 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
480 + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
483 +int sys_lremovexattr(const char *path, const char *name)
485 + return removexattr(path, name, XATTR_NOFOLLOW);
488 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
490 + return listxattr(path, list, size, XATTR_NOFOLLOW);
493 +#elif HAVE_FREEBSD_XATTRS
495 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
497 + return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
500 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
502 + return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
505 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
507 + return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
510 +int sys_lremovexattr(const char *path, const char *name)
512 + return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
515 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
517 + unsigned char keylen;
518 + ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
520 + if (len <= 0 || (size_t)len > size)
523 + /* FreeBSD puts a single-byte length before each string, with no '\0'
524 + * terminator. We need to change this into a series of null-terminted
525 + * strings. Since the size is the same, we can simply transform the
526 + * output in place. */
527 + for (off = 0; off < len; off += keylen + 1) {
528 + keylen = ((unsigned char*)list)[off];
529 + if (off + keylen >= len) {
530 + /* Should be impossible, but kernel bugs happen! */
534 + memmove(list+off, list+off+1, keylen);
535 + list[off+keylen] = '\0';
543 +#error You need to create xattr compatibility functions.
547 +#endif /* SUPPORT_XATTRS */
548 --- old/lib/sysxattrs.h
549 +++ new/lib/sysxattrs.h
551 +#ifdef SUPPORT_XATTRS
553 +#if defined HAVE_ATTR_XATTR_H
554 +#include <attr/xattr.h>
555 +#elif defined HAVE_SYS_XATTR_H
556 +#include <sys/xattr.h>
557 +#elif defined HAVE_SYS_EXTATTR_H
558 +#include <sys/extattr.h>
561 +/* Linux 2.4 does not define this as a distinct errno value: */
563 +#define ENOATTR ENODATA
566 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
567 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
568 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
569 +int sys_lremovexattr(const char *path, const char *name);
570 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
574 +/* No xattrs available */
579 @@ -608,7 +608,7 @@ static void fix_basis_dirs(void)
582 /* This is only called by the sender. */
583 -static void read_final_goodbye(int f_in, int f_out)
584 +static void read_final_goodbye(int f_in)
588 @@ -617,8 +617,8 @@ static void read_final_goodbye(int f_in,
589 if (protocol_version < 29)
592 - i = read_ndx_and_attrs(f_in, f_out, &iflags,
593 - &fnamecmp_type, xname, &xlen);
594 + i = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
599 @@ -677,7 +677,7 @@ static void do_server_sender(int f_in, i
600 io_flush(FULL_FLUSH);
602 if (protocol_version >= 24)
603 - read_final_goodbye(f_in, f_out);
604 + read_final_goodbye(f_in);
605 io_flush(FULL_FLUSH);
608 @@ -740,7 +740,7 @@ static int do_recv(int f_in, int f_out,
609 kluge_around_eof = -1;
611 /* This should only get stopped via a USR2 signal. */
612 - read_ndx_and_attrs(f_in, -1, &iflags, &fnamecmp_type,
613 + read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
616 rprintf(FERROR, "Invalid packet at end of run [%s]\n",
617 @@ -977,7 +977,7 @@ int client_run(int f_in, int f_out, pid_
618 io_flush(FULL_FLUSH);
620 if (protocol_version >= 24)
621 - read_final_goodbye(f_in, f_out);
622 + read_final_goodbye(f_in);
625 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
628 @@ -47,6 +47,7 @@ int copy_links = 0;
629 int preserve_links = 0;
630 int preserve_hard_links = 0;
631 int preserve_acls = 0;
632 +int preserve_xattrs = 0;
633 int preserve_perms = 0;
634 int preserve_executability = 0;
635 int preserve_devices = 0;
636 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
637 char const *have_inplace = "no ";
638 char const *hardlinks = "no ";
639 char const *acls = "no ";
640 + char const *xattrs = "no ";
641 char const *links = "no ";
642 char const *ipv6 = "no ";
643 STRUCT_STAT *dumstat;
644 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
649 +#ifdef SUPPORT_XATTRS
655 @@ -239,8 +243,8 @@ static void print_rsync_version(enum log
656 (int)(sizeof (int64) * 8));
657 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
658 got_socketpair, hardlinks, links, ipv6, have_inplace);
659 - rprintf(f, " %sappend, %sACLs\n",
660 - have_inplace, acls);
661 + rprintf(f, " %sappend, %sACLs, %sxattrs\n",
662 + have_inplace, acls, xattrs);
664 #ifdef MAINTAINER_MODE
665 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
666 @@ -286,7 +290,7 @@ void usage(enum logcode F)
667 rprintf(F," -q, --quiet suppress non-error messages\n");
668 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
669 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
670 - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
671 + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
672 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
673 rprintf(F," -r, --recursive recurse into directories\n");
674 rprintf(F," -R, --relative use relative path names\n");
675 @@ -311,6 +315,9 @@ void usage(enum logcode F)
677 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
679 +#ifdef SUPPORT_XATTRS
680 + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
682 rprintf(F," -o, --owner preserve owner (super-user only)\n");
683 rprintf(F," -g, --group preserve group\n");
684 rprintf(F," --devices preserve device files (super-user only)\n");
685 @@ -438,6 +445,9 @@ static struct poptOption long_options[]
686 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
687 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
688 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
689 + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
690 + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
691 + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
692 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
693 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
694 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
695 @@ -1126,6 +1136,17 @@ int parse_arguments(int *argc, const cha
700 +#ifdef SUPPORT_XATTRS
701 + preserve_xattrs = 1;
702 + preserve_perms = 1;
705 + snprintf(err_buf,sizeof(err_buf),
706 + "extended attributes are not supported on this %s\n",
707 + am_server ? "server" : "client");
712 /* A large opt value means that set_refuse_options()
713 @@ -1590,6 +1611,10 @@ void server_options(char **args,int *arg
717 +#ifdef SUPPORT_XATTRS
718 + if (preserve_xattrs)
732 extern int am_server;
733 extern int do_progress;
734 @@ -366,8 +367,8 @@ int recv_files(int f_in, char *local_nam
737 /* This call also sets cur_flist. */
738 - ndx = read_ndx_and_attrs(f_in, -1, &iflags,
739 - &fnamecmp_type, xname, &xlen);
740 + ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
742 if (ndx == NDX_DONE) {
743 if (inc_recurse && first_flist) {
744 flist_free(first_flist);
745 @@ -397,8 +398,17 @@ int recv_files(int f_in, char *local_nam
747 rprintf(FINFO, "recv_files(%s)\n", fname);
749 +#ifdef SUPPORT_XATTRS
750 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
751 + recv_xattr_request(file, f_in);
754 if (!(iflags & ITEM_TRANSFER)) {
755 maybe_log_item(file, iflags, itemizing, xname);
756 +#ifdef SUPPORT_XATTRS
757 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
758 + set_file_attrs(fname, file, NULL, fname, 0);
763 @@ -655,15 +665,15 @@ int recv_files(int f_in, char *local_nam
764 temp_copy_name = NULL;
766 temp_copy_name = partialptr;
767 - finish_transfer(fname, fnametmp, temp_copy_name,
769 + finish_transfer(fname, fnametmp, fnamecmp,
770 + temp_copy_name, file, recv_ok, 1);
771 if (fnamecmp == partialptr) {
772 do_unlink(partialptr);
773 handle_partial_dir(partialptr, PDIR_DELETE);
775 } else if (keep_partial && partialptr
776 && handle_partial_dir(partialptr, PDIR_CREATE)) {
777 - finish_transfer(partialptr, fnametmp, NULL,
778 + finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
779 file, recv_ok, !partial_dir);
780 if (delay_updates && recv_ok) {
781 bitbag_set_bit(delayed_bits, ndx);
787 extern int preserve_acls;
788 +extern int preserve_xattrs;
789 extern int preserve_perms;
790 extern int preserve_executability;
791 extern int preserve_times;
792 @@ -91,10 +92,8 @@ void setup_iconv()
796 -/* This is used by sender.c with a valid f_out, and by receive.c with
798 -int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr,
799 - uchar *type_ptr, char *buf, int *len_ptr)
800 +int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
801 + char *buf, int *len_ptr)
804 struct file_list *flist;
805 @@ -181,11 +180,6 @@ int read_ndx_and_attrs(int f_in, int f_o
807 exit_cleanup(RERR_PROTOCOL);
809 - } else if (f_out >= 0) {
811 - send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
812 - write_ndx_and_attrs(f_out, ndx, iflags,
813 - fnamecmp_type, buf, len);
817 @@ -228,8 +222,8 @@ mode_t dest_mode(mode_t flist_mode, mode
821 -int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
823 +int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp,
824 + const char *fnamecmp, int flags)
828 @@ -247,6 +241,9 @@ int set_file_attrs(char *fname, struct f
830 sx2.acc_acl = sx2.def_acl = NULL;
832 +#ifdef SUPPORT_XATTRS
835 if (!preserve_perms && S_ISDIR(new_mode)
836 && sx2.st.st_mode & S_ISGID) {
837 /* We just created this directory and its setgid
838 @@ -321,6 +318,10 @@ int set_file_attrs(char *fname, struct f
839 if (daemon_chmod_modes && !S_ISLNK(new_mode))
840 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
842 +#ifdef SUPPORT_XATTRS
843 + if (preserve_xattrs && fnamecmp)
844 + set_xattr(fname, file, fnamecmp);
847 /* It's OK to call set_acl() now, even for a dir, as the generator
848 * will enable owner-writability using chmod, if necessary.
849 @@ -353,10 +354,16 @@ int set_file_attrs(char *fname, struct f
850 rprintf(FCLIENT, "%s is uptodate\n", fname);
855 - if (preserve_acls && sxp == &sx2)
860 +#ifdef SUPPORT_XATTRS
861 + if (preserve_xattrs)
868 @@ -378,7 +385,8 @@ RETSIGTYPE sig_int(UNUSED(int val))
869 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
870 * NULL and the robust_rename() call is forced to copy the temp file, we
871 * stage the file into the partial-dir and then rename it into place. */
872 -void finish_transfer(char *fname, char *fnametmp, char *partialptr,
873 +void finish_transfer(const char *fname, const char *fnametmp,
874 + const char *fnamecmp, const char *partialptr,
875 struct file_struct *file, int ok_to_set_time,
876 int overwriting_basis)
878 @@ -395,7 +403,7 @@ void finish_transfer(char *fname, char *
881 /* Change permissions before putting the file into place. */
882 - set_file_attrs(fnametmp, file, NULL,
883 + set_file_attrs(fnametmp, file, NULL, fnamecmp,
884 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
886 /* move tmp file over real file */
887 @@ -419,7 +427,7 @@ void finish_transfer(char *fname, char *
888 fnametmp = partialptr ? partialptr : fname;
891 - set_file_attrs(fnametmp, file, NULL,
892 + set_file_attrs(fnametmp, file, NULL, fnamecmp,
893 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
898 @@ -569,6 +569,7 @@ extern int file_extra_cnt;
899 extern int preserve_uid;
900 extern int preserve_gid;
901 extern int preserve_acls;
902 +extern int preserve_xattrs;
904 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
905 #define EXTRA_LEN (sizeof (union file_extras))
906 @@ -601,7 +602,8 @@ extern int preserve_acls;
907 /* When the associated option is on, all entries will have these present: */
908 #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
909 #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
910 -#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
911 +#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->num
912 +#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->num
914 /* These items are per-entry optional and mutally exclusive: */
915 #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
916 @@ -793,9 +795,13 @@ typedef struct {
917 struct rsync_acl *acc_acl; /* access ACL */
918 struct rsync_acl *def_acl; /* default ACL */
920 +#ifdef SUPPORT_XATTRS
925 #define ACL_READY(sx) ((sx).acc_acl != NULL)
926 +#define XATTR_READY(sx) ((sx).xattr != NULL)
932 @@ -301,7 +301,7 @@ to the detailed description below for a
933 -q, --quiet suppress non-error messages
934 --no-motd suppress daemon-mode MOTD (see caveat)
935 -c, --checksum skip based on checksum, not mod-time & size
936 - -a, --archive archive mode; same as -rlptgoD (no -H, -A)
937 + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
938 --no-OPTION turn off an implied OPTION (e.g. --no-D)
939 -r, --recursive recurse into directories
940 -R, --relative use relative path names
941 @@ -324,6 +324,7 @@ to the detailed description below for a
942 -E, --executability preserve executability
943 --chmod=CHMOD affect file and/or directory permissions
944 -A, --acls preserve ACLs (implies -p)
945 + -X, --xattrs preserve extended attrs (implies -p)
946 -o, --owner preserve owner (super-user only)
947 -g, --group preserve group
948 --devices preserve device files (super-user only)
949 @@ -831,6 +832,11 @@ dit(bf(-A, --acls)) This option causes r
950 ACLs to be the same as the source ACLs. This nonstandard option only
951 works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
953 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
954 +extended attributes to be the same as the local ones. This will work
955 +only if the remote machine's rsync supports this option also. This is
956 +a non-standard option.
958 dit(bf(--chmod)) This option tells rsync to apply one or more
959 comma-separated "chmod" strings to the permission of the files in the
960 transfer. The resulting value is treated as though it was the permissions
969 extern int am_server;
970 extern int am_daemon;
971 @@ -144,8 +145,9 @@ void successful_send(int ndx)
972 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
975 -void write_ndx_and_attrs(int f_out, int ndx, int iflags,
976 - uchar fnamecmp_type, char *buf, int len)
977 +static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
978 + const char *fname, struct file_struct *file,
979 + uchar fnamecmp_type, char *buf, int len)
981 write_ndx(f_out, ndx);
982 if (protocol_version < 29)
983 @@ -155,6 +157,10 @@ void write_ndx_and_attrs(int f_out, int
984 write_byte(f_out, fnamecmp_type);
985 if (iflags & ITEM_XNAME_FOLLOWS)
986 write_vstring(f_out, buf, len);
987 +#ifdef SUPPORT_XATTRS
988 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
989 + send_xattr_request(fname, file, f_out);
993 void send_files(int f_in, int f_out)
994 @@ -183,8 +189,8 @@ void send_files(int f_in, int f_out)
995 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
997 /* This call also sets cur_flist. */
998 - ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
999 - &fnamecmp_type, xname, &xlen);
1000 + ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
1002 if (ndx == NDX_DONE) {
1003 if (inc_recurse && first_flist) {
1004 flist_free(first_flist);
1005 @@ -201,6 +207,9 @@ void send_files(int f_in, int f_out)
1010 + send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1012 file = cur_flist->files[ndx - cur_flist->ndx_start];
1013 if (F_ROOTDIR(file)) {
1014 path = F_ROOTDIR(file);
1015 @@ -215,8 +224,13 @@ void send_files(int f_in, int f_out)
1017 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
1019 + if (iflags & ITEM_REPORT_XATTR)
1020 + recv_xattr_request(file, f_in);
1022 if (!(iflags & ITEM_TRANSFER)) {
1023 maybe_log_item(file, iflags, itemizing, xname);
1024 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1025 + fnamecmp_type, xname, xlen);
1029 @@ -251,8 +265,8 @@ void send_files(int f_in, int f_out)
1031 if (!do_xfers) { /* log the transfer */
1032 log_item(FCLIENT, file, &stats, iflags, NULL);
1033 - write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1035 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1036 + fnamecmp_type, xname, xlen);
1040 @@ -305,8 +319,8 @@ void send_files(int f_in, int f_out)
1041 path,slash,fname, (double)st.st_size);
1044 - write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1046 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1047 + fnamecmp_type, xname, xlen);
1048 write_sum_head(f_xfer, s);
1051 --- old/testsuite/xattrs.test
1052 +++ new/testsuite/xattrs.test
1056 +# This program is distributable under the terms of the GNU GPL (see
1059 +# Test that rsync handles basic xattr preservation.
1061 +. $srcdir/testsuite/rsync.fns
1063 +$RSYNC --version | grep ", xattrs" >/dev/null || test_skipped "Rsync is configured without xattr support"
1066 +*protocol=29*) test_skipped "xattr support requires protocol 30" ;;
1069 +makepath "$fromdir/foo"
1070 +echo something >"$fromdir/file1"
1071 +echo else >"$fromdir/file2"
1072 +echo last >"$fromdir/foo/file3"
1074 +makepath "$todir/foo"
1075 +echo wow >"$todir/file1"
1076 +cp -p "$fromdir/foo/file3" "$todir/foo"
1078 +files='foo file1 file2 foo/file3'
1082 +setfattr -n user.short -v 'this is short' file1 2>/dev/null || test_skipped "Unable to set an xattr"
1083 +setfattr -n user.long -v 'this is a long attribute that will be truncated in the initial data send' file1
1084 +setfattr -n user.good -v 'this is good' file1
1085 +setfattr -n user.nice -v 'this is nice' file1
1087 +setfattr -n user.foo -v foo file2
1088 +setfattr -n user.bar -v bar file2
1090 +setfattr -n user.foo -v 'new foo' foo/file3
1091 +setfattr -n user.bar -v 'new bar' foo/file3
1092 +setfattr -n user.long -v 'this is also a long attribute that will be truncated in the initial data send' foo/file3
1093 +setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' foo/file3
1095 +setfattr -n user.short -v 'old short' "$todir/file1"
1096 +setfattr -n user.extra -v 'remove me' "$todir/file1"
1098 +setfattr -n user.foo -v 'old foo' "$todir/foo/file3"
1099 +setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' "$todir/foo/file3"
1101 +$RSYNC -avX . "$todir/"
1103 +getfattr -d $files >"$scratchdir/xattrs.txt"
1106 +getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
1108 +# The script would have aborted on error, so getting here means we've won.
1114 + * Extended Attribute support for rsync.
1115 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
1117 + * Copyright (C) 2004 Red Hat, Inc.
1118 + * Copyright (C) 2006, 2007 Wayne Davison
1120 + * This program is free software; you can redistribute it and/or modify
1121 + * it under the terms of the GNU General Public License version 2 as
1122 + * published by the Free Software Foundation.
1124 + * This program is distributed in the hope that it will be useful,
1125 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1126 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1127 + * GNU General Public License for more details.
1129 + * You should have received a copy of the GNU General Public License along
1130 + * with this program; if not, write to the Free Software Foundation, Inc.,
1131 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1135 +#include "lib/sysxattrs.h"
1137 +#ifdef SUPPORT_XATTRS
1139 +extern int dry_run;
1140 +extern int am_root;
1141 +extern int am_sender;
1142 +extern int read_only;
1143 +extern int list_only;
1144 +extern int checksum_seed;
1146 +#define RSYNC_XAL_INITIAL 5
1147 +#define RSYNC_XAL_LIST_INITIAL 100
1149 +#define MAX_FULL_DATUM 32
1151 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
1152 + && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
1154 +#define USER_PREFIX "user."
1155 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
1156 +#define SYSTEM_PREFIX "system."
1157 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
1159 +#ifdef HAVE_LINUX_XATTRS
1162 +#define RSYNC_PREFIX "rsync."
1163 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
1167 + char *datum, *name;
1168 + size_t datum_len, name_len;
1171 +static size_t namebuf_len = 0;
1172 +static char *namebuf = NULL;
1174 +static item_list empty_xattr = EMPTY_ITEM_LIST;
1175 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
1177 +/* ------------------------------------------------------------------------- */
1179 +static void rsync_xal_free(item_list *xalp)
1182 + rsync_xa *rxas = xalp->items;
1184 + for (i = 0; i < xalp->count; i++) {
1185 + free(rxas[i].datum);
1186 + /*free(rxas[i].name);*/
1191 +void free_xattr(statx *sxp)
1195 + rsync_xal_free(sxp->xattr);
1197 + sxp->xattr = NULL;
1200 +static int rsync_xal_compare_names(const void *x1, const void *x2)
1202 + const rsync_xa *xa1 = x1;
1203 + const rsync_xa *xa2 = x2;
1204 + return strcmp(xa1->name, xa2->name);
1207 +static ssize_t get_xattr_names(const char *fname)
1212 + namebuf_len = 1024;
1213 + namebuf = new_array(char, namebuf_len);
1215 + out_of_memory("get_xattr_names");
1218 + /* The length returned includes all the '\0' terminators. */
1219 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1220 + if (list_len > (ssize_t)namebuf_len) {
1224 + if (list_len < 0) {
1225 + if (errno == ENOTSUP)
1227 + if (errno == ERANGE) {
1228 + list_len = sys_llistxattr(fname, NULL, 0);
1229 + if (list_len < 0) {
1230 + rsyserr(FERROR, errno,
1231 + "get_xattr_names: llistxattr(\"%s\",0) failed",
1237 + namebuf_len = list_len + 1024;
1238 + namebuf = new_array(char, namebuf_len);
1240 + out_of_memory("get_xattr_names");
1241 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1242 + if (list_len < 0) {
1243 + rsyserr(FERROR, errno,
1244 + "get_xattr_names: llistxattr(\"%s\",%ld) failed",
1245 + fname, (long)namebuf_len);
1249 + rsyserr(FERROR, errno,
1250 + "get_xattr_names: llistxattr(\"%s\",%ld) failed",
1251 + fname, (long)namebuf_len);
1259 +/* On entry, the *len_ptr parameter contains the size of the extra space we
1260 + * should allocate when we create a buffer for the data. On exit, it contains
1261 + * the length of the datum. */
1262 +static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr)
1264 + size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
1267 + if (datum_len == (size_t)-1) {
1268 + if (errno == ENOTSUP)
1270 + rsyserr(FERROR, errno,
1271 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
1276 + if (datum_len + *len_ptr < datum_len /* checks for overflow */
1277 + || !(ptr = new_array(char, datum_len + *len_ptr)))
1278 + out_of_memory("get_xattr_data");
1280 + *len_ptr = datum_len;
1283 + size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
1284 + if (len != datum_len) {
1285 + if (len == (size_t)-1) {
1286 + rsyserr(FERROR, errno,
1287 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1288 + " failed", fname, name, (long)datum_len);
1291 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1292 + " returned %ld\n", fname, name,
1293 + (long)datum_len, (long)len);
1303 +static int rsync_xal_get(const char *fname, item_list *xalp)
1305 + ssize_t list_len, name_len;
1306 + size_t datum_len, name_offset;
1308 + int user_only = am_sender ? 0 : !am_root;
1310 + /* This puts the name list into the "namebuf" buffer. */
1311 + if ((list_len = get_xattr_names(fname)) < 0)
1314 + for (name = namebuf; list_len > 0; name += name_len) {
1317 + name_len = strlen(name) + 1;
1318 + list_len -= name_len;
1320 +#ifdef HAVE_LINUX_XATTRS
1321 + /* We always ignore the system namespace, and non-root
1322 + * ignores everything but the user namespace. */
1323 + if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
1324 + : HAS_PREFIX(name, SYSTEM_PREFIX))
1328 + datum_len = name_len; /* Pass extra size to get_xattr_data() */
1329 + if (!(ptr = get_xattr_data(fname, name, &datum_len)))
1332 + if (datum_len > MAX_FULL_DATUM) {
1333 + /* For large datums, we store a flag and a checksum. */
1334 + name_offset = 1 + MAX_DIGEST_LEN;
1335 + sum_init(checksum_seed);
1336 + sum_update(ptr, datum_len);
1339 + if (!(ptr = new_array(char, name_offset + name_len)))
1340 + out_of_memory("rsync_xal_get");
1344 + name_offset = datum_len;
1346 + rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
1347 + rxas->name = ptr + name_offset;
1348 + memcpy(rxas->name, name, name_len);
1349 + rxas->datum = ptr;
1350 + rxas->name_len = name_len;
1351 + rxas->datum_len = datum_len;
1353 + if (xalp->count > 1)
1354 + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
1358 +/* Read the xattr(s) for this filename. */
1359 +int get_xattr(const char *fname, statx *sxp)
1361 + sxp->xattr = new(item_list);
1362 + *sxp->xattr = empty_xattr;
1363 + if (rsync_xal_get(fname, sxp->xattr) < 0) {
1370 +static int find_matching_xattr(item_list *xalp)
1373 + item_list *lst = rsync_xal_l.items;
1375 + for (i = 0; i < rsync_xal_l.count; i++) {
1376 + rsync_xa *rxas1 = lst[i].items;
1377 + rsync_xa *rxas2 = xalp->items;
1379 + /* Wrong number of elements? */
1380 + if (lst[i].count != xalp->count)
1382 + /* any elements different? */
1383 + for (j = 0; j < xalp->count; j++) {
1384 + if (rxas1[j].name_len != rxas2[j].name_len
1385 + || rxas1[j].datum_len != rxas2[j].datum_len
1386 + || strcmp(rxas1[j].name, rxas2[j].name))
1388 + if (rxas1[j].datum_len > MAX_FULL_DATUM) {
1389 + if (memcmp(rxas1[j].datum + 1,
1390 + rxas2[j].datum + 1,
1391 + MAX_DIGEST_LEN) != 0)
1394 + if (memcmp(rxas1[j].datum, rxas2[j].datum,
1395 + rxas2[j].datum_len))
1399 + /* no differences found. This is The One! */
1400 + if (j == xalp->count)
1407 +/* Store *xalp on the end of rsync_xal_l */
1408 +static void rsync_xal_store(item_list *xalp)
1410 + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
1411 + /* Since the following call starts a new list, we know it will hold the
1412 + * entire initial-count, not just enough space for one new item. */
1413 + *new_lst = empty_xattr;
1414 + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
1415 + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
1416 + new_lst->count = xalp->count;
1420 +/* Send the make_xattr()-generated xattr list for this flist entry. */
1421 +int send_xattr(statx *sxp, int f)
1423 + int ndx = find_matching_xattr(sxp->xattr);
1425 + /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
1426 + write_abbrevint(f, ndx + 1);
1430 + int count = sxp->xattr->count;
1431 + write_abbrevint(f, count);
1432 + for (rxa = sxp->xattr->items; count--; rxa++) {
1433 +#ifdef HAVE_LINUX_XATTRS
1434 + write_abbrevint(f, rxa->name_len);
1435 + write_abbrevint(f, rxa->datum_len);
1436 + write_buf(f, rxa->name, rxa->name_len);
1438 + /* We strip the rsync prefix from disguised namespaces
1439 + * and put everything else in the user namespace. */
1440 + if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
1441 + && rxa->name[RPRE_LEN] != '%') {
1442 + write_abbrevint(f, rxa->name_len - RPRE_LEN);
1443 + write_abbrevint(f, rxa->datum_len);
1444 + write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
1446 + write_abbrevint(f, rxa->name_len + UPRE_LEN);
1447 + write_abbrevint(f, rxa->datum_len);
1448 + write_buf(f, USER_PREFIX, UPRE_LEN);
1449 + write_buf(f, rxa->name, rxa->name_len);
1452 + if (rxa->datum_len > MAX_FULL_DATUM)
1453 + write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
1455 + write_buf(f, rxa->datum, rxa->datum_len);
1457 + ndx = rsync_xal_l.count; /* pre-incremented count */
1458 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1464 +/* Return a flag indicating if we need to change a file's xattrs. If
1465 + * "find_all" is specified, also mark any abbreviated xattrs that we
1466 + * need so that send_xattr_request() can tell the sender about them. */
1467 +int xattr_diff(struct file_struct *file, statx *sxp, int find_all)
1469 + item_list *lst = rsync_xal_l.items;
1470 + rsync_xa *snd_rxa, *rec_rxa;
1471 + int snd_cnt, rec_cnt;
1472 + int cmp, same, xattrs_equal = 1;
1474 + if (!XATTR_READY(*sxp)) {
1478 + rec_rxa = sxp->xattr->items;
1479 + rec_cnt = sxp->xattr->count;
1482 + if (F_XATTR(file) >= 0)
1483 + lst += F_XATTR(file);
1485 + lst = &empty_xattr;
1487 + snd_rxa = lst->items;
1488 + snd_cnt = lst->count;
1490 + /* If the count of the sender's xattrs is different from our
1491 + * (receiver's) xattrs, the lists are not the same. */
1492 + if (snd_cnt != rec_cnt) {
1499 + cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
1502 + else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
1503 + same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1504 + && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
1505 + MAX_DIGEST_LEN) == 0;
1506 + /* Flag unrequested items that we need. */
1507 + if (!same && find_all && snd_rxa->datum[0] == 0)
1508 + snd_rxa->datum[0] = 1;
1510 + same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1511 + && memcmp(snd_rxa->datum, rec_rxa->datum,
1512 + snd_rxa->datum_len) == 0;
1533 + return !xattrs_equal;
1536 +/* When called by the generator with a NULL fname, this tells the sender
1537 + * which abbreviated xattr values we need. When called by the sender
1538 + * (with a non-NULL fname), we send all the extra xattr data it needs. */
1539 +void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
1541 + item_list *lst = rsync_xal_l.items;
1542 + int j, cnt, prior_req = -1;
1545 + lst += F_XATTR(file);
1547 + for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) {
1548 + if (rxa->datum_len <= MAX_FULL_DATUM
1549 + || rxa->datum[0] != 1)
1552 + /* Flag that we handled this abbreviated item. */
1553 + rxa->datum[0] = 2;
1555 + write_abbrevint(f_out, j - prior_req);
1562 + /* Re-read the long datum. */
1563 + if (!(ptr = get_xattr_data(fname, rxa->name, &len)))
1566 + write_abbrevint(f_out, len); /* length might have changed! */
1567 + write_buf(f_out, ptr, len);
1572 + write_byte(f_out, 0); /* end the list */
1575 +/* When called by the sender, read the request from the generator and mark
1576 + * any needed xattrs with a flag that lets us know they need to be sent to
1577 + * the receiver. When called by the receiver, reads the sent data and
1578 + * stores it in place of its checksum. */
1579 +void recv_xattr_request(struct file_struct *file, int f_in)
1581 + item_list *lst = rsync_xal_l.items;
1582 + char *old_datum, *name;
1586 + if (F_XATTR(file) >= 0)
1587 + lst += F_XATTR(file);
1589 + exit_cleanup(RERR_STREAMIO); /* XXX */
1594 + while ((rel_pos = read_abbrevint(f_in)) != 0) {
1597 + if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM
1598 + || rxa->datum[0] != 0)
1599 + exit_cleanup(RERR_STREAMIO); /* XXX */
1602 + rxa->datum[0] = 1;
1606 + old_datum = rxa->datum;
1607 + rxa->datum_len = read_abbrevint(f_in);
1609 + if (rxa->name_len + rxa->datum_len < rxa->name_len)
1610 + out_of_memory("recv_xattr_request"); /* overflow */
1611 + rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
1613 + out_of_memory("recv_xattr_request");
1614 + name = rxa->datum + rxa->datum_len;
1615 + memcpy(name, rxa->name, rxa->name_len);
1618 + read_buf(f_in, rxa->datum, rxa->datum_len);
1622 +/* ------------------------------------------------------------------------- */
1624 +/* receive and build the rsync_xattr_lists */
1625 +void receive_xattr(struct file_struct *file, int f)
1627 + static item_list temp_xattr = EMPTY_ITEM_LIST;
1629 + int ndx = read_abbrevint(f);
1631 + if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
1632 + rprintf(FERROR, "receive_xattr: xa index %d out of"
1633 + " range for %s\n", ndx, f_name(file, NULL));
1634 + exit_cleanup(RERR_STREAMIO);
1638 + F_XATTR(file) = ndx - 1;
1642 + if ((count = read_abbrevint(f)) != 0) {
1643 + (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
1644 + temp_xattr.count = 0;
1650 + size_t name_len = read_abbrevint(f);
1651 + size_t datum_len = read_abbrevint(f);
1652 + size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
1653 +#ifdef HAVE_LINUX_XATTRS
1654 + size_t extra_len = 0;
1656 + size_t extra_len = am_root ? RPRE_LEN : 0;
1657 + if (dget_len + extra_len < dget_len)
1658 + out_of_memory("receive_xattr"); /* overflow */
1660 + if (dget_len + extra_len + name_len < dget_len)
1661 + out_of_memory("receive_xattr"); /* overflow */
1662 + ptr = new_array(char, dget_len + extra_len + name_len);
1664 + out_of_memory("receive_xattr");
1665 + name = ptr + dget_len + extra_len;
1666 + read_buf(f, name, name_len);
1667 + if (dget_len == datum_len)
1668 + read_buf(f, ptr, dget_len);
1671 + read_buf(f, ptr + 1, MAX_DIGEST_LEN);
1673 +#ifdef HAVE_LINUX_XATTRS
1674 + /* Non-root can only save the user namespace. */
1675 + if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
1680 + /* This OS only has a user namespace, so we either
1681 + * strip the user prefix, or we put a non-user
1682 + * namespace inside our rsync hierarchy. */
1683 + if (HAS_PREFIX(name, USER_PREFIX)) {
1685 + name_len -= UPRE_LEN;
1686 + } else if (am_root) {
1688 + name_len += RPRE_LEN;
1689 + memcpy(name, RSYNC_PREFIX, RPRE_LEN);
1695 + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
1698 + rxa->name_len = name_len;
1699 + rxa->datum_len = datum_len;
1702 + ndx = rsync_xal_l.count; /* pre-incremented count */
1703 + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
1705 + F_XATTR(file) = ndx;
1708 +/* Turn the xattr data in statx into cached xattr data, setting the index
1709 + * values in the file struct. */
1710 +void cache_xattr(struct file_struct *file, statx *sxp)
1717 + ndx = find_matching_xattr(sxp->xattr);
1719 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1721 + F_XATTR(file) = ndx;
1724 +static int rsync_xal_set(const char *fname, item_list *xalp, const char *fnamecmp)
1726 + rsync_xa *rxas = xalp->items;
1730 + int name_len, status, ret = 0;
1732 + /* This puts the current name list into the "namebuf" buffer. */
1733 + if ((list_len = get_xattr_names(fname)) < 0)
1736 + for (i = 0; i < xalp->count; i++) {
1737 + if ((size_t)(rxas[i].name - rxas[i].datum) < rxas[i].datum_len) {
1738 + size_t len = rxas[i].name_len;
1741 + /* See if fnamecmp version is identical. */
1742 + if ((ptr = get_xattr_data(fnamecmp, rxas[i].name, &len)) != NULL
1743 + && len == rxas[i].datum_len) {
1744 + char sum[MAX_DIGEST_LEN];
1745 + sum_init(checksum_seed);
1746 + sum_update(ptr, len);
1748 + if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) == 0) {
1749 + char *name = ptr + len;
1750 + memcpy(name, rxas[i].name, rxas[i].name_len);
1751 + rxas[i].name = name;
1752 + free(rxas[i].datum);
1753 + rxas[i].datum = ptr;
1759 + rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
1760 + rxas[i].name, full_fname(fname));
1764 + if (fname == fnamecmp) /* value is already set */
1767 + status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len);
1769 + rsyserr(FERROR, errno,
1770 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1771 + fname, rxas[i].name);
1776 + /* Remove any extraneous names. */
1777 + for (name = namebuf; list_len > 0; name += name_len) {
1778 + name_len = strlen(name) + 1;
1779 + list_len -= name_len;
1781 +#ifdef HAVE_LINUX_XATTRS
1782 + /* We always ignore the system namespace, and non-root
1783 + * ignores everything but the user namespace. */
1784 + if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
1785 + : !HAS_PREFIX(name, USER_PREFIX))
1789 + for (i = 0; i < xalp->count; i++) {
1790 + if (strcmp(name, rxas[i].name) == 0)
1793 + if (i == xalp->count) {
1794 + int status = sys_lremovexattr(fname, name);
1796 + rsyserr(FERROR, errno,
1797 + "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
1807 +/* Set extended attributes on indicated filename. */
1808 +int set_xattr(const char *fname, const struct file_struct *file, const char *fnamecmp)
1811 + item_list *lst = rsync_xal_l.items;
1814 + return 1; /* FIXME: --dry-run needs to compute this value */
1816 + if (read_only || list_only) {
1821 + ndx = F_XATTR(file);
1822 + return rsync_xal_set(fname, lst + ndx, fnamecmp);
1825 +#endif /* SUPPORT_XATTRS */