- Updated to apply over the new ACL patch.
[rsync/rsync-patches.git] / xattrs.diff
CommitLineData
bbdff76a
WD
1Depends-On-Patch: acls.diff
2
3After applying this patch, run these commands for a successful build:
4
27e96866 5 ./prepare-source
f787c90c 6 ./configure --enable-acl-support --enable-xattr-support
bbdff76a
WD
7 make
8
0f6fb3c8
WD
9TODO:
10
11 - This patch needs to be rewritten to more efficiently handle large xattrs.
12
13 - We need to affect the itemized output to know when xattrs are being updated.
14
15 - We need to affect the --link-dest option to avoid hard-linking two files
16 that differ in their xattrs (when --xattrs was specified).
17
9a7eef96
WD
18--- old/Makefile.in
19+++ new/Makefile.in
4bf6f8c7 20@@ -27,13 +27,13 @@ VERSION=@VERSION@
bbdff76a
WD
21
22 HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
23 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
24- lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
25+ lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattr.o @LIBOBJS@
4bf6f8c7
WD
26 ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
27 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
bbdff76a
WD
28 OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
29 main.o checksum.o match.o syscall.o log.o backup.o
30 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
610969d1
WD
31- fileio.o batch.o clientname.o chmod.o acls.o
32+ fileio.o batch.o clientname.o chmod.o acls.o xattr.o
bbdff76a
WD
33 OBJS3=progress.o pipe.o
34 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
35 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
0f6fb3c8
WD
36--- old/acls.c
37+++ new/acls.c
38@@ -29,6 +29,7 @@ extern int am_root;
39 extern int dry_run;
40 extern int orig_umask;
41 extern int preserve_acls;
42+extern int preserve_xattrs;
43 extern unsigned int file_struct_len;
44
45 /* === ACL structures === */
46@@ -749,6 +750,10 @@ void receive_acl(struct file_struct *fil
47 type = SMB_ACL_TYPE_ACCESS;
48 racl_list = &access_acl_list;
49 ndx_ptr = (char*)file + file_struct_len;
50+#ifdef SUPPORT_XATTRS
51+ if (preserve_xattrs)
52+ ndx_ptr += 4;
53+#endif
54 do {
55 char tag = read_byte(f);
56 int ndx;
57@@ -807,6 +812,10 @@ void cache_acl(struct file_struct *file,
58 racl = sxp->acc_acl;
59 racl_list = &access_acl_list;
60 ndx_ptr = (char*)file + file_struct_len;
61+#ifdef SUPPORT_XATTRS
62+ if (preserve_xattrs)
63+ ndx_ptr += 4;
64+#endif
65 do {
66 if (!racl)
67 ndx = -1;
68@@ -922,6 +931,10 @@ int set_acl(const char *fname, const str
69
70 type = SMB_ACL_TYPE_ACCESS;
71 ndx_ptr = (char*)file + file_struct_len;
72+#ifdef SUPPORT_XATTRS
73+ if (preserve_xattrs)
74+ ndx_ptr += 4;
75+#endif
76 do {
77 acl_duo *duo_item;
78 BOOL eq;
9a7eef96
WD
79--- old/backup.c
80+++ new/backup.c
dc52bc1a 81@@ -30,6 +30,7 @@ extern char *backup_dir;
93d6ca6d
WD
82
83 extern int am_root;
84 extern int preserve_acls;
85+extern int preserve_xattrs;
86 extern int preserve_devices;
87 extern int preserve_specials;
88 extern int preserve_links;
0f6fb3c8
WD
89@@ -136,6 +137,9 @@ static int make_bak_dir(char *fullpath)
90 #ifdef SUPPORT_ACLS
91 sx.acc_acl = sx.def_acl = NULL;
92 #endif
93+#ifdef SUPPORT_XATTRS
94+ sx.xattr = NULL;
95+#endif
96 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
97 continue;
98 #ifdef SUPPORT_ACLS
99@@ -144,6 +148,12 @@ static int make_bak_dir(char *fullpath)
100 cache_acl(file, &sx);
101 }
09cc6650
WD
102 #endif
103+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
104+ if (preserve_xattrs) {
105+ get_xattr(rel, &sx);
106+ cache_xattr(file, &sx);
107+ }
09cc6650 108+#endif
0f6fb3c8
WD
109 set_file_attrs(fullpath, file, NULL, 0);
110 free(file);
bbdff76a 111 }
0f6fb3c8
WD
112@@ -195,6 +205,9 @@ static int keep_backup(char *fname)
113 #ifdef SUPPORT_ACLS
114 sx.acc_acl = sx.def_acl = NULL;
09cc6650
WD
115 #endif
116+#ifdef SUPPORT_XATTRS
0f6fb3c8 117+ sx.xattr = NULL;
09cc6650 118+#endif
bbdff76a 119
0f6fb3c8
WD
120 if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
121 return 1; /* the file could have disappeared */
122@@ -208,6 +221,12 @@ static int keep_backup(char *fname)
123 cache_acl(file, &sx);
124 }
09cc6650
WD
125 #endif
126+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
127+ if (preserve_xattrs) {
128+ get_xattr(fname, &sx);
129+ cache_xattr(file, &sx);
130+ }
09cc6650 131+#endif
bbdff76a 132
0f6fb3c8
WD
133 /* Check to see if this is a device file, or link */
134 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
9a7eef96
WD
135--- old/configure.in
136+++ new/configure.in
3fcc541f 137@@ -823,6 +823,30 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
bbdff76a
WD
138 AC_MSG_RESULT(no)
139 )
140
141+AC_CHECK_HEADERS(attr/xattr.h)
142+AC_MSG_CHECKING(whether to support extended attributes)
f787c90c
WD
143+AC_ARG_ENABLE(xattr-support,
144+AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
3b05e91f 145+[ case "$enableval" in
bbdff76a
WD
146+ yes)
147+ case "$host_os" in
148+ *linux*)
149+ AC_MSG_RESULT(Using Linux xattrs)
150+ AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
151+ ;;
152+ *)
153+ AC_MSG_RESULT(Xattrs requested but not linux. Good luck)
154+ ;;
155+ esac
156+ ;;
157+ *)
158+ AC_MSG_RESULT(no)
159+ AC_DEFINE(HAVE_NA_XATTRS, 1, [True if you don't have extended attributes])
160+ esac ],
161+ AC_MSG_RESULT(no)
162+ AC_DEFINE(HAVE_NO_XATTRL, 1, [True if you don't have extended attributes])
163+)
164+
165 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
166 AC_OUTPUT
167
9a7eef96
WD
168--- old/flist.c
169+++ new/flist.c
dc52bc1a 170@@ -41,6 +41,7 @@ extern int one_file_system;
93d6ca6d
WD
171 extern int copy_dirlinks;
172 extern int keep_dirlinks;
173 extern int preserve_acls;
174+extern int preserve_xattrs;
175 extern int preserve_links;
176 extern int preserve_hard_links;
177 extern int preserve_devices;
0f6fb3c8
WD
178@@ -502,7 +503,7 @@ static struct file_struct *receive_file_
179 char thisname[MAXPATHLEN];
180 unsigned int l1 = 0, l2 = 0;
181 int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
182-#ifdef SUPPORT_ACLS
183+#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
184 int xtra_len;
185 #endif
186 OFF_T file_length;
187@@ -614,10 +615,16 @@ static struct file_struct *receive_file_
188 xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4;
189 else
190 xtra_len = 0;
191+#elif defined SUPPORT_XATTRS
192+ xtra_len = 0;
193+#endif
194+#ifdef SUPPORT_XATTRS
195+ if (preserve_xattrs)
196+ xtra_len += 4;
197 #endif
198
199 alloc_len = file_struct_len + dirname_len + basename_len
200-#ifdef SUPPORT_ACLS
201+#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
202 + xtra_len
203 #endif
204 + linkname_len + sum_len;
205@@ -626,7 +633,7 @@ static struct file_struct *receive_file_
206 file = (struct file_struct *)bp;
207 memset(bp, 0, file_struct_len);
208 bp += file_struct_len;
209-#ifdef SUPPORT_ACLS
210+#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
211 bp += xtra_len;
212 #endif
213
214@@ -727,6 +734,10 @@ static struct file_struct *receive_file_
215 if (preserve_acls)
216 receive_acl(file, f);
217 #endif
218+#ifdef SUPPORT_XATTRS
219+ if (preserve_xattrs)
220+ receive_xattr(file, f );
221+#endif
222
223 return file;
224 }
225@@ -997,6 +1008,13 @@ static struct file_struct *send_file_nam
226 return NULL;
227 }
09cc6650
WD
228 #endif
229+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
230+ if (preserve_xattrs) {
231+ sx.xattr = NULL;
232+ if (get_xattr(fname, &sx) < 0)
233+ return NULL;
234+ }
09cc6650 235+#endif
bbdff76a 236
a2f9a486
WD
237 maybe_emit_filelist_progress(flist->count + flist_count_offset);
238
0f6fb3c8 239@@ -1009,11 +1027,19 @@ static struct file_struct *send_file_nam
93d6ca6d 240 if (preserve_acls)
0f6fb3c8 241 send_acl(&sx, f);
93d6ca6d 242 #endif
09cc6650 243+#ifdef SUPPORT_XATTRS
93d6ca6d 244+ if (preserve_xattrs)
0f6fb3c8 245+ send_xattr(&sx, f);
09cc6650 246+#endif
bbdff76a 247 } else {
93d6ca6d 248 #ifdef SUPPORT_ACLS
93d6ca6d 249 if (preserve_acls)
0f6fb3c8 250 free_acl(&sx);
09cc6650
WD
251 #endif
252+#ifdef SUPPORT_XATTRS
93d6ca6d 253+ if (preserve_xattrs)
0f6fb3c8 254+ free_xattr(&sx);
09cc6650 255+#endif
bbdff76a
WD
256 }
257 return file;
258 }
9a7eef96
WD
259--- old/lib/sysxattr.c
260+++ new/lib/sysxattr.c
0f6fb3c8 261@@ -0,0 +1,44 @@
dc52bc1a
WD
262+/*
263+ * Extended attribute support for rsync.
264+ *
265+ * Copyright (C) 2004 Red Hat, Inc.
266+ * Written by Jay Fenlason.
267+ *
268+ * This program is free software; you can redistribute it and/or modify
269+ * it under the terms of the GNU General Public License as published by
270+ * the Free Software Foundation; either version 2 of the License, or
271+ * (at your option) any later version.
272+ *
273+ * This program is distributed in the hope that it will be useful,
274+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
275+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
276+ * GNU General Public License for more details.
277+ *
278+ * You should have received a copy of the GNU General Public License along
279+ * with this program; if not, write to the Free Software Foundation, Inc.,
280+ * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
281+ */
bbdff76a
WD
282+
283+#include "rsync.h"
0f6fb3c8 284+#include "sysxattr.h"
bbdff76a 285+
09cc6650 286+#if defined HAVE_LINUX_XATTRS
bbdff76a
WD
287+
288+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
289+{
290+ return lgetxattr(path, name, value, size);
291+}
292+
293+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
294+{
295+ return lsetxattr(path, name, value, size, flags);
296+}
297+
298+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
299+{
300+ return llistxattr(path, list, size);
301+}
302+
303+#else
304+
305+#endif /* No xattrs */
9a7eef96
WD
306--- old/lib/sysxattr.h
307+++ new/lib/sysxattr.h
0f6fb3c8
WD
308@@ -0,0 +1,13 @@
309+#if defined SUPPORT_XATTRS && defined HAVE_ATTR_XATTR_H
310+#include <attr/xattr.h>
311+#endif
312+
09cc6650 313+#if defined HAVE_LINUX_XATTRS
bbdff76a
WD
314+
315+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
316+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags);
317+ssize_t sys_llistxattr(const char *path, char *list, size_t size);
318+
319+#else
320+
321+#endif /* No xattrs */
9a7eef96
WD
322--- old/options.c
323+++ new/options.c
dc52bc1a 324@@ -48,6 +48,7 @@ int copy_links = 0;
bbdff76a
WD
325 int preserve_links = 0;
326 int preserve_hard_links = 0;
327 int preserve_acls = 0;
328+int preserve_xattrs = 0;
329 int preserve_perms = 0;
4a65fe72 330 int preserve_executability = 0;
bbdff76a 331 int preserve_devices = 0;
dc52bc1a 332@@ -196,6 +197,7 @@ static void print_rsync_version(enum log
bbdff76a
WD
333 char const *have_inplace = "no ";
334 char const *hardlinks = "no ";
335 char const *acls = "no ";
336+ char const *xattrs = "no ";
337 char const *links = "no ";
338 char const *ipv6 = "no ";
339 STRUCT_STAT *dumstat;
dc52bc1a 340@@ -215,7 +217,9 @@ static void print_rsync_version(enum log
bbdff76a
WD
341 #ifdef SUPPORT_ACLS
342 acls = "";
343 #endif
344-
345+#ifdef SUPPORT_XATTRS
346+ xattrs = "";
347+#endif
348 #ifdef SUPPORT_LINKS
349 links = "";
350 #endif
dc52bc1a 351@@ -229,9 +233,9 @@ static void print_rsync_version(enum log
3b05e91f 352 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
bbdff76a
WD
353 rprintf(f, "<http://rsync.samba.org/>\n");
354 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
bc5988ec
WD
355- "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
356+ "%shard links, %sACLs, %sxattrs, %ssymlinks, batchfiles,\n",
bbdff76a
WD
357 (int) (sizeof (OFF_T) * 8),
358- got_socketpair, hardlinks, acls, links);
359+ got_socketpair, hardlinks, acls, xattrs, links);
360
361 /* Note that this field may not have type ino_t. It depends
362 * on the complicated interaction between largefile feature
dc52bc1a 363@@ -304,6 +308,9 @@ void usage(enum logcode F)
3610c43c 364 #ifdef SUPPORT_ACLS
bbdff76a 365 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
3610c43c
WD
366 #endif
367+#ifdef SUPPORT_XATTRS
bbdff76a 368+ rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
3610c43c 369+#endif
4a65fe72
WD
370 rprintf(F," --chmod=CHMOD change destination permissions\n");
371 rprintf(F," -o, --owner preserve owner (super-user only)\n");
bbdff76a 372 rprintf(F," -g, --group preserve group\n");
dc52bc1a 373@@ -422,6 +429,9 @@ static struct poptOption long_options[]
489b0a72
WD
374 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
375 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
376 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
377+ {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
378+ {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
379+ {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
380 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
381 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
382 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
0f6fb3c8 383@@ -1099,6 +1109,17 @@ int parse_arguments(int *argc, const cha
489b0a72 384 return 0;
3610c43c 385 #endif
bbdff76a
WD
386
387+ case 'X':
388+#ifdef SUPPORT_XATTRS
389+ preserve_xattrs = 1;
390+ preserve_perms = 1;
489b0a72 391+ break;
bbdff76a
WD
392+#else
393+ snprintf(err_buf,sizeof(err_buf),
394+ "extended attributes are not supported on this %s\n",
395+ am_server ? "server" : "client");
396+ return 0;
397+#endif /* SUPPORT_XATTRS */
bbdff76a
WD
398
399 default:
400 /* A large opt value means that set_refuse_options()
0f6fb3c8 401@@ -1538,6 +1559,10 @@ void server_options(char **args,int *arg
bbdff76a
WD
402 if (preserve_acls)
403 argstr[x++] = 'A';
3610c43c
WD
404 #endif
405+#ifdef SUPPORT_XATTRS
bbdff76a
WD
406+ if (preserve_xattrs)
407+ argstr[x++] = 'X';
3610c43c 408+#endif
bbdff76a
WD
409 if (preserve_uid)
410 argstr[x++] = 'o';
411 if (preserve_gid)
9a7eef96
WD
412--- old/rsync.c
413+++ new/rsync.c
93d6ca6d
WD
414@@ -34,6 +34,7 @@ extern int verbose;
415 extern int dry_run;
416 extern int daemon_log_format_has_i;
417 extern int preserve_acls;
418+extern int preserve_xattrs;
419 extern int preserve_perms;
420 extern int preserve_executability;
421 extern int preserve_times;
0f6fb3c8
WD
422@@ -223,6 +224,10 @@ int set_file_attrs(char *fname, struct f
423 if (preserve_acls && set_acl(fname, file, sxp) == 0)
09cc6650 424 updated = 1;
e5754c5f 425 #endif
09cc6650 426+#ifdef SUPPORT_XATTRS
0f6fb3c8 427+ if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
09cc6650
WD
428+ updated = 1;
429+#endif
bbdff76a 430
493b02f6 431 #ifdef HAVE_CHMOD
0f6fb3c8 432 if ((sxp->st.st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS)) {
9a7eef96
WD
433--- old/rsync.h
434+++ new/rsync.h
0f6fb3c8
WD
435@@ -494,6 +494,10 @@ struct idev {
436 #define ACLS_NEED_MASK 1
09cc6650 437 #endif
bbdff76a
WD
438
439+#ifdef HAVE_LINUX_XATTRS
440+#define SUPPORT_XATTRS 1
441+#endif
442+
0f6fb3c8
WD
443 #define GID_NONE ((gid_t)-1)
444
445 #define HL_CHECK_MASTER 0
446@@ -686,6 +690,9 @@ typedef struct {
447 struct rsync_acl *acc_acl; /* access ACL */
448 struct rsync_acl *def_acl; /* default ACL */
449 #endif
450+#ifdef SUPPORT_XATTRS
451+ item_list *xattr;
bbdff76a 452+#endif
0f6fb3c8 453 } statx;
bbdff76a 454
0f6fb3c8 455 #define ACL_READY(sx) ((sx).acc_acl != NULL)
9a7eef96
WD
456--- old/rsync.yo
457+++ new/rsync.yo
3731f97b 458@@ -322,6 +322,7 @@ to the detailed description below for a
bbdff76a 459 -p, --perms preserve permissions
4a65fe72
WD
460 -E, --executability preserve executability
461 -A, --acls preserve ACLs (implies -p) [non-standard]
462+ -X, --xattrs preserve extended attrs (implies -p) [n.s.]
463 --chmod=CHMOD change destination permissions
464 -o, --owner preserve owner (super-user only)
bbdff76a 465 -g, --group preserve group
3fcc541f
WD
466@@ -807,6 +808,11 @@ version makes it incompatible with sendi
467 rsync unless you double the bf(--acls) option (e.g. bf(-AA)). This
468 doubling is not needed when pulling files from an older rsync.
bbdff76a
WD
469
470+dit(bf(-X, --xattrs)) This option causes rsync to update the remote
471+extended attributes to be the same as the local ones. This will work
472+only if the remote machine's rsync supports this option also. This is
473+a non-standard option.
474+
4a65fe72
WD
475 dit(bf(--chmod)) This option tells rsync to apply one or more
476 comma-separated "chmod" strings to the permission of the files in the
477 transfer. The resulting value is treated as though it was the permissions
9a7eef96
WD
478--- old/xattr.c
479+++ new/xattr.c
0f6fb3c8 480@@ -0,0 +1,358 @@
dc52bc1a
WD
481+/*
482+ * Extended Attribute support for rsync.
0f6fb3c8 483+ * Written by Jay Fenlason, vaguely based on the ACLs patch.
dc52bc1a
WD
484+ *
485+ * Copyright (C) 2004 Red Hat, Inc.
0f6fb3c8 486+ * Copyright (C) 2006 Wayne Davison
dc52bc1a
WD
487+ *
488+ * This program is free software; you can redistribute it and/or modify
489+ * it under the terms of the GNU General Public License as published by
490+ * the Free Software Foundation; either version 2 of the License, or
491+ * (at your option) any later version.
492+ *
493+ * This program is distributed in the hope that it will be useful,
494+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
495+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
496+ * GNU General Public License for more details.
497+ *
498+ * You should have received a copy of the GNU General Public License along
499+ * with this program; if not, write to the Free Software Foundation, Inc.,
500+ * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
501+ */
bbdff76a
WD
502+
503+#include "rsync.h"
504+#include "lib/sysxattr.h"
505+
506+#ifdef SUPPORT_XATTRS
507+
bbdff76a 508+extern int dry_run;
0f6fb3c8 509+extern unsigned int file_struct_len;
bbdff76a
WD
510+
511+#define RSYNC_XAL_INITIAL 5
512+#define RSYNC_XAL_LIST_INITIAL 100
513+
514+typedef struct {
bbdff76a 515+ char *name;
bbdff76a 516+ char *datum;
0f6fb3c8
WD
517+ size_t name_len;
518+ size_t datum_len;
bbdff76a
WD
519+} rsync_xa;
520+
bbdff76a
WD
521+static size_t namebuf_len = 0;
522+static char *namebuf = NULL;
523+
524+static size_t datumbuf_len = 0;
525+static char *datumbuf = NULL;
526+
0f6fb3c8
WD
527+static item_list empty_xattr = EMPTY_ITEM_LIST;
528+static item_list rsync_xal_l = EMPTY_ITEM_LIST;
bbdff76a
WD
529+
530+/* ------------------------------------------------------------------------- */
531+
0f6fb3c8 532+static void rsync_xal_free(item_list *xalp)
bbdff76a
WD
533+{
534+ size_t i;
0f6fb3c8 535+ rsync_xa *rxas = xalp->items;
bbdff76a 536+
0f6fb3c8
WD
537+ for (i = 0; i < xalp->count; i++) {
538+ free(rxas[i].name);
539+ /* free(rxas[i].value); */
bbdff76a 540+ }
0f6fb3c8 541+ xalp->count = 0;
bbdff76a
WD
542+}
543+
0f6fb3c8 544+void free_xattr(statx *sxp)
bbdff76a 545+{
0f6fb3c8
WD
546+ rsync_xal_free(sxp->xattr);
547+ free(sxp->xattr);
548+ sxp->xattr = NULL;
549+}
bbdff76a 550+
0f6fb3c8
WD
551+static int rsync_xal_compare_names(const void *x1, const void *x2)
552+{
553+ const rsync_xa *xa1 = x1;
554+ const rsync_xa *xa2 = x2;
bbdff76a
WD
555+ return strcmp(xa1->name, xa2->name);
556+}
557+
0f6fb3c8 558+static int rsync_xal_get(const char *fname, item_list *xalp)
bbdff76a
WD
559+{
560+ ssize_t name_size;
561+ ssize_t datum_size;
562+ ssize_t left;
563+ char *name;
564+ size_t len;
565+ char *ptr;
566+
567+ if (!namebuf) {
568+ namebuf_len = 100;
569+ namebuf = new_array(char, namebuf_len);
570+ datumbuf_len = 100;
571+ datumbuf = new_array(char, datumbuf_len);
572+ if (!namebuf || !datumbuf)
573+ out_of_memory("rsync_xal_get");
574+ }
575+
576+ name_size = sys_llistxattr(fname, namebuf, namebuf_len);
577+ if (name_size > (ssize_t)namebuf_len) {
578+ name_size = -1;
579+ errno = ERANGE;
580+ }
581+ if (name_size < 0) {
582+ if (errno == ENOTSUP)
0f6fb3c8 583+ return 0;
bbdff76a
WD
584+ if (errno == ERANGE) {
585+ name_size = sys_llistxattr(fname, NULL, 0);
586+ if (name_size < 0) {
0f6fb3c8
WD
587+ rsyserr(FERROR, errno, "%s: rsync_xal_get: llistxattr",
588+ fname);
e5754c5f 589+ return -1;
bbdff76a
WD
590+ }
591+ namebuf = realloc_array(namebuf, char, name_size + 1);
592+ if (!namebuf)
593+ out_of_memory("rsync_xal_get");
594+ namebuf_len = name_size;
595+ name_size = sys_llistxattr(fname, namebuf, namebuf_len);
596+ if (name_size < 0) {
0f6fb3c8
WD
597+ rsyserr(FERROR, errno,
598+ "%s: rsync_xal_get: re-llistxattr failed",
599+ fname);
e5754c5f 600+ return -1;
bbdff76a
WD
601+ }
602+ } else {
0f6fb3c8
WD
603+ rsyserr(FERROR, errno,
604+ "%s: rsync_xal_get: llistxattr failed:",
605+ fname);
e5754c5f 606+ return -1;
bbdff76a
WD
607+ }
608+ }
bbdff76a 609+ if (name_size == 0)
e5754c5f 610+ return 0;
bbdff76a 611+ for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
0f6fb3c8 612+ rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
bbdff76a 613+
0f6fb3c8 614+ len = strlen(name) + 1;
bbdff76a
WD
615+ datum_size = sys_lgetxattr(fname, name, datumbuf, datumbuf_len);
616+ if (datum_size > (ssize_t)datumbuf_len) {
617+ datum_size = -1;
618+ errno = ERANGE;
619+ }
620+ if (datum_size < 0) {
621+ if (errno == ENOTSUP)
e5754c5f 622+ return -1;
bbdff76a
WD
623+ if (errno == ERANGE) {
624+ datum_size = sys_lgetxattr(fname, name, NULL, 0);
625+ if (datum_size < 0) {
0f6fb3c8
WD
626+ rsyserr(FERROR, errno,
627+ "%s: rsync_xal_get: lgetxattr %s failed",
628+ fname, name);
e5754c5f 629+ return -1;
bbdff76a
WD
630+ }
631+ datumbuf = realloc_array(datumbuf, char, datum_size + 1);
632+ if (!datumbuf)
633+ out_of_memory("rsync_xal_get");
634+ datumbuf_len = datum_size;
635+ datum_size = sys_lgetxattr(fname, name, datumbuf, datumbuf_len);
636+ if (datum_size < 0) {
0f6fb3c8
WD
637+ rsyserr(FERROR, errno,
638+ "%s: rsync_xal_get: re-lgetxattr of %s failed",
639+ name, fname);
e5754c5f 640+ return -1;
bbdff76a
WD
641+ }
642+ } else {
0f6fb3c8
WD
643+ rsyserr(FERROR, errno,
644+ "%s: rsync_xal_get: lgetxattr %s failed",
645+ fname, name);
e5754c5f 646+ return -1;
bbdff76a
WD
647+ }
648+ }
649+ ptr = new_array(char, len + datum_size);
650+ if (!ptr)
651+ out_of_memory("rsync_xal_get");
0f6fb3c8 652+ memcpy(ptr, name, len);
bbdff76a
WD
653+ if (datum_size)
654+ memcpy(ptr + len, datumbuf, datum_size);
0f6fb3c8
WD
655+ rxas->name_len = len;
656+ rxas->name = ptr;
657+ rxas->datum_len = datum_size;
658+ rxas->datum = ptr + len;
bbdff76a 659+ }
0f6fb3c8
WD
660+ if (xalp->count > 1)
661+ qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
e5754c5f 662+ return 0;
bbdff76a
WD
663+}
664+
0f6fb3c8
WD
665+/* Read the xattr(s) for this filename. */
666+int get_xattr(const char *fname, statx *sxp)
bbdff76a 667+{
0f6fb3c8
WD
668+ sxp->xattr = new(item_list);
669+ *sxp->xattr = empty_xattr;
670+ if (rsync_xal_get(fname, sxp->xattr) < 0) {
671+ free_xattr(sxp);
672+ return -1;
673+ }
674+ return 0;
bbdff76a
WD
675+}
676+
0f6fb3c8 677+static int find_matching_xattr(item_list *xalp)
bbdff76a 678+{
0f6fb3c8
WD
679+ size_t i, j;
680+ item_list *lst = rsync_xal_l.items;
bbdff76a
WD
681+
682+ for (i = 0; i < rsync_xal_l.count; i++) {
0f6fb3c8
WD
683+ rsync_xa *rxas1 = lst[i].items;
684+ rsync_xa *rxas2 = xalp->items;
685+
bbdff76a 686+ /* Wrong number of elements? */
0f6fb3c8 687+ if (lst[i].count != xalp->count)
bbdff76a
WD
688+ continue;
689+ /* any elements different? */
0f6fb3c8
WD
690+ for (j = 0; j < xalp->count; j++) {
691+ if (rxas1[j].name_len != rxas2[j].name_len
692+ || rxas1[j].datum_len != rxas2[j].datum_len
693+ || strcmp(rxas1[j].name, rxas2[j].name)
694+ || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
bbdff76a
WD
695+ break;
696+ }
697+ /* no differences found. This is The One! */
0f6fb3c8
WD
698+ if (j == xalp->count)
699+ return i;
bbdff76a 700+ }
0f6fb3c8
WD
701+
702+ return -1;
bbdff76a
WD
703+}
704+
0f6fb3c8
WD
705+/* Store *xalp on the end of rsync_xal_l */
706+static void rsync_xal_store(item_list *xalp)
bbdff76a 707+{
0f6fb3c8
WD
708+ item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
709+ EXPAND_ITEM_LIST(new_lst, item_list, xalp->count);
710+ memcpy(new_lst->items, xalp->items, xalp->count * sizeof (item_list));
711+ new_lst->count = xalp->count;
712+ xalp->count = 0;
bbdff76a
WD
713+}
714+
0f6fb3c8
WD
715+/* Send the make_xattr()-generated xattr list for this flist entry. */
716+void send_xattr(statx *sxp, int f)
bbdff76a 717+{
0f6fb3c8
WD
718+ int ndx = find_matching_xattr(sxp->xattr);
719+ if (ndx != -1) {
bbdff76a 720+ write_byte(f, 'x');
0f6fb3c8
WD
721+ write_int(f, ndx);
722+ rsync_xal_free(sxp->xattr);
bbdff76a
WD
723+ } else {
724+ rsync_xa *rxa;
0f6fb3c8 725+ int count = sxp->xattr->count;
bbdff76a
WD
726+ write_byte(f, 'X');
727+ write_int(f, count);
0f6fb3c8 728+ for (rxa = sxp->xattr->items; count--; rxa++) {
bbdff76a
WD
729+ write_int(f, rxa->name_len);
730+ write_int(f, rxa->datum_len);
731+ write_buf(f, rxa->name, rxa->name_len);
732+ write_buf(f, rxa->datum, rxa->datum_len);
733+ }
0f6fb3c8 734+ rsync_xal_store(sxp->xattr);
bbdff76a 735+ }
0f6fb3c8 736+ free_xattr(sxp);
bbdff76a
WD
737+}
738+
bbdff76a 739+/* ------------------------------------------------------------------------- */
bbdff76a 740+
0f6fb3c8 741+/* receive and build the rsync_xattr_lists */
bbdff76a
WD
742+void receive_xattr(struct file_struct *file, int f)
743+{
0f6fb3c8
WD
744+ static item_list temp_xattr = EMPTY_ITEM_LIST;
745+ int tag = read_byte(f);
746+ char *ndx_ptr = (char*)file + file_struct_len;
747+ int ndx;
bbdff76a 748+
bbdff76a 749+ if (tag == 'X') {
0f6fb3c8 750+ int i, count = read_int(f);
bbdff76a 751+ for (i = 0; i < count; i++) {
bbdff76a 752+ char *ptr;
0f6fb3c8
WD
753+ rsync_xa *rxa;
754+ size_t name_len = read_int(f);
755+ size_t datum_len = read_int(f);
3fcc541f
WD
756+ if (name_len + datum_len < name_len)
757+ out_of_memory("receive_xattr"); /* overflow */
0f6fb3c8 758+ rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
bbdff76a
WD
759+ ptr = new_array(char, name_len + datum_len);
760+ if (!ptr)
761+ out_of_memory("receive_xattr");
762+ read_buf(f, ptr, name_len);
763+ read_buf(f, ptr + name_len, datum_len);
0f6fb3c8
WD
764+ rxa->name_len = name_len;
765+ rxa->datum_len = datum_len;
766+ rxa->name = ptr;
767+ rxa->datum = ptr + name_len;
bbdff76a 768+ }
0f6fb3c8
WD
769+ ndx = rsync_xal_l.count;
770+ rsync_xal_store(&temp_xattr);
771+ } else if (tag == 'x') {
772+ ndx = read_int(f);
773+ if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
774+ rprintf(FERROR, "%s: receive_xattr: xa index %d out of range\n",
775+ f_name(file, NULL), ndx);
bbdff76a
WD
776+ exit_cleanup(RERR_STREAMIO);
777+ }
0f6fb3c8
WD
778+ } else {
779+ rprintf(FERROR,
780+ "%s: receive_xattr: unknown extended attribute type tag: %c\n",
781+ f_name(file, NULL), tag);
782+ exit_cleanup(RERR_STREAMIO);
783+ ndx = 0; /* silence a compiler warning... */
bbdff76a 784+ }
bbdff76a 785+
0f6fb3c8 786+ SIVAL(ndx_ptr, 0, ndx);
bbdff76a
WD
787+}
788+
0f6fb3c8
WD
789+/* Turn the xattr data in statx into cached xattr data, setting the index
790+ * values in the file struct. */
791+void cache_xattr(struct file_struct *file, statx *sxp)
bbdff76a 792+{
0f6fb3c8
WD
793+ char *ndx_ptr = (char*)file + file_struct_len;
794+ int ndx;
bbdff76a 795+
0f6fb3c8
WD
796+ if (!sxp->xattr)
797+ return;
bbdff76a 798+
0f6fb3c8
WD
799+ ndx = find_matching_xattr(sxp->xattr);
800+ if (ndx == -1)
801+ rsync_xal_store(sxp->xattr);
802+ free_xattr(sxp);
bbdff76a 803+
0f6fb3c8 804+ SIVAL(ndx_ptr, 0, ndx);
bbdff76a
WD
805+}
806+
0f6fb3c8 807+static int rsync_xal_set(const char *fname, item_list *xalp)
bbdff76a 808+{
0f6fb3c8
WD
809+ rsync_xa *rxas = xalp->items;
810+ size_t i;
811+ int ret = 0;
bbdff76a 812+
0f6fb3c8
WD
813+ for (i = 0; i < xalp->count; i++) {
814+ int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len, 0);
815+ if (status < 0) {
816+ rsyserr(FERROR, errno, "%s: rsync_xal_set: lsetxattr %s failed",
817+ fname, rxas[i].name);
818+ ret = -1;
819+ }
bbdff76a 820+ }
0f6fb3c8 821+ return ret;
bbdff76a
WD
822+}
823+
0f6fb3c8
WD
824+/* Set extended attributes on indicated filename. */
825+int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
bbdff76a 826+{
0f6fb3c8
WD
827+ int ndx;
828+ char *ndx_ptr = (char*)file + file_struct_len;
829+ item_list *lst = rsync_xal_l.items;
bbdff76a 830+
93d6ca6d
WD
831+ if (dry_run)
832+ return 1; /* FIXME: --dry-run needs to compute this value */
833+
0f6fb3c8
WD
834+ ndx = IVAL(ndx_ptr, 0);
835+ return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
bbdff76a
WD
836+}
837+
838+#endif /* SUPPORT_XATTRS */