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,29 @@ 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;
291 +#ifdef SUPPORT_XATTRS
292 + if (preserve_xattrs) {
293 + if (!XATTR_READY(*sxp))
294 + get_xattr(fnamecmp, sxp);
295 + if (xattr_diff(file, sxp, 1))
296 + iflags |= ITEM_REPORT_XATTR;
300 +#ifdef SUPPORT_XATTRS
301 + if (preserve_xattrs && xattr_diff(file, NULL, 1))
302 + iflags |= ITEM_REPORT_XATTR;
304 iflags |= ITEM_IS_NEW;
308 - if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
309 + if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
310 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
311 if (protocol_version >= 29) {
313 @@ -581,6 +603,10 @@ void itemize(const char *fname, struct f
314 write_byte(sock_f_out, fnamecmp_type);
315 if (iflags & ITEM_XNAME_FOLLOWS)
316 write_vstring(sock_f_out, xname, strlen(xname));
317 +#ifdef SUPPORT_XATTRS
318 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
319 + send_xattr_request(NULL, file, sock_f_out);
321 } else if (ndx >= 0) {
322 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
323 log_item(code, file, &stats, iflags, xname);
324 @@ -855,14 +881,14 @@ static int try_dests_reg(struct file_str
325 if (preserve_hard_links && F_IS_HLINKED(file))
326 finish_hard_link(file, fname, &sxp->st, itemizing, code, j);
327 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
328 - itemize(fname, file, ndx, 1, sxp,
329 + itemize(cmpbuf, file, ndx, 1, sxp,
330 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
336 - itemize(fname, file, ndx, 0, sxp, 0, 0, NULL);
337 + itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
338 if (verbose > 1 && maybe_ATTRS_REPORT)
339 rprintf(FCLIENT, "%s is uptodate\n", fname);
341 @@ -879,9 +905,13 @@ static int try_dests_reg(struct file_str
345 + set_file_attrs(fname, file, NULL, cmpbuf, 0);
347 - itemize(fname, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
348 - set_file_attrs(fname, file, NULL, 0);
349 + itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
350 +#ifdef SUPPORT_XATTRS
351 + if (preserve_xattrs)
352 + xattr_clear_locals(file);
354 if (maybe_ATTRS_REPORT
355 && ((!itemizing && verbose && match_level == 2)
356 || (verbose > 1 && match_level == 3))) {
357 @@ -1029,7 +1059,7 @@ static int try_dests_non(struct file_str
359 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
360 char *lp = match_level == 3 ? "" : NULL;
361 - itemize(fname, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
362 + itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
364 if (verbose > 1 && maybe_ATTRS_REPORT) {
365 rprintf(FCLIENT, "%s%s is uptodate\n",
366 @@ -1112,6 +1142,9 @@ static void recv_generator(char *fname,
368 sx.acc_acl = sx.def_acl = NULL;
370 +#ifdef SUPPORT_XATTRS
375 flist_free(fuzzy_dirlist);
376 @@ -1224,7 +1257,7 @@ static void recv_generator(char *fname,
380 - if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
381 + if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
382 && verbose && code != FNONE && f_out != -1)
383 rprintf(code, "%s/\n", fname);
384 if (real_ret != 0 && one_file_system)
385 @@ -1278,9 +1311,9 @@ static void recv_generator(char *fname,
386 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
387 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
388 /* The link is pointing to the right place. */
389 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
391 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
392 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
393 #ifdef SUPPORT_HARD_LINKS
394 if (preserve_hard_links && F_IS_HLINKED(file))
395 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
396 @@ -1317,7 +1350,7 @@ static void recv_generator(char *fname,
397 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
398 full_fname(fname), sl);
400 - set_file_attrs(fname, file, NULL, 0);
401 + set_file_attrs(fname, file, NULL, NULL, 0);
403 itemize(fname, file, ndx, statret, &sx,
404 ITEM_LOCAL_CHANGE, 0, NULL);
405 @@ -1357,9 +1390,9 @@ static void recv_generator(char *fname,
406 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
407 && sx.st.st_rdev == rdev) {
408 /* The device or special file is identical. */
409 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
411 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
412 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
413 #ifdef SUPPORT_HARD_LINKS
414 if (preserve_hard_links && F_IS_HLINKED(file))
415 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
416 @@ -1399,7 +1432,7 @@ static void recv_generator(char *fname,
417 rsyserr(FERROR, errno, "mknod %s failed",
420 - set_file_attrs(fname, file, NULL, 0);
421 + set_file_attrs(fname, file, NULL, NULL, 0);
423 itemize(fname, file, ndx, statret, &sx,
424 ITEM_LOCAL_CHANGE, 0, NULL);
425 @@ -1529,9 +1562,9 @@ static void recv_generator(char *fname,
426 do_unlink(partialptr);
427 handle_partial_dir(partialptr, PDIR_DELETE);
429 + set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
431 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
432 - set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
433 #ifdef SUPPORT_HARD_LINKS
434 if (preserve_hard_links && F_IS_HLINKED(file))
435 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
436 @@ -1636,6 +1669,10 @@ static void recv_generator(char *fname,
440 +#ifdef SUPPORT_XATTRS
441 + if (preserve_xattrs)
442 + free_xattr(&real_sx);
447 @@ -1657,7 +1694,7 @@ static void recv_generator(char *fname,
451 - set_file_attrs(backupptr, back_file, NULL, 0);
452 + set_file_attrs(backupptr, back_file, NULL, NULL, 0);
454 rprintf(FINFO, "backed up %s to %s\n",
456 @@ -1672,6 +1709,10 @@ static void recv_generator(char *fname,
460 +#ifdef SUPPORT_XATTRS
461 + if (preserve_xattrs)
467 --- old/lib/sysxattrs.c
468 +++ new/lib/sysxattrs.c
471 + * Extended attribute support for rsync.
473 + * Copyright (C) 2004 Red Hat, Inc.
474 + * Written by Jay Fenlason.
476 + * This program is free software; you can redistribute it and/or modify
477 + * it under the terms of the GNU General Public License as published by
478 + * the Free Software Foundation; either version 2 of the License, or
479 + * (at your option) any later version.
481 + * This program is distributed in the hope that it will be useful,
482 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
483 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
484 + * GNU General Public License for more details.
486 + * You should have received a copy of the GNU General Public License along
487 + * with this program; if not, write to the Free Software Foundation, Inc.,
488 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
492 +#include "sysxattrs.h"
494 +#ifdef SUPPORT_XATTRS
496 +#if defined HAVE_LINUX_XATTRS
498 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
500 + return lgetxattr(path, name, value, size);
503 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
505 + return fgetxattr(filedes, name, value, size);
508 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
510 + return lsetxattr(path, name, value, size, 0);
513 +int sys_lremovexattr(const char *path, const char *name)
515 + return lremovexattr(path, name);
518 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
520 + return llistxattr(path, list, size);
523 +#elif HAVE_OSX_XATTRS
525 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
527 + return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
530 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
532 + return fgetxattr(filedes, name, value, size, 0, 0);
535 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
537 + return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
540 +int sys_lremovexattr(const char *path, const char *name)
542 + return removexattr(path, name, XATTR_NOFOLLOW);
545 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
547 + return listxattr(path, list, size, XATTR_NOFOLLOW);
550 +#elif HAVE_FREEBSD_XATTRS
552 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
554 + return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
557 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
559 + return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
562 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
564 + return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
567 +int sys_lremovexattr(const char *path, const char *name)
569 + return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
572 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
574 + unsigned char keylen;
575 + ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
577 + if (len <= 0 || (size_t)len > size)
580 + /* FreeBSD puts a single-byte length before each string, with no '\0'
581 + * terminator. We need to change this into a series of null-terminted
582 + * strings. Since the size is the same, we can simply transform the
583 + * output in place. */
584 + for (off = 0; off < len; off += keylen + 1) {
585 + keylen = ((unsigned char*)list)[off];
586 + if (off + keylen >= len) {
587 + /* Should be impossible, but kernel bugs happen! */
591 + memmove(list+off, list+off+1, keylen);
592 + list[off+keylen] = '\0';
600 +#error You need to create xattr compatibility functions.
604 +#endif /* SUPPORT_XATTRS */
605 --- old/lib/sysxattrs.h
606 +++ new/lib/sysxattrs.h
608 +#ifdef SUPPORT_XATTRS
610 +#if defined HAVE_ATTR_XATTR_H
611 +#include <attr/xattr.h>
612 +#elif defined HAVE_SYS_XATTR_H
613 +#include <sys/xattr.h>
614 +#elif defined HAVE_SYS_EXTATTR_H
615 +#include <sys/extattr.h>
618 +/* Linux 2.4 does not define this as a distinct errno value: */
620 +#define ENOATTR ENODATA
623 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
624 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
625 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
626 +int sys_lremovexattr(const char *path, const char *name);
627 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
631 +/* No xattrs available */
636 @@ -608,7 +608,7 @@ static void fix_basis_dirs(void)
639 /* This is only called by the sender. */
640 -static void read_final_goodbye(int f_in, int f_out)
641 +static void read_final_goodbye(int f_in)
645 @@ -617,8 +617,8 @@ static void read_final_goodbye(int f_in,
646 if (protocol_version < 29)
649 - i = read_ndx_and_attrs(f_in, f_out, &iflags,
650 - &fnamecmp_type, xname, &xlen);
651 + i = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
656 @@ -677,7 +677,7 @@ static void do_server_sender(int f_in, i
657 io_flush(FULL_FLUSH);
659 if (protocol_version >= 24)
660 - read_final_goodbye(f_in, f_out);
661 + read_final_goodbye(f_in);
662 io_flush(FULL_FLUSH);
665 @@ -740,7 +740,7 @@ static int do_recv(int f_in, int f_out,
666 kluge_around_eof = -1;
668 /* This should only get stopped via a USR2 signal. */
669 - read_ndx_and_attrs(f_in, -1, &iflags, &fnamecmp_type,
670 + read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
673 rprintf(FERROR, "Invalid packet at end of run [%s]\n",
674 @@ -977,7 +977,7 @@ int client_run(int f_in, int f_out, pid_
675 io_flush(FULL_FLUSH);
677 if (protocol_version >= 24)
678 - read_final_goodbye(f_in, f_out);
679 + read_final_goodbye(f_in);
682 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
685 @@ -47,6 +47,7 @@ int copy_links = 0;
686 int preserve_links = 0;
687 int preserve_hard_links = 0;
688 int preserve_acls = 0;
689 +int preserve_xattrs = 0;
690 int preserve_perms = 0;
691 int preserve_executability = 0;
692 int preserve_devices = 0;
693 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
694 char const *have_inplace = "no ";
695 char const *hardlinks = "no ";
696 char const *acls = "no ";
697 + char const *xattrs = "no ";
698 char const *links = "no ";
699 char const *ipv6 = "no ";
700 STRUCT_STAT *dumstat;
701 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
706 +#ifdef SUPPORT_XATTRS
712 @@ -239,8 +243,8 @@ static void print_rsync_version(enum log
713 (int)(sizeof (int64) * 8));
714 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
715 got_socketpair, hardlinks, links, ipv6, have_inplace);
716 - rprintf(f, " %sappend, %sACLs\n",
717 - have_inplace, acls);
718 + rprintf(f, " %sappend, %sACLs, %sxattrs\n",
719 + have_inplace, acls, xattrs);
721 #ifdef MAINTAINER_MODE
722 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
723 @@ -286,7 +290,7 @@ void usage(enum logcode F)
724 rprintf(F," -q, --quiet suppress non-error messages\n");
725 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
726 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
727 - rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
728 + rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
729 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
730 rprintf(F," -r, --recursive recurse into directories\n");
731 rprintf(F," -R, --relative use relative path names\n");
732 @@ -311,6 +315,9 @@ void usage(enum logcode F)
734 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
736 +#ifdef SUPPORT_XATTRS
737 + rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
739 rprintf(F," -o, --owner preserve owner (super-user only)\n");
740 rprintf(F," -g, --group preserve group\n");
741 rprintf(F," --devices preserve device files (super-user only)\n");
742 @@ -438,6 +445,9 @@ static struct poptOption long_options[]
743 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
744 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
745 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
746 + {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
747 + {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
748 + {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
749 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
750 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
751 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
752 @@ -1126,6 +1136,17 @@ int parse_arguments(int *argc, const cha
757 +#ifdef SUPPORT_XATTRS
758 + preserve_xattrs = 1;
759 + preserve_perms = 1;
762 + snprintf(err_buf,sizeof(err_buf),
763 + "extended attributes are not supported on this %s\n",
764 + am_server ? "server" : "client");
769 /* A large opt value means that set_refuse_options()
770 @@ -1590,6 +1611,10 @@ void server_options(char **args,int *arg
774 +#ifdef SUPPORT_XATTRS
775 + if (preserve_xattrs)
789 extern int am_server;
790 extern int do_progress;
791 @@ -37,6 +38,7 @@ extern int protocol_version;
792 extern int relative_paths;
793 extern int preserve_hard_links;
794 extern int preserve_perms;
795 +extern int preserve_xattrs;
796 extern int basis_dir_cnt;
797 extern int make_backups;
798 extern int cleanup_got_literal;
799 @@ -366,8 +368,8 @@ int recv_files(int f_in, char *local_nam
802 /* This call also sets cur_flist. */
803 - ndx = read_ndx_and_attrs(f_in, -1, &iflags,
804 - &fnamecmp_type, xname, &xlen);
805 + ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
807 if (ndx == NDX_DONE) {
808 if (inc_recurse && first_flist) {
809 flist_free(first_flist);
810 @@ -397,8 +399,17 @@ int recv_files(int f_in, char *local_nam
812 rprintf(FINFO, "recv_files(%s)\n", fname);
814 +#ifdef SUPPORT_XATTRS
815 + if (iflags & ITEM_REPORT_XATTR && !dry_run)
816 + recv_xattr_request(file, f_in);
819 if (!(iflags & ITEM_TRANSFER)) {
820 maybe_log_item(file, iflags, itemizing, xname);
821 +#ifdef SUPPORT_XATTRS
822 + if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
823 + set_file_attrs(fname, file, NULL, fname, 0);
828 @@ -655,15 +666,15 @@ int recv_files(int f_in, char *local_nam
829 temp_copy_name = NULL;
831 temp_copy_name = partialptr;
832 - finish_transfer(fname, fnametmp, temp_copy_name,
834 + finish_transfer(fname, fnametmp, fnamecmp,
835 + temp_copy_name, file, recv_ok, 1);
836 if (fnamecmp == partialptr) {
837 do_unlink(partialptr);
838 handle_partial_dir(partialptr, PDIR_DELETE);
840 } else if (keep_partial && partialptr
841 && handle_partial_dir(partialptr, PDIR_CREATE)) {
842 - finish_transfer(partialptr, fnametmp, NULL,
843 + finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
844 file, recv_ok, !partial_dir);
845 if (delay_updates && recv_ok) {
846 bitbag_set_bit(delayed_bits, ndx);
852 extern int preserve_acls;
853 +extern int preserve_xattrs;
854 extern int preserve_perms;
855 extern int preserve_executability;
856 extern int preserve_times;
857 @@ -91,10 +92,8 @@ void setup_iconv()
861 -/* This is used by sender.c with a valid f_out, and by receive.c with
863 -int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr,
864 - uchar *type_ptr, char *buf, int *len_ptr)
865 +int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
866 + char *buf, int *len_ptr)
869 struct file_list *flist;
870 @@ -181,11 +180,6 @@ int read_ndx_and_attrs(int f_in, int f_o
872 exit_cleanup(RERR_PROTOCOL);
874 - } else if (f_out >= 0) {
876 - send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
877 - write_ndx_and_attrs(f_out, ndx, iflags,
878 - fnamecmp_type, buf, len);
882 @@ -228,8 +222,8 @@ mode_t dest_mode(mode_t flist_mode, mode
886 -int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
888 +int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp,
889 + const char *fnamecmp, int flags)
893 @@ -247,6 +241,9 @@ int set_file_attrs(char *fname, struct f
895 sx2.acc_acl = sx2.def_acl = NULL;
897 +#ifdef SUPPORT_XATTRS
900 if (!preserve_perms && S_ISDIR(new_mode)
901 && sx2.st.st_mode & S_ISGID) {
902 /* We just created this directory and its setgid
903 @@ -261,6 +258,11 @@ int set_file_attrs(char *fname, struct f
907 +#ifdef SUPPORT_XATTRS
908 + if (preserve_xattrs && fnamecmp)
909 + set_xattr(fname, file, fnamecmp, sxp);
912 if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && omit_dir_times))
913 flags |= ATTRS_SKIP_MTIME;
914 if (!(flags & ATTRS_SKIP_MTIME)
915 @@ -353,10 +355,16 @@ int set_file_attrs(char *fname, struct f
916 rprintf(FCLIENT, "%s is uptodate\n", fname);
921 - if (preserve_acls && sxp == &sx2)
926 +#ifdef SUPPORT_XATTRS
927 + if (preserve_xattrs)
934 @@ -378,7 +386,8 @@ RETSIGTYPE sig_int(UNUSED(int val))
935 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
936 * NULL and the robust_rename() call is forced to copy the temp file, we
937 * stage the file into the partial-dir and then rename it into place. */
938 -void finish_transfer(char *fname, char *fnametmp, char *partialptr,
939 +void finish_transfer(const char *fname, const char *fnametmp,
940 + const char *fnamecmp, const char *partialptr,
941 struct file_struct *file, int ok_to_set_time,
942 int overwriting_basis)
944 @@ -395,7 +404,7 @@ void finish_transfer(char *fname, char *
947 /* Change permissions before putting the file into place. */
948 - set_file_attrs(fnametmp, file, NULL,
949 + set_file_attrs(fnametmp, file, NULL, fnamecmp,
950 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
952 /* move tmp file over real file */
953 @@ -419,7 +428,7 @@ void finish_transfer(char *fname, char *
954 fnametmp = partialptr ? partialptr : fname;
957 - set_file_attrs(fnametmp, file, NULL,
958 + set_file_attrs(fnametmp, file, NULL, fnamecmp,
959 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
964 @@ -569,6 +569,7 @@ extern int file_extra_cnt;
965 extern int preserve_uid;
966 extern int preserve_gid;
967 extern int preserve_acls;
968 +extern int preserve_xattrs;
970 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
971 #define EXTRA_LEN (sizeof (union file_extras))
972 @@ -601,7 +602,8 @@ extern int preserve_acls;
973 /* When the associated option is on, all entries will have these present: */
974 #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
975 #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
976 -#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
977 +#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->num
978 +#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->num
980 /* These items are per-entry optional and mutally exclusive: */
981 #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
982 @@ -793,9 +795,13 @@ typedef struct {
983 struct rsync_acl *acc_acl; /* access ACL */
984 struct rsync_acl *def_acl; /* default ACL */
986 +#ifdef SUPPORT_XATTRS
991 #define ACL_READY(sx) ((sx).acc_acl != NULL)
992 +#define XATTR_READY(sx) ((sx).xattr != NULL)
998 @@ -301,7 +301,7 @@ to the detailed description below for a
999 -q, --quiet suppress non-error messages
1000 --no-motd suppress daemon-mode MOTD (see caveat)
1001 -c, --checksum skip based on checksum, not mod-time & size
1002 - -a, --archive archive mode; same as -rlptgoD (no -H, -A)
1003 + -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
1004 --no-OPTION turn off an implied OPTION (e.g. --no-D)
1005 -r, --recursive recurse into directories
1006 -R, --relative use relative path names
1007 @@ -324,6 +324,7 @@ to the detailed description below for a
1008 -E, --executability preserve executability
1009 --chmod=CHMOD affect file and/or directory permissions
1010 -A, --acls preserve ACLs (implies -p)
1011 + -X, --xattrs preserve extended attrs (implies -p)
1012 -o, --owner preserve owner (super-user only)
1013 -g, --group preserve group
1014 --devices preserve device files (super-user only)
1015 @@ -831,6 +832,11 @@ dit(bf(-A, --acls)) This option causes r
1016 ACLs to be the same as the source ACLs. This nonstandard option only
1017 works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
1019 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
1020 +extended attributes to be the same as the local ones. This will work
1021 +only if the remote machine's rsync supports this option also. This is
1022 +a non-standard option.
1024 dit(bf(--chmod)) This option tells rsync to apply one or more
1025 comma-separated "chmod" strings to the permission of the files in the
1026 transfer. The resulting value is treated as though it was the permissions
1033 +extern int dry_run;
1034 extern int do_xfers;
1035 extern int am_server;
1036 extern int am_daemon;
1037 @@ -33,6 +34,7 @@ extern int csum_length;
1038 extern int append_mode;
1039 extern int io_error;
1040 extern int allowed_lull;
1041 +extern int preserve_xattrs;
1042 extern int protocol_version;
1043 extern int remove_source_files;
1044 extern int updating_basis_file;
1045 @@ -144,8 +146,9 @@ void successful_send(int ndx)
1046 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
1049 -void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1050 - uchar fnamecmp_type, char *buf, int len)
1051 +static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1052 + const char *fname, struct file_struct *file,
1053 + uchar fnamecmp_type, char *buf, int len)
1055 write_ndx(f_out, ndx);
1056 if (protocol_version < 29)
1057 @@ -155,6 +158,10 @@ void write_ndx_and_attrs(int f_out, int
1058 write_byte(f_out, fnamecmp_type);
1059 if (iflags & ITEM_XNAME_FOLLOWS)
1060 write_vstring(f_out, buf, len);
1061 +#ifdef SUPPORT_XATTRS
1062 + if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
1063 + send_xattr_request(fname, file, f_out);
1067 void send_files(int f_in, int f_out)
1068 @@ -183,8 +190,8 @@ void send_files(int f_in, int f_out)
1069 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1071 /* This call also sets cur_flist. */
1072 - ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
1073 - &fnamecmp_type, xname, &xlen);
1074 + ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
1076 if (ndx == NDX_DONE) {
1077 if (inc_recurse && first_flist) {
1078 flist_free(first_flist);
1079 @@ -201,6 +208,9 @@ void send_files(int f_in, int f_out)
1084 + send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1086 file = cur_flist->files[ndx - cur_flist->ndx_start];
1087 if (F_ROOTDIR(file)) {
1088 path = F_ROOTDIR(file);
1089 @@ -215,8 +225,15 @@ void send_files(int f_in, int f_out)
1091 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
1093 +#ifdef SUPPORT_XATTRS
1094 + if (preserve_xattrs && iflags & ITEM_REPORT_XATTR)
1095 + recv_xattr_request(file, f_in);
1098 if (!(iflags & ITEM_TRANSFER)) {
1099 maybe_log_item(file, iflags, itemizing, xname);
1100 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1101 + fnamecmp_type, xname, xlen);
1105 @@ -251,8 +268,8 @@ void send_files(int f_in, int f_out)
1107 if (!do_xfers) { /* log the transfer */
1108 log_item(FCLIENT, file, &stats, iflags, NULL);
1109 - write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1111 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1112 + fnamecmp_type, xname, xlen);
1116 @@ -305,8 +322,8 @@ void send_files(int f_in, int f_out)
1117 path,slash,fname, (double)st.st_size);
1120 - write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1122 + write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1123 + fnamecmp_type, xname, xlen);
1124 write_sum_head(f_xfer, s);
1127 --- old/testsuite/xattrs.test
1128 +++ new/testsuite/xattrs.test
1132 +# This program is distributable under the terms of the GNU GPL (see
1135 +# Test that rsync handles basic xattr preservation.
1137 +. $srcdir/testsuite/rsync.fns
1139 +$RSYNC --version | grep ", xattrs" >/dev/null || test_skipped "Rsync is configured without xattr support"
1142 +*protocol=29*) test_skipped "xattr support requires protocol 30" ;;
1149 + setfattr -n "$xnam" -v "$xval" "${@}"
1150 + #xattr -s "$xnam" "$xval" "${@}"
1154 + getfattr -d "${@}"
1158 +makepath "$fromdir/foo"
1159 +echo something >"$fromdir/file1"
1160 +echo else >"$fromdir/file2"
1161 +echo last >"$fromdir/foo/file3"
1163 +makepath "$chkdir/foo"
1164 +echo wow >"$chkdir/file1"
1165 +cp -p "$fromdir/foo/file3" "$chkdir/foo"
1167 +files='foo file1 file2 foo/file3'
1171 +xset user.short 'this is short' file1 2>/dev/null || test_skipped "Unable to set an xattr"
1172 +xset user.long 'this is a long attribute that will be truncated in the initial data send' file1
1173 +xset user.good 'this is good' file1
1174 +xset user.nice 'this is nice' file1
1176 +xset user.foo foo file2
1177 +xset user.bar bar file2
1178 +xset user.long 'a long attribute for our new file that tests to ensure that this works' file2
1180 +xset user.foo 'new foo' foo/file3
1181 +xset user.bar 'new bar' foo/file3
1182 +xset user.long 'this is also a long attribute that will be truncated in the initial data send' foo/file3
1183 +xset user.equal 'this long attribute should remain the same and not need to be transferred' foo/file3
1185 +xset user.short 'old short' "$chkdir/file1"
1186 +xset user.extra 'remove me' "$chkdir/file1"
1188 +xset user.foo 'old foo' "$chkdir/foo/file3"
1189 +xset user.equal 'this long attribute should remain the same and not need to be transferred' "$chkdir/foo/file3"
1191 +xls $files >"$scratchdir/xattrs.txt"
1193 +# OK, let's try a simple xattr copy.
1194 +checkit "$RSYNC -avX . \"$chkdir/\"" "$fromdir" "$chkdir"
1197 +xls $files | diff $diffopt "$scratchdir/xattrs.txt" -
1201 +checkit "$RSYNC -aiX --copy-dest=../chk . ../to" "$fromdir" "$todir"
1204 +xls $files | diff $diffopt "$scratchdir/xattrs.txt" -
1207 +xset user.nice 'this is nice, but different' file1
1209 +xls $files >"$scratchdir/xattrs.txt"
1213 +checkit "$RSYNC -aiX --link-dest=../chk . ../to" "$chkdir" "$todir"
1216 +xls $files | diff $diffopt "$scratchdir/xattrs.txt" -
1218 +# The script would have aborted on error, so getting here means we've won.
1224 + * Extended Attribute support for rsync.
1225 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
1227 + * Copyright (C) 2004 Red Hat, Inc.
1228 + * Copyright (C) 2006, 2007 Wayne Davison
1230 + * This program is free software; you can redistribute it and/or modify
1231 + * it under the terms of the GNU General Public License version 2 as
1232 + * published by the Free Software Foundation.
1234 + * This program is distributed in the hope that it will be useful,
1235 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1236 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1237 + * GNU General Public License for more details.
1239 + * You should have received a copy of the GNU General Public License along
1240 + * with this program; if not, write to the Free Software Foundation, Inc.,
1241 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1245 +#include "lib/sysxattrs.h"
1247 +#ifdef SUPPORT_XATTRS
1249 +extern int dry_run;
1250 +extern int am_root;
1251 +extern int am_sender;
1252 +extern int am_generator;
1253 +extern int read_only;
1254 +extern int list_only;
1255 +extern int checksum_seed;
1257 +#define RSYNC_XAL_INITIAL 5
1258 +#define RSYNC_XAL_LIST_INITIAL 100
1260 +#define MAX_FULL_DATUM 32
1262 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
1263 + && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
1265 +#define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
1267 +#define XSTATE_ABBREV 0
1268 +#define XSTATE_DONE 1
1269 +#define XSTATE_TODO 2
1270 +#define XSTATE_LOCAL 3
1272 +#define USER_PREFIX "user."
1273 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
1274 +#define SYSTEM_PREFIX "system."
1275 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
1277 +#ifdef HAVE_LINUX_XATTRS
1280 +#define RSYNC_PREFIX "rsync."
1281 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
1285 + char *datum, *name;
1286 + size_t datum_len, name_len;
1289 +static size_t namebuf_len = 0;
1290 +static char *namebuf = NULL;
1292 +static item_list empty_xattr = EMPTY_ITEM_LIST;
1293 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
1295 +/* ------------------------------------------------------------------------- */
1297 +static void rsync_xal_free(item_list *xalp)
1300 + rsync_xa *rxas = xalp->items;
1302 + for (i = 0; i < xalp->count; i++) {
1303 + free(rxas[i].datum);
1304 + /*free(rxas[i].name);*/
1309 +void free_xattr(statx *sxp)
1313 + rsync_xal_free(sxp->xattr);
1315 + sxp->xattr = NULL;
1318 +static int rsync_xal_compare_names(const void *x1, const void *x2)
1320 + const rsync_xa *xa1 = x1;
1321 + const rsync_xa *xa2 = x2;
1322 + return strcmp(xa1->name, xa2->name);
1325 +static ssize_t get_xattr_names(const char *fname)
1330 + namebuf_len = 1024;
1331 + namebuf = new_array(char, namebuf_len);
1333 + out_of_memory("get_xattr_names");
1336 + /* The length returned includes all the '\0' terminators. */
1337 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1338 + if (list_len > (ssize_t)namebuf_len) {
1342 + if (list_len >= 0)
1344 + if (errno == ENOTSUP)
1346 + if (errno == ERANGE) {
1347 + list_len = sys_llistxattr(fname, NULL, 0);
1348 + if (list_len < 0) {
1349 + rsyserr(FERROR, errno,
1350 + "get_xattr_names: llistxattr(\"%s\",0) failed",
1356 + namebuf_len = list_len + 1024;
1357 + namebuf = new_array(char, namebuf_len);
1359 + out_of_memory("get_xattr_names");
1360 + list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1361 + if (list_len >= 0)
1365 + rsyserr(FERROR, errno,
1366 + "get_xattr_names: llistxattr(\"%s\",%ld) failed",
1367 + fname, (long)namebuf_len);
1371 +/* On entry, the *len_ptr parameter contains the size of the extra space we
1372 + * should allocate when we create a buffer for the data. On exit, it contains
1373 + * the length of the datum. */
1374 +static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
1375 + int no_missing_error)
1377 + size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
1380 + if (datum_len == (size_t)-1) {
1381 + if (errno == ENOTSUP || no_missing_error)
1383 + rsyserr(FERROR, errno,
1384 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
1389 + if (datum_len + *len_ptr < datum_len /* checks for overflow */
1390 + || !(ptr = new_array(char, datum_len + *len_ptr)))
1391 + out_of_memory("get_xattr_data");
1393 + *len_ptr = datum_len;
1396 + size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
1397 + if (len != datum_len) {
1398 + if (len == (size_t)-1) {
1399 + rsyserr(FERROR, errno,
1400 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1401 + " failed", fname, name, (long)datum_len);
1404 + "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1405 + " returned %ld\n", fname, name,
1406 + (long)datum_len, (long)len);
1416 +static int rsync_xal_get(const char *fname, item_list *xalp)
1418 + ssize_t list_len, name_len;
1419 + size_t datum_len, name_offset;
1421 +#ifdef HAVE_LINUX_XATTRS
1422 + int user_only = am_sender ? 0 : !am_root;
1425 + /* This puts the name list into the "namebuf" buffer. */
1426 + if ((list_len = get_xattr_names(fname)) < 0)
1429 + for (name = namebuf; list_len > 0; name += name_len) {
1432 + name_len = strlen(name) + 1;
1433 + list_len -= name_len;
1435 +#ifdef HAVE_LINUX_XATTRS
1436 + /* We always ignore the system namespace, and non-root
1437 + * ignores everything but the user namespace. */
1438 + if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
1439 + : HAS_PREFIX(name, SYSTEM_PREFIX))
1443 + datum_len = name_len; /* Pass extra size to get_xattr_data() */
1444 + if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
1447 + if (datum_len > MAX_FULL_DATUM) {
1448 + /* For large datums, we store a flag and a checksum. */
1449 + name_offset = 1 + MAX_DIGEST_LEN;
1450 + sum_init(checksum_seed);
1451 + sum_update(ptr, datum_len);
1454 + if (!(ptr = new_array(char, name_offset + name_len)))
1455 + out_of_memory("rsync_xal_get");
1456 + *ptr = XSTATE_ABBREV;
1459 + name_offset = datum_len;
1461 + rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
1462 + rxas->name = ptr + name_offset;
1463 + memcpy(rxas->name, name, name_len);
1464 + rxas->datum = ptr;
1465 + rxas->name_len = name_len;
1466 + rxas->datum_len = datum_len;
1468 + if (xalp->count > 1)
1469 + qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
1473 +/* Read the xattr(s) for this filename. */
1474 +int get_xattr(const char *fname, statx *sxp)
1476 + sxp->xattr = new(item_list);
1477 + *sxp->xattr = empty_xattr;
1478 + if (rsync_xal_get(fname, sxp->xattr) < 0) {
1485 +static int find_matching_xattr(item_list *xalp)
1488 + item_list *lst = rsync_xal_l.items;
1490 + for (i = 0; i < rsync_xal_l.count; i++) {
1491 + rsync_xa *rxas1 = lst[i].items;
1492 + rsync_xa *rxas2 = xalp->items;
1494 + /* Wrong number of elements? */
1495 + if (lst[i].count != xalp->count)
1497 + /* any elements different? */
1498 + for (j = 0; j < xalp->count; j++) {
1499 + if (rxas1[j].name_len != rxas2[j].name_len
1500 + || rxas1[j].datum_len != rxas2[j].datum_len
1501 + || strcmp(rxas1[j].name, rxas2[j].name))
1503 + if (rxas1[j].datum_len > MAX_FULL_DATUM) {
1504 + if (memcmp(rxas1[j].datum + 1,
1505 + rxas2[j].datum + 1,
1506 + MAX_DIGEST_LEN) != 0)
1509 + if (memcmp(rxas1[j].datum, rxas2[j].datum,
1510 + rxas2[j].datum_len))
1514 + /* no differences found. This is The One! */
1515 + if (j == xalp->count)
1522 +/* Store *xalp on the end of rsync_xal_l */
1523 +static void rsync_xal_store(item_list *xalp)
1525 + item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
1526 + /* Since the following call starts a new list, we know it will hold the
1527 + * entire initial-count, not just enough space for one new item. */
1528 + *new_lst = empty_xattr;
1529 + (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
1530 + memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
1531 + new_lst->count = xalp->count;
1535 +/* Send the make_xattr()-generated xattr list for this flist entry. */
1536 +int send_xattr(statx *sxp, int f)
1538 + int ndx = find_matching_xattr(sxp->xattr);
1540 + /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
1541 + write_abbrevint(f, ndx + 1);
1545 + int count = sxp->xattr->count;
1546 + write_abbrevint(f, count);
1547 + for (rxa = sxp->xattr->items; count--; rxa++) {
1548 +#ifdef HAVE_LINUX_XATTRS
1549 + write_abbrevint(f, rxa->name_len);
1550 + write_abbrevint(f, rxa->datum_len);
1551 + write_buf(f, rxa->name, rxa->name_len);
1553 + /* We strip the rsync prefix from disguised namespaces
1554 + * and put everything else in the user namespace. */
1555 + if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
1556 + && rxa->name[RPRE_LEN] != '%') {
1557 + write_abbrevint(f, rxa->name_len - RPRE_LEN);
1558 + write_abbrevint(f, rxa->datum_len);
1559 + write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
1561 + write_abbrevint(f, rxa->name_len + UPRE_LEN);
1562 + write_abbrevint(f, rxa->datum_len);
1563 + write_buf(f, USER_PREFIX, UPRE_LEN);
1564 + write_buf(f, rxa->name, rxa->name_len);
1567 + if (rxa->datum_len > MAX_FULL_DATUM)
1568 + write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
1570 + write_buf(f, rxa->datum, rxa->datum_len);
1572 + ndx = rsync_xal_l.count; /* pre-incremented count */
1573 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1579 +/* Return a flag indicating if we need to change a file's xattrs. If
1580 + * "find_all" is specified, also mark any abbreviated xattrs that we
1581 + * need so that send_xattr_request() can tell the sender about them. */
1582 +int xattr_diff(struct file_struct *file, statx *sxp, int find_all)
1584 + item_list *lst = rsync_xal_l.items;
1585 + rsync_xa *snd_rxa, *rec_rxa;
1586 + int snd_cnt, rec_cnt;
1587 + int cmp, same, xattrs_equal = 1;
1589 + if (sxp && XATTR_READY(*sxp)) {
1590 + rec_rxa = sxp->xattr->items;
1591 + rec_cnt = sxp->xattr->count;
1597 + if (F_XATTR(file) >= 0)
1598 + lst += F_XATTR(file);
1600 + lst = &empty_xattr;
1602 + snd_rxa = lst->items;
1603 + snd_cnt = lst->count;
1605 + /* If the count of the sender's xattrs is different from our
1606 + * (receiver's) xattrs, the lists are not the same. */
1607 + if (snd_cnt != rec_cnt) {
1614 + cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
1617 + else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
1618 + same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1619 + && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
1620 + MAX_DIGEST_LEN) == 0;
1621 + /* Flag unrequested items that we need. */
1622 + if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
1623 + snd_rxa->datum[0] = XSTATE_TODO;
1625 + same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1626 + && memcmp(snd_rxa->datum, rec_rxa->datum,
1627 + snd_rxa->datum_len) == 0;
1648 + return !xattrs_equal;
1651 +/* When called by the generator with a NULL fname, this tells the sender
1652 + * which abbreviated xattr values we need. When called by the sender
1653 + * (with a non-NULL fname), we send all the extra xattr data it needs. */
1654 +void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
1656 + item_list *lst = rsync_xal_l.items;
1657 + int j, cnt, prior_req = -1;
1660 + lst += F_XATTR(file);
1662 + for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) {
1663 + if (rxa->datum_len <= MAX_FULL_DATUM)
1665 + switch (rxa->datum[0]) {
1666 + case XSTATE_LOCAL:
1667 + /* Items set locally will get cached by receiver. */
1668 + rxa->datum[0] = XSTATE_DONE;
1676 + /* Flag that we handled this abbreviated item. */
1677 + rxa->datum[0] = XSTATE_DONE;
1679 + write_abbrevint(f_out, j - prior_req);
1686 + /* Re-read the long datum. */
1687 + if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0)))
1690 + write_abbrevint(f_out, len); /* length might have changed! */
1691 + write_buf(f_out, ptr, len);
1696 + write_byte(f_out, 0); /* end the list */
1699 +/* Any items set locally by the generator that the receiver doesn't
1700 + * get told about get changed back to XSTATE_ABBREV. */
1701 +void xattr_clear_locals(struct file_struct *file)
1703 + item_list *lst = rsync_xal_l.items;
1707 + if (F_XATTR(file) < 0)
1710 + lst += F_XATTR(file);
1712 + for (rxa = lst->items; cnt--; rxa++) {
1713 + if (rxa->datum_len <= MAX_FULL_DATUM)
1715 + if (rxa->datum[0] == XSTATE_LOCAL)
1716 + rxa->datum[0] = XSTATE_ABBREV;
1720 +/* When called by the sender, read the request from the generator and mark
1721 + * any needed xattrs with a flag that lets us know they need to be sent to
1722 + * the receiver. When called by the receiver, reads the sent data and
1723 + * stores it in place of its checksum. */
1724 +void recv_xattr_request(struct file_struct *file, int f_in)
1726 + item_list *lst = rsync_xal_l.items;
1727 + char *old_datum, *name;
1731 + if (F_XATTR(file) < 0)
1732 + exit_cleanup(RERR_STREAMIO); /* XXX */
1733 + lst += F_XATTR(file);
1738 + while ((rel_pos = read_abbrevint(f_in)) != 0) {
1741 + if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM
1742 + || rxa->datum[0] != XSTATE_ABBREV)
1743 + exit_cleanup(RERR_STREAMIO); /* XXX */
1746 + rxa->datum[0] = XSTATE_TODO;
1750 + old_datum = rxa->datum;
1751 + rxa->datum_len = read_abbrevint(f_in);
1753 + if (rxa->name_len + rxa->datum_len < rxa->name_len)
1754 + out_of_memory("recv_xattr_request"); /* overflow */
1755 + rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
1757 + out_of_memory("recv_xattr_request");
1758 + name = rxa->datum + rxa->datum_len;
1759 + memcpy(name, rxa->name, rxa->name_len);
1762 + read_buf(f_in, rxa->datum, rxa->datum_len);
1766 +/* ------------------------------------------------------------------------- */
1768 +/* receive and build the rsync_xattr_lists */
1769 +void receive_xattr(struct file_struct *file, int f)
1771 + static item_list temp_xattr = EMPTY_ITEM_LIST;
1773 + int ndx = read_abbrevint(f);
1775 + if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
1776 + rprintf(FERROR, "receive_xattr: xa index %d out of"
1777 + " range for %s\n", ndx, f_name(file, NULL));
1778 + exit_cleanup(RERR_STREAMIO);
1782 + F_XATTR(file) = ndx - 1;
1786 + if ((count = read_abbrevint(f)) != 0) {
1787 + (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
1788 + temp_xattr.count = 0;
1794 + size_t name_len = read_abbrevint(f);
1795 + size_t datum_len = read_abbrevint(f);
1796 + size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
1797 +#ifdef HAVE_LINUX_XATTRS
1798 + size_t extra_len = 0;
1800 + size_t extra_len = am_root ? RPRE_LEN : 0;
1801 + if (dget_len + extra_len < dget_len)
1802 + out_of_memory("receive_xattr"); /* overflow */
1804 + if (dget_len + extra_len + name_len < dget_len)
1805 + out_of_memory("receive_xattr"); /* overflow */
1806 + ptr = new_array(char, dget_len + extra_len + name_len);
1808 + out_of_memory("receive_xattr");
1809 + name = ptr + dget_len + extra_len;
1810 + read_buf(f, name, name_len);
1811 + if (dget_len == datum_len)
1812 + read_buf(f, ptr, dget_len);
1814 + *ptr = XSTATE_ABBREV;
1815 + read_buf(f, ptr + 1, MAX_DIGEST_LEN);
1817 +#ifdef HAVE_LINUX_XATTRS
1818 + /* Non-root can only save the user namespace. */
1819 + if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
1824 + /* This OS only has a user namespace, so we either
1825 + * strip the user prefix, or we put a non-user
1826 + * namespace inside our rsync hierarchy. */
1827 + if (HAS_PREFIX(name, USER_PREFIX)) {
1829 + name_len -= UPRE_LEN;
1830 + } else if (am_root) {
1832 + name_len += RPRE_LEN;
1833 + memcpy(name, RSYNC_PREFIX, RPRE_LEN);
1839 + rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
1842 + rxa->name_len = name_len;
1843 + rxa->datum_len = datum_len;
1846 + ndx = rsync_xal_l.count; /* pre-incremented count */
1847 + rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
1849 + F_XATTR(file) = ndx;
1852 +/* Turn the xattr data in statx into cached xattr data, setting the index
1853 + * values in the file struct. */
1854 +void cache_xattr(struct file_struct *file, statx *sxp)
1861 + ndx = find_matching_xattr(sxp->xattr);
1863 + rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1865 + F_XATTR(file) = ndx;
1868 +static int rsync_xal_set(const char *fname, item_list *xalp,
1869 + const char *fnamecmp, statx *sxp)
1871 + rsync_xa *rxas = xalp->items;
1874 + char *name, *ptr, sum[MAX_DIGEST_LEN];
1875 + int name_len, ret = 0;
1877 + /* This puts the current name list into the "namebuf" buffer. */
1878 + if ((list_len = get_xattr_names(fname)) < 0)
1881 + for (i = 0; i < xalp->count; i++) {
1882 + name = rxas[i].name;
1884 + if (XATTR_ABBREV(rxas[i])) {
1885 + /* See if the fnamecmp version is identical. */
1886 + len = name_len = rxas[i].name_len;
1887 + if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
1891 + rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
1892 + rxas[i].name, full_fname(fname));
1896 + if (len != rxas[i].datum_len) {
1898 + goto still_abbrev;
1901 + sum_init(checksum_seed);
1902 + sum_update(ptr, len);
1904 + if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
1906 + goto still_abbrev;
1909 + if (fname == fnamecmp)
1910 + ; /* Value is already set when identical */
1911 + else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
1912 + rsyserr(FERROR, errno,
1913 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1916 + } else /* make sure caller sets mtime */
1917 + sxp->st.st_mtime = (time_t)-1;
1919 + if (am_generator) { /* generator items stay abbreviated */
1920 + if (rxas[i].datum[0] == XSTATE_ABBREV)
1921 + rxas[i].datum[0] = XSTATE_LOCAL;
1926 + memcpy(ptr + len, name, name_len);
1927 + free(rxas[i].datum);
1929 + rxas[i].name = name = ptr + len;
1930 + rxas[i].datum = ptr;
1934 + if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
1935 + rsyserr(FERROR, errno,
1936 + "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1939 + } else /* make sure caller sets mtime */
1940 + sxp->st.st_mtime = (time_t)-1;
1943 + /* Remove any extraneous names. */
1944 + for (name = namebuf; list_len > 0; name += name_len) {
1945 + name_len = strlen(name) + 1;
1946 + list_len -= name_len;
1948 +#ifdef HAVE_LINUX_XATTRS
1949 + /* We always ignore the system namespace, and non-root
1950 + * ignores everything but the user namespace. */
1951 + if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
1952 + : !HAS_PREFIX(name, USER_PREFIX))
1956 + for (i = 0; i < xalp->count; i++) {
1957 + if (strcmp(name, rxas[i].name) == 0)
1960 + if (i == xalp->count) {
1961 + if (sys_lremovexattr(fname, name) < 0) {
1962 + rsyserr(FERROR, errno,
1963 + "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
1966 + } else /* make sure caller sets mtime */
1967 + sxp->st.st_mtime = (time_t)-1;
1974 +/* Set extended attributes on indicated filename. */
1975 +int set_xattr(const char *fname, const struct file_struct *file,
1976 + const char *fnamecmp, statx *sxp)
1979 + item_list *lst = rsync_xal_l.items;
1982 + return 1; /* FIXME: --dry-run needs to compute this value */
1984 + if (read_only || list_only) {
1989 + ndx = F_XATTR(file);
1990 + return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
1993 +#endif /* SUPPORT_XATTRS */