Changed the style of the diff headers (use "patch -p1" now).
[rsync/rsync-patches.git] / xattrs.diff
1 Depends-On-Patch: acls.diff
2
3 After applying this patch, run these commands for a successful build:
4
5     ./prepare-source
6     ./configure --enable-acl-support --enable-xattr-support
7     make
8
9 --- old/Makefile.in
10 +++ new/Makefile.in
11 @@ -27,13 +27,13 @@ VERSION=@VERSION@
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@
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
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 \
22 -       fileio.o batch.o clientname.o chmod.o acls.o
23 +       fileio.o batch.o clientname.o chmod.o acls.o xattr.o
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 --- old/backup.c
28 +++ new/backup.c
29 @@ -136,6 +136,7 @@ static int make_bak_dir(char *fullpath)
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 = '/';
37 @@ -190,6 +191,7 @@ static int keep_backup(char *fname)
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 */
44         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
45 @@ -267,6 +269,7 @@ static int keep_backup(char *fname)
46         }
47         set_file_attrs(buf, file, NULL, 0);
48         CLEANUP_KEEP_BACKUP_ACL();
49 +       CLEANUP_KEEP_BACKUP_XATTR();
50         free(file);
51  
52         if (verbose > 1) {
53 --- old/configure.in
54 +++ new/configure.in
55 @@ -810,6 +810,30 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
56    AC_MSG_RESULT(no)
57  )
58  
59 +AC_CHECK_HEADERS(attr/xattr.h)
60 +AC_MSG_CHECKING(whether to support extended attributes)
61 +AC_ARG_ENABLE(xattr-support,
62 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
63 +[ case "$enableval" in
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  
86 --- old/flist.c
87 +++ new/flist.c
88 @@ -969,6 +969,8 @@ static struct file_struct *send_file_nam
89                 return NULL;
90         if (MAKE_ACL(file, fname) < 0)
91                 return NULL;
92 +       if (MAKE_XATTR(file, fname) < 0)
93 +               return NULL;
94  
95         if (chmod_modes && !S_ISLNK(file->mode))
96                 file->mode = tweak_mode(file->mode, chmod_modes);
97 @@ -981,9 +983,11 @@ static struct file_struct *send_file_nam
98                 flist->files[flist->count++] = file;
99                 send_file_entry(file, f);
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  }
109 @@ -1373,6 +1377,7 @@ struct file_list *recv_file_list(int f)
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;
117 @@ -1397,6 +1402,7 @@ struct file_list *recv_file_list(int f)
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) {
124                 recv_uid_list(f, flist);
125 --- old/generator.c
126 +++ new/generator.c
127 @@ -908,6 +908,10 @@ static void recv_generator(char *fname, 
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))
137                         delete_in_dir(the_file_list, fname, file, &st);
138 --- old/lib/sysxattr.c
139 +++ new/lib/sysxattr.c
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 --- old/lib/sysxattr.h
183 +++ new/lib/sysxattr.h
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 */
194 --- old/options.c
195 +++ new/options.c
196 @@ -45,6 +45,7 @@ int copy_links = 0;
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;
202  int preserve_executability = 0;
203  int preserve_devices = 0;
204 @@ -195,6 +196,7 @@ static void print_rsync_version(enum log
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;
212 @@ -214,7 +216,9 @@ static void print_rsync_version(enum log
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
223 @@ -228,9 +232,9 @@ static void print_rsync_version(enum log
224         rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
225         rprintf(f, "<http://rsync.samba.org/>\n");
226         rprintf(f, "Capabilities: %d-bit files, %ssocketpairs, "
227 -               "%shard links, %sACLs, %ssymlinks, batchfiles,\n",
228 +               "%shard links, %sACLs, %sxattrs, %ssymlinks, batchfiles,\n",
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
235 @@ -302,6 +306,9 @@ void usage(enum logcode F)
236  #ifdef SUPPORT_ACLS
237    rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
238  #endif
239 +#ifdef SUPPORT_XATTRS
240 +  rprintf(F," -X, --xattrs                preserve extended attributes (implies --perms)\n");
241 +#endif
242    rprintf(F,"     --chmod=CHMOD           change destination permissions\n");
243    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
244    rprintf(F," -g, --group                 preserve group\n");
245 @@ -421,6 +428,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 @@ -1085,6 +1095,17 @@ int parse_arguments(int *argc, const cha
256                         return 0;
257  #endif
258  
259 +               case 'X':
260 +#ifdef SUPPORT_XATTRS
261 +                       preserve_xattrs = 1;
262 +                       preserve_perms = 1;
263 +                       break;
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 */
270  
271                 default:
272                         /* A large opt value means that set_refuse_options()
273 @@ -1530,6 +1551,10 @@ void server_options(char **args,int *arg
274         if (preserve_acls)
275                 argstr[x++] = 'A';
276  #endif
277 +#ifdef SUPPORT_XATTRS
278 +       if (preserve_xattrs)
279 +               argstr[x++] = 'X';
280 +#endif
281         if (preserve_uid)
282                 argstr[x++] = 'o';
283         if (preserve_gid)
284 --- old/rsync.c
285 +++ new/rsync.c
286 @@ -206,12 +206,15 @@ int set_file_attrs(char *fname, struct f
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)) {
298                 if (SET_ACL(fname, file) == 0)
299                         updated = 1;
300 +               if (SET_XATTR(fname, file) == 0)
301 +                       updated = 1;
302         }
303  
304         if (verbose > 1 && flags & ATTRS_REPORT) {
305 --- old/rsync.h
306 +++ new/rsync.h
307 @@ -695,6 +695,38 @@ struct chmod_mode_struct;
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()
335 +#define SET_XATTR(fname, file)                 1 /* checked return value */
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()
340 +#define DUP_XATTR(src, orig)           1 /* checked return value */
341 +#endif /* SUPPORT_XATTRS */
342 +
343  #include "proto.h"
344  
345  /* We have replacement versions of these if they're missing. */
346 --- old/rsync.yo
347 +++ new/rsync.yo
348 @@ -318,6 +318,7 @@ to the detailed description below for a 
349   -p, --perms                 preserve permissions
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)
355   -g, --group                 preserve group
356 @@ -751,6 +752,11 @@ dit(bf(-A, --acls)) This option causes r
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).
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 +
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
368 --- old/xattr.c
369 +++ new/xattr.c
370 @@ -0,0 +1,540 @@
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 +
481 +static int rsync_xal_get(const char *fname, rsync_xal *x)
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)
506 +                       return -1;
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",
511 +                                       fname, strerror(errno));
512 +                               return -1;
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));
523 +                               return -1;
524 +                       }
525 +               } else {
526 +                       rprintf(FERROR,
527 +                           "%s: rsync_xal_get: llistxattr failed: %s\n",
528 +                           fname, strerror(errno));
529 +                       return -1;
530 +               }
531 +       }
532 +       rsync_xal_free(x);
533 +       if (name_size == 0)
534 +               return 0;
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)
556 +                               return -1;
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));
563 +                                       return -1;
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));
574 +                                       return -1;
575 +                               }
576 +                       } else {
577 +                               rprintf(FERROR,
578 +                                   "%s: rsync_xal_get: lgetxattr %s failed: %s\n",
579 +                                   fname, name, strerror(errno));
580 +                               return -1;
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 +       }
598 +       return 0;
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 +
605 +int make_xattr(const struct file_struct *file, const char *fname)
606 +{
607 +       if (!preserve_xattrs || !file)
608 +               return 1;
609 +
610 +       rsync_xal_get(fname, &curr_rsync_xal);
611 +       return 0; /* TODO:  This needs to return 1 if no xattrs changed! */
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 +
667 +       if (!preserve_xattrs || !file)
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;
707 +       fname = f_name(file, NULL);
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",
774 +                               fname, (unsigned long)index);
775 +                       exit_cleanup(RERR_STREAMIO);
776 +               }
777 +               fxil.filexalidxs[fxil.count].xalidx = index;
778 +       }
779 +       fxil.count++;
780 +}
781 +
782 +static int rsync_xal_set(const char *fname, rsync_xal *x)
783 +{
784 +       size_t i;
785 +       int ret = 0;
786 +
787 +       for (i = 0; i < x->count; i++) {
788 +               int status = sys_lsetxattr(fname, x->rxas[i].name, x->rxas[i].datum, x->rxas[i].datum_len, 0);
789 +               if (status < 0) {
790 +                       rprintf(FERROR, "%s: rsync_xal_set: lsetxattr %s failed: %s\n",
791 +                               fname, x->rxas[i].name, strerror(errno));
792 +                       ret = -1;
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 +
804 +       if (!preserve_xattrs)
805 +               return 1;
806 +
807 +       if (rsync_xal_get(orig, &backup_xal) < 0)
808 +               ret = rsync_xal_set(bak, &backup_xal);
809 +       else
810 +               ret = 0;
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)
829 +               return 1;
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 +
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 +{
896 +       int xalidx;
897 +       rsync_xal *x;
898 +
899 +       if (dry_run || !preserve_xattrs)
900 +               return 1;
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]);
907 +       return rsync_xal_set(fname, x);
908 +}
909 +
910 +#endif /* SUPPORT_XATTRS */