2 * Wrapper functions for <sys/xattr.h> (or <attr/xattr.h>) and <sys/extattr.h>
5 * Daniel Drake (dsd@gentoo.org)
6 * Jonathan Pryor (jonpryor@vt.edu)
8 * Copyright (C) 2005 Daniel Drake
9 * Copyright (C) 2006 Jonathan Pryor
14 //If we're compiling to API level < 16 this won't be available
15 #if defined (HOST_ANDROID) && __ANDROID_API__ < 16
16 #define ANDROID_NO_XATTR
19 #if (defined(HAVE_SYS_XATTR_H) || defined(HAVE_ATTR_ATTR_H) || defined(HAVE_SYS_EXTATTR_H)) && !defined (ANDROID_NO_XATTR)
21 #include <sys/types.h>
24 * Where available, we prefer to use the libc implementation of the xattr
25 * syscalls. However, we also support using libattr for this on systems where
26 * libc does not provide this (e.g. glibc-2.2 and older)
27 * (configure-time magic is used to select which library to link to)
29 #ifdef HAVE_SYS_XATTR_H
31 #include <sys/xattr.h>
33 #elif HAVE_ATTR_ATTR_H
35 #include <attr/xattr.h>
37 #endif /* HAVE_SYS_XATTR_H */
39 #ifdef HAVE_SYS_EXTATTR_H
40 #include <sys/extattr.h>
55 * Linux provides extended attributes through the <sys/xattr.h> API.
56 * Any file or link can have attributes assigned to it (provided that they are
57 * supported by the backing filesystem). Each attribute has to be placed in a
58 * namespace, of which "user" is the most common. Namespaces are specified as
59 * a prefix to the attribute name, proceeded by a '.' (e.g. user.myattribute)
61 * FreeBSD provides extended attributes through the <sys/extattr.h> API.
62 * Behaviour is very similar to Linux EA's, but the namespace is specified
63 * through an enum-style parameter rather than as a prefix to an attribute
64 * name. There are also differences in the behaviour of the "list attributes"
67 * This file merges the two implementations into a single API for use by the
68 * Mono.Unix.Syscall.*xattr methods. No matter which OS you are on, things
69 * should "just work" the same as anywhere else.
71 * The API provided here leans more towards the Linux implementation. Attribute
72 * namespaces are provided as prefixes to the attribute name (followed by '.').
73 * There is no limit to the namespaces accepted by the Linux side of this
74 * implementation, but you are obviously limited to the ones available to you
76 * FreeBSD namespaces have to be converted from the textual prefix into their
77 * relevant number so that they can be used in the FreeBSD system calls.
78 * This means that the only namespaces available are the ones known by in this
79 * file (see bsd_extattr_namespaces). However, you can also specify the
80 * numericalnamespace index yourself, by using an attribute name such as
82 * (this will obviously fail on Linux, your code will no longer be 'portable')
84 * Linux {,l,f}setxattr calls have a flags parameter which allow you to control
85 * what should happen if an attribute with the same name does (or doesn't)
86 * already exist. The 'flags' parameter is available here, but because FreeBSD
87 * does not support this kind of refinement, it will fail on FreeBSD if you
88 * specify anything other than XATTR_AUTO (XATTR_AUTO will create the attribute
89 * if it doesn't already exist, and overwrite the existing attribute if it
92 * For usage and behaviour information, see the monodoc documentation on the
93 * Mono.Unix.Syscall class.
104 struct BsdNamespaceInfo
{
109 static struct BsdNamespaceInfo bsd_extattr_namespaces
[] = {
110 {"user" , EXTATTR_NAMESPACE_USER
},
111 {"system" , EXTATTR_NAMESPACE_SYSTEM
}
114 static int bsd_check_flags (gint32 flags
)
116 // BSD doesn't support flags, but always provides the same behaviour as
117 // XATTR_AUTO. So we enforce that here.
118 if (flags
!= Mono_Posix_XattrFlags_XATTR_AUTO
) {
125 // On FreeBSD, we need to convert "user.blah" into namespace 1 and attribute
126 // name "blah", or maybe "6.blah" into namespace 6 attribute "blah"
128 bsd_handle_nsprefix (const char *name
, char **_name
, int *namespace)
131 gchar
**components
= g_strsplit (name
, ".", 2);
133 // Find namespace number from textual representation
134 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++)
135 if (strcmp (bsd_extattr_namespaces
[i
].name
, components
[0]) == 0) {
136 *namespace = bsd_extattr_namespaces
[i
].value
;
140 if (*namespace == 0) {
141 // Perhaps they specified the namespace number themselves..?
143 *namespace = (int) strtol (components
[0], &endptr
, 10);
148 *_name
= g_strdup (components
[1]);
149 g_strfreev (components
);
154 init_attrlists (char *attrlists
[])
156 memset (attrlists
, 0, G_N_ELEMENTS(bsd_extattr_namespaces
) * sizeof(char*));
160 free_attrlists (char *attrlists
[])
163 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++)
164 g_free (attrlists
[i
]);
167 // Counts the number of attributes in the result of a
168 // extattr_list_*() call. Note that the format of the data
169 // is: \3one\3two\6eleven where the leading charaters represent the length
170 // of the following attribute. (the description in the man-page is wrong)
172 count_num_attrs (char *attrs
, size_t size
)
175 unsigned int num_attrs
= 0;
188 // Convert a BSD-style list buffer (see the description for count_num_attrs)
189 // into a Linux-style NULL-terminated list including namespace prefix.
191 *bsd_convert_list (const char *nsprefix
, const char *src
, size_t size
, char *dest
)
194 if (src
== NULL
|| dest
== NULL
|| size
== 0)
199 int attr_len
= (int) src
[i
];
200 int prefix_len
= strlen (nsprefix
);
202 // Add namespace prefix
203 strncpy (dest
, nsprefix
, prefix_len
);
204 dest
[prefix_len
] = '.';
205 dest
+= prefix_len
+ 1;
208 memcpy(dest
, src
+ ++i
, attr_len
);
212 dest
[attr_len
] = '\0';
213 dest
+= attr_len
+ 1;
219 // Combine all the lists of attributes that we know about into a single
220 // Linux-style buffer
222 bsd_combine_lists (char *attrlists
[], char *dest
, size_t dest_size_needed
, size_t dest_size
)
226 return dest_size_needed
;
228 if (dest_size
< dest_size_needed
) {
233 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++)
235 dest
= bsd_convert_list (bsd_extattr_namespaces
[i
].name
, attrlists
[i
], strlen (attrlists
[i
]), dest
);
237 return dest_size_needed
;
241 bsd_listxattr (const char *path
, void *list
, mph_size_t size
)
243 size_t full_size
= 0;
245 char *attrlists
[G_N_ELEMENTS(bsd_extattr_namespaces
)];
247 init_attrlists (attrlists
);
248 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++) {
252 buf_size
= (size_t) extattr_list_file (path
, i
+ 1, NULL
, 0);
256 attrlists
[i
] = g_malloc0 (buf_size
+ 1);
257 buf_size
= (size_t) extattr_list_file (path
, i
+ 1, attrlists
[i
], buf_size
);
261 num_attrs
= count_num_attrs(attrlists
[i
], buf_size
);
262 full_size
+= buf_size
+ (num_attrs
* (strlen (bsd_extattr_namespaces
[i
].name
) + 1));
265 full_size
= bsd_combine_lists (attrlists
, (char *) list
, full_size
, size
);
266 free_attrlists (attrlists
);
271 bsd_llistxattr (const char *path
, void *list
, mph_size_t size
)
273 size_t full_size
= 0;
275 char *attrlists
[G_N_ELEMENTS(bsd_extattr_namespaces
)];
277 init_attrlists (attrlists
);
278 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++) {
282 buf_size
= (size_t) extattr_list_link (path
, i
+ 1, NULL
, 0);
286 attrlists
[i
] = g_malloc0 (buf_size
+ 1);
287 buf_size
= (size_t) extattr_list_link (path
, i
+ 1, attrlists
[i
], buf_size
);
291 num_attrs
= count_num_attrs(attrlists
[i
], buf_size
);
292 full_size
+= buf_size
+ (num_attrs
* (strlen (bsd_extattr_namespaces
[i
].name
) + 1));
295 full_size
= bsd_combine_lists (attrlists
, (char *) list
, full_size
, size
);
296 free_attrlists (attrlists
);
301 bsd_flistxattr (int fd
, void *list
, mph_size_t size
)
303 size_t full_size
= 0;
305 char *attrlists
[G_N_ELEMENTS(bsd_extattr_namespaces
)];
307 init_attrlists (attrlists
);
308 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++) {
312 buf_size
= (size_t) extattr_list_fd (fd
, i
+ 1, NULL
, 0);
316 attrlists
[i
] = g_malloc0 (buf_size
+ 1);
317 buf_size
= (size_t) extattr_list_fd (fd
, i
+ 1, attrlists
[i
], buf_size
);
321 num_attrs
= count_num_attrs(attrlists
[i
], buf_size
);
322 full_size
+= buf_size
+ (num_attrs
* (strlen (bsd_extattr_namespaces
[i
].name
) + 1));
325 full_size
= bsd_combine_lists (attrlists
, (char *) list
, full_size
, size
);
326 free_attrlists (attrlists
);
337 Mono_Posix_Syscall_setxattr (const char *path
, const char *name
, unsigned char *value
, mph_size_t size
, gint32 flags
)
340 mph_return_if_size_t_overflow (size
);
345 if (Mono_Posix_FromXattrFlags (flags
, &_flags
) == -1)
348 ret
= setxattr (path
, name
, value
, (size_t) size
, 0, _flags
);
349 #else /* __APPLE__ */
350 ret
= setxattr (path
, name
, value
, (size_t) size
, _flags
);
351 #endif /* __APPLE__ */
357 if (bsd_check_flags (flags
) == -1)
359 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
361 ret
= extattr_set_file (path
, namespace, _name
, value
, (size_t) size
);
371 Mono_Posix_Syscall_lsetxattr (const char *path
, const char *name
, unsigned char *value
, mph_size_t size
, gint32 flags
)
374 mph_return_if_size_t_overflow (size
);
379 if (Mono_Posix_FromXattrFlags (flags
, &_flags
) == -1)
381 ret
= lsetxattr (path
, name
, value
, size
, _flags
);
387 if (bsd_check_flags (flags
) == -1)
389 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
391 ret
= extattr_set_link (path
, namespace, _name
, value
, (size_t) size
);
398 #endif /* !__APPLE__ */
401 Mono_Posix_Syscall_fsetxattr (int fd
, const char *name
, unsigned char *value
, mph_size_t size
, gint32 flags
)
404 mph_return_if_size_t_overflow (size
);
409 if (Mono_Posix_FromXattrFlags (flags
, &_flags
) == -1)
412 ret
= fsetxattr (fd
, name
, value
, (size_t) size
, 0, _flags
);
413 #else /* __APPLE__ */
414 ret
= fsetxattr (fd
, name
, value
, (size_t) size
, _flags
);
415 #endif /* __APPLE__ */
421 if (bsd_check_flags (flags
) == -1)
423 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
425 ret
= extattr_set_fd (fd
, namespace, _name
, value
, (size_t) size
);
434 Mono_Posix_Syscall_getxattr (const char *path
, const char *name
, unsigned char *value
, mph_size_t size
)
437 mph_return_if_size_t_overflow (size
);
441 ret
= getxattr (path
, name
, value
, (size_t) size
, 0, 0);
442 #else /* __APPLE__ */
443 ret
= getxattr (path
, name
, value
, (size_t) size
);
444 #endif /* __APPLE__ */
449 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
451 ret
= extattr_get_file (path
, namespace, _name
, value
, (size_t) size
);
461 Mono_Posix_Syscall_lgetxattr (const char *path
, const char *name
, unsigned char *value
, mph_size_t size
)
464 mph_return_if_size_t_overflow (size
);
467 ret
= lgetxattr (path
, name
, value
, (size_t) size
);
472 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
474 ret
= extattr_get_link (path
, namespace, _name
, value
, (size_t) size
);
481 #endif /* !__APPLE__ */
484 Mono_Posix_Syscall_fgetxattr (int fd
, const char *name
, unsigned char *value
, mph_size_t size
)
487 mph_return_if_size_t_overflow (size
);
491 ret
= fgetxattr (fd
, name
, value
, (size_t) size
, 0, 0);
492 #else /* __APPLE__ */
493 ret
= fgetxattr (fd
, name
, value
, (size_t) size
);
494 #endif /* __APPLE__ */
499 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
501 ret
= extattr_get_fd (fd
, namespace, _name
, value
, (size_t) size
);
510 Mono_Posix_Syscall_listxattr (const char *path
, unsigned char *list
, mph_size_t size
)
512 mph_return_if_size_t_overflow (size
);
516 return listxattr (path
, (char*) list
, (size_t) size
, 0);
517 #else /* __APPLE__ */
518 return listxattr (path
, (char*) list
, (size_t) size
);
519 #endif /* __APPLE__ */
521 return bsd_listxattr (path
, list
, size
);
527 Mono_Posix_Syscall_llistxattr (const char *path
, unsigned char *list
, mph_size_t size
)
529 mph_return_if_size_t_overflow (size
);
532 return llistxattr (path
, (char*) list
, (size_t) size
);
534 return bsd_llistxattr (path
, list
, size
);
537 #endif /* !__APPLE__ */
540 Mono_Posix_Syscall_flistxattr (int fd
, unsigned char *list
, mph_size_t size
)
542 mph_return_if_size_t_overflow (size
);
546 return flistxattr (fd
, (char*) list
, (size_t) size
, 0);
547 #else /* __APPLE__ */
548 return flistxattr (fd
, (char*) list
, (size_t) size
);
549 #endif /* __APPLE__ */
551 return bsd_flistxattr (fd
, list
, size
);
556 Mono_Posix_Syscall_removexattr (const char *path
, const char *name
)
562 ret
= removexattr (path
, name
, 0);
563 #else /* __APPLE__ */
564 ret
= removexattr (path
, name
);
565 #endif /* __APPLE__ */
570 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
572 ret
= extattr_delete_file (path
, namespace, _name
);
582 Mono_Posix_Syscall_lremovexattr (const char *path
, const char *name
)
587 ret
= lremovexattr (path
, name
);
592 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
594 ret
= extattr_delete_link (path
, namespace, _name
);
601 #endif /* !__APPLE__ */
604 Mono_Posix_Syscall_fremovexattr (int fd
, const char *name
)
610 ret
= fremovexattr (fd
, name
, 0);
611 #else /* __APPLE__ */
612 ret
= fremovexattr (fd
, name
);
613 #endif /* __APPLE__ */
618 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
620 ret
= extattr_delete_fd (fd
, namespace, _name
);
630 #endif /* HAVE_SYS_XATTR_H || HAVE_ATTR_ATTR_H || HAVE_SYS_EXTATTR_H */