1 Depends-On-Patch: acls.diff
3 After applying this patch, run these commands for a successful build:
7 ./configure --enable-acl-support --enable-xattr-support
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@
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/inffast.o zlib/inflate.o zlib/inftrees.o \
20 zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
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 );
39 @@ -189,6 +190,7 @@ static int keep_backup(char *fname)
42 PUSH_KEEP_BACKUP_ACL(file, fname, buf);
43 + PUSH_KEEP_BACKUP_XATTR(file, fname, buf);
45 /* Check to see if this is a device file, or link */
46 if (IS_DEVICE(file->mode) && am_root && preserve_devices) {
47 @@ -265,6 +267,7 @@ static int keep_backup(char *fname)
49 set_perms(buf, file, NULL, 0);
50 CLEANUP_KEEP_BACKUP_ACL();
51 + CLEANUP_KEEP_BACKUP_XATTR();
55 --- orig/configure.in 2004-08-19 19:53:27
56 +++ configure.in 2005-05-12 22:57:53
57 @@ -822,6 +822,30 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
61 +AC_CHECK_HEADERS(attr/xattr.h)
62 +AC_MSG_CHECKING(whether to support extended attributes)
63 +AC_ARG_ENABLE(xattr-support,
64 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
69 + AC_MSG_RESULT(Using Linux xattrs)
70 + AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
73 + AC_MSG_RESULT(Xattrs requested but not linux. Good luck)
79 + AC_DEFINE(HAVE_NA_XATTRS, 1, [True if you don't have extended attributes])
82 + AC_DEFINE(HAVE_NO_XATTRL, 1, [True if you don't have extended attributes])
85 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
88 --- orig/flist.c 2005-07-29 02:49:06
89 +++ flist.c 2005-10-16 23:03:04
90 @@ -968,6 +968,8 @@ static struct file_struct *send_file_nam
92 if (MAKE_ACL(file, fname) < 0)
94 + if (MAKE_XATTR(file, fname) < 0)
97 maybe_emit_filelist_progress(flist->count + flist_count_offset);
99 @@ -977,9 +979,11 @@ static struct file_struct *send_file_nam
100 flist->files[flist->count++] = file;
101 send_file_entry(file, f, base_flags);
103 + SEND_XATTR(file, f);
105 /* Cleanup unsent ACL(s). */
107 + SEND_XATTR(file, -1);
111 @@ -1327,6 +1331,7 @@ struct file_list *recv_file_list(int f)
112 file = receive_file_entry(flist, flags, f);
114 RECEIVE_ACL(file, f);
115 + RECEIVE_XATTR(file, f );
117 if (S_ISREG(file->mode))
118 stats.total_size += file->length;
119 @@ -1351,6 +1356,7 @@ struct file_list *recv_file_list(int f)
120 clean_flist(flist, relative_paths, 1);
122 SORT_FILE_ACL_INDEX_LISTS();
123 + SORT_FILE_XATTR_INDEX_LISTS();
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
129 @@ -729,6 +729,10 @@ static void recv_generator(char *fname,
131 SET_ACL(fname, file);
133 +#ifdef SUPPORT_XATTRS
135 + SET_XATTR(fname, file);
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
143 +/* Extended attribute support for rsync. */
144 +/* This file Copyright (C) 2004 Red Hat, Inc. */
145 +/* Written by Jay Fenlason */
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.
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.
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.
164 +#if defined(HAVE_LINUX_XATTRS)
166 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
168 + return lgetxattr(path, name, value, size);
171 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags)
173 + return lsetxattr(path, name, value, size, flags);
176 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
178 + return llistxattr(path, list, size);
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
187 +#if defined(HAVE_LINUX_XATTRS)
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);
195 +#endif /* No xattrs */
196 --- orig/options.c 2005-08-27 21:15:29
197 +++ options.c 2005-08-27 21:29:40
198 @@ -44,6 +44,7 @@ int copy_links = 0;
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;
206 @@ -184,6 +185,7 @@ static void print_rsync_version(enum log
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;
214 @@ -203,7 +205,9 @@ static void print_rsync_version(enum log
219 +#ifdef SUPPORT_XATTRS
225 @@ -218,9 +222,9 @@ static void print_rsync_version(enum log
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);
235 /* Note that this field may not have type ino_t. It depends
236 * on the complicated interaction between largefile feature
237 @@ -291,6 +295,7 @@ void usage(enum logcode F)
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");
245 @@ -397,6 +402,9 @@ static struct poptOption long_options[]
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 },
255 @@ -994,6 +1002,17 @@ int parse_arguments(int *argc, const cha
257 #endif /* SUPPORT_ACLS */
260 +#ifdef SUPPORT_XATTRS
261 + preserve_xattrs = 1;
262 + preserve_perms = 1;
265 + snprintf(err_buf,sizeof(err_buf),
266 + "extended attributes are not supported on this %s\n",
267 + am_server ? "server" : "client");
269 +#endif /* SUPPORT_XATTRS */
272 /* A large opt value means that set_refuse_options()
273 @@ -1418,6 +1437,8 @@ void server_options(char **args,int *arg
277 + if (preserve_xattrs)
282 --- orig/rsync.c 2004-07-03 20:11:58
283 +++ rsync.c 2005-10-16 23:19:27
284 @@ -139,12 +139,15 @@ int set_perms(char *fname,struct file_st
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)) {
296 if (SET_ACL(fname, file) == 0)
298 + if (SET_XATTR(fname, file) == 0)
302 if (verbose > 1 && flags & PERMS_REPORT) {
303 --- orig/rsync.h 2005-07-29 02:25:55
304 +++ rsync.h 2005-10-16 23:19:44
305 @@ -689,6 +689,38 @@ struct stats {
306 #endif /* SUPPORT_ACLS */
307 #include "smb_acls.h"
309 +#ifdef HAVE_LINUX_XATTRS
310 +#define SUPPORT_XATTRS 1
313 +#ifdef SUPPORT_XATTRS
314 +#ifdef HAVE_ATTR_XATTR_H
315 +#include <attr/xattr.h>
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()
333 +#define SET_XATTR(fname, file) 1 /* checked return value */
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()
338 +#define DUP_XATTR(src, orig) 1 /* checked return value */
339 +#endif /* SUPPORT_XATTRS */
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
346 @@ -317,6 +317,7 @@ to the detailed description below for a
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)
354 @@ -672,6 +673,11 @@ ACLs to be the same as the local ACLs.
355 remote machine's rsync supports this option also. This is a non-standard
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.
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
366 --- orig/xattr.c 2005-10-16 23:25:12
367 +++ xattr.c 2005-10-16 23:25:12
369 +/* Extended Attribute support for rsync */
370 +/* Copyright (C) 2004 Red Hat, Inc */
371 +/* Written by Jay Fenlason, vaguely based on the ACLs patch */
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.
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.
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.
389 +#include "lib/sysxattr.h"
391 +#ifdef SUPPORT_XATTRS
393 +extern int preserve_xattrs;
396 +#define RSYNC_XAL_INITIAL 5
397 +#define RSYNC_XAL_LIST_INITIAL 100
418 +static size_t namebuf_len = 0;
419 +static char *namebuf = NULL;
421 +static size_t datumbuf_len = 0;
422 +static char *datumbuf = NULL;
424 +static rsync_xal curr_rsync_xal = { 0, 0, NULL };
425 +static rsync_xal_list rsync_xal_l = { 0, 0, NULL };
428 +/* ------------------------------------------------------------------------- */
430 +/* the below stuff is only used by the receiver */
432 +/* structure to hold index to rsync_xal_l member corresponding to
433 + * flist->files[i] */
436 + const struct file_struct *file;
443 + file_xal_index *filexalidxs;
444 +} file_xal_index_list;
446 +static file_xal_index_list fxil = {0, 0, NULL };
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;
456 +/* ------------------------------------------------------------------------- */
458 +static void rsync_xal_free(rsync_xal *x)
462 + for (i = 0; i < x->count; i++) {
463 + free(x->rxas[i].name);
464 + /* free(x->rxas[i].value); */
469 +static int rsync_xal_compare_names(const void *x1, const void *x2)
471 + const rsync_xa *xa1;
472 + const rsync_xa *xa2;
476 + return strcmp(xa1->name, xa2->name);
479 +static int rsync_xal_get(const char *fname, rsync_xal *x)
482 + ssize_t datum_size;
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");
497 + name_size = sys_llistxattr(fname, namebuf, namebuf_len);
498 + if (name_size > (ssize_t)namebuf_len) {
502 + if (name_size < 0) {
503 + if (errno == ENOTSUP)
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",
509 + fname, strerror(errno));
512 + namebuf = realloc_array(namebuf, char, name_size + 1);
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) {
519 + "%s: rsync_xal_get: re-llistxattr failed: %s\n",
520 + fname, strerror(errno));
525 + "%s: rsync_xal_get: llistxattr failed: %s\n",
526 + fname, strerror(errno));
531 + if (name_size == 0)
533 + for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
534 + len = strlen(name) + 1;
536 + if (x->count >= x->alloc) {
538 + rsync_xa *new_rxas;
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);
543 + out_of_memory("rsync_xal_get");
544 + x->alloc = new_alloc;
545 + x->rxas = new_rxas;
547 + datum_size = sys_lgetxattr(fname, name, datumbuf, datumbuf_len);
548 + if (datum_size > (ssize_t)datumbuf_len) {
552 + if (datum_size < 0) {
553 + if (errno == ENOTSUP)
555 + if (errno == ERANGE) {
556 + datum_size = sys_lgetxattr(fname, name, NULL, 0);
557 + if (datum_size < 0) {
559 + "%s: rsync_xal_get: lgetxattr %s failed: %s\n",
560 + fname, name, strerror(errno));
563 + datumbuf = realloc_array(datumbuf, char, datum_size + 1);
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) {
570 + "%s: rsync_xal_get: re-lgetxattr of %s failed: %s\n",
571 + name, fname, strerror(errno));
576 + "%s: rsync_xal_get: lgetxattr %s failed: %s\n",
577 + fname, name, strerror(errno));
581 + ptr = new_array(char, len + datum_size);
583 + out_of_memory("rsync_xal_get");
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;
593 + if (x->count > 1) {
594 + qsort(x->rxas, x->count, sizeof (rsync_xa), rsync_xal_compare_names);
600 +/* generate the xattr(s) for this flist entry;
601 + * xattr(s) are either sent or cleaned-up by send_xattr() below */
603 +int make_xattr(const struct file_struct *file, const char *fname)
605 + if (!preserve_xattrs || !file)
608 + rsync_xal_get(fname, &curr_rsync_xal);
609 + return 0; /* TODO: This needs to return 1 if no xattrs changed! */
612 +static ssize_t rsync_xal_find_matching(void)
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)
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))
629 + /* no differences found. This is The One! */
630 + if (j == curr_rsync_xal.count)
633 + if (i < rsync_xal_l.count)
635 + return (ssize_t)-1;
638 +/* Store curr_rsync_xal on the end of rsync_xal_l */
639 +static void rsync_xal_store(void)
641 + if (rsync_xal_l.count <= rsync_xal_l.alloc) {
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);
648 + out_of_memory("rsync_xal_store");
649 + rsync_xal_l.alloc = new_alloc;
650 + rsync_xal_l.rxals = new_xal;
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;
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) */
661 +void send_xattr(const struct file_struct *file, int f)
665 + if (!preserve_xattrs || !file)
669 + rsync_xal_free(&curr_rsync_xal);
672 + index = rsync_xal_find_matching();
674 + write_byte(f, 'x');
675 + write_int(f, index);
676 + rsync_xal_free(&curr_rsync_xal);
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);
695 +/* ------------------------------------------------------------------------- */
696 +/* receive and build the rsync_xattr_lists */
698 +void receive_xattr(struct file_struct *file, int f)
703 + if (!preserve_xattrs)
705 + fname = f_name(file);
706 + tag = read_byte(f);
707 + if (tag != 'X' && tag != 'x') {
709 + "%s: receive_xattr: unknown extended attribute type tag: %c\n",
711 + exit_cleanup(RERR_STREAMIO);
714 + if (fxil.alloc <= fxil.count) {
718 + if (fxil.count < RSYNC_XAL_LIST_INITIAL)
719 + new_alloc = fxil.alloc + RSYNC_XAL_LIST_INITIAL;
721 + new_alloc = fxil.alloc * 2;
722 + new_ptr = realloc_array(fxil.filexalidxs, file_xal_index, new_alloc);
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");
731 + fxil.filexalidxs = new_ptr;
732 + fxil.alloc = new_alloc;
735 + fxil.filexalidxs[fxil.count].file = file;
740 + fxil.filexalidxs[fxil.count].xalidx = rsync_xal_l.count;
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++) {
753 + name_len = read_int(f);
754 + datum_len = read_int(f);
755 + ptr = new_array(char, name_len + datum_len);
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;
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",
772 + fname, (unsigned long)index);
773 + exit_cleanup(RERR_STREAMIO);
775 + fxil.filexalidxs[fxil.count].xalidx = index;
780 +static int rsync_xal_set(const char *fname, rsync_xal *x)
785 + for (i = 0; i < x->count; i++) {
786 + int status = sys_lsetxattr(fname, x->rxas[i].name, x->rxas[i].datum, x->rxas[i].datum_len, 0);
788 + rprintf(FERROR, "%s: rsync_xal_set: lsetxattr %s failed: %s\n",
789 + fname, x->rxas[i].name, strerror(errno));
796 +/* for duplicating xattrs on backups when using backup_dir */
798 +int dup_xattr(const char *orig, const char *bak)
802 + if (!preserve_xattrs)
805 + if (rsync_xal_get(orig, &backup_xal) < 0)
806 + ret = rsync_xal_set(bak, &backup_xal);
809 + rsync_xal_free(&backup_xal);
813 +void push_keep_backup_xattr(const struct file_struct *file, const char *orig, const char *dest)
815 + if (!preserve_xattrs)
818 + backup_orig_file = file;
819 + backup_orig_fname = orig;
820 + backup_dest_fname = dest;
821 + rsync_xal_get(orig, &backup_xal);
824 +static int set_keep_backup_xal(void)
826 + if (!preserve_xattrs)
828 + return rsync_xal_set(backup_dest_fname, &backup_xal);
831 +void cleanup_keep_backup_xattr(void)
833 + if (!preserve_xattrs)
836 + backup_orig_file = NULL;
837 + backup_orig_fname = null_string;
838 + backup_dest_fname = null_string;
839 + rsync_xal_free(&backup_xal);
842 +static int file_xal_index_compare(const void *x1, const void *x2)
844 + const file_xal_index *xa1;
845 + const file_xal_index *xa2;
849 + return xa1->file == xa2->file ? 0 : xa1->file < xa2->file ? -1 : 1;
852 +void sort_file_xattr_index_lists(void)
854 + if (!preserve_xattrs)
856 + qsort(fxil.filexalidxs, fxil.count, sizeof (file_xal_index), file_xal_index_compare);
859 +static int find_file_xal_index(const struct file_struct *file)
861 + int low = 0, high = fxil.count;
862 + const struct file_struct *file_mid;
865 + rprintf(FERROR, "find_file_xal_index: no entries\n");
866 + exit_cleanup(RERR_STREAMIO);
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)
878 + } while (low < high);
880 + file_mid = fxil.filexalidxs[low].file;
881 + if (file_mid == file)
882 + return fxil.filexalidxs[low].xalidx;
885 + "find_file_xal_index: can't find entry for file in list\n");
886 + exit_cleanup(RERR_STREAMIO);
890 +/* set extended attributes on rsync-ed or keep_backup-ed file */
892 +int set_xattr(const char *fname, const struct file_struct *file)
897 + if (dry_run || !preserve_xattrs)
899 + if (file == backup_orig_file) {
900 + if (!strcmp(fname, backup_dest_fname))
901 + return set_keep_backup_xal();
903 + xalidx = find_file_xal_index(file);
904 + x = &(rsync_xal_l.rxals[xalidx]);
905 + return rsync_xal_set(fname, x);
908 +#endif /* SUPPORT_XATTRS */