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