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