Got rid of a superfluous extern.
[rsync/rsync-patches.git] / xattrs.diff
CommitLineData
bbdff76a
WD
1Depends-On-Patch: acls.diff
2
d2dc8a18
WD
3This patch adds preliminary support for extended attributes.
4
bbdff76a
WD
5After applying this patch, run these commands for a successful build:
6
27e96866 7 ./prepare-source
f787c90c 8 ./configure --enable-acl-support --enable-xattr-support
bbdff76a
WD
9 make
10
0f6fb3c8
WD
11TODO:
12
d2dc8a18
WD
13 - This patch needs to more efficiently handle large xattrs, especially when
14 they're unchanged.
0f6fb3c8 15
a74926e4
WD
16 - Extraneous xattr values need to be removed from files that are not being
17 recreated.
18
0f6fb3c8
WD
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
9a7eef96
WD
24--- old/Makefile.in
25+++ new/Makefile.in
1ed0b5c9 26@@ -28,13 +28,13 @@ VERSION=@VERSION@
bbdff76a
WD
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@
4bf6f8c7
WD
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
bbdff76a
WD
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 \
610969d1
WD
37- fileio.o batch.o clientname.o chmod.o acls.o
38+ fileio.o batch.o clientname.o chmod.o acls.o xattr.o
bbdff76a
WD
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 \
0f6fb3c8
WD
42--- old/acls.c
43+++ new/acls.c
8889f85e
WD
44@@ -31,6 +31,7 @@ extern int read_only;
45 extern int list_only;
0f6fb3c8
WD
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 === */
8889f85e 52@@ -742,6 +743,10 @@ void receive_acl(struct file_struct *fil
0f6fb3c8
WD
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;
8889f85e 63@@ -801,6 +806,10 @@ void cache_acl(struct file_struct *file,
0f6fb3c8
WD
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;
8889f85e 74@@ -921,6 +930,10 @@ int set_acl(const char *fname, const str
0f6fb3c8
WD
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;
9a7eef96
WD
85--- old/backup.c
86+++ new/backup.c
dc52bc1a 87@@ -30,6 +30,7 @@ extern char *backup_dir;
93d6ca6d
WD
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;
0f6fb3c8
WD
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 }
09cc6650
WD
108 #endif
109+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
110+ if (preserve_xattrs) {
111+ get_xattr(rel, &sx);
112+ cache_xattr(file, &sx);
113+ }
09cc6650 114+#endif
0f6fb3c8
WD
115 set_file_attrs(fullpath, file, NULL, 0);
116 free(file);
bbdff76a 117 }
0f6fb3c8
WD
118@@ -195,6 +205,9 @@ static int keep_backup(char *fname)
119 #ifdef SUPPORT_ACLS
120 sx.acc_acl = sx.def_acl = NULL;
09cc6650
WD
121 #endif
122+#ifdef SUPPORT_XATTRS
0f6fb3c8 123+ sx.xattr = NULL;
09cc6650 124+#endif
bbdff76a 125
0f6fb3c8
WD
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 }
09cc6650
WD
131 #endif
132+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
133+ if (preserve_xattrs) {
134+ get_xattr(fname, &sx);
135+ cache_xattr(file, &sx);
136+ }
09cc6650 137+#endif
bbdff76a 138
0f6fb3c8
WD
139 /* Check to see if this is a device file, or link */
140 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
9a7eef96
WD
141--- old/configure.in
142+++ new/configure.in
49de5440 143@@ -856,6 +856,35 @@ samba_cv_HAVE_ACL_GET_PERM_NP=yes,samba_
bbdff76a
WD
144 AC_MSG_RESULT(no)
145 )
146
147+AC_CHECK_HEADERS(attr/xattr.h)
49de5440 148+AC_CHECK_HEADERS(sys/xattr.h)
bbdff76a 149+AC_MSG_CHECKING(whether to support extended attributes)
f787c90c
WD
150+AC_ARG_ENABLE(xattr-support,
151+AC_HELP_STRING([--enable-xattr-support], [Include extended attribute support (default=no)]),
3b05e91f 152+[ case "$enableval" in
bbdff76a
WD
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+ ;;
49de5440
WD
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+ ;;
bbdff76a 163+ *)
49de5440 164+ AC_MSG_RESULT(Xattrs requested but not Linux or OS X. Good luck...)
bbdff76a
WD
165+ ;;
166+ esac
167+ ;;
168+ *)
169+ AC_MSG_RESULT(no)
742378cf 170+ AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
bbdff76a
WD
171+ esac ],
172+ AC_MSG_RESULT(no)
56473cb9 173+ AC_DEFINE(HAVE_NO_XATTRS, 1, [True if you don't have extended attributes])
bbdff76a
WD
174+)
175+
176 AC_CONFIG_FILES([Makefile lib/dummy zlib/dummy popt/dummy shconfig])
177 AC_OUTPUT
178
9a7eef96
WD
179--- old/flist.c
180+++ new/flist.c
dc52bc1a 181@@ -41,6 +41,7 @@ extern int one_file_system;
93d6ca6d
WD
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;
de565f59 189@@ -498,7 +499,7 @@ static struct file_struct *receive_file_
0f6fb3c8
WD
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;
de565f59 198@@ -610,10 +611,16 @@ static struct file_struct *receive_file_
0f6fb3c8
WD
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;
de565f59 216@@ -622,7 +629,7 @@ static struct file_struct *receive_file_
0f6fb3c8
WD
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
de565f59 225@@ -723,6 +730,10 @@ static struct file_struct *receive_file_
0f6fb3c8
WD
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 }
a071aea2
WD
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
de565f59 245@@ -994,6 +1005,13 @@ static struct file_struct *send_file_nam
0f6fb3c8
WD
246 return NULL;
247 }
09cc6650
WD
248 #endif
249+#ifdef SUPPORT_XATTRS
0f6fb3c8
WD
250+ if (preserve_xattrs) {
251+ sx.xattr = NULL;
252+ if (get_xattr(fname, &sx) < 0)
253+ return NULL;
254+ }
09cc6650 255+#endif
bbdff76a 256
a2f9a486
WD
257 maybe_emit_filelist_progress(flist->count + flist_count_offset);
258
de565f59 259@@ -1006,11 +1024,19 @@ static struct file_struct *send_file_nam
93d6ca6d 260 if (preserve_acls)
0f6fb3c8 261 send_acl(&sx, f);
93d6ca6d 262 #endif
09cc6650 263+#ifdef SUPPORT_XATTRS
93d6ca6d 264+ if (preserve_xattrs)
0f6fb3c8 265+ send_xattr(&sx, f);
09cc6650 266+#endif
bbdff76a 267 } else {
93d6ca6d 268 #ifdef SUPPORT_ACLS
93d6ca6d 269 if (preserve_acls)
0f6fb3c8 270 free_acl(&sx);
09cc6650
WD
271 #endif
272+#ifdef SUPPORT_XATTRS
93d6ca6d 273+ if (preserve_xattrs)
0f6fb3c8 274+ free_xattr(&sx);
09cc6650 275+#endif
bbdff76a
WD
276 }
277 return file;
278 }
9a7eef96
WD
279--- old/lib/sysxattr.c
280+++ new/lib/sysxattr.c
56473cb9 281@@ -0,0 +1,87 @@
dc52bc1a
WD
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+ */
bbdff76a
WD
302+
303+#include "rsync.h"
0f6fb3c8 304+#include "sysxattr.h"
bbdff76a 305+
56473cb9
WD
306+#ifdef SUPPORT_XATTRS
307+
09cc6650 308+#if defined HAVE_LINUX_XATTRS
bbdff76a
WD
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+
8627d8a2
WD
315+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
316+{
0bac098f 317+ return fgetxattr(filedes, name, value, size);
8627d8a2
WD
318+}
319+
bbdff76a
WD
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+
a74926e4
WD
325+int sys_lremovexattr(const char *path, const char *name)
326+{
327+ return lremovexattr(path, name);
328+}
329+
bbdff76a
WD
330+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
331+{
332+ return llistxattr(path, list, size);
333+}
334+
49de5440
WD
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+
8627d8a2
WD
342+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
343+{
0bac098f 344+ return fgetxattr(filedes, name, value, size, 0, 0);
8627d8a2
WD
345+}
346+
49de5440
WD
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+
a74926e4
WD
352+int sys_lremovexattr(const char *path, const char *name)
353+{
354+ return removexattr(path, name, XATTR_NOFOLLOW);
355+}
356+
49de5440
WD
357+ssize_t sys_llistxattr(const char *path, char *list, size_t size)
358+{
359+ return listxattr(path, list, size, XATTR_NOFOLLOW);
360+}
361+
bbdff76a
WD
362+#else
363+
56473cb9
WD
364+#error You need to create xattr compatibility functions.
365+
366+#endif
367+
368+#endif /* SUPPORT_XATTRS */
9a7eef96
WD
369--- old/lib/sysxattr.h
370+++ new/lib/sysxattr.h
8889f85e 371@@ -0,0 +1,24 @@
49de5440 372+#ifdef SUPPORT_XATTRS
8889f85e 373+
49de5440 374+#if defined HAVE_ATTR_XATTR_H
0f6fb3c8 375+#include <attr/xattr.h>
49de5440
WD
376+#elif defined HAVE_SYS_XATTR_H
377+#include <sys/xattr.h>
378+#endif
bbdff76a 379+
a74926e4
WD
380+/* Linux 2.4 does not define this as a distinct errno value: */
381+#ifndef ENOATTR
382+#define ENOATTR ENODATA
383+#endif
384+
bbdff76a 385+ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size);
8627d8a2 386+ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size);
bbdff76a 387+int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size, int flags);
a74926e4 388+int sys_lremovexattr(const char *path, const char *name);
bbdff76a
WD
389+ssize_t sys_llistxattr(const char *path, char *list, size_t size);
390+
391+#else
392+
8889f85e 393+/* No xattrs available */
49de5440
WD
394+
395+#endif
9a7eef96
WD
396--- old/options.c
397+++ new/options.c
dc52bc1a 398@@ -48,6 +48,7 @@ int copy_links = 0;
bbdff76a
WD
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;
4a65fe72 404 int preserve_executability = 0;
bbdff76a 405 int preserve_devices = 0;
4959107f 406@@ -201,6 +202,7 @@ static void print_rsync_version(enum log
bbdff76a
WD
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;
4959107f 414@@ -220,7 +222,9 @@ static void print_rsync_version(enum log
bbdff76a
WD
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
56473cb9
WD
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);
bbdff76a
WD
433
434 /* Note that this field may not have type ino_t. It depends
435 * on the complicated interaction between largefile feature
56473cb9 436@@ -289,7 +293,7 @@ void usage(enum logcode F)
1b57ecb0 437 rprintf(F," -q, --quiet suppress non-error messages\n");
4959107f 438 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
1b57ecb0
WD
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");
56473cb9 445@@ -314,6 +318,9 @@ void usage(enum logcode F)
3610c43c 446 #ifdef SUPPORT_ACLS
bbdff76a 447 rprintf(F," -A, --acls preserve ACLs (implies --perms)\n");
3610c43c
WD
448 #endif
449+#ifdef SUPPORT_XATTRS
bbdff76a 450+ rprintf(F," -X, --xattrs preserve extended attributes (implies --perms)\n");
3610c43c 451+#endif
4a65fe72 452 rprintf(F," -o, --owner preserve owner (super-user only)\n");
bbdff76a 453 rprintf(F," -g, --group preserve group\n");
1ed0b5c9 454 rprintf(F," --devices preserve device files (super-user only)\n");
56473cb9 455@@ -436,6 +443,9 @@ static struct poptOption long_options[]
489b0a72
WD
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 },
56473cb9 465@@ -1117,6 +1127,17 @@ int parse_arguments(int *argc, const cha
489b0a72 466 return 0;
3610c43c 467 #endif
bbdff76a
WD
468
469+ case 'X':
470+#ifdef SUPPORT_XATTRS
471+ preserve_xattrs = 1;
472+ preserve_perms = 1;
489b0a72 473+ break;
bbdff76a
WD
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;
d2dc8a18 479+#endif
bbdff76a
WD
480
481 default:
482 /* A large opt value means that set_refuse_options()
56473cb9 483@@ -1563,6 +1584,10 @@ void server_options(char **args,int *arg
bbdff76a
WD
484 if (preserve_acls)
485 argstr[x++] = 'A';
3610c43c
WD
486 #endif
487+#ifdef SUPPORT_XATTRS
bbdff76a
WD
488+ if (preserve_xattrs)
489+ argstr[x++] = 'X';
3610c43c 490+#endif
bbdff76a
WD
491 if (preserve_uid)
492 argstr[x++] = 'o';
493 if (preserve_gid)
9a7eef96
WD
494--- old/rsync.c
495+++ new/rsync.c
ff318e90
WD
496@@ -33,6 +33,7 @@
497 extern int verbose;
93d6ca6d 498 extern int dry_run;
93d6ca6d
WD
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;
071c5b19
WD
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
09cc6650 508+#ifdef SUPPORT_XATTRS
0f6fb3c8 509+ if (preserve_xattrs && set_xattr(fname, file, sxp) == 0)
09cc6650
WD
510+ updated = 1;
511+#endif
071c5b19
WD
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.
9a7eef96
WD
515--- old/rsync.h
516+++ new/rsync.h
56473cb9 517@@ -500,6 +500,10 @@ struct idev {
0f6fb3c8 518 #define ACLS_NEED_MASK 1
09cc6650 519 #endif
bbdff76a 520
56473cb9 521+#ifndef HAVE_NO_XATTRS
bbdff76a
WD
522+#define SUPPORT_XATTRS 1
523+#endif
524+
0f6fb3c8
WD
525 #define GID_NONE ((gid_t)-1)
526
527 #define HL_CHECK_MASTER 0
56473cb9 528@@ -694,6 +698,9 @@ typedef struct {
0f6fb3c8
WD
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;
bbdff76a 534+#endif
0f6fb3c8 535 } statx;
bbdff76a 536
0f6fb3c8 537 #define ACL_READY(sx) ((sx).acc_acl != NULL)
9a7eef96
WD
538--- old/rsync.yo
539+++ new/rsync.yo
4959107f 540@@ -301,7 +301,7 @@ to the detailed description below for a
1b57ecb0 541 -q, --quiet suppress non-error messages
4959107f 542 --no-motd suppress daemon-mode MOTD (see caveat)
1b57ecb0
WD
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
4959107f 549@@ -324,6 +324,7 @@ to the detailed description below for a
4a65fe72 550 -E, --executability preserve executability
063cf77b 551 --chmod=CHMOD affect file and/or directory permissions
4a65fe72
WD
552 -A, --acls preserve ACLs (implies -p) [non-standard]
553+ -X, --xattrs preserve extended attrs (implies -p) [n.s.]
4a65fe72 554 -o, --owner preserve owner (super-user only)
bbdff76a 555 -g, --group preserve group
1ed0b5c9 556 --devices preserve device files (super-user only)
4959107f 557@@ -818,6 +819,11 @@ version makes it incompatible with sendi
3fcc541f
WD
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.
bbdff76a
WD
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+
4a65fe72
WD
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
9a7eef96
WD
569--- old/xattr.c
570+++ new/xattr.c
8889f85e 571@@ -0,0 +1,375 @@
dc52bc1a
WD
572+/*
573+ * Extended Attribute support for rsync.
0f6fb3c8 574+ * Written by Jay Fenlason, vaguely based on the ACLs patch.
dc52bc1a
WD
575+ *
576+ * Copyright (C) 2004 Red Hat, Inc.
0f6fb3c8 577+ * Copyright (C) 2006 Wayne Davison
dc52bc1a
WD
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+ */
bbdff76a
WD
593+
594+#include "rsync.h"
595+#include "lib/sysxattr.h"
596+
597+#ifdef SUPPORT_XATTRS
598+
bbdff76a 599+extern int dry_run;
8889f85e
WD
600+extern int read_only;
601+extern int list_only;
0f6fb3c8 602+extern unsigned int file_struct_len;
bbdff76a
WD
603+
604+#define RSYNC_XAL_INITIAL 5
605+#define RSYNC_XAL_LIST_INITIAL 100
606+
607+typedef struct {
bbdff76a 608+ char *name;
bbdff76a 609+ char *datum;
0f6fb3c8
WD
610+ size_t name_len;
611+ size_t datum_len;
bbdff76a
WD
612+} rsync_xa;
613+
bbdff76a
WD
614+static size_t namebuf_len = 0;
615+static char *namebuf = NULL;
616+
0f6fb3c8
WD
617+static item_list empty_xattr = EMPTY_ITEM_LIST;
618+static item_list rsync_xal_l = EMPTY_ITEM_LIST;
bbdff76a 619+
517cc92f 620+#ifdef HAVE_OSX_XATTRS
1e883fdf
WD
621+#define UNIQUE_PREFIX "user.0S%." /* OSX */
622+#define UPRE_LEN (sizeof UNIQUE_PREFIX - 1)
517cc92f
WD
623+#endif
624+
bbdff76a
WD
625+/* ------------------------------------------------------------------------- */
626+
0f6fb3c8 627+static void rsync_xal_free(item_list *xalp)
bbdff76a
WD
628+{
629+ size_t i;
0f6fb3c8 630+ rsync_xa *rxas = xalp->items;
bbdff76a 631+
0f6fb3c8
WD
632+ for (i = 0; i < xalp->count; i++) {
633+ free(rxas[i].name);
634+ /* free(rxas[i].value); */
bbdff76a 635+ }
0f6fb3c8 636+ xalp->count = 0;
bbdff76a
WD
637+}
638+
0f6fb3c8 639+void free_xattr(statx *sxp)
bbdff76a 640+{
0f6fb3c8
WD
641+ rsync_xal_free(sxp->xattr);
642+ free(sxp->xattr);
643+ sxp->xattr = NULL;
644+}
bbdff76a 645+
0f6fb3c8
WD
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;
bbdff76a
WD
650+ return strcmp(xa1->name, xa2->name);
651+}
652+
0f6fb3c8 653+static int rsync_xal_get(const char *fname, item_list *xalp)
bbdff76a
WD
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) {
49de5440 663+ namebuf_len = 1024;
bbdff76a 664+ namebuf = new_array(char, namebuf_len);
49de5440 665+ if (!namebuf)
bbdff76a
WD
666+ out_of_memory("rsync_xal_get");
667+ }
668+
49de5440 669+ /* The length returned includes all the '\0' terminators. */
bbdff76a
WD
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)
0f6fb3c8 677+ return 0;
bbdff76a
WD
678+ if (errno == ERANGE) {
679+ name_size = sys_llistxattr(fname, NULL, 0);
680+ if (name_size < 0) {
0f6fb3c8
WD
681+ rsyserr(FERROR, errno, "%s: rsync_xal_get: llistxattr",
682+ fname);
e5754c5f 683+ return -1;
bbdff76a 684+ }
49de5440 685+ namebuf = realloc_array(namebuf, char, name_size + 1024);
bbdff76a
WD
686+ if (!namebuf)
687+ out_of_memory("rsync_xal_get");
49de5440 688+ namebuf_len = name_size + 1024;
bbdff76a
WD
689+ name_size = sys_llistxattr(fname, namebuf, namebuf_len);
690+ if (name_size < 0) {
0f6fb3c8
WD
691+ rsyserr(FERROR, errno,
692+ "%s: rsync_xal_get: re-llistxattr failed",
693+ fname);
e5754c5f 694+ return -1;
bbdff76a
WD
695+ }
696+ } else {
0f6fb3c8
WD
697+ rsyserr(FERROR, errno,
698+ "%s: rsync_xal_get: llistxattr failed:",
699+ fname);
e5754c5f 700+ return -1;
bbdff76a
WD
701+ }
702+ }
bbdff76a 703+ if (name_size == 0)
e5754c5f 704+ return 0;
bbdff76a 705+ for (left = name_size, name = namebuf; left > 0 ; left -= len, name += len) {
0f6fb3c8 706+ rsync_xa *rxas = EXPAND_ITEM_LIST(xalp, rsync_xa, RSYNC_XAL_INITIAL);
bbdff76a 707+
0f6fb3c8 708+ len = strlen(name) + 1;
49de5440 709+ datum_size = sys_lgetxattr(fname, name, NULL, 0);
bbdff76a
WD
710+ if (datum_size < 0) {
711+ if (errno == ENOTSUP)
e5754c5f 712+ return -1;
49de5440
WD
713+ rsyserr(FERROR, errno,
714+ "%s: rsync_xal_get: lgetxattr %s failed",
715+ fname, name);
716+ return -1;
bbdff76a
WD
717+ }
718+ ptr = new_array(char, len + datum_size);
719+ if (!ptr)
720+ out_of_memory("rsync_xal_get");
0f6fb3c8 721+ memcpy(ptr, name, len);
0f6fb3c8
WD
722+ rxas->name_len = len;
723+ rxas->name = ptr;
724+ rxas->datum_len = datum_size;
725+ rxas->datum = ptr + len;
49de5440
WD
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)"
bb6b64ea
WD
736+ " returned %ld instead of %ld\n",
737+ fname, name,
738+ (long)size, (long)datum_size);
49de5440
WD
739+ }
740+ return -1;
741+ }
742+ }
bbdff76a 743+ }
0f6fb3c8
WD
744+ if (xalp->count > 1)
745+ qsort(xalp->items, xalp->count, sizeof (rsync_xa), rsync_xal_compare_names);
e5754c5f 746+ return 0;
bbdff76a
WD
747+}
748+
0f6fb3c8
WD
749+/* Read the xattr(s) for this filename. */
750+int get_xattr(const char *fname, statx *sxp)
bbdff76a 751+{
0f6fb3c8
WD
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;
bbdff76a
WD
759+}
760+
0f6fb3c8 761+static int find_matching_xattr(item_list *xalp)
bbdff76a 762+{
0f6fb3c8
WD
763+ size_t i, j;
764+ item_list *lst = rsync_xal_l.items;
bbdff76a
WD
765+
766+ for (i = 0; i < rsync_xal_l.count; i++) {
0f6fb3c8
WD
767+ rsync_xa *rxas1 = lst[i].items;
768+ rsync_xa *rxas2 = xalp->items;
769+
bbdff76a 770+ /* Wrong number of elements? */
0f6fb3c8 771+ if (lst[i].count != xalp->count)
bbdff76a
WD
772+ continue;
773+ /* any elements different? */
0f6fb3c8
WD
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))
bbdff76a
WD
779+ break;
780+ }
781+ /* no differences found. This is The One! */
0f6fb3c8
WD
782+ if (j == xalp->count)
783+ return i;
bbdff76a 784+ }
0f6fb3c8
WD
785+
786+ return -1;
bbdff76a
WD
787+}
788+
0f6fb3c8
WD
789+/* Store *xalp on the end of rsync_xal_l */
790+static void rsync_xal_store(item_list *xalp)
bbdff76a 791+{
0f6fb3c8 792+ item_list *new_lst = EXPAND_ITEM_LIST(&rsync_xal_l, item_list, RSYNC_XAL_LIST_INITIAL);
de565f59
WD
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. */
49de5440
WD
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));
0f6fb3c8
WD
798+ new_lst->count = xalp->count;
799+ xalp->count = 0;
bbdff76a
WD
800+}
801+
0f6fb3c8
WD
802+/* Send the make_xattr()-generated xattr list for this flist entry. */
803+void send_xattr(statx *sxp, int f)
bbdff76a 804+{
0f6fb3c8
WD
805+ int ndx = find_matching_xattr(sxp->xattr);
806+ if (ndx != -1) {
bbdff76a 807+ write_byte(f, 'x');
0f6fb3c8
WD
808+ write_int(f, ndx);
809+ rsync_xal_free(sxp->xattr);
bbdff76a
WD
810+ } else {
811+ rsync_xa *rxa;
0f6fb3c8 812+ int count = sxp->xattr->count;
bbdff76a
WD
813+ write_byte(f, 'X');
814+ write_int(f, count);
0f6fb3c8 815+ for (rxa = sxp->xattr->items; count--; rxa++) {
517cc92f
WD
816+#ifdef HAVE_OSX_XATTRS
817+ if (strncmp(rxa->name, "user.", 5) != 0
818+ && strncmp(rxa->name, "system.", 7) != 0) {
1e883fdf 819+ write_int(f, rxa->name_len + UPRE_LEN);
517cc92f 820+ write_int(f, rxa->datum_len);
1e883fdf 821+ write_buf(f, UNIQUE_PREFIX, UPRE_LEN);
517cc92f
WD
822+ } else
823+#endif
824+ {
825+ write_int(f, rxa->name_len);
826+ write_int(f, rxa->datum_len);
827+ }
bbdff76a
WD
828+ write_buf(f, rxa->name, rxa->name_len);
829+ write_buf(f, rxa->datum, rxa->datum_len);
830+ }
49de5440 831+ rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
bbdff76a 832+ }
0f6fb3c8 833+ free_xattr(sxp);
bbdff76a
WD
834+}
835+
bbdff76a 836+/* ------------------------------------------------------------------------- */
bbdff76a 837+
0f6fb3c8 838+/* receive and build the rsync_xattr_lists */
bbdff76a
WD
839+void receive_xattr(struct file_struct *file, int f)
840+{
0f6fb3c8
WD
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;
bbdff76a 845+
bbdff76a 846+ if (tag == 'X') {
0f6fb3c8 847+ int i, count = read_int(f);
bbdff76a 848+ for (i = 0; i < count; i++) {
bbdff76a 849+ char *ptr;
0f6fb3c8
WD
850+ rsync_xa *rxa;
851+ size_t name_len = read_int(f);
852+ size_t datum_len = read_int(f);
3fcc541f
WD
853+ if (name_len + datum_len < name_len)
854+ out_of_memory("receive_xattr"); /* overflow */
0f6fb3c8 855+ rxa = EXPAND_ITEM_LIST(&temp_xattr, rsync_xa, count);
bbdff76a
WD
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);
0f6fb3c8
WD
861+ rxa->name_len = name_len;
862+ rxa->datum_len = datum_len;
863+ rxa->name = ptr;
864+ rxa->datum = ptr + name_len;
517cc92f 865+#ifdef HAVE_OSX_XATTRS
1e883fdf
WD
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);
517cc92f
WD
869+ }
870+#endif
bbdff76a 871+ }
49de5440
WD
872+ ndx = rsync_xal_l.count; /* pre-incremented count */
873+ rsync_xal_store(&temp_xattr); /* adds item to rsync_xal_l */
0f6fb3c8
WD
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);
bbdff76a
WD
879+ exit_cleanup(RERR_STREAMIO);
880+ }
0f6fb3c8
WD
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... */
bbdff76a 887+ }
bbdff76a 888+
0f6fb3c8 889+ SIVAL(ndx_ptr, 0, ndx);
bbdff76a
WD
890+}
891+
0f6fb3c8
WD
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)
bbdff76a 895+{
0f6fb3c8
WD
896+ char *ndx_ptr = (char*)file + file_struct_len;
897+ int ndx;
bbdff76a 898+
0f6fb3c8
WD
899+ if (!sxp->xattr)
900+ return;
bbdff76a 901+
0f6fb3c8
WD
902+ ndx = find_matching_xattr(sxp->xattr);
903+ if (ndx == -1)
49de5440 904+ rsync_xal_store(sxp->xattr); /* adds item to rsync_xal_l */
0f6fb3c8 905+ free_xattr(sxp);
bbdff76a 906+
0f6fb3c8 907+ SIVAL(ndx_ptr, 0, ndx);
bbdff76a
WD
908+}
909+
0f6fb3c8 910+static int rsync_xal_set(const char *fname, item_list *xalp)
bbdff76a 911+{
0f6fb3c8
WD
912+ rsync_xa *rxas = xalp->items;
913+ size_t i;
914+ int ret = 0;
bbdff76a 915+
0f6fb3c8
WD
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+ }
bbdff76a 923+ }
0f6fb3c8 924+ return ret;
bbdff76a
WD
925+}
926+
0f6fb3c8
WD
927+/* Set extended attributes on indicated filename. */
928+int set_xattr(const char *fname, const struct file_struct *file, UNUSED(statx *sxp))
bbdff76a 929+{
0f6fb3c8
WD
930+ int ndx;
931+ char *ndx_ptr = (char*)file + file_struct_len;
932+ item_list *lst = rsync_xal_l.items;
bbdff76a 933+
93d6ca6d
WD
934+ if (dry_run)
935+ return 1; /* FIXME: --dry-run needs to compute this value */
936+
8889f85e
WD
937+ if (read_only || list_only) {
938+ errno = EROFS;
939+ return -1;
940+ }
941+
0f6fb3c8
WD
942+ ndx = IVAL(ndx_ptr, 0);
943+ return rsync_xal_set(fname, lst + ndx); /* TODO: This needs to return 1 if no xattrs changed! */
bbdff76a
WD
944+}
945+
946+#endif /* SUPPORT_XATTRS */