2 * Extended attribute support for rsync.
4 * Copyright (C) 2004 Red Hat, Inc.
5 * Copyright (C) 2003-2022 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 #ifdef HAVE_OSX_XATTRS
28 #define GETXATTR_FETCH_LIMIT (64*1024*1024)
31 #if defined HAVE_LINUX_XATTRS
33 ssize_t
sys_lgetxattr(const char *path
, const char *name
, void *value
, size_t size
)
35 return lgetxattr(path
, name
, value
, size
);
38 ssize_t
sys_fgetxattr(int filedes
, const char *name
, void *value
, size_t size
)
40 return fgetxattr(filedes
, name
, value
, size
);
43 int sys_lsetxattr(const char *path
, const char *name
, const void *value
, size_t size
)
45 return lsetxattr(path
, name
, value
, size
, 0);
48 int sys_lremovexattr(const char *path
, const char *name
)
50 return lremovexattr(path
, name
);
53 ssize_t
sys_llistxattr(const char *path
, char *list
, size_t size
)
55 return llistxattr(path
, list
, size
);
60 ssize_t
sys_lgetxattr(const char *path
, const char *name
, void *value
, size_t size
)
62 ssize_t len
= getxattr(path
, name
, value
, size
, 0, XATTR_NOFOLLOW
);
64 /* If we're retrieving data, handle resource forks > 64MB specially */
65 if (value
!= NULL
&& len
== GETXATTR_FETCH_LIMIT
&& (size_t)len
< size
) {
66 /* getxattr will only return 64MB of data at a time, need to call again with a new offset */
67 u_int32_t offset
= len
;
68 size_t data_retrieved
= len
;
69 while (data_retrieved
< size
) {
70 len
= getxattr(path
, name
, (char*)value
+ offset
, size
- data_retrieved
, offset
, XATTR_NOFOLLOW
);
73 data_retrieved
+= len
;
74 offset
+= (u_int32_t
)len
;
82 ssize_t
sys_fgetxattr(int filedes
, const char *name
, void *value
, size_t size
)
84 return fgetxattr(filedes
, name
, value
, size
, 0, 0);
87 int sys_lsetxattr(const char *path
, const char *name
, const void *value
, size_t size
)
89 return setxattr(path
, name
, value
, size
, 0, XATTR_NOFOLLOW
);
92 int sys_lremovexattr(const char *path
, const char *name
)
94 return removexattr(path
, name
, XATTR_NOFOLLOW
);
97 ssize_t
sys_llistxattr(const char *path
, char *list
, size_t size
)
99 return listxattr(path
, list
, size
, XATTR_NOFOLLOW
);
102 #elif HAVE_FREEBSD_XATTRS
104 ssize_t
sys_lgetxattr(const char *path
, const char *name
, void *value
, size_t size
)
106 return extattr_get_link(path
, EXTATTR_NAMESPACE_USER
, name
, value
, size
);
109 ssize_t
sys_fgetxattr(int filedes
, const char *name
, void *value
, size_t size
)
111 return extattr_get_fd(filedes
, EXTATTR_NAMESPACE_USER
, name
, value
, size
);
114 int sys_lsetxattr(const char *path
, const char *name
, const void *value
, size_t size
)
116 return extattr_set_link(path
, EXTATTR_NAMESPACE_USER
, name
, value
, size
);
119 int sys_lremovexattr(const char *path
, const char *name
)
121 return extattr_delete_link(path
, EXTATTR_NAMESPACE_USER
, name
);
124 ssize_t
sys_llistxattr(const char *path
, char *list
, size_t size
)
126 unsigned char keylen
;
127 ssize_t off
, len
= extattr_list_link(path
, EXTATTR_NAMESPACE_USER
, list
, size
);
129 if (len
<= 0 || (size_t)len
> size
)
132 /* FreeBSD puts a single-byte length before each string, with no '\0'
133 * terminator. We need to change this into a series of null-terminted
134 * strings. Since the size is the same, we can simply transform the
135 * output in place. */
136 for (off
= 0; off
< len
; off
+= keylen
+ 1) {
137 keylen
= ((unsigned char*)list
)[off
];
138 if (off
+ keylen
>= len
) {
139 /* Should be impossible, but kernel bugs happen! */
143 memmove(list
+off
, list
+off
+1, keylen
);
144 list
[off
+keylen
] = '\0';
150 #elif HAVE_SOLARIS_XATTRS
152 static ssize_t
read_xattr(int attrfd
, void *buf
, size_t buflen
)
157 if (fstat(attrfd
, &sb
) < 0)
159 else if (sb
.st_size
> SSIZE_MAX
) {
162 } else if (buflen
== 0)
164 else if (sb
.st_size
> buflen
) {
169 for (bufpos
= 0; bufpos
< sb
.st_size
; ) {
170 ssize_t cnt
= read(attrfd
, (char*)buf
+ bufpos
, sb
.st_size
- bufpos
);
172 if (cnt
< 0 && errno
== EINTR
)
187 ssize_t
sys_lgetxattr(const char *path
, const char *name
, void *value
, size_t size
)
191 if ((attrfd
= attropen(path
, name
, O_RDONLY
)) < 0) {
196 return read_xattr(attrfd
, value
, size
);
199 ssize_t
sys_fgetxattr(int filedes
, const char *name
, void *value
, size_t size
)
203 if ((attrfd
= openat(filedes
, name
, O_RDONLY
|O_XATTR
, 0)) < 0) {
208 return read_xattr(attrfd
, value
, size
);
211 int sys_lsetxattr(const char *path
, const char *name
, const void *value
, size_t size
)
215 mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
217 if ((attrfd
= attropen(path
, name
, O_CREAT
|O_TRUNC
|O_WRONLY
, mode
)) < 0)
220 for (bufpos
= 0; bufpos
< size
; ) {
221 ssize_t cnt
= write(attrfd
, (char*)value
+ bufpos
, size
);
223 if (cnt
< 0 && errno
== EINTR
)
233 return bufpos
> 0 ? 0 : -1;
236 int sys_lremovexattr(const char *path
, const char *name
)
241 if ((attrdirfd
= attropen(path
, ".", O_RDONLY
)) < 0)
244 ret
= unlinkat(attrdirfd
, name
, 0);
251 ssize_t
sys_llistxattr(const char *path
, char *list
, size_t size
)
258 if ((attrdirfd
= attropen(path
, ".", O_RDONLY
)) < 0) {
263 if ((dirp
= fdopendir(attrdirfd
)) == NULL
) {
268 while ((dp
= readdir(dirp
))) {
269 int len
= strlen(dp
->d_name
);
271 if (dp
->d_name
[0] == '.' && (len
== 1 || (len
== 2 && dp
->d_name
[1] == '.')))
273 if (len
== 11 && dp
->d_name
[0] == 'S' && strncmp(dp
->d_name
, "SUNWattr_r", 10) == 0
274 && (dp
->d_name
[10] == 'o' || dp
->d_name
[10] == 'w'))
278 if ((size_t)ret
> size
) {
285 memcpy(list
, dp
->d_name
, len
+1);
297 #error You need to create xattr compatibility functions.
301 #endif /* SUPPORT_XATTRS */