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