2 Copyright (C) 2002 Andreas Gruenbacher <agruen@suse.de>, SuSE Linux AG.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this manual. If not, see <http://www.gnu.org/licenses/>.
18 /* Copy extended attributes between files. */
20 #if defined (HAVE_CONFIG_H)
24 #include <sys/types.h>
29 #if defined(HAVE_ALLOCA_H)
33 #if defined(HAVE_ATTR_XATTR_H)
34 # include <attr/xattr.h>
37 #if defined(HAVE_ATTR_LIBATTR_H)
38 # include "attr/libattr.h"
41 #define ERROR_CONTEXT_MACROS
42 #include "error_context.h"
48 #if defined(HAVE_ALLOCA)
49 # define my_alloc(size) alloca (size)
50 # define my_free(ptr) do { } while(0)
52 # define my_alloc(size) malloc (size)
53 # define my_free(ptr) free (ptr)
56 /* Copy extended attributes from src_path to dst_path. If the file
57 has an extended Access ACL (system.posix_acl_access) and that is
58 copied successfully, the file mode permission bits are copied as
59 a side effect. This may not always the case, so the file mode
60 and/or ownership must be copied separately. */
62 attr_copy_file(const char *src_path, const char *dst_path,
63 int (*check) (const char *, struct error_context *),
64 struct error_context *ctx)
66 #if defined(HAVE_LISTXATTR) && defined(HAVE_GETXATTR) && defined(HAVE_SETXATTR)
69 char *names = NULL, *end_names, *name, *value = NULL;
70 unsigned int setxattr_ENOTSUP = 0;
72 /* ignore acls by default */
74 check = attr_copy_check_permissions;
76 size = llistxattr (src_path, NULL, 0);
78 if (errno != ENOSYS && errno != ENOTSUP) {
79 const char *qpath = quote (ctx, src_path);
80 error (ctx, _("listing attributes of %s"), qpath);
81 quote_free (ctx, qpath);
86 names = (char *) my_alloc (size+1);
92 size = llistxattr (src_path, names, size);
94 const char *qpath = quote (ctx, src_path);
95 error (ctx, _("listing attributes of %s"), qpath);
96 quote_free (ctx, qpath);
101 end_names = names + size;
104 for (name = names; name != end_names; name = strchr(name, '\0') + 1) {
107 /* check if this attribute shall be preserved */
108 if (!*name || !check(name, ctx))
111 size = lgetxattr (src_path, name, NULL, 0);
113 const char *qpath = quote (ctx, src_path);
114 const char *qname = quote (ctx, name);
115 error (ctx, _("getting attribute %s of %s"),
117 quote_free (ctx, qname);
118 quote_free (ctx, qpath);
122 value = (char *) realloc (old_value = value, size);
123 if (size != 0 && value == NULL) {
128 size = lgetxattr (src_path, name, value, size);
130 const char *qpath = quote (ctx, src_path);
131 const char *qname = quote (ctx, name);
132 error (ctx, _("getting attribute %s of %s"),
134 quote_free (ctx, qname);
135 quote_free (ctx, qpath);
139 if (lsetxattr (dst_path, name, value, size, 0) != 0) {
140 if (errno == ENOTSUP)
143 const char *qpath = quote (ctx, dst_path);
144 if (errno == ENOSYS) {
145 error (ctx, _("setting attributes for "
148 /* no hope of getting any further */
151 const char *qname = quote (ctx, name);
152 error (ctx, _("setting attribute %s for "
153 "%s"), qname, qpath);
154 quote_free (ctx, qname);
157 quote_free (ctx, qpath);
161 if (setxattr_ENOTSUP) {
162 const char *qpath = quote (ctx, dst_path);
164 error (ctx, _("setting attributes for %s"), qpath);
166 quote_free (ctx, qpath);