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