Simplified the build instructions now that we have "prepare-source".
[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
610969d1
WD
9--- orig/Makefile.in 2005-11-07 04:31:05
10+++ Makefile.in 2005-11-07 04:38:36
4bf6f8c7 11@@ -27,13 +27,13 @@ VERSION=@VERSION@
bbdff76a
WD
12
13 HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
14 LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
15- lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
16+ lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattr.o @LIBOBJS@
4bf6f8c7
WD
17 ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
18 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
bbdff76a
WD
19 OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
20 main.o checksum.o match.o syscall.o log.o backup.o
21 OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
610969d1
WD
22- fileio.o batch.o clientname.o chmod.o acls.o
23+ fileio.o batch.o clientname.o chmod.o acls.o xattr.o
bbdff76a
WD
24 OBJS3=progress.o pipe.o
25 DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
26 popt_OBJS=popt/findme.o popt/popt.o popt/poptconfig.o \
27--- orig/backup.c 2004-10-06 00:13:09
28+++ backup.c 2005-03-03 01:20:46
4a65fe72 29@@ -136,6 +136,7 @@ static int make_bak_dir(char *fullpath)
bbdff76a
WD
30 do_lchown(fullpath, st.st_uid, st.st_gid);
31 do_chmod(fullpath, st.st_mode);
32 (void)DUP_ACL(end, fullpath, st.st_mode);
33+ (void)DUP_XATTR(end, fullpath );
34 }
35 }
36 *p = '/';
4a65fe72 37@@ -190,6 +191,7 @@ static int keep_backup(char *fname)
bbdff76a
WD
38 return 0;
39
40 PUSH_KEEP_BACKUP_ACL(file, fname, buf);
41+ PUSH_KEEP_BACKUP_XATTR(file, fname, buf);
42
43 /* Check to see if this is a device file, or link */
4a65fe72
WD
44 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
45@@ -267,6 +269,7 @@ static int keep_backup(char *fname)
bbdff76a 46 }
4a65fe72 47 set_file_attrs(buf, file, NULL, 0);
bbdff76a
WD
48 CLEANUP_KEEP_BACKUP_ACL();
49+ CLEANUP_KEEP_BACKUP_XATTR();
50 free(file);
51
52 if (verbose > 1) {
53--- orig/configure.in 2004-08-19 19:53:27
54+++ configure.in 2005-05-12 22:57:53
27e96866 55@@ -810,6 +810,30 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
bbdff76a
WD
56 AC_MSG_RESULT(no)
57 )
58
59+AC_CHECK_HEADERS(attr/xattr.h)
60+AC_MSG_CHECKING(whether to support extended attributes)
f787c90c
WD
61+AC_ARG_ENABLE(xattr-support,
62+AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
3b05e91f 63+[ case "$enableval" in
bbdff76a
WD
64+ yes)
65+ case "$host_os" in
66+ *linux*)
67+ AC_MSG_RESULT(Using Linux xattrs)
68+ AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
69+ ;;
70+ *)
71+ AC_MSG_RESULT(Xattrs requested but not linux. Good luck)
72+ ;;
73+ esac
74+ ;;
75+ *)
76+ AC_MSG_RESULT(no)
77+ AC_DEFINE(HAVE_NA_XATTRS, 1, [True if you don't have extended attributes])
78+ esac ],
79+ AC_MSG_RESULT(no)
80+ AC_DEFINE(HAVE_NO_XATTRL, 1, [True if you don't have extended attributes])
81+)
82+
83 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
84 AC_OUTPUT
85
4a65fe72 86--- orig/flist.c 2006-01-31 02:37:33
e5754c5f 87+++ flist.c 2005-10-16 23:03:04
4a65fe72 88@@ -969,6 +969,8 @@ static struct file_struct *send_file_nam
bbdff76a 89 return NULL;
489b0a72 90 if (MAKE_ACL(file, fname) < 0)
bbdff76a 91 return NULL;
e5754c5f 92+ if (MAKE_XATTR(file, fname) < 0)
bbdff76a
WD
93+ return NULL;
94
3b05e91f
WD
95 if (chmod_modes && !S_ISLNK(file->mode))
96 file->mode = tweak_mode(file->mode, chmod_modes);
4a65fe72 97@@ -981,9 +983,11 @@ static struct file_struct *send_file_nam
bbdff76a 98 flist->files[flist->count++] = file;
3b05e91f 99 send_file_entry(file, f);
bbdff76a
WD
100 SEND_ACL(file, f);
101+ SEND_XATTR(file, f);
102 } else {
103 /* Cleanup unsent ACL(s). */
104 SEND_ACL(file, -1);
105+ SEND_XATTR(file, -1);
106 }
107 return file;
108 }
4a65fe72 109@@ -1373,6 +1377,7 @@ struct file_list *recv_file_list(int f)
bbdff76a
WD
110 file = receive_file_entry(flist, flags, f);
111
112 RECEIVE_ACL(file, f);
113+ RECEIVE_XATTR(file, f );
114
115 if (S_ISREG(file->mode))
116 stats.total_size += file->length;
4a65fe72 117@@ -1397,6 +1402,7 @@ struct file_list *recv_file_list(int f)
bbdff76a
WD
118 clean_flist(flist, relative_paths, 1);
119
120 SORT_FILE_ACL_INDEX_LISTS();
121+ SORT_FILE_XATTR_INDEX_LISTS();
122
123 if (f >= 0) {
4a65fe72 124 recv_uid_list(f, flist);
27e96866 125--- orig/generator.c 2006-02-04 22:26:45
bbdff76a 126+++ generator.c 2005-05-12 23:21:08
27e96866 127@@ -908,6 +908,10 @@ static void recv_generator(char *fname,
bbdff76a
WD
128 if (f_out == -1)
129 SET_ACL(fname, file);
130 #endif
131+#ifdef SUPPORT_XATTRS
132+ if (f_out == -1)
133+ SET_XATTR(fname, file);
134+#endif
135 if (delete_during && f_out != -1 && !phase && dry_run < 2
136 && (file->flags & FLAG_DEL_HERE))
3b05e91f 137 delete_in_dir(the_file_list, fname, file, &st);
bbdff76a
WD
138--- orig/lib/sysxattr.c 2005-05-12 23:23:15
139+++ lib/sysxattr.c 2005-05-12 23:23:15
140@@ -0,0 +1,41 @@
141+/* Extended attribute support for rsync. */
142+/* This file Copyright (C) 2004 Red Hat, Inc. */
143+/* Written by Jay Fenlason */
144+
145+/* This program is free software; you can redistribute it and/or modify
146+ it under the terms of the GNU General Public License as published by
147+ the Free Software Foundation; either version 2 of the License, or
148+ (at your option) any later version.
149+
150+ This program is distributed in the hope that it will be useful,
151+ but WITHOUT ANY WARRANTY; without even the implied warranty of
152+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
153+ GNU General Public License for more details.
154+
155+ You should have received a copy of the GNU General Public License
156+ along with this program; if not, write to the Free Software
157+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
158+*/
159+
160+#include "rsync.h"
161+
162+#if defined(HAVE_LINUX_XATTRS)
163+
164+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
165+{
166+ return lgetxattr(path, name, value, size);
167+}
168+
169+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
170+{
171+ return lsetxattr(path, name, value, size, flags);
172+}
173+
174+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
175+{
176+ return llistxattr(path, list, size);
177+}
178+
179+#else
180+
181+#endif /* No xattrs */
182--- orig/lib/sysxattr.h 2005-05-12 23:56:31
183+++ lib/sysxattr.h 2005-05-12 23:56:31
184@@ -0,0 +1,9 @@
185+#if defined(HAVE_LINUX_XATTRS)
186+
187+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
188+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags);
189+ssize_t sys_llistxattr(const char *path, char *list, size_t size);
190+
191+#else
192+
193+#endif /* No xattrs */
3610c43c
WD
194--- orig/options.c 2006-02-04 19:58:53
195+++ options.c 2006-02-04 20:05:35
610969d1 196@@ -45,6 +45,7 @@ int copy_links = 0;
bbdff76a
WD
197 int preserve_links = 0;
198 int preserve_hard_links = 0;
199 int preserve_acls = 0;
200+int preserve_xattrs = 0;
201 int preserve_perms = 0;
4a65fe72 202 int preserve_executability = 0;
bbdff76a 203 int preserve_devices = 0;
4a65fe72 204@@ -195,6 +196,7 @@ static void print_rsync_version(enum log
bbdff76a
WD
205 char const *have_inplace = "no ";
206 char const *hardlinks = "no ";
207 char const *acls = "no ";
208+ char const *xattrs = "no ";
209 char const *links = "no ";
210 char const *ipv6 = "no ";
211 STRUCT_STAT *dumstat;
4a65fe72 212@@ -214,7 +216,9 @@ static void print_rsync_version(enum log
bbdff76a
WD
213 #ifdef SUPPORT_ACLS
214 acls = "";
215 #endif
216-
217+#ifdef SUPPORT_XATTRS
218+ xattrs = "";
219+#endif
220 #ifdef SUPPORT_LINKS
221 links = "";
222 #endif
4a65fe72 223@@ -228,9 +232,9 @@ static void print_rsync_version(enum log
3b05e91f 224 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
bbdff76a
WD
225 rprintf(f, "<http://rsync.samba.org/>\n");
226 rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
bc5988ec
WD
227- "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
228+ "%shard links, %sACLs, %sxattrs, %ssymlinks, batchfiles,\n",
bbdff76a
WD
229 (int) (sizeof (OFF_T) * 8),
230- got_socketpair, hardlinks, acls, links);
231+ got_socketpair, hardlinks, acls, xattrs, links);
232
233 /* Note that this field may not have type ino_t. It depends
234 * on the complicated interaction between largefile feature
3610c43c
WD
235@@ -302,6 +306,9 @@ void usage(enum logcode F)
236 #ifdef SUPPORT_ACLS
bbdff76a 237 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
3610c43c
WD
238 #endif
239+#ifdef SUPPORT_XATTRS
bbdff76a 240+ rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
3610c43c 241+#endif
4a65fe72
WD
242 rprintf(F," --chmod=CHMOD change destination permissions\n");
243 rprintf(F," -o, --owner preserve owner (super-user only)\n");
bbdff76a 244 rprintf(F," -g, --group preserve group\n");
3610c43c 245@@ -421,6 +428,9 @@ static struct poptOption long_options[]
489b0a72
WD
246 {"acls", 'A', POPT_ARG_NONE, 0, 'A', 0, 0 },
247 {"no-acls", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
248 {"no-A", 0, POPT_ARG_VAL, &preserve_acls, 0, 0, 0 },
249+ {"xattrs", 'X', POPT_ARG_NONE, 0, 'X', 0, 0 },
250+ {"no-xattrs", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
251+ {"no-X", 0, POPT_ARG_VAL, &preserve_xattrs, 0, 0, 0 },
252 {"times", 't', POPT_ARG_VAL, &preserve_times, 1, 0, 0 },
253 {"no-times", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
254 {"no-t", 0, POPT_ARG_VAL, &preserve_times, 0, 0, 0 },
3610c43c 255@@ -1085,6 +1095,17 @@ int parse_arguments(int *argc, const cha
489b0a72 256 return 0;
3610c43c 257 #endif
bbdff76a
WD
258
259+ case 'X':
260+#ifdef SUPPORT_XATTRS
261+ preserve_xattrs = 1;
262+ preserve_perms = 1;
489b0a72 263+ break;
bbdff76a
WD
264+#else
265+ snprintf(err_buf,sizeof(err_buf),
266+ "extended attributes are not supported on this %s\n",
267+ am_server ? "server" : "client");
268+ return 0;
269+#endif /* SUPPORT_XATTRS */
bbdff76a
WD
270
271 default:
272 /* A large opt value means that set_refuse_options()
3610c43c 273@@ -1530,6 +1551,10 @@ void server_options(char **args,int *arg
bbdff76a
WD
274 if (preserve_acls)
275 argstr[x++] = 'A';
3610c43c
WD
276 #endif
277+#ifdef SUPPORT_XATTRS
bbdff76a
WD
278+ if (preserve_xattrs)
279+ argstr[x++] = 'X';
3610c43c 280+#endif
bbdff76a
WD
281 if (preserve_uid)
282 argstr[x++] = 'o';
283 if (preserve_gid)
3610c43c 284--- orig/rsync.c 2006-02-04 19:53:13
e5754c5f 285+++ rsync.c 2005-10-16 23:19:27
27e96866 286@@ -206,12 +206,15 @@ int set_file_attrs(char *fname, struct f
e5754c5f
WD
287 }
288 #endif
289
290- /* If this is a directory, SET_ACL() will be called on the cleanup
291- * receive_generator() pass (if we called it here, we might clobber
292- * writability on the directory). Everything else is OK to do now. */
293+ /* If this is a directory, SET_ACL() and/or SET_XATTR() will be called
294+ * on the cleanup receive_generator() pass -- if we called it here, we
295+ * might clobber writability on the dir (SELinux security contexts are
296+ * stored in xattrs). Everything else is OK to do now. */
297 if (!S_ISDIR(st->st_mode)) {
bbdff76a
WD
298 if (SET_ACL(fname, file) == 0)
299 updated = 1;
bbdff76a 300+ if (SET_XATTR(fname, file) == 0)
e5754c5f
WD
301+ updated = 1;
302 }
bbdff76a 303
4a65fe72 304 if (verbose > 1 && flags & ATTRS_REPORT) {
bc5988ec 305--- orig/rsync.h 2006-01-31 19:27:00
e5754c5f 306+++ rsync.h 2005-10-16 23:19:44
3610c43c 307@@ -695,6 +695,38 @@ struct chmod_mode_struct;
bbdff76a
WD
308 #endif /* SUPPORT_ACLS */
309 #include "smb_acls.h"
310
311+#ifdef HAVE_LINUX_XATTRS
312+#define SUPPORT_XATTRS 1
313+#endif
314+
315+#ifdef SUPPORT_XATTRS
316+#ifdef HAVE_ATTR_XATTR_H
317+#include <attr/xattr.h>
318+#endif
319+#define MAKE_XATTR(file, fname) make_xattr(file, fname)
320+#define SEND_XATTR(file, f) send_xattr(file, f)
321+#define RECEIVE_XATTR(file, f) receive_xattr(file, f)
322+#define SORT_FILE_XATTR_INDEX_LISTS() sort_file_xattr_index_lists()
323+#define SET_XATTR(fname, file) set_xattr(fname, file)
324+#define NEXT_XATTR_UID() next_xattr_uid()
325+#define XATTR_UID_MAP(uid) xattr_uid_map(uid)
326+#define PUSH_KEEP_BACKUP_XATTR(file, orig, dest) \
327+ push_keep_backup_xattr(file, orig, dest)
328+#define CLEANUP_KEEP_BACKUP_XATTR() cleanup_keep_backup_xattr()
329+#define DUP_XATTR(orig, dest) dup_xattr(orig, dest)
330+#else /* SUPPORT_XATTRS */
331+#define MAKE_XATTR(file, fname) 1 /* checked return value */
332+#define SEND_XATTR(file, f)
333+#define RECEIVE_XATTR(file, f)
334+#define SORT_FILE_XATTR_INDEX_LISTS()
e5754c5f 335+#define SET_XATTR(fname, file) 1 /* checked return value */
bbdff76a
WD
336+#define NEXT_XATTR_UID()
337+#define XATTR_UID_MAP(uid)
338+#define PUSH_KEEP_BACKUP_XATTR(file, orig, dest)
339+#define CLEANUP_KEEP_BACKUP_XATTR()
e5754c5f 340+#define DUP_XATTR(src, orig) 1 /* checked return value */
bbdff76a
WD
341+#endif /* SUPPORT_XATTRS */
342+
343 #include "proto.h"
344
345 /* We have replacement versions of these if they're missing. */
27e96866 346--- orig/rsync.yo 2006-02-05 06:55:30
4a65fe72
WD
347+++ rsync.yo 2006-01-31 03:16:46
348@@ -318,6 +318,7 @@ to the detailed description below for a
bbdff76a 349 -p, --perms preserve permissions
4a65fe72
WD
350 -E, --executability preserve executability
351 -A, --acls preserve ACLs (implies -p) [non-standard]
352+ -X, --xattrs preserve extended attrs (implies -p) [n.s.]
353 --chmod=CHMOD change destination permissions
354 -o, --owner preserve owner (super-user only)
bbdff76a 355 -g, --group preserve group
27e96866 356@@ -751,6 +752,11 @@ dit(bf(-A, --acls)) This option causes r
4a65fe72
WD
357 ACLs to be the same as the source ACLs. This nonstandard option only
358 works if the remote rsync also supports it. bf(--acls) implies bf(--perms).
bbdff76a
WD
359
360+dit(bf(-X, --xattrs)) This option causes rsync to update the remote
361+extended attributes to be the same as the local ones. This will work
362+only if the remote machine's rsync supports this option also. This is
363+a non-standard option.
364+
4a65fe72
WD
365 dit(bf(--chmod)) This option tells rsync to apply one or more
366 comma-separated "chmod" strings to the permission of the files in the
367 transfer. The resulting value is treated as though it was the permissions
e5754c5f
WD
368--- orig/xattr.c 2005-10-16 23:25:12
369+++ xattr.c 2005-10-16 23:25:12
370@@ -0,0 +1,540 @@
bbdff76a
WD
371+/* Extended Attribute support for rsync */
372+/* Copyright (C) 2004 Red Hat, Inc */
373+/* Written by Jay Fenlason, vaguely based on the ACLs patch */
374+
375+/* This program is free software; you can redistribute it and/or modify
376+ it under the terms of the GNU General Public License as published by
377+ the Free Software Foundation; either version 2 of the License, or
378+ (at your option) any later version.
379+
380+ This program is distributed in the hope that it will be useful,
381+ but WITHOUT ANY WARRANTY; without even the implied warranty of
382+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
383+ GNU General Public License for more details.
384+
385+ You should have received a copy of the GNU General Public License
386+ along with this program; if not, write to the Free Software
387+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
388+*/
389+
390+#include "rsync.h"
391+#include "lib/sysxattr.h"
392+
393+#ifdef SUPPORT_XATTRS
394+
395+extern int preserve_xattrs;
396+extern int dry_run;
397+
398+#define RSYNC_XAL_INITIAL 5
399+#define RSYNC_XAL_LIST_INITIAL 100
400+
401+typedef struct {
402+ size_t name_len;
403+ char *name;
404+ size_t datum_len;
405+ char *datum;
406+} rsync_xa;
407+
408+typedef struct {
409+ size_t count;
410+ size_t alloc;
411+ rsync_xa *rxas;
412+} rsync_xal;
413+
414+typedef struct {
415+ size_t count;
416+ size_t alloc;
417+ rsync_xal *rxals;
418+} rsync_xal_list;
419+
420+static size_t namebuf_len = 0;
421+static char *namebuf = NULL;
422+
423+static size_t datumbuf_len = 0;
424+static char *datumbuf = NULL;
425+
426+static rsync_xal curr_rsync_xal = { 0, 0, NULL };
427+static rsync_xal_list rsync_xal_l = { 0, 0, NULL };
428+
429+
430+/* ------------------------------------------------------------------------- */
431+
432+/* the below stuff is only used by the receiver */
433+
434+/* structure to hold index to rsync_xal_l member corresponding to
435+ * flist->files[i] */
436+
437+typedef struct {
438+ const struct file_struct *file;
439+ int xalidx;
440+} file_xal_index;
441+
442+typedef struct {
443+ size_t count;
444+ size_t alloc;
445+ file_xal_index *filexalidxs;
446+} file_xal_index_list;
447+
448+static file_xal_index_list fxil = {0, 0, NULL };
449+
450+/* stuff for redirecting calls to set_acl() from set_perms()
451+ * for keep_backup() */
452+static const struct file_struct *backup_orig_file = NULL;
453+static const char null_string[] = "";
454+static const char *backup_orig_fname = null_string;
455+static const char *backup_dest_fname = null_string;
456+static rsync_xal backup_xal;
457+
458+/* ------------------------------------------------------------------------- */
459+
460+static void rsync_xal_free(rsync_xal *x)
461+{
462+ size_t i;
463+
464+ for (i = 0; i < x->count; i++) {
465+ free(x->rxas[i].name);
466+ /* free(x->rxas[i].value); */
467+ }
468+ x->count = 0;
469+}
470+
471+static int rsync_xal_compare_names(const void *x1, const void *x2)
472+{
473+ const rsync_xa *xa1;
474+ const rsync_xa *xa2;
475+
476+ xa1 = x1;
477+ xa2 = x2;
478+ return strcmp(xa1->name, xa2->name);
479+}
480+
e5754c5f 481+static int rsync_xal_get(const char *fname, rsync_xal *x)
bbdff76a
WD
482+{
483+ ssize_t name_size;
484+ ssize_t datum_size;
485+ ssize_t left;
486+ char *name;
487+ size_t len;
488+ char *ptr;
489+
490+ if (!namebuf) {
491+ namebuf_len = 100;
492+ namebuf = new_array(char, namebuf_len);
493+ datumbuf_len = 100;
494+ datumbuf = new_array(char, datumbuf_len);
495+ if (!namebuf || !datumbuf)
496+ out_of_memory("rsync_xal_get");
497+ }
498+
499+ name_size = sys_llistxattr(fname, namebuf, namebuf_len);
500+ if (name_size > (ssize_t)namebuf_len) {
501+ name_size = -1;
502+ errno = ERANGE;
503+ }
504+ if (name_size < 0) {
505+ if (errno == ENOTSUP)
e5754c5f 506+ return -1;
bbdff76a
WD
507+ if (errno == ERANGE) {
508+ name_size = sys_llistxattr(fname, NULL, 0);
509+ if (name_size < 0) {
510+ rprintf(FERROR, "%s: rsync_xal_get: llistxattr: %s\n",
e5754c5f
WD
511+ fname, strerror(errno));
512+ return -1;
bbdff76a
WD
513+ }
514+ namebuf = realloc_array(namebuf, char, name_size + 1);
515+ if (!namebuf)
516+ out_of_memory("rsync_xal_get");
517+ namebuf_len = name_size;
518+ name_size = sys_llistxattr(fname, namebuf, namebuf_len);
519+ if (name_size < 0) {
520+ rprintf(FERROR,
521+ "%s: rsync_xal_get: re-llistxattr failed: %s\n",
522+ fname, strerror(errno));
e5754c5f 523+ return -1;
bbdff76a
WD
524+ }
525+ } else {
526+ rprintf(FERROR,
527+ "%s: rsync_xal_get: llistxattr failed: %s\n",
528+ fname, strerror(errno));
e5754c5f 529+ return -1;
bbdff76a
WD
530+ }
531+ }
532+ rsync_xal_free(x);
533+ if (name_size == 0)
e5754c5f 534+ return 0;
bbdff76a
WD
535+ for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
536+ len = strlen(name) + 1;
537+
538+ if (x->count >= x->alloc) {
539+ size_t new_alloc;
540+ rsync_xa *new_rxas;
541+
542+ new_alloc = x->alloc < RSYNC_XAL_INITIAL ? RSYNC_XAL_INITIAL : x->alloc * 2;
543+ new_rxas = realloc_array(x->rxas, rsync_xa, new_alloc);
544+ if (!new_rxas)
545+ out_of_memory("rsync_xal_get");
546+ x->alloc = new_alloc;
547+ x->rxas = new_rxas;
548+ }
549+ datum_size = sys_lgetxattr(fname, name, datumbuf, datumbuf_len);
550+ if (datum_size > (ssize_t)datumbuf_len) {
551+ datum_size = -1;
552+ errno = ERANGE;
553+ }
554+ if (datum_size < 0) {
555+ if (errno == ENOTSUP)
e5754c5f 556+ return -1;
bbdff76a
WD
557+ if (errno == ERANGE) {
558+ datum_size = sys_lgetxattr(fname, name, NULL, 0);
559+ if (datum_size < 0) {
560+ rprintf(FERROR,
561+ "%s: rsync_xal_get: lgetxattr %s failed: %s\n",
562+ fname, name, strerror(errno));
e5754c5f 563+ return -1;
bbdff76a
WD
564+ }
565+ datumbuf = realloc_array(datumbuf, char, datum_size + 1);
566+ if (!datumbuf)
567+ out_of_memory("rsync_xal_get");
568+ datumbuf_len = datum_size;
569+ datum_size = sys_lgetxattr(fname, name, datumbuf, datumbuf_len);
570+ if (datum_size < 0) {
571+ rprintf(FERROR,
572+ "%s: rsync_xal_get: re-lgetxattr of %s failed: %s\n",
573+ name, fname, strerror(errno));
e5754c5f 574+ return -1;
bbdff76a
WD
575+ }
576+ } else {
577+ rprintf(FERROR,
578+ "%s: rsync_xal_get: lgetxattr %s failed: %s\n",
579+ fname, name, strerror(errno));
e5754c5f 580+ return -1;
bbdff76a
WD
581+ }
582+ }
583+ ptr = new_array(char, len + datum_size);
584+ if (!ptr)
585+ out_of_memory("rsync_xal_get");
586+ strcpy(ptr, name);
587+ if (datum_size)
588+ memcpy(ptr + len, datumbuf, datum_size);
589+ x->rxas[curr_rsync_xal.count].name_len = len;
590+ x->rxas[curr_rsync_xal.count].name = ptr;
591+ x->rxas[curr_rsync_xal.count].datum_len = datum_size;
592+ x->rxas[curr_rsync_xal.count].datum = ptr + len;
593+ x->count++;
594+ }
595+ if (x->count > 1) {
596+ qsort(x->rxas, x->count, sizeof (rsync_xa), rsync_xal_compare_names);
597+ }
e5754c5f 598+ return 0;
bbdff76a
WD
599+}
600+
601+
602+/* generate the xattr(s) for this flist entry;
603+ * xattr(s) are either sent or cleaned-up by send_xattr() below */
604+
e5754c5f 605+int make_xattr(const struct file_struct *file, const char *fname)
bbdff76a 606+{
a8801c39 607+ if (!preserve_xattrs || !file)
e5754c5f 608+ return 1;
bbdff76a
WD
609+
610+ rsync_xal_get(fname, &curr_rsync_xal);
e5754c5f 611+ return 0; /* TODO: This needs to return 1 if no xattrs changed! */
bbdff76a
WD
612+}
613+
614+static ssize_t rsync_xal_find_matching(void)
615+{
616+ size_t i;
617+ size_t j;
618+
619+ for (i = 0; i < rsync_xal_l.count; i++) {
620+ /* Wrong number of elements? */
621+ if (rsync_xal_l.rxals[i].count != curr_rsync_xal.count)
622+ continue;
623+ /* any elements different? */
624+ for (j = 0; j < curr_rsync_xal.count; j++) {
625+ if (rsync_xal_l.rxals[i].rxas[j].name_len != curr_rsync_xal.rxas[j].name_len
626+ || rsync_xal_l.rxals[i].rxas[j].datum_len != curr_rsync_xal.rxas[j].datum_len
627+ || strcmp(rsync_xal_l.rxals[i].rxas[j].name, curr_rsync_xal.rxas[j].name)
628+ || memcmp(rsync_xal_l.rxals[i].rxas[j].datum, curr_rsync_xal.rxas[j].datum, curr_rsync_xal.rxas[j].datum_len))
629+ break;
630+ }
631+ /* no differences found. This is The One! */
632+ if (j == curr_rsync_xal.count)
633+ break;
634+ }
635+ if (i < rsync_xal_l.count)
636+ return i;
637+ return (ssize_t)-1;
638+}
639+
640+/* Store curr_rsync_xal on the end of rsync_xal_l */
641+static void rsync_xal_store(void)
642+{
643+ if (rsync_xal_l.count <= rsync_xal_l.alloc) {
644+ size_t new_alloc;
645+ void *new_xal;
646+
647+ new_alloc = rsync_xal_l.count < RSYNC_XAL_LIST_INITIAL ? RSYNC_XAL_LIST_INITIAL : rsync_xal_l.count * 2;
648+ new_xal = realloc_array(rsync_xal_l.rxals, rsync_xal, new_alloc);
649+ if (!new_xal)
650+ out_of_memory("rsync_xal_store");
651+ rsync_xal_l.alloc = new_alloc;
652+ rsync_xal_l.rxals = new_xal;
653+ }
654+ rsync_xal_l.rxals[rsync_xal_l.count] = curr_rsync_xal;
655+ rsync_xal_l.count++;
656+ curr_rsync_xal.count = 0;
657+ curr_rsync_xal.alloc = 0;
658+}
659+
660+/* send the make_xattr()-generated xattr list for this flist entry,
661+ * or clean up after an flist entry that's not being sent (f == -1) */
662+
663+void send_xattr(const struct file_struct *file, int f)
664+{
665+ ssize_t index;
666+
a8801c39 667+ if (!preserve_xattrs || !file)
bbdff76a
WD
668+ return;
669+
670+ if (f == -1) {
671+ rsync_xal_free(&curr_rsync_xal);
672+ return;
673+ }
674+ index = rsync_xal_find_matching();
675+ if (index != -1) {
676+ write_byte(f, 'x');
677+ write_int(f, index);
678+ rsync_xal_free(&curr_rsync_xal);
679+ } else {
680+ rsync_xa *rxa;
681+ size_t count;
682+
683+ count = curr_rsync_xal.count;
684+ write_byte(f, 'X');
685+ write_int(f, count);
686+ for (rxa = curr_rsync_xal.rxas; count--; rxa++) {
687+ write_int(f, rxa->name_len);
688+ write_int(f, rxa->datum_len);
689+ write_buf(f, rxa->name, rxa->name_len);
690+ write_buf(f, rxa->datum, rxa->datum_len);
691+ }
692+ rsync_xal_store();
693+ }
694+}
695+
696+
697+/* ------------------------------------------------------------------------- */
698+/* receive and build the rsync_xattr_lists */
699+
700+void receive_xattr(struct file_struct *file, int f)
701+{
702+ char *fname;
703+ int tag;
704+
705+ if (!preserve_xattrs)
706+ return;
bd68c3c2 707+ fname = f_name(file, NULL);
bbdff76a
WD
708+ tag = read_byte(f);
709+ if (tag != 'X' && tag != 'x') {
710+ rprintf(FERROR,
711+ "%s: receive_xattr: unknown extended attribute type tag: %c\n",
712+ fname, tag);
713+ exit_cleanup(RERR_STREAMIO);
714+ }
715+
716+ if (fxil.alloc <= fxil.count) {
717+ void *new_ptr;
718+ size_t new_alloc;
719+
720+ if (fxil.count < RSYNC_XAL_LIST_INITIAL)
721+ new_alloc = fxil.alloc + RSYNC_XAL_LIST_INITIAL;
722+ else
723+ new_alloc = fxil.alloc * 2;
724+ new_ptr = realloc_array(fxil.filexalidxs, file_xal_index, new_alloc);
725+ if (!new_ptr)
726+ out_of_memory("receive_xattr");
727+ if (verbose >= 3) {
728+ rprintf(FINFO, "receive_xattr to %lu bytes, %s move\n",
729+ (unsigned long)(new_alloc * sizeof (file_xal_index)),
730+ fxil.filexalidxs == new_ptr ? "did not" : "did");
731+ }
732+
733+ fxil.filexalidxs = new_ptr;
734+ fxil.alloc = new_alloc;
735+ }
736+
737+ fxil.filexalidxs[fxil.count].file = file;
738+ if (tag == 'X') {
739+ size_t count;
740+ size_t i;
741+
742+ fxil.filexalidxs[fxil.count].xalidx = rsync_xal_l.count;
743+
744+ count = read_int(f);
745+ curr_rsync_xal.count = count;
746+ curr_rsync_xal.alloc = count;
747+ curr_rsync_xal.rxas = new_array(rsync_xa, count);
748+ if (!curr_rsync_xal.rxas)
749+ out_of_memory("receive_xattr");
750+ for (i = 0; i < count; i++) {
751+ size_t name_len;
752+ size_t datum_len;
753+ char *ptr;
754+
755+ name_len = read_int(f);
756+ datum_len = read_int(f);
757+ ptr = new_array(char, name_len + datum_len);
758+ if (!ptr)
759+ out_of_memory("receive_xattr");
760+ read_buf(f, ptr, name_len);
761+ read_buf(f, ptr + name_len, datum_len);
762+ curr_rsync_xal.rxas[i].name_len = name_len;
763+ curr_rsync_xal.rxas[i].datum_len = datum_len;
764+ curr_rsync_xal.rxas[i].name = ptr;
765+ curr_rsync_xal.rxas[i].datum = ptr + name_len;
766+ }
767+ rsync_xal_store();
768+ } else {
769+ size_t index;
770+
771+ index = read_int(f);
772+ if (index >= rsync_xal_l.count) {
773+ rprintf(FERROR, "%s: receive_xattr: xa index %lu out of range\n",
e5754c5f 774+ fname, (unsigned long)index);
bbdff76a
WD
775+ exit_cleanup(RERR_STREAMIO);
776+ }
777+ fxil.filexalidxs[fxil.count].xalidx = index;
778+ }
779+ fxil.count++;
780+}
781+
e5754c5f 782+static int rsync_xal_set(const char *fname, rsync_xal *x)
bbdff76a
WD
783+{
784+ size_t i;
e5754c5f 785+ int ret = 0;
bbdff76a 786+
bbdff76a 787+ for (i = 0; i < x->count; i++) {
e5754c5f 788+ int status = sys_lsetxattr(fname, x->rxas[i].name, x->rxas[i].datum, x->rxas[i].datum_len, 0);
bbdff76a
WD
789+ if (status < 0) {
790+ rprintf(FERROR, "%s: rsync_xal_set: lsetxattr %s failed: %s\n",
791+ fname, x->rxas[i].name, strerror(errno));
e5754c5f 792+ ret = -1;
bbdff76a
WD
793+ }
794+ }
795+ return ret;
796+}
797+
798+/* for duplicating xattrs on backups when using backup_dir */
799+
800+int dup_xattr(const char *orig, const char *bak)
801+{
802+ int ret;
803+
bbdff76a 804+ if (!preserve_xattrs)
e5754c5f 805+ return 1;
bbdff76a 806+
e5754c5f 807+ if (rsync_xal_get(orig, &backup_xal) < 0)
bbdff76a 808+ ret = rsync_xal_set(bak, &backup_xal);
e5754c5f
WD
809+ else
810+ ret = 0;
bbdff76a
WD
811+ rsync_xal_free(&backup_xal);
812+ return ret;
813+}
814+
815+void push_keep_backup_xattr(const struct file_struct *file, const char *orig, const char *dest)
816+{
817+ if (!preserve_xattrs)
818+ return;
819+
820+ backup_orig_file = file;
821+ backup_orig_fname = orig;
822+ backup_dest_fname = dest;
823+ rsync_xal_get(orig, &backup_xal);
824+}
825+
826+static int set_keep_backup_xal(void)
827+{
828+ if (!preserve_xattrs)
e5754c5f 829+ return 1;
bbdff76a
WD
830+ return rsync_xal_set(backup_dest_fname, &backup_xal);
831+}
832+
833+void cleanup_keep_backup_xattr(void)
834+{
835+ if (!preserve_xattrs)
836+ return;
837+
838+ backup_orig_file = NULL;
839+ backup_orig_fname = null_string;
840+ backup_dest_fname = null_string;
841+ rsync_xal_free(&backup_xal);
842+}
843+
844+static int file_xal_index_compare(const void *x1, const void *x2)
845+{
846+ const file_xal_index *xa1;
847+ const file_xal_index *xa2;
848+
849+ xa1 = x1;
850+ xa2 = x2;
851+ return xa1->file == xa2->file ? 0 : xa1->file < xa2->file ? -1 : 1;
852+}
853+
854+void sort_file_xattr_index_lists(void)
855+{
856+ if (!preserve_xattrs)
857+ return;
858+ qsort(fxil.filexalidxs, fxil.count, sizeof (file_xal_index), file_xal_index_compare);
859+}
860+
861+static int find_file_xal_index(const struct file_struct *file)
862+{
863+ int low = 0, high = fxil.count;
864+ const struct file_struct *file_mid;
865+
866+ if (!high--) {
867+ rprintf(FERROR, "find_file_xal_index: no entries\n");
868+ exit_cleanup(RERR_STREAMIO);
869+ return -1;
870+ }
871+ do {
872+ int mid = (high + low) / 2;
873+ file_mid = fxil.filexalidxs[mid].file;
874+ if (file_mid == file)
875+ return fxil.filexalidxs[mid].xalidx;
876+ if (file_mid > file)
877+ high = mid - 1;
878+ else
879+ low = mid + 1;
880+ } while (low < high);
881+ if (low == high) {
882+ file_mid = fxil.filexalidxs[low].file;
883+ if (file_mid == file)
884+ return fxil.filexalidxs[low].xalidx;
885+ }
886+ rprintf(FERROR,
887+ "find_file_xal_index: can't find entry for file in list\n");
888+ exit_cleanup(RERR_STREAMIO);
889+ return -1;
890+}
891+
bbdff76a
WD
892+/* set extended attributes on rsync-ed or keep_backup-ed file */
893+
894+int set_xattr(const char *fname, const struct file_struct *file)
895+{
bbdff76a
WD
896+ int xalidx;
897+ rsync_xal *x;
898+
bbdff76a 899+ if (dry_run || !preserve_xattrs)
e5754c5f 900+ return 1;
bbdff76a
WD
901+ if (file == backup_orig_file) {
902+ if (!strcmp(fname, backup_dest_fname))
903+ return set_keep_backup_xal();
904+ }
905+ xalidx = find_file_xal_index(file);
906+ x = &(rsync_xal_l.rxals[xalidx]);
e5754c5f 907+ return rsync_xal_set(fname, x);
bbdff76a
WD
908+}
909+
910+#endif /* SUPPORT_XATTRS */