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