Fixed failing hunks and added preallocate.diff.
[rsync-patches.git] / xattrs.diff
1 This patch adds basic support for extended attributes.  It works, but there
2 are some things that still needs to be improved (see TODO list below).
3
4 To use this patch, run these commands for a successful build:
5
6     patch -p1 <patches/acls.diff
7     patch -p1 <patches/xattrs.diff
8     ./prepare-source
9     ./configure --enable-acl-support --enable-xattr-support
10     make
11
12 Alternately, if you don't want ACL support, configure it this way:
13
14     ./configure --enable-xattr-support
15
16 TODO:
17
18  - This patch needs to more efficiently handle large xattrs, especially when
19    they're unchanged.
20
21  - Extraneous xattr values need to be removed from files that are not being
22    recreated.
23
24  - We need to affect the itemized output to know when xattrs are being updated.
25
26  - We need to affect the --link-dest option to avoid hard-linking two files
27    that differ in their xattrs (when --xattrs was specified).
28
29 --- old/Makefile.in
30 +++ new/Makefile.in
31 @@ -28,13 +28,13 @@ VERSION=@VERSION@
32  
33  HEADERS=byteorder.h config.h errcode.h proto.h rsync.h smb_acls.h lib/pool_alloc.h
34  LIBOBJ=lib/wildmatch.o lib/compat.o lib/snprintf.o lib/mdfour.o \
35 -       lib/permstring.o lib/pool_alloc.o lib/sysacls.o @LIBOBJS@
36 +       lib/permstring.o lib/pool_alloc.o lib/sysacls.o lib/sysxattr.o @LIBOBJS@
37  ZLIBOBJ=zlib/deflate.o zlib/inffast.o zlib/inflate.o zlib/inftrees.o \
38         zlib/trees.o zlib/zutil.o zlib/adler32.o zlib/compress.o zlib/crc32.o
39  OBJS1=rsync.o generator.o receiver.o cleanup.o sender.o exclude.o util.o \
40         main.o checksum.o match.o syscall.o log.o backup.o
41  OBJS2=options.o flist.o io.o compat.o hlink.o token.o uidlist.o socket.o \
42 -       fileio.o batch.o clientname.o chmod.o acls.o
43 +       fileio.o batch.o clientname.o chmod.o acls.o xattr.o
44  OBJS3=progress.o pipe.o
45  DAEMON_OBJ = params.o loadparm.o clientserver.o access.o connection.o authenticate.o
46  popt_OBJS=popt/findme.o  popt/popt.o  popt/poptconfig.o \
47 --- old/acls.c
48 +++ new/acls.c
49 @@ -30,6 +30,7 @@ extern int read_only;
50  extern int list_only;
51  extern int orig_umask;
52  extern int preserve_acls;
53 +extern int preserve_xattrs;
54  extern unsigned int file_struct_len;
55  
56  /* === ACL structures === */
57 @@ -741,6 +742,10 @@ void receive_acl(struct file_struct *fil
58         type = SMB_ACL_TYPE_ACCESS;
59         racl_list = &access_acl_list;
60         ndx_ptr = (char*)file + file_struct_len;
61 +#ifdef SUPPORT_XATTRS
62 +       if (preserve_xattrs)
63 +               ndx_ptr += 4;
64 +#endif
65         do {
66                 char tag = read_byte(f);
67                 int ndx;
68 @@ -800,6 +805,10 @@ void cache_acl(struct file_struct *file,
69         racl = sxp->acc_acl;
70         racl_list = &access_acl_list;
71         ndx_ptr = (char*)file + file_struct_len;
72 +#ifdef SUPPORT_XATTRS
73 +       if (preserve_xattrs)
74 +               ndx_ptr += 4;
75 +#endif
76         do {
77                 if (!racl)
78                         ndx = -1;
79 @@ -920,6 +929,10 @@ int set_acl(const char *fname, const str
80  
81         type = SMB_ACL_TYPE_ACCESS;
82         ndx_ptr = (char*)file + file_struct_len;
83 +#ifdef SUPPORT_XATTRS
84 +       if (preserve_xattrs)
85 +               ndx_ptr += 4;
86 +#endif
87         do {
88                 acl_duo *duo_item;
89                 BOOL eq;
90 --- old/backup.c
91 +++ new/backup.c
92 @@ -30,6 +30,7 @@ extern char *backup_dir;
93  
94  extern int am_root;
95  extern int preserve_acls;
96 +extern int preserve_xattrs;
97  extern int preserve_devices;
98  extern int preserve_specials;
99  extern int preserve_links;
100 @@ -136,6 +137,9 @@ static int make_bak_dir(char *fullpath)
101  #ifdef SUPPORT_ACLS
102                                 sx.acc_acl = sx.def_acl = NULL;
103  #endif
104 +#ifdef SUPPORT_XATTRS
105 +                               sx.xattr = NULL;
106 +#endif
107                                 if (!(file = make_file(rel, NULL, NULL, 0, NO_FILTERS)))
108                                         continue;
109  #ifdef SUPPORT_ACLS
110 @@ -144,6 +148,12 @@ static int make_bak_dir(char *fullpath)
111                                         cache_acl(file, &sx);
112                                 }
113  #endif
114 +#ifdef SUPPORT_XATTRS
115 +                               if (preserve_xattrs) {
116 +                                       get_xattr(rel, &sx);
117 +                                       cache_xattr(file, &sx);
118 +                               }
119 +#endif
120                                 set_file_attrs(fullpath, file, NULL, 0);
121                                 free(file);
122                         }
123 @@ -195,6 +205,9 @@ static int keep_backup(const char *fname
124  #ifdef SUPPORT_ACLS
125         sx.acc_acl = sx.def_acl = NULL;
126  #endif
127 +#ifdef SUPPORT_XATTRS
128 +       sx.xattr = NULL;
129 +#endif
130  
131         if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
132                 return 1; /* the file could have disappeared */
133 @@ -208,6 +221,12 @@ static int keep_backup(const char *fname
134                 cache_acl(file, &sx);
135         }
136  #endif
137 +#ifdef SUPPORT_XATTRS
138 +       if (preserve_xattrs) {
139 +               get_xattr(fname, &sx);
140 +               cache_xattr(file, &sx);
141 +       }
142 +#endif
143  
144         /* Check to see if this is a device file, or link */
145         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
146 --- old/configure.in
147 +++ new/configure.in
148 @@ -878,6 +878,40 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
149    AC_MSG_RESULT(no)
150  )
151  
152 +AC_CHECK_HEADERS(attr/xattr.h)
153 +AC_CHECK_HEADERS(sys/xattr.h)
154 +AC_CHECK_HEADERS(sys/extattr.h)
155 +AC_MSG_CHECKING(whether to support extended attributes)
156 +AC_ARG_ENABLE(xattr-support,
157 +AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
158 +[ case "$enableval" in
159 +  yes)
160 +      case "$host_os" in
161 +      *linux*)
162 +            AC_MSG_RESULT(Using Linux xattrs)
163 +            AC_DEFINE(HAVE_LINUX_XATTRS, 1, [True if you have Linux xattrs])
164 +            ;;
165 +      darwin*)
166 +            AC_MSG_RESULT(Using OS X xattrs)
167 +            AC_DEFINE(HAVE_OSX_XATTRS, 1, [True if you have Mac OS X xattrs])
168 +            ;;
169 +      freebsd*)
170 +            AC_MSG_RESULT(Using FreeBSD extattrs)
171 +            AC_DEFINE(HAVE_FREEBSD_XATTRS, 1, [True if you have FreeBSD xattrs])
172 +            ;;
173 +      *)
174 +            AC_MSG_RESULT(Xattrs requested but not Linux or OS X.  Good luck...)
175 +            ;;
176 +      esac
177 +      ;;
178 +  *)
179 +      AC_MSG_RESULT(no)
180 +      AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
181 +  esac ],
182 +  AC_MSG_RESULT(no)
183 +  AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
184 +)
185 +
186  AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
187  AC_OUTPUT
188  
189 --- old/flist.c
190 +++ new/flist.c
191 @@ -41,6 +41,7 @@ extern int one_file_system;
192  extern int copy_dirlinks;
193  extern int keep_dirlinks;
194  extern int preserve_acls;
195 +extern int preserve_xattrs;
196  extern int preserve_links;
197  extern int preserve_hard_links;
198  extern int preserve_devices;
199 @@ -499,7 +500,7 @@ static struct file_struct *receive_file_
200         char thisname[MAXPATHLEN];
201         unsigned int l1 = 0, l2 = 0;
202         int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
203 -#ifdef SUPPORT_ACLS
204 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
205         int xtra_len;
206  #endif
207         OFF_T file_length;
208 @@ -611,10 +612,16 @@ static struct file_struct *receive_file_
209                 xtra_len = (S_ISDIR(mode) ? 2 : 1) * 4;
210         else
211                 xtra_len = 0;
212 +#elif defined SUPPORT_XATTRS
213 +       xtra_len = 0;
214 +#endif
215 +#ifdef SUPPORT_XATTRS
216 +       if (preserve_xattrs)
217 +               xtra_len += 4;
218  #endif
219  
220         alloc_len = file_struct_len + dirname_len + basename_len
221 -#ifdef SUPPORT_ACLS
222 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
223                   + xtra_len
224  #endif
225                   + linkname_len + sum_len;
226 @@ -623,7 +630,7 @@ static struct file_struct *receive_file_
227         file = (struct file_struct *)bp;
228         memset(bp, 0, file_struct_len);
229         bp += file_struct_len;
230 -#ifdef SUPPORT_ACLS
231 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
232         bp += xtra_len;
233  #endif
234  
235 @@ -724,6 +731,10 @@ static struct file_struct *receive_file_
236         if (preserve_acls)
237                 receive_acl(file, f);
238  #endif
239 +#ifdef SUPPORT_XATTRS
240 +       if (preserve_xattrs)
241 +               receive_xattr(file, f );
242 +#endif
243  
244         return file;
245  }
246 @@ -982,7 +993,7 @@ static struct file_struct *send_file_nam
247                                           unsigned short flags)
248  {
249         struct file_struct *file;
250 -#ifdef SUPPORT_ACLS
251 +#if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
252         statx sx;
253  #endif
254  
255 @@ -1002,6 +1013,13 @@ static struct file_struct *send_file_nam
256                         return NULL;
257         }
258  #endif
259 +#ifdef SUPPORT_XATTRS
260 +       if (preserve_xattrs && f >= 0) {
261 +               sx.xattr = NULL;
262 +               if (get_xattr(fname, &sx) < 0)
263 +                       return NULL;
264 +       }
265 +#endif
266  
267         maybe_emit_filelist_progress(flist->count + flist_count_offset);
268  
269 @@ -1014,11 +1032,19 @@ static struct file_struct *send_file_nam
270                 if (preserve_acls && f >= 0)
271                         send_acl(&sx, f);
272  #endif
273 +#ifdef SUPPORT_XATTRS
274 +               if (preserve_xattrs && f >= 0)
275 +                       send_xattr(&sx, f);
276 +#endif
277         } else {
278  #ifdef SUPPORT_ACLS
279                 if (preserve_acls && f >= 0)
280                         free_acl(&sx);
281  #endif
282 +#ifdef SUPPORT_XATTRS
283 +               if (preserve_xattrs && f >= 0)
284 +                       free_xattr(&sx);
285 +#endif
286         }
287         return file;
288  }
289 --- old/lib/sysxattr.c
290 +++ new/lib/sysxattr.c
291 @@ -0,0 +1,135 @@
292 +/*
293 + * Extended attribute support for rsync.
294 + *
295 + * Copyright (C) 2004 Red Hat, Inc.
296 + * Written by Jay Fenlason.
297 + *
298 + * This program is free software; you can redistribute it and/or modify
299 + * it under the terms of the GNU General Public License as published by
300 + * the Free Software Foundation; either version 2 of the License, or
301 + * (at your option) any later version.
302 + *
303 + * This program is distributed in the hope that it will be useful,
304 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
305 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
306 + * GNU General Public License for more details.
307 + *
308 + * You should have received a copy of the GNU General Public License along
309 + * with this program; if not, write to the Free Software Foundation, Inc.,
310 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
311 + */
312 +
313 +#include "rsync.h"
314 +#include "sysxattr.h"
315 +
316 +#ifdef SUPPORT_XATTRS
317 +
318 +#if defined HAVE_LINUX_XATTRS
319 +
320 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
321 +{
322 +       return lgetxattr(path, name, value, size);
323 +}
324 +
325 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
326 +{
327 +       return fgetxattr(filedes, name, value, size);
328 +}
329 +
330 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
331 +{
332 +       return lsetxattr(path, name, value, size, 0);
333 +}
334 +
335 +int sys_lremovexattr(const char *path, const char *name)
336 +{
337 +       return lremovexattr(path, name);
338 +}
339 +
340 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
341 +{
342 +       return llistxattr(path, list, size);
343 +}
344 +
345 +#elif HAVE_OSX_XATTRS
346 +
347 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
348 +{
349 +       return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
350 +}
351 +
352 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
353 +{
354 +       return fgetxattr(filedes, name, value, size, 0, 0);
355 +}
356 +
357 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
358 +{
359 +       return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
360 +}
361 +
362 +int sys_lremovexattr(const char *path, const char *name)
363 +{
364 +       return removexattr(path, name, XATTR_NOFOLLOW);
365 +}
366 +
367 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
368 +{
369 +       return listxattr(path, list, size, XATTR_NOFOLLOW);
370 +}
371 +
372 +#elif HAVE_FREEBSD_XATTRS
373 +
374 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
375 +{
376 +       return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
377 +}
378 +
379 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
380 +{
381 +       return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
382 +}
383 +
384 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
385 +{
386 +       return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
387 +}
388 +
389 +int sys_lremovexattr(const char *path, const char *name)
390 +{
391 +       return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
392 +}
393 +
394 +ssize_t sys_llistxattr(const char *path, char *list, size_t size)
395 +{
396 +       unsigned char keylen;
397 +       ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
398 +
399 +       if (len <= 0 || (size_t)len > size)
400 +               return len;
401 +
402 +       /* FreeBSD puts a single-byte length before each string, with no '\0'
403 +        * terminator.  We need to change this into a series of null-terminted
404 +        * strings.  Since the size is the same, we can simply transform the
405 +        * output in place. */
406 +       for (off = 0; off < len; off += keylen + 1) {
407 +               keylen = ((unsigned char*)list)[off];
408 +               if (off + keylen >= len) {
409 +                       /* Should be impossible, but kernel bugs happen! */
410 +                       errno = EINVAL;
411 +                       return -1;
412 +               }
413 +               memmove(list+off, list+off+1, keylen);
414 +               list[off+keylen] = '\0';
415 +       }
416 +
417 +       return len;
418 +}
419 +
420 +#else
421 +
422 +#error You need to create xattr compatibility functions.
423 +
424 +#endif
425 +
426 +#endif /* SUPPORT_XATTRS */
427 --- old/lib/sysxattr.h
428 +++ new/lib/sysxattr.h
429 @@ -0,0 +1,26 @@
430 +#ifdef SUPPORT_XATTRS
431 +
432 +#if defined HAVE_ATTR_XATTR_H
433 +#include <attr/xattr.h>
434 +#elif defined HAVE_SYS_XATTR_H
435 +#include <sys/xattr.h>
436 +#elif defined HAVE_SYS_EXTATTR_H
437 +#include <sys/extattr.h>
438 +#endif
439 +
440 +/* Linux 2.4 does not define this as a distinct errno value: */
441 +#ifndef ENOATTR
442 +#define ENOATTR ENODATA
443 +#endif
444 +
445 +ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
446 +ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
447 +int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size);
448 +int sys_lremovexattr(const char *path, const char *name);
449 +ssize_t sys_llistxattr(const char *path, char *list, size_t size);
450 +
451 +#else
452 +
453 +/* No xattrs available */
454 +
455 +#endif
456 --- old/options.c
457 +++ new/options.c
458 @@ -48,6 +48,7 @@ int copy_links = 0;
459  int preserve_links = 0;
460  int preserve_hard_links = 0;
461  int preserve_acls = 0;
462 +int preserve_xattrs = 0;
463  int preserve_perms = 0;
464  int preserve_executability = 0;
465  int preserve_devices = 0;
466 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
467         char const *have_inplace = "no ";
468         char const *hardlinks = "no ";
469         char const *acls = "no ";
470 +       char const *xattrs = "no ";
471         char const *links = "no ";
472         char const *ipv6 = "no ";
473         STRUCT_STAT *dumstat;
474 @@ -220,7 +222,9 @@ static void print_rsync_version(enum log
475  #ifdef SUPPORT_ACLS
476         acls = "";
477  #endif
478 -
479 +#ifdef SUPPORT_XATTRS
480 +       xattrs = "";
481 +#endif
482  #ifdef SUPPORT_LINKS
483         links = "";
484  #endif
485 @@ -239,8 +243,8 @@ static void print_rsync_version(enum log
486                 (int)(sizeof (int64) * 8));
487         rprintf(f, "    %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
488                 got_socketpair, hardlinks, links, ipv6, have_inplace);
489 -       rprintf(f, "    %sappend, %sACLs\n",
490 -               have_inplace, acls);
491 +       rprintf(f, "    %sappend, %sACLs, %sxattrs\n",
492 +               have_inplace, acls, xattrs);
493  
494  #ifdef MAINTAINER_MODE
495         rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
496 @@ -286,7 +290,7 @@ void usage(enum logcode F)
497    rprintf(F," -q, --quiet                 suppress non-error messages\n");
498    rprintf(F,"     --no-motd               suppress daemon-mode MOTD (see manpage caveat)\n");
499    rprintf(F," -c, --checksum              skip based on checksum, not mod-time & size\n");
500 -  rprintf(F," -a, --archive               archive mode; same as -rlptgoD (no -H, -A)\n");
501 +  rprintf(F," -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)\n");
502    rprintf(F,"     --no-OPTION             turn off an implied OPTION (e.g. --no-D)\n");
503    rprintf(F," -r, --recursive             recurse into directories\n");
504    rprintf(F," -R, --relative              use relative path names\n");
505 @@ -311,6 +315,9 @@ void usage(enum logcode F)
506  #ifdef SUPPORT_ACLS
507    rprintf(F," -A, --acls                  preserve ACLs (implies --perms)\n");
508  #endif
509 +#ifdef SUPPORT_XATTRS
510 +  rprintf(F," -X, --xattrs                preserve extended attributes (implies --perms)\n");
511 +#endif
512    rprintf(F," -o, --owner                 preserve owner (super-user only)\n");
513    rprintf(F," -g, --group                 preserve group\n");
514    rprintf(F,"     --devices               preserve device files (super-user only)\n");
515 @@ -434,6 +441,9 @@ static struct poptOption long_options[] 
516    {"acls",            'A', POPT_ARG_NONE,   0, 'A', 0, 0 },
517    {"no-acls",          0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
518    {"no-A",             0,  POPT_ARG_VAL,    &preserve_acls, 0, 0, 0 },
519 +  {"xattrs",          'X', POPT_ARG_NONE,   0, 'X', 0, 0 },
520 +  {"no-xattrs",        0,  POPT_ARG_VAL,    &preserve_xattrs, 0, 0, 0 },
521 +  {"no-X",             0,  POPT_ARG_VAL,    &preserve_xattrs, 0, 0, 0 },
522    {"times",           't', POPT_ARG_VAL,    &preserve_times, 1, 0, 0 },
523    {"no-times",         0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
524    {"no-t",             0,  POPT_ARG_VAL,    &preserve_times, 0, 0, 0 },
525 @@ -1116,6 +1126,17 @@ int parse_arguments(int *argc, const cha
526                         return 0;
527  #endif
528  
529 +               case 'X':
530 +#ifdef SUPPORT_XATTRS
531 +                       preserve_xattrs = 1;
532 +                       preserve_perms = 1;
533 +                       break;
534 +#else
535 +                       snprintf(err_buf,sizeof(err_buf),
536 +                                "extended attributes are not supported on this %s\n",
537 +                                am_server ? "server" : "client");
538 +                       return 0;
539 +#endif
540  
541                 default:
542                         /* A large opt value means that set_refuse_options()
543 @@ -1563,6 +1584,10 @@ void server_options(char **args,int *arg
544         if (preserve_acls)
545                 argstr[x++] = 'A';
546  #endif
547 +#ifdef SUPPORT_XATTRS
548 +       if (preserve_xattrs)
549 +               argstr[x++] = 'X';
550 +#endif
551         if (preserve_uid)
552                 argstr[x++] = 'o';
553         if (preserve_gid)
554 --- old/rsync.c
555 +++ new/rsync.c
556 @@ -33,6 +33,7 @@
557  extern int verbose;
558  extern int dry_run;
559  extern int preserve_acls;
560 +extern int preserve_xattrs;
561  extern int preserve_perms;
562  extern int preserve_executability;
563  extern int preserve_times;
564 @@ -218,6 +219,10 @@ int set_file_attrs(char *fname, struct f
565         if (daemon_chmod_modes && !S_ISLNK(new_mode))
566                 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
567  
568 +#ifdef SUPPORT_XATTRS
569 +       if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
570 +               updated = 1;
571 +#endif
572  #ifdef SUPPORT_ACLS
573         /* It's OK to call set_acl() now, even for a dir, as the generator
574          * will enable owner-writability using chmod, if necessary.
575 --- old/rsync.h
576 +++ new/rsync.h
577 @@ -501,6 +501,10 @@ struct idev {
578  #define ACLS_NEED_MASK 1
579  #endif
580  
581 +#ifndef HAVE_NO_XATTRS
582 +#define SUPPORT_XATTRS 1
583 +#endif
584 +
585  #define GID_NONE ((gid_t)-1)
586  
587  #define HL_CHECK_MASTER        0
588 @@ -699,6 +703,9 @@ typedef struct {
589      struct rsync_acl *acc_acl; /* access ACL */
590      struct rsync_acl *def_acl; /* default ACL */
591  #endif
592 +#ifdef SUPPORT_XATTRS
593 +    item_list *xattr;
594 +#endif
595  } statx;
596  
597  #define ACL_READY(sx) ((sx).acc_acl != NULL)
598 --- old/rsync.yo
599 +++ new/rsync.yo
600 @@ -301,7 +301,7 @@ to the detailed description below for a 
601   -q, --quiet                 suppress non-error messages
602       --no-motd               suppress daemon-mode MOTD (see caveat)
603   -c, --checksum              skip based on checksum, not mod-time & size
604 - -a, --archive               archive mode; same as -rlptgoD (no -H, -A)
605 + -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
606       --no-OPTION             turn off an implied OPTION (e.g. --no-D)
607   -r, --recursive             recurse into directories
608   -R, --relative              use relative path names
609 @@ -324,6 +324,7 @@ to the detailed description below for a 
610   -E, --executability         preserve executability
611       --chmod=CHMOD           affect file and/or directory permissions
612   -A, --acls                  preserve ACLs (implies -p) [non-standard]
613 + -X, --xattrs                preserve extended attrs (implies -p) [n.s.]
614   -o, --owner                 preserve owner (super-user only)
615   -g, --group                 preserve group
616       --devices               preserve device files (super-user only)
617 @@ -819,6 +820,11 @@ version makes it incompatible with sendi
618  rsync unless you double the bf(--acls) option (e.g. bf(-AA)).  This
619  doubling is not needed when pulling files from an older rsync.
620  
621 +dit(bf(-X, --xattrs)) This option causes rsync to update the remote
622 +extended attributes to be the same as the local ones.  This will work
623 +only if the remote machine's rsync supports this option also. This is
624 +a non-standard option.
625 +
626  dit(bf(--chmod)) This option tells rsync to apply one or more
627  comma-separated "chmod" strings to the permission of the files in the
628  transfer.  The resulting value is treated as though it was the permissions
629 --- old/xattr.c
630 +++ new/xattr.c
631 @@ -0,0 +1,417 @@
632 +/*
633 + * Extended Attribute support for rsync.
634 + * Written by Jay Fenlason, vaguely based on the ACLs patch.
635 + *
636 + * Copyright (C) 2004 Red Hat, Inc.
637 + * Copyright (C) 2006 Wayne Davison
638 + *
639 + * This program is free software; you can redistribute it and/or modify
640 + * it under the terms of the GNU General Public License as published by
641 + * the Free Software Foundation; either version 2 of the License, or
642 + * (at your option) any later version.
643 + *
644 + * This program is distributed in the hope that it will be useful,
645 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
646 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
647 + * GNU General Public License for more details.
648 + *
649 + * You should have received a copy of the GNU General Public License along
650 + * with this program; if not, write to the Free Software Foundation, Inc.,
651 + * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
652 + */
653 +
654 +#include "rsync.h"
655 +#include "lib/sysxattr.h"
656 +
657 +#ifdef SUPPORT_XATTRS
658 +
659 +extern int dry_run;
660 +extern int am_root;
661 +extern int read_only;
662 +extern int list_only;
663 +extern unsigned int file_struct_len;
664 +
665 +#define RSYNC_XAL_INITIAL 5
666 +#define RSYNC_XAL_LIST_INITIAL 100
667 +
668 +#define HAS_PREFIX(str, prfx) (*(str) == *(prfx) \
669 +                           && strncmp(str, prfx, sizeof (prfx) - 1) == 0)
670 +
671 +#define USER_PREFIX "user."
672 +#define UPRE_LEN ((int)sizeof USER_PREFIX - 1)
673 +#define SYSTEM_PREFIX "system."
674 +#define SPRE_LEN ((int)sizeof SYSTEM_PREFIX - 1)
675 +
676 +#ifdef HAVE_LINUX_XATTRS
677 +#define RPRE_LEN 0
678 +#else
679 +#define RSYNC_PREFIX "rsync."
680 +#define RPRE_LEN ((int)sizeof RSYNC_PREFIX - 1)
681 +#endif
682 +
683 +typedef struct {
684 +       char *datum, *name;
685 +       size_t datum_len, name_len;
686 +} rsync_xa;
687 +
688 +static size_t namebuf_len = 0;
689 +static char *namebuf = NULL;
690 +
691 +static item_list empty_xattr = EMPTY_ITEM_LIST;
692 +static item_list rsync_xal_l = EMPTY_ITEM_LIST;
693 +
694 +/* ------------------------------------------------------------------------- */
695 +
696 +static void rsync_xal_free(item_list *xalp)
697 +{
698 +       size_t i;
699 +       rsync_xa *rxas = xalp->items;
700 +
701 +       for (i = 0; i < xalp->count; i++) {
702 +               free(rxas[i].datum);
703 +               /*free(rxas[i].name);*/
704 +       }
705 +       xalp->count = 0;
706 +}
707 +
708 +void free_xattr(statx *sxp)
709 +{
710 +       rsync_xal_free(sxp->xattr);
711 +       free(sxp->xattr);
712 +       sxp->xattr = NULL;
713 +}
714 +
715 +static int rsync_xal_compare_names(const void *x1, const void *x2)
716 +{
717 +       const rsync_xa *xa1 = x1;
718 +       const rsync_xa *xa2 = x2;
719 +       return strcmp(xa1->name, xa2->name);
720 +}
721 +
722 +static int rsync_xal_get(const char *fname, item_list *xalp)
723 +{
724 +       ssize_t list_len, name_len, datum_len;
725 +       char *name, *ptr;
726 +
727 +       if (!namebuf) {
728 +               namebuf_len = 1024;
729 +               namebuf = new_array(char, namebuf_len);
730 +               if (!namebuf)
731 +                       out_of_memory("rsync_xal_get");
732 +       }
733 +
734 +       /* The length returned includes all the '\0' terminators. */
735 +       list_len = sys_llistxattr(fname, namebuf, namebuf_len);
736 +       if (list_len > (ssize_t)namebuf_len) {
737 +               list_len = -1;
738 +               errno = ERANGE;
739 +       }
740 +       if (list_len < 0) {
741 +               if (errno == ENOTSUP)
742 +                       return 0;
743 +               if (errno == ERANGE) {
744 +                       list_len = sys_llistxattr(fname, NULL, 0);
745 +                       if (list_len < 0) {
746 +                               rsyserr(FERROR, errno,
747 +                                       "rsync_xal_get: llistxattr(\"%s\",0) failed",
748 +                                       fname);
749 +                               return -1;
750 +                       }
751 +                       namebuf = realloc_array(namebuf, char, list_len + 1024);
752 +                       if (!namebuf)
753 +                               out_of_memory("rsync_xal_get");
754 +                       namebuf_len = list_len + 1024;
755 +                       list_len = sys_llistxattr(fname, namebuf, namebuf_len);
756 +                       if (list_len < 0) {
757 +                               rsyserr(FERROR, errno,
758 +                                       "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
759 +                                       fname, (long)namebuf_len);
760 +                               return -1;
761 +                       }
762 +               } else {
763 +                       rsyserr(FERROR, errno,
764 +                               "rsync_xal_get: llistxattr(\"%s\",%ld) failed",
765 +                               fname, (long)namebuf_len);
766 +                       return -1;
767 +               }
768 +       }
769 +
770 +       for (name = namebuf; list_len > 0; name += name_len) {
771 +               rsync_xa *rxas;
772 +
773 +               name_len = strlen(name) + 1;
774 +               list_len -= name_len;
775 +
776 +#ifdef HAVE_LINUX_XATTRS
777 +               /* We don't send the system namespace. */
778 +               if (HAS_PREFIX(name, SYSTEM_PREFIX))
779 +                       continue;
780 +#endif
781 +
782 +               datum_len = sys_lgetxattr(fname, name, NULL, 0);
783 +               if (datum_len < 0) {
784 +                       if (errno == ENOTSUP)
785 +                               return -1;
786 +                       rsyserr(FERROR, errno,
787 +                           "rsync_xal_get: lgetxattr(\"%s\",\"%s\",0) failed",
788 +                           fname, name);
789 +                       return -1;
790 +               }
791 +               ptr = new_array(char, name_len + datum_len);
792 +               if (!ptr)
793 +                       out_of_memory("rsync_xal_get");
794 +               if (datum_len) {
795 +                       ssize_t len = sys_lgetxattr(fname, name, ptr, datum_len);
796 +                       if (len != datum_len) {
797 +                               if (len < 0) {
798 +                                       rsyserr(FERROR, errno,
799 +                                           "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
800 +                                           " failed", fname, name, (long)datum_len);
801 +                               } else {
802 +                                       rprintf(FERROR,
803 +                                           "rsync_xal_get: lgetxattr(\"%s\",\"%s\",%ld)"
804 +                                           " returned %ld\n", fname, name,
805 +                                           (long)datum_len, (long)len);
806 +                               }
807 +                               free(ptr);
808 +                               return -1;
809 +                       }
810 +               }
811 +               rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
812 +               rxas->name = ptr + datum_len;
813 +               rxas->datum = ptr;
814 +               rxas->name_len = name_len;
815 +               rxas->datum_len = datum_len;
816 +               memcpy(rxas->name, name, name_len);
817 +       }
818 +       if (xalp->count > 1)
819 +               qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
820 +       return 0;
821 +}
822 +
823 +/* Read the xattr(s) for this filename. */
824 +int get_xattr(const char *fname, statx *sxp)
825 +{
826 +       sxp->xattr = new(item_list);
827 +       *sxp->xattr = empty_xattr;
828 +       if (rsync_xal_get(fname, sxp->xattr) < 0) {
829 +               free_xattr(sxp);
830 +               return -1;
831 +       }
832 +       return 0;
833 +}
834 +
835 +static int find_matching_xattr(item_list *xalp)
836 +{
837 +       size_t i, j;
838 +       item_list *lst = rsync_xal_l.items;
839 +
840 +       for (i = 0; i < rsync_xal_l.count; i++) {
841 +               rsync_xa *rxas1 = lst[i].items;
842 +               rsync_xa *rxas2 = xalp->items;
843 +
844 +               /* Wrong number of elements? */
845 +               if (lst[i].count != xalp->count)
846 +                       continue;
847 +               /* any elements different? */
848 +               for (j = 0; j < xalp->count; j++) {
849 +                       if (rxas1[j].name_len != rxas2[j].name_len
850 +                        || rxas1[j].datum_len != rxas2[j].datum_len
851 +                        || strcmp(rxas1[j].name, rxas2[j].name)
852 +                        || memcmp(rxas1[j].datum, rxas2[j].datum, rxas2[j].datum_len))
853 +                               break;
854 +               }
855 +               /* no differences found.  This is The One! */
856 +               if (j == xalp->count)
857 +                       return i;
858 +       }
859 +
860 +       return -1;
861 +}
862 +
863 +/* Store *xalp on the end of rsync_xal_l */
864 +static void rsync_xal_store(item_list *xalp)
865 +{
866 +       item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
867 +       /* Since the following call starts a new list, we know it will hold the
868 +        * entire initial-count, not just enough space for one new item. */
869 +       *new_lst = empty_xattr;
870 +       (void)EXPAND_ITEM_LIST(new_lst, rsync_xa, xalp->count);
871 +       memcpy(new_lst->items, xalp->items, xalp->count * sizeof (rsync_xa));
872 +       new_lst->count = xalp->count;
873 +       xalp->count = 0;
874 +}
875 +
876 +/* Send the make_xattr()-generated xattr list for this flist entry. */
877 +void send_xattr(statx *sxp, int f)
878 +{
879 +       int ndx = find_matching_xattr(sxp->xattr);
880 +       if (ndx != -1) {
881 +               write_byte(f, 'x');
882 +               write_int(f, ndx);
883 +               rsync_xal_free(sxp->xattr);
884 +       } else {
885 +               rsync_xa *rxa;
886 +               int count = sxp->xattr->count;
887 +               write_byte(f, 'X');
888 +               write_int(f, count);
889 +               for (rxa = sxp->xattr->items; count--; rxa++) {
890 +#ifdef HAVE_LINUX_XATTRS
891 +                       write_int(f, rxa->name_len);
892 +                       write_int(f, rxa->datum_len);
893 +                       write_buf(f, rxa->name, rxa->name_len);
894 +#else
895 +                       /* We strip the rsync prefix from disguised namespaces
896 +                        * and put everything else in the user namespace. */
897 +                       if (HAS_PREFIX(rxa->name, RSYNC_PREFIX)
898 +                        && rxa->name[RPRE_LEN] != '%') {
899 +                               write_int(f, rxa->name_len - RPRE_LEN);
900 +                               write_int(f, rxa->datum_len);
901 +                               write_buf(f, rxa->name + RPRE_LEN, rxa->name_len - RPRE_LEN);
902 +                       } else {
903 +                               write_int(f, rxa->name_len + UPRE_LEN);
904 +                               write_int(f, rxa->datum_len);
905 +                               write_buf(f, USER_PREFIX, UPRE_LEN);
906 +                               write_buf(f, rxa->name, rxa->name_len);
907 +                       }
908 +#endif
909 +                       write_buf(f, rxa->datum, rxa->datum_len);
910 +               }
911 +               rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
912 +       }
913 +       free_xattr(sxp);
914 +}
915 +
916 +/* ------------------------------------------------------------------------- */
917 +
918 +/* receive and build the rsync_xattr_lists */
919 +void receive_xattr(struct file_struct *file, int f)
920 +{
921 +       static item_list temp_xattr = EMPTY_ITEM_LIST;
922 +       char *ndx_ptr = (char*)file + file_struct_len;
923 +       int ndx, tag = read_byte(f);
924 +
925 +       if (tag == 'X') {
926 +               int i, count = read_int(f);
927 +               for (i = 0; i < count; i++) {
928 +                       char *ptr, *name;
929 +                       rsync_xa *rxa;
930 +                       size_t name_len = read_int(f);
931 +                       size_t datum_len = read_int(f);
932 +#ifdef HAVE_LINUX_XATTRS
933 +                       size_t extra_len = 0;
934 +#else
935 +                       size_t extra_len = am_root ? RPRE_LEN : 0;
936 +                       if (datum_len + extra_len < datum_len)
937 +                               out_of_memory("receive_xattr"); /* overflow */
938 +#endif
939 +                       if (name_len + datum_len + extra_len < name_len)
940 +                               out_of_memory("receive_xattr"); /* overflow */
941 +                       ptr = new_array(char, name_len + datum_len + extra_len);
942 +                       if (!ptr)
943 +                               out_of_memory("receive_xattr");
944 +                       name = ptr + datum_len + extra_len;
945 +                       read_buf(f, name, name_len);
946 +                       read_buf(f, ptr, datum_len);
947 +#ifdef HAVE_LINUX_XATTRS
948 +                       /* Non-root can only save the user namespace. */
949 +                       if (!am_root && !HAS_PREFIX(name, USER_PREFIX)) {
950 +                               free(ptr);
951 +                               continue;
952 +                       }
953 +#else
954 +                       /* This OS only has a user namespace, so we either
955 +                        * strip the user prefix, or we put a non-user
956 +                        * namespace inside our rsync hierarchy. */
957 +                       if (HAS_PREFIX(name, USER_PREFIX)) {
958 +                               name += UPRE_LEN;
959 +                               name_len -= UPRE_LEN;
960 +                       } else if (am_root) {
961 +                               name -= RPRE_LEN;
962 +                               name_len += RPRE_LEN;
963 +                               memcpy(name, RSYNC_PREFIX, RPRE_LEN);
964 +                       } else {
965 +                               free(ptr);
966 +                               continue;
967 +                       }
968 +#endif
969 +                       rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
970 +                       rxa->name = name;
971 +                       rxa->datum = ptr;
972 +                       rxa->name_len = name_len;
973 +                       rxa->datum_len = datum_len;
974 +               }
975 +               ndx = rsync_xal_l.count; /* pre-incremented count */
976 +               rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
977 +       } else if (tag == 'x') {
978 +               ndx = read_int(f);
979 +               if (ndx < 0 || (size_t)ndx >= rsync_xal_l.count) {
980 +                       rprintf(FERROR, "receive_xattr: xa index %d out of"
981 +                               " range for %s\n", ndx, f_name(file, NULL));
982 +                       exit_cleanup(RERR_STREAMIO);
983 +               }
984 +       } else {
985 +               rprintf(FERROR, "receive_xattr: unknown extended attribute"
986 +                       " type tag (%c) for %s\n", tag, f_name(file, NULL));
987 +               exit_cleanup(RERR_STREAMIO);
988 +       }
989 +
990 +       SIVAL(ndx_ptr, 0, ndx);
991 +}
992 +
993 +/* Turn the xattr data in statx into cached xattr data, setting the index
994 + * values in the file struct. */
995 +void cache_xattr(struct file_struct *file, statx *sxp)
996 +{
997 +       char *ndx_ptr = (char*)file + file_struct_len;
998 +       int ndx;
999 +
1000 +       if (!sxp->xattr)
1001 +               return;
1002 +
1003 +       ndx = find_matching_xattr(sxp->xattr);
1004 +       if (ndx == -1)
1005 +               rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
1006 +       free_xattr(sxp);
1007 +
1008 +       SIVAL(ndx_ptr, 0, ndx);
1009 +}
1010 +
1011 +static int rsync_xal_set(const char *fname, item_list *xalp)
1012 +{
1013 +       rsync_xa *rxas = xalp->items;
1014 +       size_t i;
1015 +       int ret = 0;
1016 +
1017 +       for (i = 0; i < xalp->count; i++) {
1018 +               int status = sys_lsetxattr(fname, rxas[i].name, rxas[i].datum, rxas[i].datum_len);
1019 +               if (status < 0) {
1020 +                       rsyserr(FERROR, errno,
1021 +                               "rsync_xal_set: lsetxattr(\"%s\",\"%s\") failed",
1022 +                               fname, rxas[i].name);
1023 +                       ret = -1;
1024 +               }
1025 +       }
1026 +       return ret;
1027 +}
1028 +
1029 +/* Set extended attributes on indicated filename. */
1030 +int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
1031 +{
1032 +       int ndx;
1033 +       char *ndx_ptr = (char*)file + file_struct_len;
1034 +       item_list *lst = rsync_xal_l.items;
1035 +
1036 +       if (dry_run)
1037 +               return 1; /* FIXME: --dry-run needs to compute this value */
1038 +
1039 +       if (read_only || list_only) {
1040 +               errno = EROFS;
1041 +               return -1;
1042 +       }
1043 +
1044 +       ndx = IVAL(ndx_ptr, 0);
1045 +       return rsync_xal_set(fname, lst + ndx); /* TODO:  This needs to return 1 if no xattrs changed! */
1046 +}
1047 +
1048 +#endif /* SUPPORT_XATTRS */