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