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