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