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