Updated the opening comments to mention how to apply the patch
[rsync/rsync-patches.git] / xattrs.diff
CommitLineData
03019e41
WD
1This patch adds basic support for extended attributes. It works, but there
2are some things that still needs to be improved (see TODO list below).
bbdff76a 3
03019e41 4To use this patch, run these commands for a successful build:
bbdff76a 5
03019e41
WD
6 patch -p1 <patches/acls.diff
7 patch -p1 <patches/xattrs.diff
27e96866 8 ./prepare-source
f787c90c 9 ./configure --enable-acl-support --enable-xattr-support
bbdff76a
WD
10 make
11
03019e41
WD
12Alternately, if you don't want ACL support, configure it this way:
13
14 ./configure --enable-xattr-support
15
0f6fb3c8
WD
16TODO:
17
d2dc8a18
WD
18 - This patch needs to more efficiently handle large xattrs, especially when
19 they're unchanged.
0f6fb3c8 20
a74926e4
WD
21 - Extraneous xattr values need to be removed from files that are not being
22 recreated.
23
0f6fb3c8
WD
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
9a7eef96
WD
29--- old/Makefile.in
30+++ new/Makefile.in
1ed0b5c9 31@@ -28,13 +28,13 @@ VERSION=@VERSION@
bbdff76a
WD
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@
4bf6f8c7
WD
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
bbdff76a
WD
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 \
610969d1
WD
42- fileio.o batch.o clientname.o chmod.o acls.o
43+ fileio.o batch.o clientname.o chmod.o acls.o xattr.o
bbdff76a
WD
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 \
0f6fb3c8
WD
47--- old/acls.c
48+++ new/acls.c
03019e41 49@@ -30,6 +30,7 @@ extern int read_only;
8889f85e 50 extern int list_only;
0f6fb3c8
WD
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 === */
03019e41 57@@ -741,6 +742,10 @@ void receive_acl(struct file_struct *fil
0f6fb3c8
WD
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;
03019e41 68@@ -800,6 +805,10 @@ void cache_acl(struct file_struct *file,
0f6fb3c8
WD
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;
03019e41 79@@ -920,6 +929,10 @@ int set_acl(const char *fname, const str
0f6fb3c8
WD
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;
9a7eef96
WD
90--- old/backup.c
91+++ new/backup.c
dc52bc1a 92@@ -30,6 +30,7 @@ extern char *backup_dir;
93d6ca6d
WD
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;
0f6fb3c8
WD
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 }
09cc6650
WD
113 #endif
114+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
115+ if (preserve_xattrs) {
116+ get_xattr(rel, &sx);
117+ cache_xattr(file, &sx);
118+ }
09cc6650 119+#endif
0f6fb3c8
WD
120 set_file_attrs(fullpath, file, NULL, 0);
121 free(file);
bbdff76a 122 }
0f6fb3c8
WD
123@@ -195,6 +205,9 @@ static int keep_backup(char *fname)
124 #ifdef SUPPORT_ACLS
125 sx.acc_acl = sx.def_acl = NULL;
09cc6650
WD
126 #endif
127+#ifdef SUPPORT_XATTRS
0f6fb3c8 128+ sx.xattr = NULL;
09cc6650 129+#endif
bbdff76a 130
0f6fb3c8
WD
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 }
09cc6650
WD
136 #endif
137+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
138+ if (preserve_xattrs) {
139+ get_xattr(fname, &sx);
140+ cache_xattr(file, &sx);
141+ }
09cc6650 142+#endif
bbdff76a 143
0f6fb3c8
WD
144 /* Check to see if this is a device file, or link */
145 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
9a7eef96
WD
146--- old/configure.in
147+++ new/configure.in
49de5440 148@@ -856,6 +856,35 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
bbdff76a
WD
149 AC_MSG_RESULT(no)
150 )
151
152+AC_CHECK_HEADERS(attr/xattr.h)
49de5440 153+AC_CHECK_HEADERS(sys/xattr.h)
bbdff76a 154+AC_MSG_CHECKING(whether to support extended attributes)
f787c90c
WD
155+AC_ARG_ENABLE(xattr-support,
156+AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
3b05e91f 157+[ case "$enableval" in
bbdff76a
WD
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+ ;;
49de5440
WD
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+ ;;
bbdff76a 168+ *)
49de5440 169+ AC_MSG_RESULT(Xattrs requested but not Linux or OS X. Good luck...)
bbdff76a
WD
170+ ;;
171+ esac
172+ ;;
173+ *)
174+ AC_MSG_RESULT(no)
742378cf 175+ AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
bbdff76a
WD
176+ esac ],
177+ AC_MSG_RESULT(no)
56473cb9 178+ AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
bbdff76a
WD
179+)
180+
181 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
182 AC_OUTPUT
183
9a7eef96
WD
184--- old/flist.c
185+++ new/flist.c
dc52bc1a 186@@ -41,6 +41,7 @@ extern int one_file_system;
93d6ca6d
WD
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;
de565f59 194@@ -498,7 +499,7 @@ static struct file_struct *receive_file_
0f6fb3c8
WD
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;
de565f59 203@@ -610,10 +611,16 @@ static struct file_struct *receive_file_
0f6fb3c8
WD
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;
de565f59 221@@ -622,7 +629,7 @@ static struct file_struct *receive_file_
0f6fb3c8
WD
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
de565f59 230@@ -723,6 +730,10 @@ static struct file_struct *receive_file_
0f6fb3c8
WD
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 }
a071aea2
WD
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
de565f59 250@@ -994,6 +1005,13 @@ static struct file_struct *send_file_nam
0f6fb3c8
WD
251 return NULL;
252 }
09cc6650
WD
253 #endif
254+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
255+ if (preserve_xattrs) {
256+ sx.xattr = NULL;
257+ if (get_xattr(fname, &sx) < 0)
258+ return NULL;
259+ }
09cc6650 260+#endif
bbdff76a 261
a2f9a486
WD
262 maybe_emit_filelist_progress(flist->count + flist_count_offset);
263
de565f59 264@@ -1006,11 +1024,19 @@ static struct file_struct *send_file_nam
93d6ca6d 265 if (preserve_acls)
0f6fb3c8 266 send_acl(&sx, f);
93d6ca6d 267 #endif
09cc6650 268+#ifdef SUPPORT_XATTRS
93d6ca6d 269+ if (preserve_xattrs)
0f6fb3c8 270+ send_xattr(&sx, f);
09cc6650 271+#endif
bbdff76a 272 } else {
93d6ca6d 273 #ifdef SUPPORT_ACLS
93d6ca6d 274 if (preserve_acls)
0f6fb3c8 275 free_acl(&sx);
09cc6650
WD
276 #endif
277+#ifdef SUPPORT_XATTRS
93d6ca6d 278+ if (preserve_xattrs)
0f6fb3c8 279+ free_xattr(&sx);
09cc6650 280+#endif
bbdff76a
WD
281 }
282 return file;
283 }
9a7eef96
WD
284--- old/lib/sysxattr.c
285+++ new/lib/sysxattr.c
56473cb9 286@@ -0,0 +1,87 @@
dc52bc1a
WD
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+ */
bbdff76a
WD
307+
308+#include "rsync.h"
0f6fb3c8 309+#include "sysxattr.h"
bbdff76a 310+
56473cb9
WD
311+#ifdef SUPPORT_XATTRS
312+
09cc6650 313+#if defined HAVE_LINUX_XATTRS
bbdff76a
WD
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+
8627d8a2
WD
320+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
321+{
0bac098f 322+ return fgetxattr(filedes, name, value, size);
8627d8a2
WD
323+}
324+
bbdff76a
WD
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+
a74926e4
WD
330+int sys_lremovexattr(const char *path, const char *name)
331+{
332+ return lremovexattr(path, name);
333+}
334+
bbdff76a
WD
335+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
336+{
337+ return llistxattr(path, list, size);
338+}
339+
49de5440
WD
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+
8627d8a2
WD
347+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
348+{
0bac098f 349+ return fgetxattr(filedes, name, value, size, 0, 0);
8627d8a2
WD
350+}
351+
49de5440
WD
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+
a74926e4
WD
357+int sys_lremovexattr(const char *path, const char *name)
358+{
359+ return removexattr(path, name, XATTR_NOFOLLOW);
360+}
361+
49de5440
WD
362+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
363+{
364+ return listxattr(path, list, size, XATTR_NOFOLLOW);
365+}
366+
bbdff76a
WD
367+#else
368+
56473cb9
WD
369+#error You need to create xattr compatibility functions.
370+
371+#endif
372+
373+#endif /* SUPPORT_XATTRS */
9a7eef96
WD
374--- old/lib/sysxattr.h
375+++ new/lib/sysxattr.h
8889f85e 376@@ -0,0 +1,24 @@
49de5440 377+#ifdef SUPPORT_XATTRS
8889f85e 378+
49de5440 379+#if defined HAVE_ATTR_XATTR_H
0f6fb3c8 380+#include <attr/xattr.h>
49de5440
WD
381+#elif defined HAVE_SYS_XATTR_H
382+#include <sys/xattr.h>
383+#endif
bbdff76a 384+
a74926e4
WD
385+/* Linux 2.4 does not define this as a distinct errno value: */
386+#ifndef ENOATTR
387+#define ENOATTR ENODATA
388+#endif
389+
bbdff76a 390+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
8627d8a2 391+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
bbdff76a 392+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags);
a74926e4 393+int sys_lremovexattr(const char *path, const char *name);
bbdff76a
WD
394+ssize_t sys_llistxattr(const char *path, char *list, size_t size);
395+
396+#else
397+
8889f85e 398+/* No xattrs available */
49de5440
WD
399+
400+#endif
9a7eef96
WD
401--- old/options.c
402+++ new/options.c
dc52bc1a 403@@ -48,6 +48,7 @@ int copy_links = 0;
bbdff76a
WD
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;
4a65fe72 409 int preserve_executability = 0;
bbdff76a 410 int preserve_devices = 0;
4959107f 411@@ -201,6 +202,7 @@ static void print_rsync_version(enum log
bbdff76a
WD
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;
4959107f 419@@ -220,7 +222,9 @@ static void print_rsync_version(enum log
bbdff76a
WD
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
56473cb9
WD
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);
bbdff76a
WD
438
439 /* Note that this field may not have type ino_t. It depends
440 * on the complicated interaction between largefile feature
56473cb9 441@@ -289,7 +293,7 @@ void usage(enum logcode F)
1b57ecb0 442 rprintf(F," -q, --quiet suppress non-error messages\n");
4959107f 443 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
1b57ecb0
WD
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");
56473cb9 450@@ -314,6 +318,9 @@ void usage(enum logcode F)
3610c43c 451 #ifdef SUPPORT_ACLS
bbdff76a 452 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
3610c43c
WD
453 #endif
454+#ifdef SUPPORT_XATTRS
bbdff76a 455+ rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
3610c43c 456+#endif
4a65fe72 457 rprintf(F," -o, --owner preserve owner (super-user only)\n");
bbdff76a 458 rprintf(F," -g, --group preserve group\n");
1ed0b5c9 459 rprintf(F," --devices preserve device files (super-user only)\n");
56473cb9 460@@ -436,6 +443,9 @@ static struct poptOption long_options[]
489b0a72
WD
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 },
56473cb9 470@@ -1117,6 +1127,17 @@ int parse_arguments(int *argc, const cha
489b0a72 471 return 0;
3610c43c 472 #endif
bbdff76a
WD
473
474+ case 'X':
475+#ifdef SUPPORT_XATTRS
476+ preserve_xattrs = 1;
477+ preserve_perms = 1;
489b0a72 478+ break;
bbdff76a
WD
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;
d2dc8a18 484+#endif
bbdff76a
WD
485
486 default:
487 /* A large opt value means that set_refuse_options()
56473cb9 488@@ -1563,6 +1584,10 @@ void server_options(char **args,int *arg
bbdff76a
WD
489 if (preserve_acls)
490 argstr[x++] = 'A';
3610c43c
WD
491 #endif
492+#ifdef SUPPORT_XATTRS
bbdff76a
WD
493+ if (preserve_xattrs)
494+ argstr[x++] = 'X';
3610c43c 495+#endif
bbdff76a
WD
496 if (preserve_uid)
497 argstr[x++] = 'o';
498 if (preserve_gid)
9a7eef96
WD
499--- old/rsync.c
500+++ new/rsync.c
ff318e90
WD
501@@ -33,6 +33,7 @@
502 extern int verbose;
93d6ca6d 503 extern int dry_run;
93d6ca6d
WD
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;
03019e41 509@@ -218,6 +219,10 @@ int set_file_attrs(char *fname, struct f
071c5b19
WD
510 if (daemon_chmod_modes && !S_ISLNK(new_mode))
511 new_mode = tweak_mode(new_mode, daemon_chmod_modes);
512
09cc6650 513+#ifdef SUPPORT_XATTRS
0f6fb3c8 514+ if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
09cc6650
WD
515+ updated = 1;
516+#endif
071c5b19
WD
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.
9a7eef96
WD
520--- old/rsync.h
521+++ new/rsync.h
56473cb9 522@@ -500,6 +500,10 @@ struct idev {
0f6fb3c8 523 #define ACLS_NEED_MASK 1
09cc6650 524 #endif
bbdff76a 525
56473cb9 526+#ifndef HAVE_NO_XATTRS
bbdff76a
WD
527+#define SUPPORT_XATTRS 1
528+#endif
529+
0f6fb3c8
WD
530 #define GID_NONE ((gid_t)-1)
531
532 #define HL_CHECK_MASTER 0
56473cb9 533@@ -694,6 +698,9 @@ typedef struct {
0f6fb3c8
WD
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;
bbdff76a 539+#endif
0f6fb3c8 540 } statx;
bbdff76a 541
0f6fb3c8 542 #define ACL_READY(sx) ((sx).acc_acl != NULL)
9a7eef96
WD
543--- old/rsync.yo
544+++ new/rsync.yo
4959107f 545@@ -301,7 +301,7 @@ to the detailed description below for a
1b57ecb0 546 -q, --quiet suppress non-error messages
4959107f 547 --no-motd suppress daemon-mode MOTD (see caveat)
1b57ecb0
WD
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
4959107f 554@@ -324,6 +324,7 @@ to the detailed description below for a
4a65fe72 555 -E, --executability preserve executability
063cf77b 556 --chmod=CHMOD affect file and/or directory permissions
4a65fe72
WD
557 -A, --acls preserve ACLs (implies -p) [non-standard]
558+ -X, --xattrs preserve extended attrs (implies -p) [n.s.]
4a65fe72 559 -o, --owner preserve owner (super-user only)
bbdff76a 560 -g, --group preserve group
1ed0b5c9 561 --devices preserve device files (super-user only)
4959107f 562@@ -818,6 +819,11 @@ version makes it incompatible with sendi
3fcc541f
WD
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.
bbdff76a
WD
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+
4a65fe72
WD
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
9a7eef96
WD
574--- old/xattr.c
575+++ new/xattr.c
8889f85e 576@@ -0,0 +1,375 @@
dc52bc1a
WD
577+/*
578+ * Extended Attribute support for rsync.
0f6fb3c8 579+ * Written by Jay Fenlason, vaguely based on the ACLs patch.
dc52bc1a
WD
580+ *
581+ * Copyright (C) 2004 Red Hat, Inc.
0f6fb3c8 582+ * Copyright (C) 2006 Wayne Davison
dc52bc1a
WD
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+ */
bbdff76a
WD
598+
599+#include "rsync.h"
600+#include "lib/sysxattr.h"
601+
602+#ifdef SUPPORT_XATTRS
603+
bbdff76a 604+extern int dry_run;
8889f85e
WD
605+extern int read_only;
606+extern int list_only;
0f6fb3c8 607+extern unsigned int file_struct_len;
bbdff76a
WD
608+
609+#define RSYNC_XAL_INITIAL 5
610+#define RSYNC_XAL_LIST_INITIAL 100
611+
612+typedef struct {
bbdff76a 613+ char *name;
bbdff76a 614+ char *datum;
0f6fb3c8
WD
615+ size_t name_len;
616+ size_t datum_len;
bbdff76a
WD
617+} rsync_xa;
618+
bbdff76a
WD
619+static size_t namebuf_len = 0;
620+static char *namebuf = NULL;
621+
0f6fb3c8
WD
622+static item_list empty_xattr = EMPTY_ITEM_LIST;
623+static item_list rsync_xal_l = EMPTY_ITEM_LIST;
bbdff76a 624+
517cc92f 625+#ifdef HAVE_OSX_XATTRS
1e883fdf
WD
626+#define UNIQUE_PREFIX "user.0S%." /* OSX */
627+#define UPRE_LEN (sizeof UNIQUE_PREFIX - 1)
517cc92f
WD
628+#endif
629+
bbdff76a
WD
630+/* ------------------------------------------------------------------------- */
631+
0f6fb3c8 632+static void rsync_xal_free(item_list *xalp)
bbdff76a
WD
633+{
634+ size_t i;
0f6fb3c8 635+ rsync_xa *rxas = xalp->items;
bbdff76a 636+
0f6fb3c8
WD
637+ for (i = 0; i < xalp->count; i++) {
638+ free(rxas[i].name);
639+ /* free(rxas[i].value); */
bbdff76a 640+ }
0f6fb3c8 641+ xalp->count = 0;
bbdff76a
WD
642+}
643+
0f6fb3c8 644+void free_xattr(statx *sxp)
bbdff76a 645+{
0f6fb3c8
WD
646+ rsync_xal_free(sxp->xattr);
647+ free(sxp->xattr);
648+ sxp->xattr = NULL;
649+}
bbdff76a 650+
0f6fb3c8
WD
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;
bbdff76a
WD
655+ return strcmp(xa1->name, xa2->name);
656+}
657+
0f6fb3c8 658+static int rsync_xal_get(const char *fname, item_list *xalp)
bbdff76a
WD
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) {
49de5440 668+ namebuf_len = 1024;
bbdff76a 669+ namebuf = new_array(char, namebuf_len);
49de5440 670+ if (!namebuf)
bbdff76a
WD
671+ out_of_memory("rsync_xal_get");
672+ }
673+
49de5440 674+ /* The length returned includes all the '\0' terminators. */
bbdff76a
WD
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)
0f6fb3c8 682+ return 0;
bbdff76a
WD
683+ if (errno == ERANGE) {
684+ name_size = sys_llistxattr(fname, NULL, 0);
685+ if (name_size < 0) {
0f6fb3c8
WD
686+ rsyserr(FERROR, errno, "%s: rsync_xal_get: llistxattr",
687+ fname);
e5754c5f 688+ return -1;
bbdff76a 689+ }
49de5440 690+ namebuf = realloc_array(namebuf, char, name_size + 1024);
bbdff76a
WD
691+ if (!namebuf)
692+ out_of_memory("rsync_xal_get");
49de5440 693+ namebuf_len = name_size + 1024;
bbdff76a
WD
694+ name_size = sys_llistxattr(fname, namebuf, namebuf_len);
695+ if (name_size < 0) {
0f6fb3c8
WD
696+ rsyserr(FERROR, errno,
697+ "%s: rsync_xal_get: re-llistxattr failed",
698+ fname);
e5754c5f 699+ return -1;
bbdff76a
WD
700+ }
701+ } else {
0f6fb3c8
WD
702+ rsyserr(FERROR, errno,
703+ "%s: rsync_xal_get: llistxattr failed:",
704+ fname);
e5754c5f 705+ return -1;
bbdff76a
WD
706+ }
707+ }
bbdff76a 708+ if (name_size == 0)
e5754c5f 709+ return 0;
bbdff76a 710+ for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
0f6fb3c8 711+ rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
bbdff76a 712+
0f6fb3c8 713+ len = strlen(name) + 1;
49de5440 714+ datum_size = sys_lgetxattr(fname, name, NULL, 0);
bbdff76a
WD
715+ if (datum_size < 0) {
716+ if (errno == ENOTSUP)
e5754c5f 717+ return -1;
49de5440
WD
718+ rsyserr(FERROR, errno,
719+ "%s: rsync_xal_get: lgetxattr %s failed",
720+ fname, name);
721+ return -1;
bbdff76a
WD
722+ }
723+ ptr = new_array(char, len + datum_size);
724+ if (!ptr)
725+ out_of_memory("rsync_xal_get");
0f6fb3c8 726+ memcpy(ptr, name, len);
0f6fb3c8
WD
727+ rxas->name_len = len;
728+ rxas->name = ptr;
729+ rxas->datum_len = datum_size;
730+ rxas->datum = ptr + len;
49de5440
WD
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)"
bb6b64ea
WD
741+ " returned %ld instead of %ld\n",
742+ fname, name,
743+ (long)size, (long)datum_size);
49de5440
WD
744+ }
745+ return -1;
746+ }
747+ }
bbdff76a 748+ }
0f6fb3c8
WD
749+ if (xalp->count > 1)
750+ qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
e5754c5f 751+ return 0;
bbdff76a
WD
752+}
753+
0f6fb3c8
WD
754+/* Read the xattr(s) for this filename. */
755+int get_xattr(const char *fname, statx *sxp)
bbdff76a 756+{
0f6fb3c8
WD
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;
bbdff76a
WD
764+}
765+
0f6fb3c8 766+static int find_matching_xattr(item_list *xalp)
bbdff76a 767+{
0f6fb3c8
WD
768+ size_t i, j;
769+ item_list *lst = rsync_xal_l.items;
bbdff76a
WD
770+
771+ for (i = 0; i < rsync_xal_l.count; i++) {
0f6fb3c8
WD
772+ rsync_xa *rxas1 = lst[i].items;
773+ rsync_xa *rxas2 = xalp->items;
774+
bbdff76a 775+ /* Wrong number of elements? */
0f6fb3c8 776+ if (lst[i].count != xalp->count)
bbdff76a
WD
777+ continue;
778+ /* any elements different? */
0f6fb3c8
WD
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))
bbdff76a
WD
784+ break;
785+ }
786+ /* no differences found. This is The One! */
0f6fb3c8
WD
787+ if (j == xalp->count)
788+ return i;
bbdff76a 789+ }
0f6fb3c8
WD
790+
791+ return -1;
bbdff76a
WD
792+}
793+
0f6fb3c8
WD
794+/* Store *xalp on the end of rsync_xal_l */
795+static void rsync_xal_store(item_list *xalp)
bbdff76a 796+{
0f6fb3c8 797+ item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
de565f59
WD
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. */
49de5440
WD
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));
0f6fb3c8
WD
803+ new_lst->count = xalp->count;
804+ xalp->count = 0;
bbdff76a
WD
805+}
806+
0f6fb3c8
WD
807+/* Send the make_xattr()-generated xattr list for this flist entry. */
808+void send_xattr(statx *sxp, int f)
bbdff76a 809+{
0f6fb3c8
WD
810+ int ndx = find_matching_xattr(sxp->xattr);
811+ if (ndx != -1) {
bbdff76a 812+ write_byte(f, 'x');
0f6fb3c8
WD
813+ write_int(f, ndx);
814+ rsync_xal_free(sxp->xattr);
bbdff76a
WD
815+ } else {
816+ rsync_xa *rxa;
0f6fb3c8 817+ int count = sxp->xattr->count;
bbdff76a
WD
818+ write_byte(f, 'X');
819+ write_int(f, count);
0f6fb3c8 820+ for (rxa = sxp->xattr->items; count--; rxa++) {
517cc92f
WD
821+#ifdef HAVE_OSX_XATTRS
822+ if (strncmp(rxa->name, "user.", 5) != 0
823+ && strncmp(rxa->name, "system.", 7) != 0) {
1e883fdf 824+ write_int(f, rxa->name_len + UPRE_LEN);
517cc92f 825+ write_int(f, rxa->datum_len);
1e883fdf 826+ write_buf(f, UNIQUE_PREFIX, UPRE_LEN);
517cc92f
WD
827+ } else
828+#endif
829+ {
830+ write_int(f, rxa->name_len);
831+ write_int(f, rxa->datum_len);
832+ }
bbdff76a
WD
833+ write_buf(f, rxa->name, rxa->name_len);
834+ write_buf(f, rxa->datum, rxa->datum_len);
835+ }
49de5440 836+ rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
bbdff76a 837+ }
0f6fb3c8 838+ free_xattr(sxp);
bbdff76a
WD
839+}
840+
bbdff76a 841+/* ------------------------------------------------------------------------- */
bbdff76a 842+
0f6fb3c8 843+/* receive and build the rsync_xattr_lists */
bbdff76a
WD
844+void receive_xattr(struct file_struct *file, int f)
845+{
0f6fb3c8
WD
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;
bbdff76a 850+
bbdff76a 851+ if (tag == 'X') {
0f6fb3c8 852+ int i, count = read_int(f);
bbdff76a 853+ for (i = 0; i < count; i++) {
bbdff76a 854+ char *ptr;
0f6fb3c8
WD
855+ rsync_xa *rxa;
856+ size_t name_len = read_int(f);
857+ size_t datum_len = read_int(f);
3fcc541f
WD
858+ if (name_len + datum_len < name_len)
859+ out_of_memory("receive_xattr"); /* overflow */
0f6fb3c8 860+ rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
bbdff76a
WD
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);
0f6fb3c8
WD
866+ rxa->name_len = name_len;
867+ rxa->datum_len = datum_len;
868+ rxa->name = ptr;
869+ rxa->datum = ptr + name_len;
517cc92f 870+#ifdef HAVE_OSX_XATTRS
1e883fdf
WD
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);
517cc92f
WD
874+ }
875+#endif
bbdff76a 876+ }
49de5440
WD
877+ ndx = rsync_xal_l.count; /* pre-incremented count */
878+ rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
0f6fb3c8
WD
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);
bbdff76a
WD
884+ exit_cleanup(RERR_STREAMIO);
885+ }
0f6fb3c8
WD
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... */
bbdff76a 892+ }
bbdff76a 893+
0f6fb3c8 894+ SIVAL(ndx_ptr, 0, ndx);
bbdff76a
WD
895+}
896+
0f6fb3c8
WD
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)
bbdff76a 900+{
0f6fb3c8
WD
901+ char *ndx_ptr = (char*)file + file_struct_len;
902+ int ndx;
bbdff76a 903+
0f6fb3c8
WD
904+ if (!sxp->xattr)
905+ return;
bbdff76a 906+
0f6fb3c8
WD
907+ ndx = find_matching_xattr(sxp->xattr);
908+ if (ndx == -1)
49de5440 909+ rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
0f6fb3c8 910+ free_xattr(sxp);
bbdff76a 911+
0f6fb3c8 912+ SIVAL(ndx_ptr, 0, ndx);
bbdff76a
WD
913+}
914+
0f6fb3c8 915+static int rsync_xal_set(const char *fname, item_list *xalp)
bbdff76a 916+{
0f6fb3c8
WD
917+ rsync_xa *rxas = xalp->items;
918+ size_t i;
919+ int ret = 0;
bbdff76a 920+
0f6fb3c8
WD
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+ }
bbdff76a 928+ }
0f6fb3c8 929+ return ret;
bbdff76a
WD
930+}
931+
0f6fb3c8
WD
932+/* Set extended attributes on indicated filename. */
933+int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
bbdff76a 934+{
0f6fb3c8
WD
935+ int ndx;
936+ char *ndx_ptr = (char*)file + file_struct_len;
937+ item_list *lst = rsync_xal_l.items;
bbdff76a 938+
93d6ca6d
WD
939+ if (dry_run)
940+ return 1; /* FIXME: --dry-run needs to compute this value */
941+
8889f85e
WD
942+ if (read_only || list_only) {
943+ errno = EROFS;
944+ return -1;
945+ }
946+
0f6fb3c8
WD
947+ ndx = IVAL(ndx_ptr, 0);
948+ return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
bbdff76a
WD
949+}
950+
951+#endif /* SUPPORT_XATTRS */