Moved the xattr setting and added code to make sure that we set
[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
acf4467b
WD
897@@ -261,6 +258,11 @@ int set_file_attrs(char *fname, struct f
898 get_acl(fname, sxp);
899 #endif
071c5b19 900
09cc6650 901+#ifdef SUPPORT_XATTRS
3e6568cf 902+ if (preserve_xattrs && fnamecmp)
acf4467b 903+ set_xattr(fname, file, fnamecmp, sxp);
09cc6650 904+#endif
acf4467b
WD
905+
906 if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && omit_dir_times))
907 flags |= ATTRS_SKIP_MTIME;
908 if (!(flags & ATTRS_SKIP_MTIME)
909@@ -353,10 +355,16 @@ int set_file_attrs(char *fname, struct f
64e98de1
WD
910 rprintf(FCLIENT, "%s is uptodate\n", fname);
911 }
912 cleanup:
913+ if (sxp == &sx2) {
914 #ifdef SUPPORT_ACLS
915- if (preserve_acls && sxp == &sx2)
916- free_acl(&sx2);
917+ if (preserve_acls)
918+ free_acl(&sx2);
3e6568cf 919+#endif
64e98de1
WD
920+#ifdef SUPPORT_XATTRS
921+ if (preserve_xattrs)
922+ free_xattr(&sx2);
3e6568cf 923 #endif
64e98de1
WD
924+ }
925 return updated;
926 }
0f6fb3c8 927
acf4467b 928@@ -378,7 +386,8 @@ RETSIGTYPE sig_int(UNUSED(int val))
3e6568cf
WD
929 * attributes (e.g. permissions, ownership, etc.). If partialptr is not
930 * NULL and the robust_rename() call is forced to copy the temp file, we
931 * stage the file into the partial-dir and then rename it into place. */
932-void finish_transfer(char *fname, char *fnametmp, char *partialptr,
933+void finish_transfer(const char *fname, const char *fnametmp,
934+ const char *fnamecmp, const char *partialptr,
935 struct file_struct *file, int ok_to_set_time,
936 int overwriting_basis)
937 {
acf4467b 938@@ -395,7 +404,7 @@ void finish_transfer(char *fname, char *
3e6568cf
WD
939 return;
940
941 /* Change permissions before putting the file into place. */
942- set_file_attrs(fnametmp, file, NULL,
943+ set_file_attrs(fnametmp, file, NULL, fnamecmp,
944 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
945
946 /* move tmp file over real file */
acf4467b 947@@ -419,7 +428,7 @@ void finish_transfer(char *fname, char *
3e6568cf
WD
948 fnametmp = partialptr ? partialptr : fname;
949
950 do_set_file_attrs:
951- set_file_attrs(fnametmp, file, NULL,
952+ set_file_attrs(fnametmp, file, NULL, fnamecmp,
953 ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
954
955 if (partialptr) {
64e98de1
WD
956--- old/rsync.h
957+++ new/rsync.h
958@@ -569,6 +569,7 @@ extern int file_extra_cnt;
6eba67ec
WD
959 extern int preserve_uid;
960 extern int preserve_gid;
961 extern int preserve_acls;
962+extern int preserve_xattrs;
963
964 #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
965 #define EXTRA_LEN (sizeof (union file_extras))
64e98de1
WD
966@@ -601,7 +602,8 @@ extern int preserve_acls;
967 /* When the associated option is on, all entries will have these present: */
fc068916
WD
968 #define F_OWNER(f) REQ_EXTRA(f, preserve_uid)->unum
969 #define F_GROUP(f) REQ_EXTRA(f, preserve_gid)->unum
64e98de1
WD
970-#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->unum
971+#define F_ACL(f) REQ_EXTRA(f, preserve_acls)->num
972+#define F_XATTR(f) REQ_EXTRA(f, preserve_xattrs)->num
70891d26 973
8aec0853 974 /* These items are per-entry optional and mutally exclusive: */
fdf967c7 975 #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num
64e98de1 976@@ -793,9 +795,13 @@ typedef struct {
0f6fb3c8
WD
977 struct rsync_acl *acc_acl; /* access ACL */
978 struct rsync_acl *def_acl; /* default ACL */
979 #endif
980+#ifdef SUPPORT_XATTRS
981+ item_list *xattr;
bbdff76a 982+#endif
0f6fb3c8 983 } statx;
bbdff76a 984
0f6fb3c8 985 #define ACL_READY(sx) ((sx).acc_acl != NULL)
64e98de1
WD
986+#define XATTR_READY(sx) ((sx).xattr != NULL)
987
988 #include "proto.h"
989
9a7eef96
WD
990--- old/rsync.yo
991+++ new/rsync.yo
4959107f 992@@ -301,7 +301,7 @@ to the detailed description below for a
1b57ecb0 993 -q, --quiet suppress non-error messages
4959107f 994 --no-motd suppress daemon-mode MOTD (see caveat)
1b57ecb0
WD
995 -c, --checksum skip based on checksum, not mod-time & size
996- -a, --archive archive mode; same as -rlptgoD (no -H, -A)
997+ -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
998 --no-OPTION turn off an implied OPTION (e.g. --no-D)
999 -r, --recursive recurse into directories
1000 -R, --relative use relative path names
4959107f 1001@@ -324,6 +324,7 @@ to the detailed description below for a
4a65fe72 1002 -E, --executability preserve executability
063cf77b 1003 --chmod=CHMOD affect file and/or directory permissions
4306c620
WD
1004 -A, --acls preserve ACLs (implies -p)
1005+ -X, --xattrs preserve extended attrs (implies -p)
4a65fe72 1006 -o, --owner preserve owner (super-user only)
bbdff76a 1007 -g, --group preserve group
1ed0b5c9 1008 --devices preserve device files (super-user only)
4306c620
WD
1009@@ -831,6 +832,11 @@ dit(bf(-A, --acls)) This option causes r
1010 ACLs to be the same as the source ACLs. This nonstandard option only
1011 works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
bbdff76a
WD
1012
1013+dit(bf(-X, --xattrs)) This option causes rsync to update the remote
1014+extended attributes to be the same as the local ones. This will work
1015+only if the remote machine's rsync supports this option also. This is
1016+a non-standard option.
1017+
4a65fe72
WD
1018 dit(bf(--chmod)) This option tells rsync to apply one or more
1019 comma-separated "chmod" strings to the permission of the files in the
1020 transfer. The resulting value is treated as though it was the permissions
64e98de1
WD
1021--- old/sender.c
1022+++ new/sender.c
1023@@ -22,6 +22,7 @@
1024 #include "rsync.h"
1025
1026 extern int verbose;
1027+extern int dry_run;
1028 extern int do_xfers;
1029 extern int am_server;
1030 extern int am_daemon;
acf4467b
WD
1031@@ -33,6 +34,7 @@ extern int csum_length;
1032 extern int append_mode;
1033 extern int io_error;
1034 extern int allowed_lull;
1035+extern int preserve_xattrs;
1036 extern int protocol_version;
1037 extern int remove_source_files;
1038 extern int updating_basis_file;
1039@@ -144,8 +146,9 @@ void successful_send(int ndx)
64e98de1
WD
1040 rsyserr(FERROR, errno, "sender failed to remove %s", fname);
1041 }
1042
1043-void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1044- uchar fnamecmp_type, char *buf, int len)
1045+static void write_ndx_and_attrs(int f_out, int ndx, int iflags,
1046+ const char *fname, struct file_struct *file,
1047+ uchar fnamecmp_type, char *buf, int len)
1048 {
1049 write_ndx(f_out, ndx);
1050 if (protocol_version < 29)
acf4467b 1051@@ -155,6 +158,10 @@ void write_ndx_and_attrs(int f_out, int
64e98de1
WD
1052 write_byte(f_out, fnamecmp_type);
1053 if (iflags & ITEM_XNAME_FOLLOWS)
1054 write_vstring(f_out, buf, len);
1055+#ifdef SUPPORT_XATTRS
acf4467b 1056+ if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
64e98de1
WD
1057+ send_xattr_request(fname, file, f_out);
1058+#endif
1059 }
1060
1061 void send_files(int f_in, int f_out)
acf4467b 1062@@ -183,8 +190,8 @@ void send_files(int f_in, int f_out)
64e98de1
WD
1063 send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1064
1065 /* This call also sets cur_flist. */
1066- ndx = read_ndx_and_attrs(f_in, f_out, &iflags,
1067- &fnamecmp_type, xname, &xlen);
1068+ ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
1069+ xname, &xlen);
1070 if (ndx == NDX_DONE) {
1071 if (inc_recurse && first_flist) {
1072 flist_free(first_flist);
acf4467b 1073@@ -201,6 +208,9 @@ void send_files(int f_in, int f_out)
64e98de1
WD
1074 continue;
1075 }
1076
1077+ if (inc_recurse)
1078+ send_extra_file_list(f_out, FILECNT_LOOKAHEAD);
1079+
1080 file = cur_flist->files[ndx - cur_flist->ndx_start];
1081 if (F_ROOTDIR(file)) {
1082 path = F_ROOTDIR(file);
acf4467b 1083@@ -215,8 +225,15 @@ void send_files(int f_in, int f_out)
64e98de1
WD
1084 if (verbose > 2)
1085 rprintf(FINFO, "send_files(%d, %s%s%s)\n", ndx, path,slash,fname);
1086
acf4467b
WD
1087+#ifdef SUPPORT_XATTRS
1088+ if (preserve_xattrs && iflags & ITEM_REPORT_XATTR)
64e98de1 1089+ recv_xattr_request(file, f_in);
acf4467b 1090+#endif
64e98de1
WD
1091+
1092 if (!(iflags & ITEM_TRANSFER)) {
1093 maybe_log_item(file, iflags, itemizing, xname);
1094+ write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1095+ fnamecmp_type, xname, xlen);
1096 continue;
1097 }
1098 if (phase == 2) {
acf4467b 1099@@ -251,8 +268,8 @@ void send_files(int f_in, int f_out)
64e98de1
WD
1100
1101 if (!do_xfers) { /* log the transfer */
1102 log_item(FCLIENT, file, &stats, iflags, NULL);
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 continue;
1108 }
1109
acf4467b 1110@@ -305,8 +322,8 @@ void send_files(int f_in, int f_out)
64e98de1
WD
1111 path,slash,fname, (double)st.st_size);
1112 }
1113
1114- write_ndx_and_attrs(f_out, ndx, iflags, fnamecmp_type,
1115- xname, xlen);
1116+ write_ndx_and_attrs(f_out, ndx, iflags, fname, file,
1117+ fnamecmp_type, xname, xlen);
1118 write_sum_head(f_xfer, s);
1119
1120 if (verbose > 2)
1121--- old/testsuite/xattrs.test
1122+++ new/testsuite/xattrs.test
3fbc83e8 1123@@ -0,0 +1,76 @@
64e98de1
WD
1124+#! /bin/sh
1125+
1126+# This program is distributable under the terms of the GNU GPL (see
1127+# COPYING).
1128+
1129+# Test that rsync handles basic xattr preservation.
1130+
1131+. $srcdir/testsuite/rsync.fns
1132+
1133+$RSYNC --version | grep ", xattrs" >/dev/null || test_skipped "Rsync is configured without xattr support"
1134+
1135+case "$RSYNC" in
1136+*protocol=29*) test_skipped "xattr support requires protocol 30" ;;
1137+esac
1138+
1139+makepath "$fromdir/foo"
1140+echo something >"$fromdir/file1"
1141+echo else >"$fromdir/file2"
1142+echo last >"$fromdir/foo/file3"
1143+
3fbc83e8
WD
1144+makepath "$chkdir/foo"
1145+echo wow >"$chkdir/file1"
1146+cp -p "$fromdir/foo/file3" "$chkdir/foo"
64e98de1
WD
1147+
1148+files='foo file1 file2 foo/file3'
1149+
1150+cd "$fromdir"
1151+
64e98de1
WD
1152+setfattr -n user.short -v 'this is short' file1 2>/dev/null || test_skipped "Unable to set an xattr"
1153+setfattr -n user.long -v 'this is a long attribute that will be truncated in the initial data send' file1
1154+setfattr -n user.good -v 'this is good' file1
1155+setfattr -n user.nice -v 'this is nice' file1
1156+
1157+setfattr -n user.foo -v foo file2
1158+setfattr -n user.bar -v bar file2
1159+
1160+setfattr -n user.foo -v 'new foo' foo/file3
1161+setfattr -n user.bar -v 'new bar' foo/file3
1162+setfattr -n user.long -v 'this is also a long attribute that will be truncated in the initial data send' foo/file3
1163+setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' foo/file3
1164+
3fbc83e8
WD
1165+setfattr -n user.short -v 'old short' "$chkdir/file1"
1166+setfattr -n user.extra -v 'remove me' "$chkdir/file1"
1167+
1168+setfattr -n user.foo -v 'old foo' "$chkdir/foo/file3"
1169+setfattr -n user.equal -v 'this long attribute should remain the same and not need to be transferred' "$chkdir/foo/file3"
1170+
1171+getfattr -d $files >"$scratchdir/xattrs.txt"
1172+
1173+# OK, let's try a simple xattr copy.
1174+checkit "$RSYNC -avX . \"$chkdir/\"" "$fromdir" "$chkdir"
64e98de1 1175+
3fbc83e8
WD
1176+cd "$chkdir"
1177+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
64e98de1 1178+
3fbc83e8
WD
1179+cd "$fromdir"
1180+
1181+checkit "$RSYNC -aiX --copy-dest=../chk . ../to" "$fromdir" "$todir"
1182+
1183+cd "$todir"
1184+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
1185+
1186+cd "$fromdir"
1187+setfattr -n user.nice -v 'this is nice, but different' file1
64e98de1
WD
1188+
1189+getfattr -d $files >"$scratchdir/xattrs.txt"
1190+
3fbc83e8
WD
1191+rm -rf "$todir"
1192+
1193+checkit "$RSYNC -aiX --link-dest=../chk . ../to" "$chkdir" "$todir"
1194+
64e98de1
WD
1195+cd "$todir"
1196+getfattr -d $files | diff $diffopt "$scratchdir/xattrs.txt" -
1197+
1198+# The script would have aborted on error, so getting here means we've won.
1199+exit 0
1200--- old/xattrs.c
1201+++ new/xattrs.c
acf4467b 1202@@ -0,0 +1,775 @@
dc52bc1a
WD
1203+/*
1204+ * Extended Attribute support for rsync.
0f6fb3c8 1205+ * Written by Jay Fenlason, vaguely based on the ACLs patch.
dc52bc1a
WD
1206+ *
1207+ * Copyright (C) 2004 Red Hat, Inc.
64e98de1 1208+ * Copyright (C) 2006, 2007 Wayne Davison
dc52bc1a
WD
1209+ *
1210+ * This program is free software; you can redistribute it and/or modify
64e98de1
WD
1211+ * it under the terms of the GNU General Public License version 2 as
1212+ * published by the Free Software Foundation.
dc52bc1a
WD
1213+ *
1214+ * This program is distributed in the hope that it will be useful,
1215+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1216+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1217+ * GNU General Public License for more details.
1218+ *
1219+ * You should have received a copy of the GNU General Public License along
1220+ * with this program; if not, write to the Free Software Foundation, Inc.,
1221+ * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1222+ */
bbdff76a
WD
1223+
1224+#include "rsync.h"
64e98de1 1225+#include "lib/sysxattrs.h"
bbdff76a
WD
1226+
1227+#ifdef SUPPORT_XATTRS
1228+
bbdff76a 1229+extern int dry_run;
3f24a3a8 1230+extern int am_root;
64e98de1 1231+extern int am_sender;
3fbc83e8 1232+extern int am_generator;
8889f85e
WD
1233+extern int read_only;
1234+extern int list_only;
64e98de1 1235+extern int checksum_seed;
bbdff76a
WD
1236+
1237+#define RSYNC_XAL_INITIAL 5
1238+#define RSYNC_XAL_LIST_INITIAL 100
1239+
64e98de1
WD
1240+#define MAX_FULL_DATUM 32
1241+
922f4409
WD
1242+#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
1243+ && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
1244+
3fbc83e8
WD
1245+#define XATTR_ABBREV(x) ((size_t)((x).name - (x).datum) < (x).datum_len)
1246+
1247+#define XSTATE_ABBREV 0
1248+#define XSTATE_DONE 1
1249+#define XSTATE_TODO 2
1250+#define XSTATE_LOCAL 3
1251+
3f24a3a8 1252+#define USER_PREFIX "user."
27502d8d
WD
1253+#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
1254+#define SYSTEM_PREFIX "system."
1255+#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
1256+
1257+#ifdef HAVE_LINUX_XATTRS
1258+#define RPRE_LEN 0
1259+#else
1260+#define RSYNC_PREFIX "rsync."
1261+#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
1262+#endif
3f24a3a8 1263+
bbdff76a 1264+typedef struct {
27502d8d
WD
1265+ char *datum, *name;
1266+ size_t datum_len, name_len;
bbdff76a
WD
1267+} rsync_xa;
1268+
bbdff76a
WD
1269+static size_t namebuf_len = 0;
1270+static char *namebuf = NULL;
1271+
0f6fb3c8
WD
1272+static item_list empty_xattr = EMPTY_ITEM_LIST;
1273+static item_list rsync_xal_l = EMPTY_ITEM_LIST;
bbdff76a
WD
1274+
1275+/* ------------------------------------------------------------------------- */
1276+
0f6fb3c8 1277+static void rsync_xal_free(item_list *xalp)
bbdff76a
WD
1278+{
1279+ size_t i;
0f6fb3c8 1280+ rsync_xa *rxas = xalp->items;
bbdff76a 1281+
0f6fb3c8 1282+ for (i = 0; i < xalp->count; i++) {
27502d8d
WD
1283+ free(rxas[i].datum);
1284+ /*free(rxas[i].name);*/
bbdff76a 1285+ }
0f6fb3c8 1286+ xalp->count = 0;
bbdff76a
WD
1287+}
1288+
0f6fb3c8 1289+void free_xattr(statx *sxp)
bbdff76a 1290+{
64e98de1
WD
1291+ if (!sxp->xattr)
1292+ return;
0f6fb3c8
WD
1293+ rsync_xal_free(sxp->xattr);
1294+ free(sxp->xattr);
1295+ sxp->xattr = NULL;
1296+}
bbdff76a 1297+
0f6fb3c8
WD
1298+static int rsync_xal_compare_names(const void *x1, const void *x2)
1299+{
1300+ const rsync_xa *xa1 = x1;
1301+ const rsync_xa *xa2 = x2;
bbdff76a
WD
1302+ return strcmp(xa1->name, xa2->name);
1303+}
1304+
64e98de1 1305+static ssize_t get_xattr_names(const char *fname)
bbdff76a 1306+{
526184b9 1307+ ssize_t list_len;
bbdff76a
WD
1308+
1309+ if (!namebuf) {
49de5440 1310+ namebuf_len = 1024;
bbdff76a 1311+ namebuf = new_array(char, namebuf_len);
49de5440 1312+ if (!namebuf)
64e98de1 1313+ out_of_memory("get_xattr_names");
bbdff76a
WD
1314+ }
1315+
49de5440 1316+ /* The length returned includes all the '\0' terminators. */
27502d8d
WD
1317+ list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1318+ if (list_len > (ssize_t)namebuf_len) {
1319+ list_len = -1;
bbdff76a
WD
1320+ errno = ERANGE;
1321+ }
27502d8d 1322+ if (list_len < 0) {
bbdff76a 1323+ if (errno == ENOTSUP)
0f6fb3c8 1324+ return 0;
bbdff76a 1325+ if (errno == ERANGE) {
27502d8d
WD
1326+ list_len = sys_llistxattr(fname, NULL, 0);
1327+ if (list_len < 0) {
892d2287 1328+ rsyserr(FERROR, errno,
64e98de1 1329+ "get_xattr_names: llistxattr(\"%s\",0) failed",
0f6fb3c8 1330+ fname);
e5754c5f 1331+ return -1;
bbdff76a 1332+ }
64e98de1
WD
1333+ if (namebuf_len)
1334+ free(namebuf);
27502d8d 1335+ namebuf_len = list_len + 1024;
64e98de1
WD
1336+ namebuf = new_array(char, namebuf_len);
1337+ if (!namebuf)
1338+ out_of_memory("get_xattr_names");
27502d8d
WD
1339+ list_len = sys_llistxattr(fname, namebuf, namebuf_len);
1340+ if (list_len < 0) {
0f6fb3c8 1341+ rsyserr(FERROR, errno,
64e98de1 1342+ "get_xattr_names: llistxattr(\"%s\",%ld) failed",
892d2287 1343+ fname, (long)namebuf_len);
e5754c5f 1344+ return -1;
bbdff76a
WD
1345+ }
1346+ } else {
0f6fb3c8 1347+ rsyserr(FERROR, errno,
64e98de1 1348+ "get_xattr_names: llistxattr(\"%s\",%ld) failed",
892d2287 1349+ fname, (long)namebuf_len);
e5754c5f 1350+ return -1;
bbdff76a
WD
1351+ }
1352+ }
27502d8d 1353+
526184b9
WD
1354+ return list_len;
1355+}
1356+
64e98de1
WD
1357+/* On entry, the *len_ptr parameter contains the size of the extra space we
1358+ * should allocate when we create a buffer for the data. On exit, it contains
1359+ * the length of the datum. */
3fbc83e8
WD
1360+static char *get_xattr_data(const char *fname, const char *name, size_t *len_ptr,
1361+ int no_missing_error)
64e98de1
WD
1362+{
1363+ size_t datum_len = sys_lgetxattr(fname, name, NULL, 0);
1364+ char *ptr;
1365+
1366+ if (datum_len == (size_t)-1) {
3fbc83e8 1367+ if (errno == ENOTSUP || no_missing_error)
64e98de1
WD
1368+ return NULL;
1369+ rsyserr(FERROR, errno,
1370+ "get_xattr_data: lgetxattr(\"%s\",\"%s\",0) failed",
1371+ fname, name);
1372+ return NULL;
1373+ }
1374+
64e98de1
WD
1375+ if (datum_len + *len_ptr < datum_len /* checks for overflow */
1376+ || !(ptr = new_array(char, datum_len + *len_ptr)))
1377+ out_of_memory("get_xattr_data");
1378+
9c18601b
WD
1379+ *len_ptr = datum_len;
1380+
64e98de1
WD
1381+ if (datum_len) {
1382+ size_t len = sys_lgetxattr(fname, name, ptr, datum_len);
1383+ if (len != datum_len) {
1384+ if (len == (size_t)-1) {
1385+ rsyserr(FERROR, errno,
1386+ "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1387+ " failed", fname, name, (long)datum_len);
1388+ } else {
1389+ rprintf(FERROR,
1390+ "get_xattr_data: lgetxattr(\"%s\",\"%s\",%ld)"
1391+ " returned %ld\n", fname, name,
1392+ (long)datum_len, (long)len);
1393+ }
1394+ free(ptr);
1395+ return NULL;
1396+ }
1397+ }
1398+
1399+ return ptr;
1400+}
1401+
526184b9
WD
1402+static int rsync_xal_get(const char *fname, item_list *xalp)
1403+{
64e98de1
WD
1404+ ssize_t list_len, name_len;
1405+ size_t datum_len, name_offset;
526184b9 1406+ char *name, *ptr;
64e98de1 1407+ int user_only = am_sender ? 0 : !am_root;
526184b9
WD
1408+
1409+ /* This puts the name list into the "namebuf" buffer. */
64e98de1 1410+ if ((list_len = get_xattr_names(fname)) < 0)
526184b9
WD
1411+ return -1;
1412+
892d2287 1413+ for (name = namebuf; list_len > 0; name += name_len) {
3f24a3a8 1414+ rsync_xa *rxas;
bbdff76a 1415+
27502d8d 1416+ name_len = strlen(name) + 1;
892d2287 1417+ list_len -= name_len;
3f24a3a8
WD
1418+
1419+#ifdef HAVE_LINUX_XATTRS
64e98de1
WD
1420+ /* We always ignore the system namespace, and non-root
1421+ * ignores everything but the user namespace. */
1422+ if (user_only ? !HAS_PREFIX(name, USER_PREFIX)
1423+ : HAS_PREFIX(name, SYSTEM_PREFIX))
3f24a3a8
WD
1424+ continue;
1425+#endif
1426+
64e98de1 1427+ datum_len = name_len; /* Pass extra size to get_xattr_data() */
3fbc83e8 1428+ if (!(ptr = get_xattr_data(fname, name, &datum_len, 0)))
49de5440 1429+ return -1;
64e98de1
WD
1430+
1431+ if (datum_len > MAX_FULL_DATUM) {
1432+ /* For large datums, we store a flag and a checksum. */
1433+ name_offset = 1 + MAX_DIGEST_LEN;
1434+ sum_init(checksum_seed);
1435+ sum_update(ptr, datum_len);
1436+ free(ptr);
1437+
1438+ if (!(ptr = new_array(char, name_offset + name_len)))
1439+ out_of_memory("rsync_xal_get");
3fbc83e8 1440+ *ptr = XSTATE_ABBREV;
64e98de1
WD
1441+ sum_end(ptr + 1);
1442+ } else
1443+ name_offset = datum_len;
1444+
27502d8d 1445+ rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
64e98de1
WD
1446+ rxas->name = ptr + name_offset;
1447+ memcpy(rxas->name, name, name_len);
b54d5ec2 1448+ rxas->datum = ptr;
27502d8d
WD
1449+ rxas->name_len = name_len;
1450+ rxas->datum_len = datum_len;
bbdff76a 1451+ }
0f6fb3c8
WD
1452+ if (xalp->count > 1)
1453+ qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
e5754c5f 1454+ return 0;
bbdff76a
WD
1455+}
1456+
0f6fb3c8
WD
1457+/* Read the xattr(s) for this filename. */
1458+int get_xattr(const char *fname, statx *sxp)
bbdff76a 1459+{
0f6fb3c8
WD
1460+ sxp->xattr = new(item_list);
1461+ *sxp->xattr = empty_xattr;
1462+ if (rsync_xal_get(fname, sxp->xattr) < 0) {
1463+ free_xattr(sxp);
1464+ return -1;
1465+ }
1466+ return 0;
bbdff76a
WD
1467+}
1468+
0f6fb3c8 1469+static int find_matching_xattr(item_list *xalp)
bbdff76a 1470+{
0f6fb3c8
WD
1471+ size_t i, j;
1472+ item_list *lst = rsync_xal_l.items;
bbdff76a
WD
1473+
1474+ for (i = 0; i < rsync_xal_l.count; i++) {
0f6fb3c8
WD
1475+ rsync_xa *rxas1 = lst[i].items;
1476+ rsync_xa *rxas2 = xalp->items;
1477+
bbdff76a 1478+ /* Wrong number of elements? */
0f6fb3c8 1479+ if (lst[i].count != xalp->count)
bbdff76a
WD
1480+ continue;
1481+ /* any elements different? */
0f6fb3c8
WD
1482+ for (j = 0; j < xalp->count; j++) {
1483+ if (rxas1[j].name_len != rxas2[j].name_len
1484+ || rxas1[j].datum_len != rxas2[j].datum_len
64e98de1 1485+ || strcmp(rxas1[j].name, rxas2[j].name))
bbdff76a 1486+ break;
64e98de1
WD
1487+ if (rxas1[j].datum_len > MAX_FULL_DATUM) {
1488+ if (memcmp(rxas1[j].datum + 1,
1489+ rxas2[j].datum + 1,
1490+ MAX_DIGEST_LEN) != 0)
1491+ break;
1492+ } else {
1493+ if (memcmp(rxas1[j].datum, rxas2[j].datum,
1494+ rxas2[j].datum_len))
1495+ break;
1496+ }
bbdff76a
WD
1497+ }
1498+ /* no differences found. This is The One! */
0f6fb3c8
WD
1499+ if (j == xalp->count)
1500+ return i;
bbdff76a 1501+ }
0f6fb3c8
WD
1502+
1503+ return -1;
bbdff76a
WD
1504+}
1505+
0f6fb3c8
WD
1506+/* Store *xalp on the end of rsync_xal_l */
1507+static void rsync_xal_store(item_list *xalp)
bbdff76a 1508+{
0f6fb3c8 1509+ item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
de565f59
WD
1510+ /* Since the following call starts a new list, we know it will hold the
1511+ * entire initial-count, not just enough space for one new item. */
49de5440
WD
1512+ *new_lst = empty_xattr;
1513+ (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
1514+ memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
0f6fb3c8
WD
1515+ new_lst->count = xalp->count;
1516+ xalp->count = 0;
bbdff76a
WD
1517+}
1518+
0f6fb3c8 1519+/* Send the make_xattr()-generated xattr list for this flist entry. */
64e98de1 1520+int send_xattr(statx *sxp, int f)
bbdff76a 1521+{
0f6fb3c8 1522+ int ndx = find_matching_xattr(sxp->xattr);
64e98de1
WD
1523+
1524+ /* Send 0 (-1 + 1) to indicate that literal xattr data follows. */
1525+ write_abbrevint(f, ndx + 1);
1526+
1527+ if (ndx < 0) {
bbdff76a 1528+ rsync_xa *rxa;
0f6fb3c8 1529+ int count = sxp->xattr->count;
64e98de1 1530+ write_abbrevint(f, count);
0f6fb3c8 1531+ for (rxa = sxp->xattr->items; count--; rxa++) {
27502d8d 1532+#ifdef HAVE_LINUX_XATTRS
64e98de1
WD
1533+ write_abbrevint(f, rxa->name_len);
1534+ write_abbrevint(f, rxa->datum_len);
27502d8d
WD
1535+ write_buf(f, rxa->name, rxa->name_len);
1536+#else
1537+ /* We strip the rsync prefix from disguised namespaces
1538+ * and put everything else in the user namespace. */
dfe2b257
WD
1539+ if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
1540+ && rxa->name[RPRE_LEN] != '%') {
64e98de1
WD
1541+ write_abbrevint(f, rxa->name_len - RPRE_LEN);
1542+ write_abbrevint(f, rxa->datum_len);
27502d8d
WD
1543+ write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
1544+ } else {
64e98de1
WD
1545+ write_abbrevint(f, rxa->name_len + UPRE_LEN);
1546+ write_abbrevint(f, rxa->datum_len);
3f24a3a8 1547+ write_buf(f, USER_PREFIX, UPRE_LEN);
27502d8d 1548+ write_buf(f, rxa->name, rxa->name_len);
3f24a3a8 1549+ }
27502d8d 1550+#endif
64e98de1
WD
1551+ if (rxa->datum_len > MAX_FULL_DATUM)
1552+ write_buf(f, rxa->datum + 1, MAX_DIGEST_LEN);
1553+ else
1554+ write_buf(f, rxa->datum, rxa->datum_len);
bbdff76a 1555+ }
64e98de1 1556+ ndx = rsync_xal_l.count; /* pre-incremented count */
49de5440 1557+ rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
bbdff76a 1558+ }
64e98de1
WD
1559+
1560+ return ndx;
1561+}
1562+
1563+/* Return a flag indicating if we need to change a file's xattrs. If
1564+ * "find_all" is specified, also mark any abbreviated xattrs that we
1565+ * need so that send_xattr_request() can tell the sender about them. */
1566+int xattr_diff(struct file_struct *file, statx *sxp, int find_all)
1567+{
1568+ item_list *lst = rsync_xal_l.items;
1569+ rsync_xa *snd_rxa, *rec_rxa;
1570+ int snd_cnt, rec_cnt;
1571+ int cmp, same, xattrs_equal = 1;
1572+
1573+ if (!XATTR_READY(*sxp)) {
1574+ rec_rxa = NULL;
1575+ rec_cnt = 0;
1576+ } else {
1577+ rec_rxa = sxp->xattr->items;
1578+ rec_cnt = sxp->xattr->count;
1579+ }
1580+
1581+ if (F_XATTR(file) >= 0)
1582+ lst += F_XATTR(file);
1583+ else
1584+ lst = &empty_xattr;
1585+
1586+ snd_rxa = lst->items;
1587+ snd_cnt = lst->count;
1588+
1589+ /* If the count of the sender's xattrs is different from our
1590+ * (receiver's) xattrs, the lists are not the same. */
1591+ if (snd_cnt != rec_cnt) {
1592+ if (!find_all)
1593+ return 1;
1594+ xattrs_equal = 0;
1595+ }
1596+
1597+ while (snd_cnt) {
1598+ cmp = rec_cnt ? strcmp(snd_rxa->name, rec_rxa->name) : -1;
1599+ if (cmp > 0)
1600+ same = 0;
1601+ else if (snd_rxa->datum_len > MAX_FULL_DATUM) {
1602+ same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1603+ && memcmp(snd_rxa->datum + 1, rec_rxa->datum + 1,
1604+ MAX_DIGEST_LEN) == 0;
1605+ /* Flag unrequested items that we need. */
3fbc83e8
WD
1606+ if (!same && find_all && snd_rxa->datum[0] == XSTATE_ABBREV)
1607+ snd_rxa->datum[0] = XSTATE_TODO;
64e98de1
WD
1608+ } else {
1609+ same = cmp == 0 && snd_rxa->datum_len == rec_rxa->datum_len
1610+ && memcmp(snd_rxa->datum, rec_rxa->datum,
1611+ snd_rxa->datum_len) == 0;
1612+ }
1613+ if (!same) {
1614+ if (!find_all)
1615+ return 1;
1616+ xattrs_equal = 0;
1617+ }
1618+
1619+ if (cmp <= 0) {
1620+ snd_rxa++;
1621+ snd_cnt--;
1622+ }
1623+ if (cmp >= 0) {
1624+ rec_rxa++;
1625+ rec_cnt--;
1626+ }
1627+ }
1628+
1629+ if (rec_cnt)
1630+ xattrs_equal = 0;
1631+
1632+ return !xattrs_equal;
1633+}
1634+
1635+/* When called by the generator with a NULL fname, this tells the sender
1636+ * which abbreviated xattr values we need. When called by the sender
9c18601b 1637+ * (with a non-NULL fname), we send all the extra xattr data it needs. */
64e98de1
WD
1638+void send_xattr_request(const char *fname, struct file_struct *file, int f_out)
1639+{
1640+ item_list *lst = rsync_xal_l.items;
1641+ int j, cnt, prior_req = -1;
1642+ rsync_xa *rxa;
1643+
1644+ lst += F_XATTR(file);
1645+ cnt = lst->count;
1646+ for (rxa = lst->items, j = 0; j < cnt; rxa++, j++) {
3fbc83e8 1647+ if (rxa->datum_len <= MAX_FULL_DATUM)
64e98de1 1648+ continue;
3fbc83e8
WD
1649+ switch (rxa->datum[0]) {
1650+ case XSTATE_LOCAL:
1651+ /* Items set locally will get cached by receiver. */
1652+ rxa->datum[0] = XSTATE_DONE;
1653+ continue;
1654+ case XSTATE_TODO:
1655+ break;
1656+ default:
1657+ continue;
1658+ }
64e98de1
WD
1659+
1660+ /* Flag that we handled this abbreviated item. */
3fbc83e8 1661+ rxa->datum[0] = XSTATE_DONE;
64e98de1
WD
1662+
1663+ write_abbrevint(f_out, j - prior_req);
1664+ prior_req = j;
1665+
1666+ if (fname) {
1667+ size_t len = 0;
1668+ char *ptr;
1669+
1670+ /* Re-read the long datum. */
3fbc83e8 1671+ if (!(ptr = get_xattr_data(fname, rxa->name, &len, 0)))
64e98de1
WD
1672+ continue;
1673+
1674+ write_abbrevint(f_out, len); /* length might have changed! */
1675+ write_buf(f_out, ptr, len);
1676+ free(ptr);
1677+ }
1678+ }
1679+
1680+ write_byte(f_out, 0); /* end the list */
1681+}
1682+
3fbc83e8
WD
1683+/* Any items set locally by the generator that the receiver doesn't
1684+ * get told about get changed back to XSTATE_ABBREV. */
1685+void xattr_clear_locals(struct file_struct *file)
1686+{
1687+ item_list *lst = rsync_xal_l.items;
1688+ rsync_xa *rxa;
1689+ int cnt;
1690+
1691+ if (F_XATTR(file) < 0)
1692+ return;
1693+
1694+ lst += F_XATTR(file);
1695+ cnt = lst->count;
1696+ for (rxa = lst->items; cnt--; rxa++) {
1697+ if (rxa->datum_len <= MAX_FULL_DATUM)
1698+ continue;
1699+ if (rxa->datum[0] == XSTATE_LOCAL)
1700+ rxa->datum[0] = XSTATE_ABBREV;
1701+ }
1702+}
1703+
9c18601b
WD
1704+/* When called by the sender, read the request from the generator and mark
1705+ * any needed xattrs with a flag that lets us know they need to be sent to
1706+ * the receiver. When called by the receiver, reads the sent data and
1707+ * stores it in place of its checksum. */
64e98de1
WD
1708+void recv_xattr_request(struct file_struct *file, int f_in)
1709+{
1710+ item_list *lst = rsync_xal_l.items;
1711+ char *old_datum, *name;
1712+ rsync_xa *rxa;
1713+ int rel_pos, cnt;
1714+
3fbc83e8 1715+ if (F_XATTR(file) < 0)
64e98de1 1716+ exit_cleanup(RERR_STREAMIO); /* XXX */
3fbc83e8 1717+ lst += F_XATTR(file);
64e98de1
WD
1718+
1719+ cnt = lst->count;
1720+ rxa = lst->items;
1721+ rxa -= 1;
1722+ while ((rel_pos = read_abbrevint(f_in)) != 0) {
1723+ rxa += rel_pos;
1724+ cnt -= rel_pos;
1725+ if (cnt < 0 || rxa->datum_len <= MAX_FULL_DATUM
3fbc83e8 1726+ || rxa->datum[0] != XSTATE_ABBREV)
64e98de1
WD
1727+ exit_cleanup(RERR_STREAMIO); /* XXX */
1728+
1729+ if (am_sender) {
3fbc83e8 1730+ rxa->datum[0] = XSTATE_TODO;
64e98de1
WD
1731+ continue;
1732+ }
1733+
1734+ old_datum = rxa->datum;
1735+ rxa->datum_len = read_abbrevint(f_in);
1736+
1737+ if (rxa->name_len + rxa->datum_len < rxa->name_len)
1738+ out_of_memory("recv_xattr_request"); /* overflow */
1739+ rxa->datum = new_array(char, rxa->datum_len + rxa->name_len);
1740+ if (!rxa->datum)
1741+ out_of_memory("recv_xattr_request");
1742+ name = rxa->datum + rxa->datum_len;
1743+ memcpy(name, rxa->name, rxa->name_len);
1744+ rxa->name = name;
1745+ free(old_datum);
1746+ read_buf(f_in, rxa->datum, rxa->datum_len);
1747+ }
bbdff76a
WD
1748+}
1749+
bbdff76a 1750+/* ------------------------------------------------------------------------- */
bbdff76a 1751+
0f6fb3c8 1752+/* receive and build the rsync_xattr_lists */
bbdff76a
WD
1753+void receive_xattr(struct file_struct *file, int f)
1754+{
0f6fb3c8 1755+ static item_list temp_xattr = EMPTY_ITEM_LIST;
64e98de1
WD
1756+ int count;
1757+ int ndx = read_abbrevint(f);
1758+
1759+ if (ndx < 0 || (size_t)ndx > rsync_xal_l.count) {
1760+ rprintf(FERROR, "receive_xattr: xa index %d out of"
1761+ " range for %s\n", ndx, f_name(file, NULL));
1762+ exit_cleanup(RERR_STREAMIO);
1763+ }
1764+
1765+ if (ndx != 0) {
1766+ F_XATTR(file) = ndx - 1;
1767+ return;
1768+ }
1769+
1770+ if ((count = read_abbrevint(f)) != 0) {
1771+ (void)EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
1772+ temp_xattr.count = 0;
1773+ }
1774+
1775+ while (count--) {
1776+ char *ptr, *name;
1777+ rsync_xa *rxa;
1778+ size_t name_len = read_abbrevint(f);
1779+ size_t datum_len = read_abbrevint(f);
1780+ size_t dget_len = datum_len > MAX_FULL_DATUM ? 1 + MAX_DIGEST_LEN : datum_len;
bcecb5e3 1781+#ifdef HAVE_LINUX_XATTRS
64e98de1 1782+ size_t extra_len = 0;
bcecb5e3 1783+#else
64e98de1
WD
1784+ size_t extra_len = am_root ? RPRE_LEN : 0;
1785+ if (dget_len + extra_len < dget_len)
1786+ out_of_memory("receive_xattr"); /* overflow */
27502d8d 1787+#endif
64e98de1
WD
1788+ if (dget_len + extra_len + name_len < dget_len)
1789+ out_of_memory("receive_xattr"); /* overflow */
1790+ ptr = new_array(char, dget_len + extra_len + name_len);
1791+ if (!ptr)
1792+ out_of_memory("receive_xattr");
1793+ name = ptr + dget_len + extra_len;
1794+ read_buf(f, name, name_len);
1795+ if (dget_len == datum_len)
1796+ read_buf(f, ptr, dget_len);
1797+ else {
3fbc83e8 1798+ *ptr = XSTATE_ABBREV;
64e98de1
WD
1799+ read_buf(f, ptr + 1, MAX_DIGEST_LEN);
1800+ }
3f24a3a8 1801+#ifdef HAVE_LINUX_XATTRS
64e98de1
WD
1802+ /* Non-root can only save the user namespace. */
1803+ if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
1804+ free(ptr);
1805+ continue;
bbdff76a 1806+ }
64e98de1
WD
1807+#else
1808+ /* This OS only has a user namespace, so we either
1809+ * strip the user prefix, or we put a non-user
1810+ * namespace inside our rsync hierarchy. */
1811+ if (HAS_PREFIX(name, USER_PREFIX)) {
1812+ name += UPRE_LEN;
1813+ name_len -= UPRE_LEN;
1814+ } else if (am_root) {
1815+ name -= RPRE_LEN;
1816+ name_len += RPRE_LEN;
1817+ memcpy(name, RSYNC_PREFIX, RPRE_LEN);
1818+ } else {
1819+ free(ptr);
1820+ continue;
bbdff76a 1821+ }
64e98de1
WD
1822+#endif
1823+ rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, 1);
1824+ rxa->name = name;
1825+ rxa->datum = ptr;
1826+ rxa->name_len = name_len;
1827+ rxa->datum_len = datum_len;
bbdff76a 1828+ }
bbdff76a 1829+
64e98de1
WD
1830+ ndx = rsync_xal_l.count; /* pre-incremented count */
1831+ rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
1832+
70891d26 1833+ F_XATTR(file) = ndx;
bbdff76a
WD
1834+}
1835+
0f6fb3c8
WD
1836+/* Turn the xattr data in statx into cached xattr data, setting the index
1837+ * values in the file struct. */
1838+void cache_xattr(struct file_struct *file, statx *sxp)
bbdff76a 1839+{
0f6fb3c8 1840+ int ndx;
bbdff76a 1841+
0f6fb3c8
WD
1842+ if (!sxp->xattr)
1843+ return;
bbdff76a 1844+
0f6fb3c8 1845+ ndx = find_matching_xattr(sxp->xattr);
64e98de1 1846+ if (ndx < 0)
49de5440 1847+ rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
bbdff76a 1848+
70891d26 1849+ F_XATTR(file) = ndx;
bbdff76a
WD
1850+}
1851+
acf4467b
WD
1852+static int rsync_xal_set(const char *fname, item_list *xalp,
1853+ const char *fnamecmp, statx *sxp)
bbdff76a 1854+{
0f6fb3c8 1855+ rsync_xa *rxas = xalp->items;
526184b9 1856+ ssize_t list_len;
3fbc83e8
WD
1857+ size_t i, len;
1858+ char *name, *ptr, sum[MAX_DIGEST_LEN];
1859+ int name_len, ret = 0;
526184b9
WD
1860+
1861+ /* This puts the current name list into the "namebuf" buffer. */
64e98de1 1862+ if ((list_len = get_xattr_names(fname)) < 0)
526184b9 1863+ return -1;
bbdff76a 1864+
0f6fb3c8 1865+ for (i = 0; i < xalp->count; i++) {
3fbc83e8
WD
1866+ name = rxas[i].name;
1867+
1868+ if (XATTR_ABBREV(rxas[i])) {
1869+ /* See if the fnamecmp version is identical. */
1870+ len = name_len = rxas[i].name_len;
1871+ if ((ptr = get_xattr_data(fnamecmp, name, &len, 1)) == NULL) {
1872+ still_abbrev:
1873+ if (am_generator)
1874+ continue;
3e6568cf
WD
1875+ rprintf(FERROR, "Missing abbreviated xattr value, %s, for %s\n",
1876+ rxas[i].name, full_fname(fname));
1877+ ret = -1;
1878+ continue;
1879+ }
3fbc83e8
WD
1880+ if (len != rxas[i].datum_len) {
1881+ free(ptr);
1882+ goto still_abbrev;
1883+ }
1884+
1885+ sum_init(checksum_seed);
1886+ sum_update(ptr, len);
1887+ sum_end(sum);
1888+ if (memcmp(sum, rxas[i].datum + 1, MAX_DIGEST_LEN) != 0) {
1889+ free(ptr);
1890+ goto still_abbrev;
1891+ }
1892+
acf4467b
WD
1893+ if (fname == fnamecmp)
1894+ ; /* Value is already set when identical */
1895+ else if (sys_lsetxattr(fname, name, ptr, len) < 0) {
3fbc83e8
WD
1896+ rsyserr(FERROR, errno,
1897+ "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1898+ fname, name);
1899+ ret = -1;
acf4467b
WD
1900+ } else /* make sure caller sets mtime */
1901+ sxp->st.st_mtime = (time_t)-1;
3fbc83e8
WD
1902+
1903+ if (am_generator) { /* generator items stay abbreviated */
1904+ if (rxas[i].datum[0] == XSTATE_ABBREV)
1905+ rxas[i].datum[0] = XSTATE_LOCAL;
1906+ free(ptr);
3e6568cf 1907+ continue;
3fbc83e8
WD
1908+ }
1909+
1910+ memcpy(ptr + len, name, name_len);
1911+ free(rxas[i].datum);
1912+
1913+ rxas[i].name = name = ptr + len;
1914+ rxas[i].datum = ptr;
1915+ continue;
64e98de1 1916+ }
3fbc83e8
WD
1917+
1918+ if (sys_lsetxattr(fname, name, rxas[i].datum, rxas[i].datum_len) < 0) {
892d2287
WD
1919+ rsyserr(FERROR, errno,
1920+ "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
3fbc83e8 1921+ fname, name);
0f6fb3c8 1922+ ret = -1;
acf4467b
WD
1923+ } else /* make sure caller sets mtime */
1924+ sxp->st.st_mtime = (time_t)-1;
bbdff76a 1925+ }
526184b9
WD
1926+
1927+ /* Remove any extraneous names. */
1928+ for (name = namebuf; list_len > 0; name += name_len) {
1929+ name_len = strlen(name) + 1;
1930+ list_len -= name_len;
1931+
e3cd70c8
WD
1932+#ifdef HAVE_LINUX_XATTRS
1933+ /* We always ignore the system namespace, and non-root
1934+ * ignores everything but the user namespace. */
1935+ if (am_root ? HAS_PREFIX(name, SYSTEM_PREFIX)
1936+ : !HAS_PREFIX(name, USER_PREFIX))
1937+ continue;
1938+#endif
1939+
526184b9
WD
1940+ for (i = 0; i < xalp->count; i++) {
1941+ if (strcmp(name, rxas[i].name) == 0)
1942+ break;
1943+ }
1944+ if (i == xalp->count) {
3fbc83e8 1945+ if (sys_lremovexattr(fname, name) < 0) {
526184b9
WD
1946+ rsyserr(FERROR, errno,
1947+ "rsync_xal_clear: lremovexattr(\"%s\",\"%s\") failed",
1948+ fname, name);
1949+ ret = -1;
acf4467b
WD
1950+ } else /* make sure caller sets mtime */
1951+ sxp->st.st_mtime = (time_t)-1;
526184b9
WD
1952+ }
1953+ }
1954+
0f6fb3c8 1955+ return ret;
bbdff76a
WD
1956+}
1957+
0f6fb3c8 1958+/* Set extended attributes on indicated filename. */
acf4467b
WD
1959+int set_xattr(const char *fname, const struct file_struct *file,
1960+ const char *fnamecmp, statx *sxp)
bbdff76a 1961+{
0f6fb3c8 1962+ int ndx;
0f6fb3c8 1963+ item_list *lst = rsync_xal_l.items;
bbdff76a 1964+
93d6ca6d
WD
1965+ if (dry_run)
1966+ return 1; /* FIXME: --dry-run needs to compute this value */
1967+
8889f85e
WD
1968+ if (read_only || list_only) {
1969+ errno = EROFS;
1970+ return -1;
1971+ }
1972+
70891d26 1973+ ndx = F_XATTR(file);
acf4467b 1974+ return rsync_xal_set(fname, lst + ndx, fnamecmp, sxp);
bbdff76a
WD
1975+}
1976+
1977+#endif /* SUPPORT_XATTRS */