2 * Wrapper functions for <sys/xattr.h> (or <attr/xattr.h>) and <sys/extattr.h>
5 * Daniel Drake (dsd@gentoo.org)
7 * Copyright (C) 2005 Daniel Drake
12 #if defined(HAVE_SYS_XATTR_H) || defined(HAVE_ATTR_ATTR_H) || defined(HAVE_SYS_EXTATTR_H)
14 #include <sys/types.h>
17 * Where available, we prefer to use the libc implementation of the xattr
18 * syscalls. However, we also support using libattr for this on systems where
19 * libc does not provide this (e.g. glibc-2.2 and older)
20 * (configure-time magic is used to select which library to link to)
22 #ifdef HAVE_SYS_XATTR_H
24 #include <sys/xattr.h>
26 #elif HAVE_ATTR_ATTR_H
28 #include <attr/xattr.h>
30 #endif /* HAVE_SYS_XATTR_H */
32 #ifdef HAVE_SYS_EXTATTR_H
33 #include <sys/extattr.h>
48 * Linux provides extended attributes through the <sys/xattr.h> API.
49 * Any file or link can have attributes assigned to it (provided that they are
50 * supported by the backing filesystem). Each attribute has to be placed in a
51 * namespace, of which "user" is the most common. Namespaces are specified as
52 * a prefix to the attribute name, proceeded by a '.' (e.g. user.myattribute)
54 * FreeBSD provides extended attributes through the <sys/extattr.h> API.
55 * Behaviour is very similar to Linux EA's, but the namespace is specified
56 * through an enum-style parameter rather than as a prefix to an attribute
57 * name. There are also differences in the behaviour of the "list attributes"
60 * This file merges the two implementations into a single API for use by the
61 * Mono.Unix.Syscall.*xattr methods. No matter which OS you are on, things
62 * should "just work" the same as anywhere else.
64 * The API provided here leans more towards the Linux implementation. Attribute
65 * namespaces are provided as prefixes to the attribute name (followed by '.').
66 * There is no limit to the namespaces accepted by the Linux side of this
67 * implementation, but you are obviously limited to the ones available to you
69 * FreeBSD namespaces have to be converted from the textual prefix into their
70 * relevant number so that they can be used in the FreeBSD system calls.
71 * This means that the only namespaces available are the ones known by in this
72 * file (see bsd_extattr_namespaces). However, you can also specify the
73 * numericalnamespace index yourself, by using an attribute name such as
75 * (this will obviously fail on Linux, your code will no longer be 'portable')
77 * Linux {,l,f}setxattr calls have a flags parameter which allow you to control
78 * what should happen if an attribute with the same name does (or doesn't)
79 * already exist. The 'flags' parameter is available here, but because FreeBSD
80 * does not support this kind of refinement, it will fail on FreeBSD if you
81 * specify anything other than XATTR_AUTO (XATTR_AUTO will create the attribute
82 * if it doesn't already exist, and overwrite the existing attribute if it
85 * For usage and behaviour information, see the monodoc documentation on the
86 * Mono.Unix.Syscall class.
97 struct BsdNamespaceInfo {
102 static struct BsdNamespaceInfo bsd_extattr_namespaces[] = {
103 {"user" , EXTATTR_NAMESPACE_USER},
104 {"system" , EXTATTR_NAMESPACE_SYSTEM}
107 static int bsd_check_flags (gint32 flags)
109 // BSD doesn't support flags, but always provides the same behaviour as
110 // XATTR_AUTO. So we enforce that here.
111 if (flags != Mono_Posix_XattrFlags_XATTR_AUTO) {
118 // On FreeBSD, we need to convert "user.blah" into namespace 1 and attribute
119 // name "blah", or maybe "6.blah" into namespace 6 attribute "blah"
121 bsd_handle_nsprefix (const char *name, char **_name, int *namespace)
124 gchar **components = g_strsplit (name, ".", 2);
126 // Find namespace number from textual representation
127 for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
128 if (strcmp (bsd_extattr_namespaces[i].name, components[0]) == 0) {
129 *namespace = bsd_extattr_namespaces[i].value;
133 if (*namespace == 0) {
134 // Perhaps they specified the namespace number themselves..?
136 *namespace = (int) strtol (components[0], &endptr, 10);
141 *_name = g_strdup (components[1]);
142 g_strfreev (components);
147 init_attrlists (char *attrlists[])
149 memset (attrlists, 0, G_N_ELEMENTS(bsd_extattr_namespaces) * sizeof(char*));
153 free_attrlists (char *attrlists[])
156 for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
157 g_free (attrlists[i]);
160 // Counts the number of attributes in the result of a
161 // extattr_list_*() call. Note that the format of the data
162 // is: \3one\3two\6eleven where the leading charaters represent the length
163 // of the following attribute. (the description in the man-page is wrong)
165 count_num_attrs (char *attrs, size_t size)
168 unsigned int num_attrs = 0;
181 // Convert a BSD-style list buffer (see the description for count_num_attrs)
182 // into a Linux-style NULL-terminated list including namespace prefix.
184 *bsd_convert_list (const char *nsprefix, const char *src, size_t size, char *dest)
187 if (src == NULL || dest == NULL || size == 0)
192 int attr_len = (int) src[i];
193 int prefix_len = strlen (nsprefix);
195 // Add namespace prefix
196 strncpy (dest, nsprefix, prefix_len);
197 dest[prefix_len] = '.';
198 dest += prefix_len + 1;
201 memcpy(dest, src + ++i, attr_len);
205 dest[attr_len] = '\0';
206 dest += attr_len + 1;
212 // Combine all the lists of attributes that we know about into a single
213 // Linux-style buffer
215 bsd_combine_lists (char *attrlists[], char *dest, size_t dest_size_needed, size_t dest_size)
219 return dest_size_needed;
221 if (dest_size < dest_size_needed) {
226 for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
228 dest = bsd_convert_list (bsd_extattr_namespaces[i].name, attrlists[i], strlen (attrlists[i]), dest);
230 return dest_size_needed;
234 bsd_listxattr (const char *path, void *list, mph_size_t size)
236 size_t full_size = 0;
238 char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
240 init_attrlists (attrlists);
241 for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
245 buf_size = (size_t) extattr_list_file (path, i + 1, NULL, 0);
249 attrlists[i] = g_malloc0 (buf_size + 1);
250 buf_size = (size_t) extattr_list_file (path, i + 1, attrlists[i], buf_size);
254 num_attrs = count_num_attrs(attrlists[i], buf_size);
255 full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
258 full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
259 free_attrlists (attrlists);
264 bsd_llistxattr (const char *path, void *list, mph_size_t size)
266 size_t full_size = 0;
268 char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
270 init_attrlists (attrlists);
271 for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
275 buf_size = (size_t) extattr_list_link (path, i + 1, NULL, 0);
279 attrlists[i] = g_malloc0 (buf_size + 1);
280 buf_size = (size_t) extattr_list_link (path, i + 1, attrlists[i], buf_size);
284 num_attrs = count_num_attrs(attrlists[i], buf_size);
285 full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
288 full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
289 free_attrlists (attrlists);
294 bsd_flistxattr (int fd, void *list, mph_size_t size)
296 size_t full_size = 0;
298 char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
300 init_attrlists (attrlists);
301 for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
305 buf_size = (size_t) extattr_list_fd (fd, i + 1, NULL, 0);
309 attrlists[i] = g_malloc0 (buf_size + 1);
310 buf_size = (size_t) extattr_list_fd (fd, i + 1, attrlists[i], buf_size);
314 num_attrs = count_num_attrs(attrlists[i], buf_size);
315 full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
318 full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
319 free_attrlists (attrlists);
330 Mono_Posix_Syscall_setxattr (const char *path, const char *name, void *value, mph_size_t size, gint32 flags)
333 mph_return_if_size_t_overflow (size);
338 if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
341 ret = setxattr (path, name, value, (size_t) size, 0, _flags);
342 #else /* __APPLE__ */
343 ret = setxattr (path, name, value, (size_t) size, _flags);
344 #endif /* __APPLE__ */
350 if (bsd_check_flags (flags) == -1)
352 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
354 ret = extattr_set_file (path, namespace, _name, value, (size_t) size);
364 Mono_Posix_Syscall_lsetxattr (const char *path, const char *name, void *value, mph_size_t size, gint32 flags)
367 mph_return_if_size_t_overflow (size);
372 if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
374 ret = lsetxattr (path, name, value, size, _flags);
380 if (bsd_check_flags (flags) == -1)
382 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
384 ret = extattr_set_link (path, namespace, _name, value, (size_t) size);
391 #endif /* !__APPLE__ */
394 Mono_Posix_Syscall_fsetxattr (int fd, const char *name, void *value, mph_size_t size, gint32 flags)
397 mph_return_if_size_t_overflow (size);
402 if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
405 ret = fsetxattr (fd, name, value, (size_t) size, 0, _flags);
406 #else /* __APPLE__ */
407 ret = fsetxattr (fd, name, value, (size_t) size, _flags);
408 #endif /* __APPLE__ */
414 if (bsd_check_flags (flags) == -1)
416 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
418 ret = extattr_set_fd (fd, namespace, _name, value, (size_t) size);
427 Mono_Posix_Syscall_getxattr (const char *path, const char *name, void *value, mph_size_t size)
430 mph_return_if_size_t_overflow (size);
434 ret = getxattr (path, name, value, (size_t) size, 0, 0);
435 #else /* __APPLE__ */
436 ret = getxattr (path, name, value, (size_t) size);
437 #endif /* __APPLE__ */
442 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
444 ret = extattr_get_file (path, namespace, _name, value, (size_t) size);
454 Mono_Posix_Syscall_lgetxattr (const char *path, const char *name, void *value, mph_size_t size)
457 mph_return_if_size_t_overflow (size);
460 ret = lgetxattr (path, name, value, (size_t) size);
465 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
467 ret = extattr_get_link (path, namespace, _name, value, (size_t) size);
474 #endif /* !__APPLE__ */
477 Mono_Posix_Syscall_fgetxattr (int fd, const char *name, void *value, mph_size_t size)
480 mph_return_if_size_t_overflow (size);
484 ret = fgetxattr (fd, name, value, (size_t) size, 0, 0);
485 #else /* __APPLE__ */
486 ret = fgetxattr (fd, name, value, (size_t) size);
487 #endif /* __APPLE__ */
492 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
494 ret = extattr_get_fd (fd, namespace, _name, value, (size_t) size);
503 Mono_Posix_Syscall_listxattr (const char *path, void *list, mph_size_t size)
505 mph_return_if_size_t_overflow (size);
509 return listxattr (path, list, (size_t) size, 0);
510 #else /* __APPLE__ */
511 return listxattr (path, list, (size_t) size);
512 #endif /* __APPLE__ */
514 return bsd_listxattr (path, list, size);
520 Mono_Posix_Syscall_llistxattr (const char *path, void *list, mph_size_t size)
522 mph_return_if_size_t_overflow (size);
525 return llistxattr (path, list, (size_t) size);
527 return bsd_llistxattr (path, list, size);
530 #endif /* !__APPLE__ */
533 Mono_Posix_Syscall_flistxattr (int fd, void *list, mph_size_t size)
535 mph_return_if_size_t_overflow (size);
539 return flistxattr (fd, list, (size_t) size, 0);
540 #else /* __APPLE__ */
541 return flistxattr (fd, list, (size_t) size);
542 #endif /* __APPLE__ */
544 return bsd_flistxattr (fd, list, size);
549 Mono_Posix_Syscall_removexattr (const char *path, const char *name)
555 ret = removexattr (path, name, 0);
556 #else /* __APPLE__ */
557 ret = removexattr (path, name);
558 #endif /* __APPLE__ */
563 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
565 ret = extattr_delete_file (path, namespace, _name);
575 Mono_Posix_Syscall_lremovexattr (const char *path, const char *name)
580 ret = lremovexattr (path, name);
585 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
587 ret = extattr_delete_link (path, namespace, _name);
594 #endif /* !__APPLE__ */
597 Mono_Posix_Syscall_fremovexattr (int fd, const char *name)
603 ret = fremovexattr (fd, name, 0);
604 #else /* __APPLE__ */
605 ret = fremovexattr (fd, name);
606 #endif /* __APPLE__ */
611 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
613 ret = extattr_delete_fd (fd, namespace, _name);
623 #endif /* HAVE_SYS_XATTR_H || HAVE_ATTR_ATTR_H || HAVE_SYS_EXTATTR_H */