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