2 * Extended attribute support for rsync.
4 * Copyright (C) 2004 Red Hat, Inc.
5 * Copyright (C) 2003-2008 Wayne Davison
6 * Written by Jay Fenlason.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
23 #include "sysxattrs.h"
27 #if defined HAVE_LINUX_XATTRS
29 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
31 return lgetxattr(path, name, value, size);
34 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
36 return fgetxattr(filedes, name, value, size);
39 int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
41 return lsetxattr(path, name, value, size, 0);
44 int sys_lremovexattr(const char *path, const char *name)
46 return lremovexattr(path, name);
49 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
51 return llistxattr(path, list, size);
56 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
58 return getxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
61 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
63 return fgetxattr(filedes, name, value, size, 0, 0);
66 int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
68 return setxattr(path, name, value, size, 0, XATTR_NOFOLLOW);
71 int sys_lremovexattr(const char *path, const char *name)
73 return removexattr(path, name, XATTR_NOFOLLOW);
76 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
78 return listxattr(path, list, size, XATTR_NOFOLLOW);
81 #elif HAVE_FREEBSD_XATTRS
83 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
85 return extattr_get_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
88 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
90 return extattr_get_fd(filedes, EXTATTR_NAMESPACE_USER, name, value, size);
93 int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
95 return extattr_set_link(path, EXTATTR_NAMESPACE_USER, name, value, size);
98 int sys_lremovexattr(const char *path, const char *name)
100 return extattr_delete_link(path, EXTATTR_NAMESPACE_USER, name);
103 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
105 unsigned char keylen;
106 ssize_t off, len = extattr_list_link(path, EXTATTR_NAMESPACE_USER, list, size);
108 if (len <= 0 || (size_t)len > size)
111 /* FreeBSD puts a single-byte length before each string, with no '\0'
112 * terminator. We need to change this into a series of null-terminted
113 * strings. Since the size is the same, we can simply transform the
114 * output in place. */
115 for (off = 0; off < len; off += keylen + 1) {
116 keylen = ((unsigned char*)list)[off];
117 if (off + keylen >= len) {
118 /* Should be impossible, but kernel bugs happen! */
122 memmove(list+off, list+off+1, keylen);
123 list[off+keylen] = '\0';
129 #elif HAVE_SOLARIS_XATTRS
131 static ssize_t read_xattr(int attrfd, void *buf, size_t buflen)
136 if (fstat(attrfd, &sb) < 0)
138 else if (sb.st_size > SSIZE_MAX) {
141 } else if (buflen == 0)
143 else if (sb.st_size > buflen) {
148 for (bufpos = 0; bufpos < sb.st_size; ) {
149 ssize_t cnt = read(attrfd, buf + bufpos, sb.st_size - bufpos);
151 if (cnt < 0 && errno == EINTR)
166 ssize_t sys_lgetxattr(const char *path, const char *name, void *value, size_t size)
170 if ((attrfd = attropen(path, name, O_RDONLY)) < 0) {
175 return read_xattr(attrfd, value, size);
178 ssize_t sys_fgetxattr(int filedes, const char *name, void *value, size_t size)
182 if ((attrfd = openat(filedes, name, O_RDONLY|O_XATTR, 0)) < 0) {
187 return read_xattr(attrfd, value, size);
190 int sys_lsetxattr(const char *path, const char *name, const void *value, size_t size)
194 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
196 if ((attrfd = attropen(path, name, O_CREAT|O_TRUNC|O_WRONLY, mode)) < 0)
199 for (bufpos = 0; bufpos < size; ) {
200 ssize_t cnt = write(attrfd, value+bufpos, size);
202 if (cnt < 0 && errno == EINTR)
212 return bufpos > 0 ? 0 : -1;
215 int sys_lremovexattr(const char *path, const char *name)
220 if ((attrdirfd = attropen(path, ".", O_RDONLY)) < 0)
223 ret = unlinkat(attrdirfd, name, 0);
230 ssize_t sys_llistxattr(const char *path, char *list, size_t size)
237 if ((attrdirfd = attropen(path, ".", O_RDONLY)) < 0) {
242 if ((dirp = fdopendir(attrdirfd)) == NULL) {
247 while ((dp = readdir(dirp))) {
248 int len = strlen(dp->d_name);
250 if (dp->d_name[0] == '.' && (len == 1 || (len == 2 && dp->d_name[1] == '.')))
252 if (len == 11 && dp->d_name[0] == 'S' && strncmp(dp->d_name, "SUNWattr_r", 10) == 0
253 && (dp->d_name[10] == 'o' || dp->d_name[10] == 'w'))
256 if ((ret += len+1) > size) {
263 memcpy(list, dp->d_name, len+1);
275 #error You need to create xattr compatibility functions.
279 #endif /* SUPPORT_XATTRS */