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