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