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