More xattr improvments/fixes, including tests for proper functioning
[rsync/rsync-patches.git] / xattrs.diff
CommitLineData
64e98de1 1This patch adds support for extended attributes.
bbdff76a 2
03019e41 3To use this patch, run these commands for a successful build:
bbdff76a 4
03019e41 5 patch -p1 <patches/xattrs.diff
27e96866 6 ./prepare-source
64e98de1 7 ./configure
bbdff76a
WD
8 make
9
64e98de1 10CAUTION: this patch has been recently reworked, and needs more testing!
03019e41 11
9a7eef96
WD
12--- old/Makefile.in
13+++ new/Makefile.in
1ed0b5c9 14@@ -28,13 +28,13 @@ VERSION=@VERSION@
bbdff76a
WD
15
16 HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
87a38eea 17 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o lib/md5.o \
bbdff76a 18- lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
64e98de1 19+ lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattrs.o @LIBOBJS@
4bf6f8c7
WD
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
590329e5
WD
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 \
610969d1 25- fileio.o batch.o clientname.o chmod.o acls.o
64e98de1 26+ fileio.o batch.o clientname.o chmod.o acls.o xattrs.o
bbdff76a
WD
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 \
9a7eef96
WD
30--- old/backup.c
31+++ new/backup.c
526184b9 32@@ -23,6 +23,7 @@
70891d26 33 extern int verbose;
93d6ca6d
WD
34 extern int am_root;
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;
526184b9 40@@ -134,6 +135,9 @@ static int make_bak_dir(char *fullpath)
0f6fb3c8
WD
41 #ifdef SUPPORT_ACLS
42 sx.acc_acl = sx.def_acl = NULL;
43 #endif
44+#ifdef SUPPORT_XATTRS
45+ sx.xattr = NULL;
46+#endif
47 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
48 continue;
49 #ifdef SUPPORT_ACLS
3e6568cf 50@@ -143,7 +147,14 @@ static int make_bak_dir(char *fullpath)
4306c620 51 free_acl(&sx);
0f6fb3c8 52 }
09cc6650 53 #endif
3e6568cf 54- set_file_attrs(fullpath, file, NULL, 0);
09cc6650 55+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
56+ if (preserve_xattrs) {
57+ get_xattr(rel, &sx);
58+ cache_xattr(file, &sx);
64e98de1 59+ free_xattr(&sx);
0f6fb3c8 60+ }
09cc6650 61+#endif
3e6568cf 62+ set_file_attrs(fullpath, file, NULL, NULL, 0);
0f6fb3c8 63 free(file);
bbdff76a 64 }
3e6568cf 65 }
64e98de1 66@@ -194,6 +205,9 @@ static int keep_backup(const char *fname
0f6fb3c8
WD
67 #ifdef SUPPORT_ACLS
68 sx.acc_acl = sx.def_acl = NULL;
09cc6650
WD
69 #endif
70+#ifdef SUPPORT_XATTRS
0f6fb3c8 71+ sx.xattr = NULL;
09cc6650 72+#endif
bbdff76a 73
0f6fb3c8
WD
74 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
75 return 1; /* the file could have disappeared */
64e98de1 76@@ -210,6 +224,13 @@ static int keep_backup(const char *fname
4306c620 77 free_acl(&sx);
0f6fb3c8 78 }
09cc6650
WD
79 #endif
80+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
81+ if (preserve_xattrs) {
82+ get_xattr(fname, &sx);
83+ cache_xattr(file, &sx);
64e98de1 84+ free_xattr(&sx);
0f6fb3c8 85+ }
09cc6650 86+#endif
bbdff76a 87
0f6fb3c8
WD
88 /* Check to see if this is a device file, or link */
89 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
3e6568cf
WD
90@@ -288,7 +309,7 @@ static int keep_backup(const char *fname
91 robust_unlink(fname); /* Just in case... */
92 }
93 }
94- set_file_attrs(buf, file, NULL, 0);
95+ set_file_attrs(buf, file, NULL, fname, 0);
96 unmake_file(file);
97
98 if (verbose > 1) {
99--- old/cleanup.c
100+++ new/cleanup.c
101@@ -142,7 +142,7 @@ NORETURN void _exit_cleanup(int code, co
102 flush_write_file(cleanup_fd_w);
103 close(cleanup_fd_w);
104 }
105- finish_transfer(cleanup_new_fname, fname, NULL,
106+ finish_transfer(cleanup_new_fname, fname, NULL, NULL,
107 cleanup_file, 0, !partial_dir);
108 }
109
fdf967c7
WD
110--- old/compat.c
111+++ new/compat.c
64e98de1
WD
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)
7e27b6c0 121 preserve_gid = ++file_extra_cnt;
fdf967c7 122 if (preserve_acls && !am_sender)
7e27b6c0 123 preserve_acls = ++file_extra_cnt;
64e98de1 124+ if (preserve_xattrs)
7e27b6c0 125+ preserve_xattrs = ++file_extra_cnt;
fdf967c7
WD
126
127 if (remote_protocol == 0) {
128 if (!read_batch)
64e98de1
WD
129@@ -119,6 +122,13 @@ void setup_protocol(int f_out,int f_in)
130 protocol_version);
131 exit_cleanup(RERR_PROTOCOL);
132 }
133+ if (preserve_xattrs) {
134+ rprintf(FERROR,
135+ "--xattrs requires protocol 30 or higher"
136+ " (negotiated %d).\n",
137+ protocol_version);
138+ exit_cleanup(RERR_PROTOCOL);
139+ }
140 }
141
142 if (delete_mode && !(delete_before+delete_during+delete_after)) {
9a7eef96
WD
143--- old/configure.in
144+++ new/configure.in
64e98de1 145@@ -891,6 +891,46 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
4306c620
WD
146 esac
147 fi
bbdff76a
WD
148
149+AC_CHECK_HEADERS(attr/xattr.h)
49de5440 150+AC_CHECK_HEADERS(sys/xattr.h)
742a4103 151+AC_CHECK_HEADERS(sys/extattr.h)
64e98de1
WD
152+
153+#################################################
154+# check for extended attribute support
bbdff76a 155+AC_MSG_CHECKING(whether to support extended attributes)
f787c90c 156+AC_ARG_ENABLE(xattr-support,
64e98de1
WD
157+ AC_HELP_STRING([--disable-xattr-support],
158+ [Turn off extended attribute support]))
159+
160+if test x"$enable_xattr_support" = x"no"; then
161+ AC_MSG_RESULT(no)
162+else
163+ case "$host_os" in
164+ *linux*)
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])
168+ ;;
169+ darwin*)
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)
173+ ;;
174+ freebsd*)
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)
178+ ;;
179+ *)
180+ if test x"$enable_xattr_support" = x"yes"; then
181+ AC_MSG_ERROR(Failed to find extended attribute support)
182+ else
183+ AC_MSG_RESULT(No extended attribute support found)
184+ fi
185+ ;;
186+ esac
187+fi
bbdff76a
WD
188+
189 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
190 AC_OUTPUT
191
9a7eef96
WD
192--- old/flist.c
193+++ new/flist.c
87a38eea 194@@ -43,6 +43,7 @@ extern int one_file_system;
93d6ca6d
WD
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;
87a38eea 202@@ -888,6 +889,10 @@ static struct file_struct *recv_file_ent
4306c620 203 if (preserve_acls && !S_ISLNK(mode))
0f6fb3c8
WD
204 receive_acl(file, f);
205 #endif
206+#ifdef SUPPORT_XATTRS
207+ if (preserve_xattrs)
208+ receive_xattr(file, f );
209+#endif
210
524e5ed5
WD
211 if (S_ISREG(mode) || S_ISLNK(mode))
212 stats.total_size += file_length;
87a38eea 213@@ -1160,7 +1165,7 @@ static struct file_struct *send_file_nam
fc068916 214 int flags, int filter_flags)
a071aea2
WD
215 {
216 struct file_struct *file;
217-#ifdef SUPPORT_ACLS
218+#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
219 statx sx;
220 #endif
221
87a38eea 222@@ -1179,6 +1184,13 @@ static struct file_struct *send_file_nam
0f6fb3c8
WD
223 return NULL;
224 }
09cc6650
WD
225 #endif
226+#ifdef SUPPORT_XATTRS
e70fd6a8 227+ if (preserve_xattrs && f >= 0) {
0f6fb3c8
WD
228+ sx.xattr = NULL;
229+ if (get_xattr(fname, &sx) < 0)
230+ return NULL;
231+ }
09cc6650 232+#endif
bbdff76a 233
a2f9a486
WD
234 maybe_emit_filelist_progress(flist->count + flist_count_offset);
235
64e98de1 236@@ -1192,6 +1204,12 @@ static struct file_struct *send_file_nam
4306c620
WD
237 free_acl(&sx);
238 }
09cc6650
WD
239 #endif
240+#ifdef SUPPORT_XATTRS
64e98de1
WD
241+ if (preserve_xattrs) {
242+ F_XATTR(file) = send_xattr(&sx, f);
243+ free_xattr(&sx);
244+ }
09cc6650 245+#endif
7c15d5aa 246 }
bbdff76a
WD
247 return file;
248 }
64e98de1
WD
249--- old/generator.c
250+++ new/generator.c
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;
3fbc83e8 259@@ -532,11 +533,19 @@ int unchanged_attrs(const char *fname, s
64e98de1
WD
260 return 0;
261 }
262 #endif
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))
268+ return 0;
269+ }
270+#endif
271
272 return 1;
273 }
3fbc83e8
WD
274
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,
278 const char *xname)
279 {
280@@ -562,16 +571,24 @@ void itemize(const char *fname, struct f
281 #ifdef SUPPORT_ACLS
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)
64e98de1
WD
287 iflags |= ITEM_REPORT_ACL;
288 }
289 #endif
290+#ifdef SUPPORT_XATTRS
291+ if (preserve_xattrs) {
292+ if (!XATTR_READY(*sxp))
3fbc83e8 293+ get_xattr(fnamecmp, sxp);
64e98de1
WD
294+ if (xattr_diff(file, sxp, 1))
295+ iflags |= ITEM_REPORT_XATTR;
296+ }
297+#endif
298 } else
299 iflags |= ITEM_IS_NEW;
300
301 iflags &= 0xffff;
302- if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
303+ if ((iflags & (SIGNIFICANT_ITEM_FLAGS|ITEM_REPORT_XATTR) || verbose > 1
304 || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
305 if (protocol_version >= 29) {
306 if (ndx >= 0)
307@@ -581,6 +598,10 @@ void itemize(const char *fname, struct f
308 write_byte(sock_f_out, fnamecmp_type);
309 if (iflags & ITEM_XNAME_FOLLOWS)
310 write_vstring(sock_f_out, xname, strlen(xname));
311+#ifdef SUPPORT_XATTRS
312+ if (iflags & ITEM_REPORT_XATTR && !dry_run)
313+ send_xattr_request(NULL, file, sock_f_out);
314+#endif
315 } else if (ndx >= 0) {
316 enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
317 log_item(code, file, &stats, iflags, xname);
3fbc83e8
WD
318@@ -855,14 +876,14 @@ static int try_dests_reg(struct file_str
319 if (preserve_hard_links && F_IS_HLINKED(file))
320 finish_hard_link(file, fname, &sxp->st, itemizing, code, j);
321 if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
322- itemize(fname, file, ndx, 1, sxp,
323+ itemize(cmpbuf, file, ndx, 1, sxp,
324 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
325 0, "");
326 }
327 } else
328 #endif
329 if (itemizing)
330- itemize(fname, file, ndx, 0, sxp, 0, 0, NULL);
331+ itemize(cmpbuf, file, ndx, 0, sxp, 0, 0, NULL);
332 if (verbose > 1 && maybe_ATTRS_REPORT)
333 rprintf(FCLIENT, "%s is uptodate\n", fname);
334 return -2;
335@@ -879,9 +900,13 @@ static int try_dests_reg(struct file_str
336 }
337 return -1;
3e6568cf 338 }
3fbc83e8 339+ set_file_attrs(fname, file, NULL, cmpbuf, 0);
3e6568cf 340 if (itemizing)
3fbc83e8 341- itemize(fname, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
3e6568cf 342- set_file_attrs(fname, file, NULL, 0);
3fbc83e8
WD
343+ itemize(cmpbuf, file, ndx, 0, sxp, ITEM_LOCAL_CHANGE, 0, NULL);
344+#ifdef SUPPORT_XATTRS
345+ if (preserve_xattrs)
346+ xattr_clear_locals(file);
347+#endif
3e6568cf
WD
348 if (maybe_ATTRS_REPORT
349 && ((!itemizing && verbose && match_level == 2)
350 || (verbose > 1 && match_level == 3))) {
3fbc83e8
WD
351@@ -1029,7 +1054,7 @@ static int try_dests_non(struct file_str
352 : ITEM_LOCAL_CHANGE
353 + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
354 char *lp = match_level == 3 ? "" : NULL;
355- itemize(fname, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
356+ itemize(cmpbuf, file, ndx, 0, sxp, chg + ITEM_MATCHED, 0, lp);
357 }
358 if (verbose > 1 && maybe_ATTRS_REPORT) {
359 rprintf(FCLIENT, "%s%s is uptodate\n",
360@@ -1112,6 +1137,9 @@ static void recv_generator(char *fname,
64e98de1
WD
361 #ifdef SUPPORT_ACLS
362 sx.acc_acl = sx.def_acl = NULL;
363 #endif
364+#ifdef SUPPORT_XATTRS
365+ sx.xattr = NULL;
366+#endif
367 if (dry_run > 1) {
368 if (fuzzy_dirlist) {
369 flist_free(fuzzy_dirlist);
3fbc83e8 370@@ -1224,7 +1252,7 @@ static void recv_generator(char *fname,
3e6568cf
WD
371 goto cleanup;
372 }
373 }
374- if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, 0)
375+ if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
376 && verbose && code != FNONE && f_out != -1)
377 rprintf(code, "%s/\n", fname);
378 if (real_ret != 0 && one_file_system)
3fbc83e8
WD
379@@ -1278,9 +1306,9 @@ static void recv_generator(char *fname,
380 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
381 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
3e6568cf 382 /* The link is pointing to the right place. */
3fbc83e8 383+ set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
3e6568cf
WD
384 if (itemizing)
385 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
386- set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
3e6568cf
WD
387 #ifdef SUPPORT_HARD_LINKS
388 if (preserve_hard_links && F_IS_HLINKED(file))
389 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
3fbc83e8 390@@ -1317,7 +1345,7 @@ static void recv_generator(char *fname,
3e6568cf
WD
391 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
392 full_fname(fname), sl);
393 } else {
394- set_file_attrs(fname, file, NULL, 0);
395+ set_file_attrs(fname, file, NULL, NULL, 0);
396 if (itemizing) {
397 itemize(fname, file, ndx, statret, &sx,
398 ITEM_LOCAL_CHANGE, 0, NULL);
3fbc83e8
WD
399@@ -1357,9 +1385,9 @@ static void recv_generator(char *fname,
400 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
401 && sx.st.st_rdev == rdev) {
3e6568cf 402 /* The device or special file is identical. */
3fbc83e8 403+ set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
3e6568cf
WD
404 if (itemizing)
405 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
406- set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
3e6568cf
WD
407 #ifdef SUPPORT_HARD_LINKS
408 if (preserve_hard_links && F_IS_HLINKED(file))
409 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
3fbc83e8 410@@ -1399,7 +1427,7 @@ static void recv_generator(char *fname,
3e6568cf
WD
411 rsyserr(FERROR, errno, "mknod %s failed",
412 full_fname(fname));
413 } else {
414- set_file_attrs(fname, file, NULL, 0);
415+ set_file_attrs(fname, file, NULL, NULL, 0);
416 if (itemizing) {
417 itemize(fname, file, ndx, statret, &sx,
418 ITEM_LOCAL_CHANGE, 0, NULL);
3fbc83e8
WD
419@@ -1529,9 +1557,9 @@ static void recv_generator(char *fname,
420 do_unlink(partialptr);
421 handle_partial_dir(partialptr, PDIR_DELETE);
3e6568cf 422 }
3fbc83e8 423+ set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
3e6568cf
WD
424 if (itemizing)
425 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
426- set_file_attrs(fname, file, &sx, maybe_ATTRS_REPORT);
3e6568cf
WD
427 #ifdef SUPPORT_HARD_LINKS
428 if (preserve_hard_links && F_IS_HLINKED(file))
429 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
3fbc83e8 430@@ -1636,6 +1664,10 @@ static void recv_generator(char *fname,
64e98de1
WD
431 if (preserve_acls)
432 free_acl(&real_sx);
433 #endif
434+#ifdef SUPPORT_XATTRS
435+ if (preserve_xattrs)
436+ free_xattr(&real_sx);
437+#endif
438 }
439
440 if (!do_xfers) {
3fbc83e8 441@@ -1657,7 +1689,7 @@ static void recv_generator(char *fname,
3e6568cf
WD
442
443 if (f_copy >= 0) {
444 close(f_copy);
445- set_file_attrs(backupptr, back_file, NULL, 0);
446+ set_file_attrs(backupptr, back_file, NULL, NULL, 0);
447 if (verbose > 1) {
448 rprintf(FINFO, "backed up %s to %s\n",
449 fname, backupptr);
3fbc83e8 450@@ -1672,6 +1704,10 @@ static void recv_generator(char *fname,
64e98de1
WD
451 if (preserve_acls)
452 free_acl(&sx);
453 #endif
454+#ifdef SUPPORT_XATTRS
455+ if (preserve_xattrs)
456+ free_xattr(&sx);
457+#endif
458 return;
459 }
460
461--- old/lib/sysxattrs.c
462+++ new/lib/sysxattrs.c
bdeab877 463@@ -0,0 +1,135 @@
dc52bc1a
WD
464+/*
465+ * Extended attribute support for rsync.
466+ *
467+ * Copyright (C) 2004 Red Hat, Inc.
468+ * Written by Jay Fenlason.
469+ *
470+ * This program is free software; you can redistribute it and/or modify
471+ * it under the terms of the GNU General Public License as published by
472+ * the Free Software Foundation; either version 2 of the License, or
473+ * (at your option) any later version.
474+ *
475+ * This program is distributed in the hope that it will be useful,
476+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
477+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
478+ * GNU General Public License for more details.
479+ *
480+ * You should have received a copy of the GNU General Public License along
481+ * with this program; if not, write to the Free Software Foundation, Inc.,
482+ * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
483+ */
bbdff76a
WD
484+
485+#include "rsync.h"
64e98de1 486+#include "sysxattrs.h"
bbdff76a 487+
56473cb9
WD
488+#ifdef SUPPORT_XATTRS
489+
09cc6650 490+#if defined HAVE_LINUX_XATTRS
bbdff76a
WD
491+
492+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
493+{
494+ return lgetxattr(path, name, value, size);
495+}
496+
8627d8a2
WD
497+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
498+{
0bac098f 499+ return fgetxattr(filedes, name, value, size);
8627d8a2
WD
500+}
501+
742a4103 502+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
bbdff76a 503+{
742a4103 504+ return lsetxattr(path, name, value, size, 0);
bbdff76a
WD
505+}
506+
a74926e4
WD
507+int sys_lremovexattr(const char *path, const char *name)
508+{
509+ return lremovexattr(path, name);
510+}
511+
bbdff76a
WD
512+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
513+{
514+ return llistxattr(path, list, size);
515+}
516+
49de5440
WD
517+#elif HAVE_OSX_XATTRS
518+
519+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
520+{
521+ return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
522+}
523+
8627d8a2
WD
524+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
525+{
0bac098f 526+ return fgetxattr(filedes, name, value, size, 0, 0);
8627d8a2
WD
527+}
528+
742a4103 529+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
49de5440 530+{
742a4103 531+ return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
49de5440
WD
532+}
533+
a74926e4
WD
534+int sys_lremovexattr(const char *path, const char *name)
535+{
536+ return removexattr(path, name, XATTR_NOFOLLOW);
537+}
538+
49de5440
WD
539+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
540+{
541+ return listxattr(path, list, size, XATTR_NOFOLLOW);
542+}
543+
742a4103
WD
544+#elif HAVE_FREEBSD_XATTRS
545+
546+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
547+{
548+ return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
549+}
550+
551+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
552+{
553+ return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
554+}
555+
556+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
557+{
558+ return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
559+}
560+
561+int sys_lremovexattr(const char *path, const char *name)
562+{
563+ return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
564+}
565+
566+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
567+{
568+ unsigned char keylen;
569+ ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
570+
571+ if (len <= 0 || (size_t)len > size)
572+ return len;
573+
574+ /* FreeBSD puts a single-byte length before each string, with no '\0'
575+ * terminator. We need to change this into a series of null-terminted
576+ * strings. Since the size is the same, we can simply transform the
577+ * output in place. */
bdeab877 578+ for (off = 0; off < len; off += keylen + 1) {
742a4103
WD
579+ keylen = ((unsigned char*)list)[off];
580+ if (off + keylen >= len) {
581+ /* Should be impossible, but kernel bugs happen! */
582+ errno = EINVAL;
583+ return -1;
584+ }
585+ memmove(list+off, list+off+1, keylen);
bdeab877 586+ list[off+keylen] = '\0';
742a4103
WD
587+ }
588+
589+ return len;
590+}
591+
bbdff76a
WD
592+#else
593+
56473cb9
WD
594+#error You need to create xattr compatibility functions.
595+
596+#endif
597+
598+#endif /* SUPPORT_XATTRS */
64e98de1
WD
599--- old/lib/sysxattrs.h
600+++ new/lib/sysxattrs.h
742a4103 601@@ -0,0 +1,26 @@
49de5440 602+#ifdef SUPPORT_XATTRS
8889f85e 603+
49de5440 604+#if defined HAVE_ATTR_XATTR_H
0f6fb3c8 605+#include <attr/xattr.h>
49de5440
WD
606+#elif defined HAVE_SYS_XATTR_H
607+#include <sys/xattr.h>
742a4103
WD
608+#elif defined HAVE_SYS_EXTATTR_H
609+#include <sys/extattr.h>
49de5440 610+#endif
bbdff76a 611+
a74926e4
WD
612+/* Linux 2.4 does not define this as a distinct errno value: */
613+#ifndef ENOATTR
614+#define ENOATTR ENODATA
615+#endif
616+
bbdff76a 617+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
8627d8a2 618+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
742a4103 619+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
a74926e4 620+int sys_lremovexattr(const char *path, const char *name);
bbdff76a
WD
621+ssize_t sys_llistxattr(const char *path, char *list, size_t size);
622+
623+#else
624+
8889f85e 625+/* No xattrs available */
49de5440
WD
626+
627+#endif
64e98de1
WD
628--- old/main.c
629+++ new/main.c
630@@ -608,7 +608,7 @@ static void fix_basis_dirs(void)
631 }
632
633 /* This is only called by the sender. */
634-static void read_final_goodbye(int f_in, int f_out)
635+static void read_final_goodbye(int f_in)
636 {
637 int i, iflags, xlen;
638 uchar fnamecmp_type;
639@@ -617,8 +617,8 @@ static void read_final_goodbye(int f_in,
640 if (protocol_version < 29)
641 i = read_int(f_in);
642 else {
643- i = read_ndx_and_attrs(f_in, f_out, &iflags,
644- &fnamecmp_type, xname, &xlen);
645+ i = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
646+ xname, &xlen);
647 }
648
649 if (i != NDX_DONE) {
650@@ -677,7 +677,7 @@ static void do_server_sender(int f_in, i
651 io_flush(FULL_FLUSH);
652 handle_stats(f_out);
653 if (protocol_version >= 24)
654- read_final_goodbye(f_in, f_out);
655+ read_final_goodbye(f_in);
656 io_flush(FULL_FLUSH);
657 exit_cleanup(0);
658 }
659@@ -740,7 +740,7 @@ static int do_recv(int f_in, int f_out,
660 kluge_around_eof = -1;
661
662 /* This should only get stopped via a USR2 signal. */
663- read_ndx_and_attrs(f_in, -1, &iflags, &fnamecmp_type,
664+ read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
665 xname, &xlen);
666
667 rprintf(FERROR, "Invalid packet at end of run [%s]\n",
668@@ -977,7 +977,7 @@ int client_run(int f_in, int f_out, pid_
669 io_flush(FULL_FLUSH);
670 handle_stats(-1);
671 if (protocol_version >= 24)
672- read_final_goodbye(f_in, f_out);
673+ read_final_goodbye(f_in);
674 if (pid != -1) {
675 if (verbose > 3)
676 rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
9a7eef96
WD
677--- old/options.c
678+++ new/options.c
526184b9 679@@ -47,6 +47,7 @@ int copy_links = 0;
bbdff76a
WD
680 int preserve_links = 0;
681 int preserve_hard_links = 0;
682 int preserve_acls = 0;
683+int preserve_xattrs = 0;
684 int preserve_perms = 0;
4a65fe72 685 int preserve_executability = 0;
bbdff76a 686 int preserve_devices = 0;
87a38eea 687@@ -201,6 +202,7 @@ static void print_rsync_version(enum log
bbdff76a
WD
688 char const *have_inplace = "no ";
689 char const *hardlinks = "no ";
690 char const *acls = "no ";
691+ char const *xattrs = "no ";
692 char const *links = "no ";
693 char const *ipv6 = "no ";
694 STRUCT_STAT *dumstat;
87a38eea 695@@ -220,7 +222,9 @@ static void print_rsync_version(enum log
bbdff76a
WD
696 #ifdef SUPPORT_ACLS
697 acls = "";
698 #endif
699-
700+#ifdef SUPPORT_XATTRS
701+ xattrs = "";
702+#endif
703 #ifdef SUPPORT_LINKS
704 links = "";
705 #endif
87a38eea 706@@ -239,8 +243,8 @@ static void print_rsync_version(enum log
5e3c6c93
WD
707 (int)(sizeof (int64) * 8));
708 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
709 got_socketpair, hardlinks, links, ipv6, have_inplace);
710- rprintf(f, " %sappend, %sACLs\n",
711- have_inplace, acls);
712+ rprintf(f, " %sappend, %sACLs, %sxattrs\n",
713+ have_inplace, acls, xattrs);
56473cb9 714
5e3c6c93
WD
715 #ifdef MAINTAINER_MODE
716 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
87a38eea 717@@ -286,7 +290,7 @@ void usage(enum logcode F)
1b57ecb0 718 rprintf(F," -q, --quiet suppress non-error messages\n");
4959107f 719 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
1b57ecb0
WD
720 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
721- rprintf(F," -a, --archive archive mode; same as -rlptgoD (no -H, -A)\n");
722+ rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
723 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
724 rprintf(F," -r, --recursive recurse into directories\n");
725 rprintf(F," -R, --relative use relative path names\n");
87a38eea 726@@ -311,6 +315,9 @@ void usage(enum logcode F)
3610c43c 727 #ifdef SUPPORT_ACLS
bbdff76a 728 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
3610c43c
WD
729 #endif
730+#ifdef SUPPORT_XATTRS
bbdff76a 731+ rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
3610c43c 732+#endif
4a65fe72 733 rprintf(F," -o, --owner preserve owner (super-user only)\n");
bbdff76a 734 rprintf(F," -g, --group preserve group\n");
1ed0b5c9 735 rprintf(F," --devices preserve device files (super-user only)\n");
87a38eea 736@@ -438,6 +445,9 @@ static struct poptOption long_options[]
489b0a72
WD
737 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
738 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
739 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
740+ {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
741+ {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
742+ {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
743 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
744 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
745 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
87a38eea 746@@ -1126,6 +1136,17 @@ int parse_arguments(int *argc, const cha
489b0a72 747 return 0;
3610c43c 748 #endif
bbdff76a
WD
749
750+ case 'X':
751+#ifdef SUPPORT_XATTRS
752+ preserve_xattrs = 1;
753+ preserve_perms = 1;
489b0a72 754+ break;
bbdff76a
WD
755+#else
756+ snprintf(err_buf,sizeof(err_buf),
757+ "extended attributes are not supported on this %s\n",
758+ am_server ? "server" : "client");
759+ return 0;
d2dc8a18 760+#endif
bbdff76a
WD
761
762 default:
763 /* A large opt value means that set_refuse_options()
87a38eea 764@@ -1590,6 +1611,10 @@ void server_options(char **args,int *arg
bbdff76a
WD
765 if (preserve_acls)
766 argstr[x++] = 'A';
3610c43c
WD
767 #endif
768+#ifdef SUPPORT_XATTRS
bbdff76a
WD
769+ if (preserve_xattrs)
770+ argstr[x++] = 'X';
3610c43c 771+#endif
fc068916
WD
772 if (recurse)
773 argstr[x++] = 'r';
774 if (always_checksum)
64e98de1
WD
775--- old/receiver.c
776+++ new/receiver.c
777@@ -22,6 +22,7 @@
778 #include "rsync.h"
779
780 extern int verbose;
781+extern int dry_run;
782 extern int do_xfers;
783 extern int am_server;
784 extern int do_progress;
3fbc83e8
WD
785@@ -37,6 +38,7 @@ extern int protocol_version;
786 extern int relative_paths;
787 extern int preserve_hard_links;
788 extern int preserve_perms;
789+extern int preserve_xattrs;
790 extern int basis_dir_cnt;
791 extern int make_backups;
792 extern int cleanup_got_literal;
793@@ -366,8 +368,8 @@ int recv_files(int f_in, char *local_nam
64e98de1
WD
794 cleanup_disable();
795
796 /* This call also sets cur_flist. */
797- ndx = read_ndx_and_attrs(f_in, -1, &iflags,
798- &fnamecmp_type, xname, &xlen);
799+ ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
800+ xname, &xlen);
801 if (ndx == NDX_DONE) {
802 if (inc_recurse && first_flist) {
803 flist_free(first_flist);
3fbc83e8 804@@ -397,8 +399,17 @@ int recv_files(int f_in, char *local_nam
64e98de1
WD
805 if (verbose > 2)
806 rprintf(FINFO, "recv_files(%s)\n", fname);
807
808+#ifdef SUPPORT_XATTRS
809+ if (iflags & ITEM_REPORT_XATTR && !dry_run)
810+ recv_xattr_request(file, f_in);
811+#endif
812+
813 if (!(iflags & ITEM_TRANSFER)) {
814 maybe_log_item(file, iflags, itemizing, xname);
815+#ifdef SUPPORT_XATTRS
3fbc83e8 816+ if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
3e6568cf 817+ set_file_attrs(fname, file, NULL, fname, 0);
64e98de1
WD
818+#endif
819 continue;
820 }
821 if (phase == 2) {
3fbc83e8 822@@ -655,15 +666,15 @@ int recv_files(int f_in, char *local_nam
3e6568cf
WD
823 temp_copy_name = NULL;
824 else
825 temp_copy_name = partialptr;
826- finish_transfer(fname, fnametmp, temp_copy_name,
827- file, recv_ok, 1);
828+ finish_transfer(fname, fnametmp, fnamecmp,
829+ temp_copy_name, file, recv_ok, 1);
830 if (fnamecmp == partialptr) {
831 do_unlink(partialptr);
832 handle_partial_dir(partialptr, PDIR_DELETE);
833 }
834 } else if (keep_partial && partialptr
835 && handle_partial_dir(partialptr, PDIR_CREATE)) {
836- finish_transfer(partialptr, fnametmp, NULL,
837+ finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
838 file, recv_ok, !partial_dir);
839 if (delay_updates && recv_ok) {
840 bitbag_set_bit(delayed_bits, ndx);
9a7eef96
WD
841--- old/rsync.c
842+++ new/rsync.c
526184b9 843@@ -32,6 +32,7 @@
ff318e90 844 extern int verbose;
93d6ca6d 845 extern int dry_run;
93d6ca6d
WD
846 extern int preserve_acls;
847+extern int preserve_xattrs;
848 extern int preserve_perms;
849 extern int preserve_executability;
850 extern int preserve_times;
64e98de1
WD
851@@ -91,10 +92,8 @@ void setup_iconv()
852 }
853 #endif
854
855-/* This is used by sender.c with a valid f_out, and by receive.c with
856- * f_out = -1. */
857-int read_ndx_and_attrs(int f_in, int f_out, int *iflag_ptr,
858- uchar *type_ptr, char *buf, int *len_ptr)
859+int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
860+ char *buf, int *len_ptr)
861 {
862 int len, iflags = 0;
863 struct file_list *flist;
864@@ -181,11 +180,6 @@ int read_ndx_and_attrs(int f_in, int f_o
865 ndx, who_am_i());
866 exit_cleanup(RERR_PROTOCOL);
867 }
868- } else if (f_out >= 0) {
869- if (inc_recurse)
870- send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
871- write_ndx_and_attrs(f_out, ndx, iflags,
872- fnamecmp_type, buf, len);
873 }
874
875 *iflag_ptr = iflags;
3e6568cf
WD
876@@ -228,8 +222,8 @@ mode_t dest_mode(mode_t flist_mode, mode
877 return new_mode;
878 }
879
880-int set_file_attrs(char *fname, struct file_struct *file, statx *sxp,
881- int flags)
882+int set_file_attrs(const char *fname, struct file_struct *file, statx *sxp,
883+ const char *fnamecmp, int flags)
884 {
885 int updated = 0;
886 statx sx2;
64e98de1
WD
887@@ -247,6 +241,9 @@ int set_file_attrs(char *fname, struct f
888 #ifdef SUPPORT_ACLS
889 sx2.acc_acl = sx2.def_acl = NULL;
890 #endif
891+#ifdef SUPPORT_XATTRS
892+ sx2.xattr = NULL;
893+#endif
894 if (!preserve_perms && S_ISDIR(new_mode)
895 && sx2.st.st_mode & S_ISGID) {
896 /* We just created this directory and its setgid
897@@ -321,6 +318,10 @@ int set_file_attrs(char *fname, struct f
071c5b19
WD
898 if (daemon_chmod_modes && !S_ISLNK(new_mode))
899 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
900
09cc6650 901+#ifdef SUPPORT_XATTRS
3e6568cf
WD
902+ if (preserve_xattrs && fnamecmp)
903+ set_xattr(fname, file, fnamecmp);
09cc6650 904+#endif
071c5b19
WD
905 #ifdef SUPPORT_ACLS
906 /* It's OK to call set_acl() now, even for a dir, as the generator
907 * will enable owner-writability using chmod, if necessary.
64e98de1
WD
908@@ -353,10 +354,16 @@ int set_file_attrs(char *fname, struct f
909 rprintf(FCLIENT, "%s is uptodate\n", fname);
910 }
911 cleanup:
912+ if (sxp == &sx2) {
913 #ifdef SUPPORT_ACLS
914- if (preserve_acls && sxp == &sx2)
915- free_acl(&sx2);
916+ if (preserve_acls)
917+ free_acl(&sx2);
3e6568cf 918+#endif
64e98de1
WD
919+#ifdef SUPPORT_XATTRS
920+ if (preserve_xattrs)
921+ free_xattr(&sx2);
3e6568cf 922 #endif
64e98de1
WD
923+ }
924 return updated;
925 }
0f6fb3c8 926
3e6568cf
WD
927@@ -378,7 +385,8 @@ RETSIGTYPE sig_int(UNUSED(int val))
928 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
929 * NULL and the robust_rename() call is forced to copy the temp file, we
930 * stage the file into the partial-dir and then rename it into place. */
931-void finish_transfer(char *fname, char *fnametmp, char *partialptr,
932+void finish_transfer(const char *fname, const char *fnametmp,
933+ const char *fnamecmp, const char *partialptr,
934 struct file_struct *file, int ok_to_set_time,
935 int overwriting_basis)
936 {
937@@ -395,7 +403,7 @@ void finish_transfer(char *fname, char *
938 return;
939
940 /* Change permissions before putting the file into place. */
941- set_file_attrs(fnametmp, file, NULL,
942+ set_file_attrs(fnametmp, file, NULL, fnamecmp,
943 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
944
945 /* move tmp file over real file */
946@@ -419,7 +427,7 @@ void finish_transfer(char *fname, char *
947 fnametmp = partialptr ? partialptr : fname;
948
949 do_set_file_attrs:
950- set_file_attrs(fnametmp, file, NULL,
951+ set_file_attrs(fnametmp, file, NULL, fnamecmp,
952 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
953
954 if (partialptr) {
64e98de1
WD
955--- old/rsync.h
956+++ new/rsync.h
957@@ -569,6 +569,7 @@ extern int file_extra_cnt;
6eba67ec
WD
958 extern int preserve_uid;
959 extern int preserve_gid;
960 extern int preserve_acls;
961+extern int preserve_xattrs;
962
963 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
964 #define EXTRA_LEN (sizeof (union file_extras))
64e98de1
WD
965@@ -601,7 +602,8 @@ extern int preserve_acls;
966 /* When the associated option is on, all entries will have these present: */
fc068916
WD
967 #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
968 #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
64e98de1
WD
969-#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
970+#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->num
971+#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->num
70891d26 972
8aec0853 973 /* These items are per-entry optional and mutally exclusive: */
fdf967c7 974 #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
64e98de1 975@@ -793,9 +795,13 @@ typedef struct {
0f6fb3c8
WD
976 struct rsync_acl *acc_acl; /* access ACL */
977 struct rsync_acl *def_acl; /* default ACL */
978 #endif
979+#ifdef SUPPORT_XATTRS
980+ item_list *xattr;
bbdff76a 981+#endif
0f6fb3c8 982 } statx;
bbdff76a 983
0f6fb3c8 984 #define ACL_READY(sx) ((sx).acc_acl != NULL)
64e98de1
WD
985+#define XATTR_READY(sx) ((sx).xattr != NULL)
986
987 #include "proto.h"
988
9a7eef96
WD
989--- old/rsync.yo
990+++ new/rsync.yo
4959107f 991@@ -301,7 +301,7 @@ to the detailed description below for a
1b57ecb0 992 -q, --quiet suppress non-error messages
4959107f 993 --no-motd suppress daemon-mode MOTD (see caveat)
1b57ecb0
WD
994 -c, --checksum skip based on checksum, not mod-time & size
995- -a, --archive archive mode; same as -rlptgoD (no -H, -A)
996+ -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
997 --no-OPTION turn off an implied OPTION (e.g. --no-D)
998 -r, --recursive recurse into directories
999 -R, --relative use relative path names
4959107f 1000@@ -324,6 +324,7 @@ to the detailed description below for a
4a65fe72 1001 -E, --executability preserve executability
063cf77b 1002 --chmod=CHMOD affect file and/or directory permissions
4306c620
WD
1003 -A, --acls preserve ACLs (implies -p)
1004+ -X, --xattrs preserve extended attrs (implies -p)
4a65fe72 1005 -o, --owner preserve owner (super-user only)
bbdff76a 1006 -g, --group preserve group
1ed0b5c9 1007 --devices preserve device files (super-user only)
4306c620
WD
1008@@ -831,6 +832,11 @@ dit(bf(-A, --acls)) This option causes r
1009 ACLs to be the same as the source ACLs. This nonstandard option only
1010 works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
bbdff76a
WD
1011
1012+dit(bf(-X, --xattrs)) This option causes rsync to update the remote
1013+extended attributes to be the same as the local ones. This will work
1014+only if the remote machine's rsync supports this option also. This is
1015+a non-standard option.
1016+
4a65fe72
WD
1017 dit(bf(--chmod)) This option tells rsync to apply one or more
1018 comma-separated "chmod" strings to the permission of the files in the
1019 transfer. The resulting value is treated as though it was the permissions
64e98de1
WD
1020--- old/sender.c
1021+++ new/sender.c
1022@@ -22,6 +22,7 @@
1023 #include "rsync.h"
1024
1025 extern int verbose;
1026+extern int dry_run;
1027 extern int do_xfers;
1028 extern int am_server;
1029 extern int am_daemon;
1030@@ -144,8 +145,9 @@ void successful_send(int ndx)
1031 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
1032 }
1033
1034-void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1035- uchar fnamecmp_type, char *buf, int len)
1036+static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1037+ const char *fname, struct file_struct *file,
1038+ uchar fnamecmp_type, char *buf, int len)
1039 {
1040 write_ndx(f_out, ndx);
1041 if (protocol_version < 29)
1042@@ -155,6 +157,10 @@ void write_ndx_and_attrs(int f_out, int
1043 write_byte(f_out, fnamecmp_type);
1044 if (iflags & ITEM_XNAME_FOLLOWS)
1045 write_vstring(f_out, buf, len);
1046+#ifdef SUPPORT_XATTRS
1047+ if (iflags & ITEM_REPORT_XATTR && !dry_run)
1048+ send_xattr_request(fname, file, f_out);
1049+#endif
1050 }
1051
1052 void send_files(int f_in, int f_out)
1053@@ -183,8 +189,8 @@ void send_files(int f_in, int f_out)
1054 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1055
1056 /* This call also sets cur_flist. */
1057- ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
1058- &fnamecmp_type, xname, &xlen);
1059+ ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
1060+ xname, &xlen);
1061 if (ndx == NDX_DONE) {
1062 if (inc_recurse && first_flist) {
1063 flist_free(first_flist);
1064@@ -201,6 +207,9 @@ void send_files(int f_in, int f_out)
1065 continue;
1066 }
1067
1068+ if (inc_recurse)
1069+ send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1070+
1071 file = cur_flist->files[ndx - cur_flist->ndx_start];
1072 if (F_ROOTDIR(file)) {
1073 path = F_ROOTDIR(file);
1074@@ -215,8 +224,13 @@ void send_files(int f_in, int f_out)
1075 if (verbose > 2)
1076 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
1077
1078+ if (iflags & ITEM_REPORT_XATTR)
1079+ recv_xattr_request(file, f_in);
1080+
1081 if (!(iflags & ITEM_TRANSFER)) {
1082 maybe_log_item(file, iflags, itemizing, xname);
1083+ write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1084+ fnamecmp_type, xname, xlen);
1085 continue;
1086 }
1087 if (phase == 2) {
1088@@ -251,8 +265,8 @@ void send_files(int f_in, int f_out)
1089
1090 if (!do_xfers) { /* log the transfer */
1091 log_item(FCLIENT, file, &stats, iflags, NULL);
1092- write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1093- xname, xlen);
1094+ write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1095+ fnamecmp_type, xname, xlen);
1096 continue;
1097 }
1098
1099@@ -305,8 +319,8 @@ void send_files(int f_in, int f_out)
1100 path,slash,fname, (double)st.st_size);
1101 }
1102
1103- write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1104- xname, xlen);
1105+ write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1106+ fnamecmp_type, xname, xlen);
1107 write_sum_head(f_xfer, s);
1108
1109 if (verbose > 2)
1110--- old/testsuite/xattrs.test
1111+++ new/testsuite/xattrs.test
3fbc83e8 1112@@ -0,0 +1,76 @@
64e98de1
WD
1113+#! /bin/sh
1114+
1115+# This program is distributable under the terms of the GNU GPL (see
1116+# COPYING).
1117+
1118+# Test that rsync handles basic xattr preservation.
1119+
1120+. $srcdir/testsuite/rsync.fns
1121+
1122+$RSYNC --version | grep ", xattrs" >/dev/null || test_skipped "Rsync is configured without xattr support"
1123+
1124+case "$RSYNC" in
1125+*protocol=29*) test_skipped "xattr support requires protocol 30" ;;
1126+esac
1127+
1128+makepath "$fromdir/foo"
1129+echo something >"$fromdir/file1"
1130+echo else >"$fromdir/file2"
1131+echo last >"$fromdir/foo/file3"
1132+
3fbc83e8
WD
1133+makepath "$chkdir/foo"
1134+echo wow >"$chkdir/file1"
1135+cp -p "$fromdir/foo/file3" "$chkdir/foo"
64e98de1
WD
1136+
1137+files='foo file1 file2 foo/file3'
1138+
1139+cd "$fromdir"
1140+
64e98de1
WD
1141+setfattr -n user.short -v 'this is short' file1 2>/dev/null || test_skipped "Unable to set an xattr"
1142+setfattr -n user.long -v 'this is a long attribute that will be truncated in the initial data send' file1
1143+setfattr -n user.good -v 'this is good' file1
1144+setfattr -n user.nice -v 'this is nice' file1
1145+
1146+setfattr -n user.foo -v foo file2
1147+setfattr -n user.bar -v bar file2
1148+
1149+setfattr -n user.foo -v 'new foo' foo/file3
1150+setfattr -n user.bar -v 'new bar' foo/file3
1151+setfattr -n user.long -v 'this is also a long attribute that will be truncated in the initial data send' foo/file3
1152+setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' foo/file3
1153+
3fbc83e8
WD
1154+setfattr -n user.short -v 'old short' "$chkdir/file1"
1155+setfattr -n user.extra -v 'remove me' "$chkdir/file1"
1156+
1157+setfattr -n user.foo -v 'old foo' "$chkdir/foo/file3"
1158+setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' "$chkdir/foo/file3"
1159+
1160+getfattr -d $files >"$scratchdir/xattrs.txt"
1161+
1162+# OK, let's try a simple xattr copy.
1163+checkit "$RSYNC -avX . \"$chkdir/\"" "$fromdir" "$chkdir"
64e98de1 1164+
3fbc83e8
WD
1165+cd "$chkdir"
1166+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
64e98de1 1167+
3fbc83e8
WD
1168+cd "$fromdir"
1169+
1170+checkit "$RSYNC -aiX --copy-dest=../chk . ../to" "$fromdir" "$todir"
1171+
1172+cd "$todir"
1173+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
1174+
1175+cd "$fromdir"
1176+setfattr -n user.nice -v 'this is nice, but different' file1
64e98de1
WD
1177+
1178+getfattr -d $files >"$scratchdir/xattrs.txt"
1179+
3fbc83e8
WD
1180+rm -rf "$todir"
1181+
1182+checkit "$RSYNC -aiX --link-dest=../chk . ../to" "$chkdir" "$todir"
1183+
64e98de1
WD
1184+cd "$todir"
1185+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
1186+
1187+# The script would have aborted on error, so getting here means we've won.
1188+exit 0
1189--- old/xattrs.c
1190+++ new/xattrs.c
3fbc83e8 1191@@ -0,0 +1,769 @@
dc52bc1a
WD
1192+/*
1193+ * Extended Attribute support for rsync.
0f6fb3c8 1194+ * Written by Jay Fenlason, vaguely based on the ACLs patch.
dc52bc1a
WD
1195+ *
1196+ * Copyright (C) 2004 Red Hat, Inc.
64e98de1 1197+ * Copyright (C) 2006, 2007 Wayne Davison
dc52bc1a
WD
1198+ *
1199+ * This program is free software; you can redistribute it and/or modify
64e98de1
WD
1200+ * it under the terms of the GNU General Public License version 2 as
1201+ * published by the Free Software Foundation.
dc52bc1a
WD
1202+ *
1203+ * This program is distributed in the hope that it will be useful,
1204+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1205+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1206+ * GNU General Public License for more details.
1207+ *
1208+ * You should have received a copy of the GNU General Public License along
1209+ * with this program; if not, write to the Free Software Foundation, Inc.,
1210+ * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1211+ */
bbdff76a
WD
1212+
1213+#include "rsync.h"
64e98de1 1214+#include "lib/sysxattrs.h"
bbdff76a
WD
1215+
1216+#ifdef SUPPORT_XATTRS
1217+
bbdff76a 1218+extern int dry_run;
3f24a3a8 1219+extern int am_root;
64e98de1 1220+extern int am_sender;
3fbc83e8 1221+extern int am_generator;
8889f85e
WD
1222+extern int read_only;
1223+extern int list_only;
64e98de1 1224+extern int checksum_seed;
bbdff76a
WD
1225+
1226+#define RSYNC_XAL_INITIAL 5
1227+#define RSYNC_XAL_LIST_INITIAL 100
1228+
64e98de1
WD
1229+#define MAX_FULL_DATUM 32
1230+
922f4409
WD
1231+#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
1232+ && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
1233+
3fbc83e8
WD
1234+#define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
1235+
1236+#define XSTATE_ABBREV 0
1237+#define XSTATE_DONE 1
1238+#define XSTATE_TODO 2
1239+#define XSTATE_LOCAL 3
1240+
3f24a3a8 1241+#define USER_PREFIX "user."
27502d8d
WD
1242+#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
1243+#define SYSTEM_PREFIX "system."
1244+#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
1245+
1246+#ifdef HAVE_LINUX_XATTRS
1247+#define RPRE_LEN 0
1248+#else
1249+#define RSYNC_PREFIX "rsync."
1250+#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
1251+#endif
3f24a3a8 1252+
bbdff76a 1253+typedef struct {
27502d8d
WD
1254+ char *datum, *name;
1255+ size_t datum_len, name_len;
bbdff76a
WD
1256+} rsync_xa;
1257+
bbdff76a
WD
1258+static size_t namebuf_len = 0;
1259+static char *namebuf = NULL;
1260+
0f6fb3c8
WD
1261+static item_list empty_xattr = EMPTY_ITEM_LIST;
1262+static item_list rsync_xal_l = EMPTY_ITEM_LIST;
bbdff76a
WD
1263+
1264+/* ------------------------------------------------------------------------- */
1265+
0f6fb3c8 1266+static void rsync_xal_free(item_list *xalp)
bbdff76a
WD
1267+{
1268+ size_t i;
0f6fb3c8 1269+ rsync_xa *rxas = xalp->items;
bbdff76a 1270+
0f6fb3c8 1271+ for (i = 0; i < xalp->count; i++) {
27502d8d
WD
1272+ free(rxas[i].datum);
1273+ /*free(rxas[i].name);*/
bbdff76a 1274+ }
0f6fb3c8 1275+ xalp->count = 0;
bbdff76a
WD
1276+}
1277+
0f6fb3c8 1278+void free_xattr(statx *sxp)
bbdff76a 1279+{
64e98de1
WD
1280+ if (!sxp->xattr)
1281+ return;
0f6fb3c8
WD
1282+ rsync_xal_free(sxp->xattr);
1283+ free(sxp->xattr);
1284+ sxp->xattr = NULL;
1285+}
bbdff76a 1286+
0f6fb3c8
WD
1287+static int rsync_xal_compare_names(const void *x1, const void *x2)
1288+{
1289+ const rsync_xa *xa1 = x1;
1290+ const rsync_xa *xa2 = x2;
bbdff76a
WD
1291+ return strcmp(xa1->name, xa2->name);
1292+}
1293+
64e98de1 1294+static ssize_t get_xattr_names(const char *fname)
bbdff76a 1295+{
526184b9 1296+ ssize_t list_len;
bbdff76a
WD
1297+
1298+ if (!namebuf) {
49de5440 1299+ namebuf_len = 1024;
bbdff76a 1300+ namebuf = new_array(char, namebuf_len);
49de5440 1301+ if (!namebuf)
64e98de1 1302+ out_of_memory("get_xattr_names");
bbdff76a
WD
1303+ }
1304+
49de5440 1305+ /* The length returned includes all the '\0' terminators. */
27502d8d
WD
1306+ list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1307+ if (list_len > (ssize_t)namebuf_len) {
1308+ list_len = -1;
bbdff76a
WD
1309+ errno = ERANGE;
1310+ }
27502d8d 1311+ if (list_len < 0) {
bbdff76a 1312+ if (errno == ENOTSUP)
0f6fb3c8 1313+ return 0;
bbdff76a 1314+ if (errno == ERANGE) {
27502d8d
WD
1315+ list_len = sys_llistxattr(fname, NULL, 0);
1316+ if (list_len < 0) {
892d2287 1317+ rsyserr(FERROR, errno,
64e98de1 1318+ "get_xattr_names: llistxattr(\"%s\",0) failed",
0f6fb3c8 1319+ fname);
e5754c5f 1320+ return -1;
bbdff76a 1321+ }
64e98de1
WD
1322+ if (namebuf_len)
1323+ free(namebuf);
27502d8d 1324+ namebuf_len = list_len + 1024;
64e98de1
WD
1325+ namebuf = new_array(char, namebuf_len);
1326+ if (!namebuf)
1327+ out_of_memory("get_xattr_names");
27502d8d
WD
1328+ list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1329+ if (list_len < 0) {
0f6fb3c8 1330+ rsyserr(FERROR, errno,
64e98de1 1331+ "get_xattr_names: llistxattr(\"%s\",%ld) failed",
892d2287 1332+ fname, (long)namebuf_len);
e5754c5f 1333+ return -1;
bbdff76a
WD
1334+ }
1335+ } else {
0f6fb3c8 1336+ rsyserr(FERROR, errno,
64e98de1 1337+ "get_xattr_names: llistxattr(\"%s\",%ld) failed",
892d2287 1338+ fname, (long)namebuf_len);
e5754c5f 1339+ return -1;
bbdff76a
WD
1340+ }
1341+ }
27502d8d 1342+
526184b9
WD
1343+ return list_len;
1344+}
1345+
64e98de1
WD
1346+/* On entry, the *len_ptr parameter contains the size of the extra space we
1347+ * should allocate when we create a buffer for the data. On exit, it contains
1348+ * the length of the datum. */
3fbc83e8
WD
1349+static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
1350+ int no_missing_error)
64e98de1
WD
1351+{
1352+ size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
1353+ char *ptr;
1354+
1355+ if (datum_len == (size_t)-1) {
3fbc83e8 1356+ if (errno == ENOTSUP || no_missing_error)
64e98de1
WD
1357+ return NULL;
1358+ rsyserr(FERROR, errno,
1359+ "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
1360+ fname, name);
1361+ return NULL;
1362+ }
1363+
64e98de1
WD
1364+ if (datum_len + *len_ptr < datum_len /* checks for overflow */
1365+ || !(ptr = new_array(char, datum_len + *len_ptr)))
1366+ out_of_memory("get_xattr_data");
1367+
9c18601b
WD
1368+ *len_ptr = datum_len;
1369+
64e98de1
WD
1370+ if (datum_len) {
1371+ size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
1372+ if (len != datum_len) {
1373+ if (len == (size_t)-1) {
1374+ rsyserr(FERROR, errno,
1375+ "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1376+ " failed", fname, name, (long)datum_len);
1377+ } else {
1378+ rprintf(FERROR,
1379+ "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1380+ " returned %ld\n", fname, name,
1381+ (long)datum_len, (long)len);
1382+ }
1383+ free(ptr);
1384+ return NULL;
1385+ }
1386+ }
1387+
1388+ return ptr;
1389+}
1390+
526184b9
WD
1391+static int rsync_xal_get(const char *fname, item_list *xalp)
1392+{
64e98de1
WD
1393+ ssize_t list_len, name_len;
1394+ size_t datum_len, name_offset;
526184b9 1395+ char *name, *ptr;
64e98de1 1396+ int user_only = am_sender ? 0 : !am_root;
526184b9
WD
1397+
1398+ /* This puts the name list into the "namebuf" buffer. */
64e98de1 1399+ if ((list_len = get_xattr_names(fname)) < 0)
526184b9
WD
1400+ return -1;
1401+
892d2287 1402+ for (name = namebuf; list_len > 0; name += name_len) {
3f24a3a8 1403+ rsync_xa *rxas;
bbdff76a 1404+
27502d8d 1405+ name_len = strlen(name) + 1;
892d2287 1406+ list_len -= name_len;
3f24a3a8
WD
1407+
1408+#ifdef HAVE_LINUX_XATTRS
64e98de1
WD
1409+ /* We always ignore the system namespace, and non-root
1410+ * ignores everything but the user namespace. */
1411+ if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
1412+ : HAS_PREFIX(name, SYSTEM_PREFIX))
3f24a3a8
WD
1413+ continue;
1414+#endif
1415+
64e98de1 1416+ datum_len = name_len; /* Pass extra size to get_xattr_data() */
3fbc83e8 1417+ if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
49de5440 1418+ return -1;
64e98de1
WD
1419+
1420+ if (datum_len > MAX_FULL_DATUM) {
1421+ /* For large datums, we store a flag and a checksum. */
1422+ name_offset = 1 + MAX_DIGEST_LEN;
1423+ sum_init(checksum_seed);
1424+ sum_update(ptr, datum_len);
1425+ free(ptr);
1426+
1427+ if (!(ptr = new_array(char, name_offset + name_len)))
1428+ out_of_memory("rsync_xal_get");
3fbc83e8 1429+ *ptr = XSTATE_ABBREV;
64e98de1
WD
1430+ sum_end(ptr + 1);
1431+ } else
1432+ name_offset = datum_len;
1433+
27502d8d 1434+ rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
64e98de1
WD
1435+ rxas->name = ptr + name_offset;
1436+ memcpy(rxas->name, name, name_len);
b54d5ec2 1437+ rxas->datum = ptr;
27502d8d
WD
1438+ rxas->name_len = name_len;
1439+ rxas->datum_len = datum_len;
bbdff76a 1440+ }
0f6fb3c8
WD
1441+ if (xalp->count > 1)
1442+ qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
e5754c5f 1443+ return 0;
bbdff76a
WD
1444+}
1445+
0f6fb3c8
WD
1446+/* Read the xattr(s) for this filename. */
1447+int get_xattr(const char *fname, statx *sxp)
bbdff76a 1448+{
0f6fb3c8
WD
1449+ sxp->xattr = new(item_list);
1450+ *sxp->xattr = empty_xattr;
1451+ if (rsync_xal_get(fname, sxp->xattr) < 0) {
1452+ free_xattr(sxp);
1453+ return -1;
1454+ }
1455+ return 0;
bbdff76a
WD
1456+}
1457+
0f6fb3c8 1458+static int find_matching_xattr(item_list *xalp)
bbdff76a 1459+{
0f6fb3c8
WD
1460+ size_t i, j;
1461+ item_list *lst = rsync_xal_l.items;
bbdff76a
WD
1462+
1463+ for (i = 0; i < rsync_xal_l.count; i++) {
0f6fb3c8
WD
1464+ rsync_xa *rxas1 = lst[i].items;
1465+ rsync_xa *rxas2 = xalp->items;
1466+
bbdff76a 1467+ /* Wrong number of elements? */
0f6fb3c8 1468+ if (lst[i].count != xalp->count)
bbdff76a
WD
1469+ continue;
1470+ /* any elements different? */
0f6fb3c8
WD
1471+ for (j = 0; j < xalp->count; j++) {
1472+ if (rxas1[j].name_len != rxas2[j].name_len
1473+ || rxas1[j].datum_len != rxas2[j].datum_len
64e98de1 1474+ || strcmp(rxas1[j].name, rxas2[j].name))
bbdff76a 1475+ break;
64e98de1
WD
1476+ if (rxas1[j].datum_len > MAX_FULL_DATUM) {
1477+ if (memcmp(rxas1[j].datum + 1,
1478+ rxas2[j].datum + 1,
1479+ MAX_DIGEST_LEN) != 0)
1480+ break;
1481+ } else {
1482+ if (memcmp(rxas1[j].datum, rxas2[j].datum,
1483+ rxas2[j].datum_len))
1484+ break;
1485+ }
bbdff76a
WD
1486+ }
1487+ /* no differences found. This is The One! */
0f6fb3c8
WD
1488+ if (j == xalp->count)
1489+ return i;
bbdff76a 1490+ }
0f6fb3c8
WD
1491+
1492+ return -1;
bbdff76a
WD
1493+}
1494+
0f6fb3c8
WD
1495+/* Store *xalp on the end of rsync_xal_l */
1496+static void rsync_xal_store(item_list *xalp)
bbdff76a 1497+{
0f6fb3c8 1498+ item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
de565f59
WD
1499+ /* Since the following call starts a new list, we know it will hold the
1500+ * entire initial-count, not just enough space for one new item. */
49de5440
WD
1501+ *new_lst = empty_xattr;
1502+ (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
1503+ memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
0f6fb3c8
WD
1504+ new_lst->count = xalp->count;
1505+ xalp->count = 0;
bbdff76a
WD
1506+}
1507+
0f6fb3c8 1508+/* Send the make_xattr()-generated xattr list for this flist entry. */
64e98de1 1509+int send_xattr(statx *sxp, int f)
bbdff76a 1510+{
0f6fb3c8 1511+ int ndx = find_matching_xattr(sxp->xattr);
64e98de1
WD
1512+
1513+ /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
1514+ write_abbrevint(f, ndx + 1);
1515+
1516+ if (ndx < 0) {
bbdff76a 1517+ rsync_xa *rxa;
0f6fb3c8 1518+ int count = sxp->xattr->count;
64e98de1 1519+ write_abbrevint(f, count);
0f6fb3c8 1520+ for (rxa = sxp->xattr->items; count--; rxa++) {
27502d8d 1521+#ifdef HAVE_LINUX_XATTRS
64e98de1
WD
1522+ write_abbrevint(f, rxa->name_len);
1523+ write_abbrevint(f, rxa->datum_len);
27502d8d
WD
1524+ write_buf(f, rxa->name, rxa->name_len);
1525+#else
1526+ /* We strip the rsync prefix from disguised namespaces
1527+ * and put everything else in the user namespace. */
dfe2b257
WD
1528+ if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
1529+ && rxa->name[RPRE_LEN] != '%') {
64e98de1
WD
1530+ write_abbrevint(f, rxa->name_len - RPRE_LEN);
1531+ write_abbrevint(f, rxa->datum_len);
27502d8d
WD
1532+ write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
1533+ } else {
64e98de1
WD
1534+ write_abbrevint(f, rxa->name_len + UPRE_LEN);
1535+ write_abbrevint(f, rxa->datum_len);
3f24a3a8 1536+ write_buf(f, USER_PREFIX, UPRE_LEN);
27502d8d 1537+ write_buf(f, rxa->name, rxa->name_len);
3f24a3a8 1538+ }
27502d8d 1539+#endif
64e98de1
WD
1540+ if (rxa->datum_len > MAX_FULL_DATUM)
1541+ write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
1542+ else
1543+ write_buf(f, rxa->datum, rxa->datum_len);
bbdff76a 1544+ }
64e98de1 1545+ ndx = rsync_xal_l.count; /* pre-incremented count */
49de5440 1546+ rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
bbdff76a 1547+ }
64e98de1
WD
1548+
1549+ return ndx;
1550+}
1551+
1552+/* Return a flag indicating if we need to change a file's xattrs. If
1553+ * "find_all" is specified, also mark any abbreviated xattrs that we
1554+ * need so that send_xattr_request() can tell the sender about them. */
1555+int xattr_diff(struct file_struct *file, statx *sxp, int find_all)
1556+{
1557+ item_list *lst = rsync_xal_l.items;
1558+ rsync_xa *snd_rxa, *rec_rxa;
1559+ int snd_cnt, rec_cnt;
1560+ int cmp, same, xattrs_equal = 1;
1561+
1562+ if (!XATTR_READY(*sxp)) {
1563+ rec_rxa = NULL;
1564+ rec_cnt = 0;
1565+ } else {
1566+ rec_rxa = sxp->xattr->items;
1567+ rec_cnt = sxp->xattr->count;
1568+ }
1569+
1570+ if (F_XATTR(file) >= 0)
1571+ lst += F_XATTR(file);
1572+ else
1573+ lst = &empty_xattr;
1574+
1575+ snd_rxa = lst->items;
1576+ snd_cnt = lst->count;
1577+
1578+ /* If the count of the sender's xattrs is different from our
1579+ * (receiver's) xattrs, the lists are not the same. */
1580+ if (snd_cnt != rec_cnt) {
1581+ if (!find_all)
1582+ return 1;
1583+ xattrs_equal = 0;
1584+ }
1585+
1586+ while (snd_cnt) {
1587+ cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
1588+ if (cmp > 0)
1589+ same = 0;
1590+ else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
1591+ same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1592+ && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
1593+ MAX_DIGEST_LEN) == 0;
1594+ /* Flag unrequested items that we need. */
3fbc83e8
WD
1595+ if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
1596+ snd_rxa->datum[0] = XSTATE_TODO;
64e98de1
WD
1597+ } else {
1598+ same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1599+ && memcmp(snd_rxa->datum, rec_rxa->datum,
1600+ snd_rxa->datum_len) == 0;
1601+ }
1602+ if (!same) {
1603+ if (!find_all)
1604+ return 1;
1605+ xattrs_equal = 0;
1606+ }
1607+
1608+ if (cmp <= 0) {
1609+ snd_rxa++;
1610+ snd_cnt--;
1611+ }
1612+ if (cmp >= 0) {
1613+ rec_rxa++;
1614+ rec_cnt--;
1615+ }
1616+ }
1617+
1618+ if (rec_cnt)
1619+ xattrs_equal = 0;
1620+
1621+ return !xattrs_equal;
1622+}
1623+
1624+/* When called by the generator with a NULL fname, this tells the sender
1625+ * which abbreviated xattr values we need. When called by the sender
9c18601b 1626+ * (with a non-NULL fname), we send all the extra xattr data it needs. */
64e98de1
WD
1627+void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
1628+{
1629+ item_list *lst = rsync_xal_l.items;
1630+ int j, cnt, prior_req = -1;
1631+ rsync_xa *rxa;
1632+
1633+ lst += F_XATTR(file);
1634+ cnt = lst->count;
1635+ for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) {
3fbc83e8 1636+ if (rxa->datum_len <= MAX_FULL_DATUM)
64e98de1 1637+ continue;
3fbc83e8
WD
1638+ switch (rxa->datum[0]) {
1639+ case XSTATE_LOCAL:
1640+ /* Items set locally will get cached by receiver. */
1641+ rxa->datum[0] = XSTATE_DONE;
1642+ continue;
1643+ case XSTATE_TODO:
1644+ break;
1645+ default:
1646+ continue;
1647+ }
64e98de1
WD
1648+
1649+ /* Flag that we handled this abbreviated item. */
3fbc83e8 1650+ rxa->datum[0] = XSTATE_DONE;
64e98de1
WD
1651+
1652+ write_abbrevint(f_out, j - prior_req);
1653+ prior_req = j;
1654+
1655+ if (fname) {
1656+ size_t len = 0;
1657+ char *ptr;
1658+
1659+ /* Re-read the long datum. */
3fbc83e8 1660+ if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0)))
64e98de1
WD
1661+ continue;
1662+
1663+ write_abbrevint(f_out, len); /* length might have changed! */
1664+ write_buf(f_out, ptr, len);
1665+ free(ptr);
1666+ }
1667+ }
1668+
1669+ write_byte(f_out, 0); /* end the list */
1670+}
1671+
3fbc83e8
WD
1672+/* Any items set locally by the generator that the receiver doesn't
1673+ * get told about get changed back to XSTATE_ABBREV. */
1674+void xattr_clear_locals(struct file_struct *file)
1675+{
1676+ item_list *lst = rsync_xal_l.items;
1677+ rsync_xa *rxa;
1678+ int cnt;
1679+
1680+ if (F_XATTR(file) < 0)
1681+ return;
1682+
1683+ lst += F_XATTR(file);
1684+ cnt = lst->count;
1685+ for (rxa = lst->items; cnt--; rxa++) {
1686+ if (rxa->datum_len <= MAX_FULL_DATUM)
1687+ continue;
1688+ if (rxa->datum[0] == XSTATE_LOCAL)
1689+ rxa->datum[0] = XSTATE_ABBREV;
1690+ }
1691+}
1692+
9c18601b
WD
1693+/* When called by the sender, read the request from the generator and mark
1694+ * any needed xattrs with a flag that lets us know they need to be sent to
1695+ * the receiver. When called by the receiver, reads the sent data and
1696+ * stores it in place of its checksum. */
64e98de1
WD
1697+void recv_xattr_request(struct file_struct *file, int f_in)
1698+{
1699+ item_list *lst = rsync_xal_l.items;
1700+ char *old_datum, *name;
1701+ rsync_xa *rxa;
1702+ int rel_pos, cnt;
1703+
3fbc83e8 1704+ if (F_XATTR(file) < 0)
64e98de1 1705+ exit_cleanup(RERR_STREAMIO); /* XXX */
3fbc83e8 1706+ lst += F_XATTR(file);
64e98de1
WD
1707+
1708+ cnt = lst->count;
1709+ rxa = lst->items;
1710+ rxa -= 1;
1711+ while ((rel_pos = read_abbrevint(f_in)) != 0) {
1712+ rxa += rel_pos;
1713+ cnt -= rel_pos;
1714+ if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM
3fbc83e8 1715+ || rxa->datum[0] != XSTATE_ABBREV)
64e98de1
WD
1716+ exit_cleanup(RERR_STREAMIO); /* XXX */
1717+
1718+ if (am_sender) {
3fbc83e8 1719+ rxa->datum[0] = XSTATE_TODO;
64e98de1
WD
1720+ continue;
1721+ }
1722+
1723+ old_datum = rxa->datum;
1724+ rxa->datum_len = read_abbrevint(f_in);
1725+
1726+ if (rxa->name_len + rxa->datum_len < rxa->name_len)
1727+ out_of_memory("recv_xattr_request"); /* overflow */
1728+ rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
1729+ if (!rxa->datum)
1730+ out_of_memory("recv_xattr_request");
1731+ name = rxa->datum + rxa->datum_len;
1732+ memcpy(name, rxa->name, rxa->name_len);
1733+ rxa->name = name;
1734+ free(old_datum);
1735+ read_buf(f_in, rxa->datum, rxa->datum_len);
1736+ }
bbdff76a
WD
1737+}
1738+
bbdff76a 1739+/* ------------------------------------------------------------------------- */
bbdff76a 1740+
0f6fb3c8 1741+/* receive and build the rsync_xattr_lists */
bbdff76a
WD
1742+void receive_xattr(struct file_struct *file, int f)
1743+{
0f6fb3c8 1744+ static item_list temp_xattr = EMPTY_ITEM_LIST;
64e98de1
WD
1745+ int count;
1746+ int ndx = read_abbrevint(f);
1747+
1748+ if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
1749+ rprintf(FERROR, "receive_xattr: xa index %d out of"
1750+ " range for %s\n", ndx, f_name(file, NULL));
1751+ exit_cleanup(RERR_STREAMIO);
1752+ }
1753+
1754+ if (ndx != 0) {
1755+ F_XATTR(file) = ndx - 1;
1756+ return;
1757+ }
1758+
1759+ if ((count = read_abbrevint(f)) != 0) {
1760+ (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
1761+ temp_xattr.count = 0;
1762+ }
1763+
1764+ while (count--) {
1765+ char *ptr, *name;
1766+ rsync_xa *rxa;
1767+ size_t name_len = read_abbrevint(f);
1768+ size_t datum_len = read_abbrevint(f);
1769+ size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
bcecb5e3 1770+#ifdef HAVE_LINUX_XATTRS
64e98de1 1771+ size_t extra_len = 0;
bcecb5e3 1772+#else
64e98de1
WD
1773+ size_t extra_len = am_root ? RPRE_LEN : 0;
1774+ if (dget_len + extra_len < dget_len)
1775+ out_of_memory("receive_xattr"); /* overflow */
27502d8d 1776+#endif
64e98de1
WD
1777+ if (dget_len + extra_len + name_len < dget_len)
1778+ out_of_memory("receive_xattr"); /* overflow */
1779+ ptr = new_array(char, dget_len + extra_len + name_len);
1780+ if (!ptr)
1781+ out_of_memory("receive_xattr");
1782+ name = ptr + dget_len + extra_len;
1783+ read_buf(f, name, name_len);
1784+ if (dget_len == datum_len)
1785+ read_buf(f, ptr, dget_len);
1786+ else {
3fbc83e8 1787+ *ptr = XSTATE_ABBREV;
64e98de1
WD
1788+ read_buf(f, ptr + 1, MAX_DIGEST_LEN);
1789+ }
3f24a3a8 1790+#ifdef HAVE_LINUX_XATTRS
64e98de1
WD
1791+ /* Non-root can only save the user namespace. */
1792+ if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
1793+ free(ptr);
1794+ continue;
bbdff76a 1795+ }
64e98de1
WD
1796+#else
1797+ /* This OS only has a user namespace, so we either
1798+ * strip the user prefix, or we put a non-user
1799+ * namespace inside our rsync hierarchy. */
1800+ if (HAS_PREFIX(name, USER_PREFIX)) {
1801+ name += UPRE_LEN;
1802+ name_len -= UPRE_LEN;
1803+ } else if (am_root) {
1804+ name -= RPRE_LEN;
1805+ name_len += RPRE_LEN;
1806+ memcpy(name, RSYNC_PREFIX, RPRE_LEN);
1807+ } else {
1808+ free(ptr);
1809+ continue;
bbdff76a 1810+ }
64e98de1
WD
1811+#endif
1812+ rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
1813+ rxa->name = name;
1814+ rxa->datum = ptr;
1815+ rxa->name_len = name_len;
1816+ rxa->datum_len = datum_len;
bbdff76a 1817+ }
bbdff76a 1818+
64e98de1
WD
1819+ ndx = rsync_xal_l.count; /* pre-incremented count */
1820+ rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
1821+
70891d26 1822+ F_XATTR(file) = ndx;
bbdff76a
WD
1823+}
1824+
0f6fb3c8
WD
1825+/* Turn the xattr data in statx into cached xattr data, setting the index
1826+ * values in the file struct. */
1827+void cache_xattr(struct file_struct *file, statx *sxp)
bbdff76a 1828+{
0f6fb3c8 1829+ int ndx;
bbdff76a 1830+
0f6fb3c8
WD
1831+ if (!sxp->xattr)
1832+ return;
bbdff76a 1833+
0f6fb3c8 1834+ ndx = find_matching_xattr(sxp->xattr);
64e98de1 1835+ if (ndx < 0)
49de5440 1836+ rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
bbdff76a 1837+
70891d26 1838+ F_XATTR(file) = ndx;
bbdff76a
WD
1839+}
1840+
3e6568cf 1841+static int rsync_xal_set(const char *fname, item_list *xalp, const char *fnamecmp)
bbdff76a 1842+{
0f6fb3c8 1843+ rsync_xa *rxas = xalp->items;
526184b9 1844+ ssize_t list_len;
3fbc83e8
WD
1845+ size_t i, len;
1846+ char *name, *ptr, sum[MAX_DIGEST_LEN];
1847+ int name_len, ret = 0;
526184b9
WD
1848+
1849+ /* This puts the current name list into the "namebuf" buffer. */
64e98de1 1850+ if ((list_len = get_xattr_names(fname)) < 0)
526184b9 1851+ return -1;
bbdff76a 1852+
0f6fb3c8 1853+ for (i = 0; i < xalp->count; i++) {
3fbc83e8
WD
1854+ name = rxas[i].name;
1855+
1856+ if (XATTR_ABBREV(rxas[i])) {
1857+ /* See if the fnamecmp version is identical. */
1858+ len = name_len = rxas[i].name_len;
1859+ if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
1860+ still_abbrev:
1861+ if (am_generator)
1862+ continue;
3e6568cf
WD
1863+ rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
1864+ rxas[i].name, full_fname(fname));
1865+ ret = -1;
1866+ continue;
1867+ }
3fbc83e8
WD
1868+ if (len != rxas[i].datum_len) {
1869+ free(ptr);
1870+ goto still_abbrev;
1871+ }
1872+
1873+ sum_init(checksum_seed);
1874+ sum_update(ptr, len);
1875+ sum_end(sum);
1876+ if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
1877+ free(ptr);
1878+ goto still_abbrev;
1879+ }
1880+
1881+ if (fname != fnamecmp /* value is already set */
1882+ && sys_lsetxattr(fname, name, ptr, len) < 0) {
1883+ rsyserr(FERROR, errno,
1884+ "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1885+ fname, name);
1886+ ret = -1;
1887+ }
1888+
1889+ if (am_generator) { /* generator items stay abbreviated */
1890+ if (rxas[i].datum[0] == XSTATE_ABBREV)
1891+ rxas[i].datum[0] = XSTATE_LOCAL;
1892+ free(ptr);
3e6568cf 1893+ continue;
3fbc83e8
WD
1894+ }
1895+
1896+ memcpy(ptr + len, name, name_len);
1897+ free(rxas[i].datum);
1898+
1899+ rxas[i].name = name = ptr + len;
1900+ rxas[i].datum = ptr;
1901+ continue;
64e98de1 1902+ }
3fbc83e8
WD
1903+
1904+ if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
892d2287
WD
1905+ rsyserr(FERROR, errno,
1906+ "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
3fbc83e8 1907+ fname, name);
0f6fb3c8
WD
1908+ ret = -1;
1909+ }
bbdff76a 1910+ }
526184b9
WD
1911+
1912+ /* Remove any extraneous names. */
1913+ for (name = namebuf; list_len > 0; name += name_len) {
1914+ name_len = strlen(name) + 1;
1915+ list_len -= name_len;
1916+
e3cd70c8
WD
1917+#ifdef HAVE_LINUX_XATTRS
1918+ /* We always ignore the system namespace, and non-root
1919+ * ignores everything but the user namespace. */
1920+ if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
1921+ : !HAS_PREFIX(name, USER_PREFIX))
1922+ continue;
1923+#endif
1924+
526184b9
WD
1925+ for (i = 0; i < xalp->count; i++) {
1926+ if (strcmp(name, rxas[i].name) == 0)
1927+ break;
1928+ }
1929+ if (i == xalp->count) {
3fbc83e8 1930+ if (sys_lremovexattr(fname, name) < 0) {
526184b9
WD
1931+ rsyserr(FERROR, errno,
1932+ "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
1933+ fname, name);
1934+ ret = -1;
1935+ }
1936+ }
1937+ }
1938+
0f6fb3c8 1939+ return ret;
bbdff76a
WD
1940+}
1941+
0f6fb3c8 1942+/* Set extended attributes on indicated filename. */
3e6568cf 1943+int set_xattr(const char *fname, const struct file_struct *file, const char *fnamecmp)
bbdff76a 1944+{
0f6fb3c8 1945+ int ndx;
0f6fb3c8 1946+ item_list *lst = rsync_xal_l.items;
bbdff76a 1947+
93d6ca6d
WD
1948+ if (dry_run)
1949+ return 1; /* FIXME: --dry-run needs to compute this value */
1950+
8889f85e
WD
1951+ if (read_only || list_only) {
1952+ errno = EROFS;
1953+ return -1;
1954+ }
1955+
70891d26 1956+ ndx = F_XATTR(file);
3e6568cf 1957+ return rsync_xal_set(fname, lst + ndx, fnamecmp);
bbdff76a
WD
1958+}
1959+
1960+#endif /* SUPPORT_XATTRS */