2006-09-13 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono/afaerber.git] / support / sys-xattr.c
blob7e4cce239c1c11fcc667d4a429f4db7d6306d819
1 /*
2 * Wrapper functions for <sys/xattr.h> (or <attr/xattr.h>) and <sys/extattr.h>
4 * Authors:
5 * Daniel Drake (dsd@gentoo.org)
7 * Copyright (C) 2005 Daniel Drake
8 */
10 #include <config.h>
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
23 // libc
24 #include <sys/xattr.h>
25 #define EA_UNIX
26 #elif HAVE_ATTR_ATTR_H
27 // libattr
28 #include <attr/xattr.h>
29 #define EA_UNIX
30 #endif /* HAVE_SYS_XATTR_H */
32 #ifdef HAVE_SYS_EXTATTR_H
33 #include <sys/extattr.h>
34 #include <sys/uio.h>
35 #define EA_BSD
36 #endif
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <stdlib.h>
44 #include "map.h"
45 #include "mph.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"
58 * system calls.
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
68 * on the system.
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
74 * "5.myattr".
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
83 * already set).
85 * For usage and behaviour information, see the monodoc documentation on the
86 * Mono.Unix.Syscall class.
89 G_BEGIN_DECLS
92 // HELPER FUNCTIONS
95 #ifdef EA_BSD
97 struct BsdNamespaceInfo {
98 const char *name;
99 int value;
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) {
112 errno = EINVAL;
113 return -1;
115 return 0;
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"
120 static int
121 bsd_handle_nsprefix (const char *name, char **_name, int *namespace)
123 int i;
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;
130 break;
133 if (*namespace == 0) {
134 // Perhaps they specified the namespace number themselves..?
135 char *endptr;
136 *namespace = (int) strtol (components[0], &endptr, 10);
137 if (*endptr != '\0')
138 return -1;
141 *_name = g_strdup (components[1]);
142 g_strfreev (components);
143 return 0;
146 static void
147 init_attrlists (char *attrlists[])
149 memset (attrlists, 0, G_N_ELEMENTS(bsd_extattr_namespaces) * sizeof(char*));
152 static void
153 free_attrlists (char *attrlists[])
155 int i;
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)
164 static unsigned int
165 count_num_attrs (char *attrs, size_t size)
167 size_t i = 0;
168 unsigned int num_attrs = 0;
170 if (!attrs || !size)
171 return 0;
173 while (i < size) {
174 num_attrs++;
175 i += attrs[i] + 1;
178 return num_attrs;
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.
183 static char
184 *bsd_convert_list (const char *nsprefix, const char *src, size_t size, char *dest)
186 size_t i = 0;
187 if (src == NULL || dest == NULL || size == 0)
188 return NULL;
190 while (i < size) {
191 // Read length
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;
200 // Copy attribute
201 memcpy(dest, src + ++i, attr_len);
203 // NULL-terminate
204 i += attr_len;
205 dest[attr_len] = '\0';
206 dest += attr_len + 1;
209 return dest;
212 // Combine all the lists of attributes that we know about into a single
213 // Linux-style buffer
214 static ssize_t
215 bsd_combine_lists (char *attrlists[], char *dest, size_t dest_size_needed, size_t dest_size)
217 int i;
218 if (!dest)
219 return dest_size_needed;
221 if (dest_size < dest_size_needed) {
222 errno = ERANGE;
223 return -1;
226 for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
227 if (attrlists[i])
228 dest = bsd_convert_list (bsd_extattr_namespaces[i].name, attrlists[i], strlen (attrlists[i]), dest);
230 return dest_size_needed;
233 static mph_ssize_t
234 bsd_listxattr (const char *path, void *list, mph_size_t size)
236 size_t full_size = 0;
237 int i;
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++) {
242 size_t buf_size;
243 int num_attrs;
245 buf_size = (size_t) extattr_list_file (path, i + 1, NULL, 0);
246 if (buf_size == -1)
247 continue;
249 attrlists[i] = g_malloc0 (buf_size + 1);
250 buf_size = (size_t) extattr_list_file (path, i + 1, attrlists[i], buf_size);
251 if (buf_size == -1)
252 continue;
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);
260 return full_size;
263 static mph_ssize_t
264 bsd_llistxattr (const char *path, void *list, mph_size_t size)
266 size_t full_size = 0;
267 int i;
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++) {
272 size_t buf_size;
273 int num_attrs;
275 buf_size = (size_t) extattr_list_link (path, i + 1, NULL, 0);
276 if (buf_size == -1)
277 continue;
279 attrlists[i] = g_malloc0 (buf_size + 1);
280 buf_size = (size_t) extattr_list_link (path, i + 1, attrlists[i], buf_size);
281 if (buf_size == -1)
282 continue;
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);
290 return full_size;
293 static mph_ssize_t
294 bsd_flistxattr (int fd, void *list, mph_size_t size)
296 size_t full_size = 0;
297 int i;
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++) {
302 size_t buf_size;
303 int num_attrs;
305 buf_size = (size_t) extattr_list_fd (fd, i + 1, NULL, 0);
306 if (buf_size == -1)
307 continue;
309 attrlists[i] = g_malloc0 (buf_size + 1);
310 buf_size = (size_t) extattr_list_fd (fd, i + 1, attrlists[i], buf_size);
311 if (buf_size == -1)
312 continue;
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);
320 return full_size;
323 #endif /* EA_BSD */
326 // THE PROVIDED API
329 gint32
330 Mono_Posix_Syscall_setxattr (const char *path, const char *name, void *value, mph_size_t size, gint32 flags)
332 gint32 ret;
333 mph_return_if_size_t_overflow (size);
335 #ifdef EA_UNIX
337 int _flags;
338 if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
339 return -1;
340 #if __APPLE__
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__ */
346 #else /* EA_UNIX */
348 char *_name;
349 int namespace;
350 if (bsd_check_flags (flags) == -1)
351 return -1;
352 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
353 return -1;
354 ret = extattr_set_file (path, namespace, _name, value, (size_t) size);
355 g_free (_name);
357 #endif /* EA_UNIX */
359 return ret;
362 #if !__APPLE__
363 gint32
364 Mono_Posix_Syscall_lsetxattr (const char *path, const char *name, void *value, mph_size_t size, gint32 flags)
366 gint32 ret;
367 mph_return_if_size_t_overflow (size);
369 #ifdef EA_UNIX
371 int _flags;
372 if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
373 return -1;
374 ret = lsetxattr (path, name, value, size, _flags);
376 #else /* EA_UNIX */
378 char *_name;
379 int namespace;
380 if (bsd_check_flags (flags) == -1)
381 return -1;
382 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
383 return -1;
384 ret = extattr_set_link (path, namespace, _name, value, (size_t) size);
385 g_free (_name);
387 #endif /* EA_UNIX */
389 return ret;
391 #endif /* !__APPLE__ */
393 gint32
394 Mono_Posix_Syscall_fsetxattr (int fd, const char *name, void *value, mph_size_t size, gint32 flags)
396 gint32 ret;
397 mph_return_if_size_t_overflow (size);
399 #ifdef EA_UNIX
401 int _flags;
402 if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
403 return -1;
404 #if __APPLE__
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__ */
410 #else /* EA_UNIX */
412 char *_name;
413 int namespace;
414 if (bsd_check_flags (flags) == -1)
415 return -1;
416 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
417 return -1;
418 ret = extattr_set_fd (fd, namespace, _name, value, (size_t) size);
419 g_free (_name);
421 #endif /* EA_UNIX */
423 return ret;
426 mph_ssize_t
427 Mono_Posix_Syscall_getxattr (const char *path, const char *name, void *value, mph_size_t size)
429 mph_ssize_t ret;
430 mph_return_if_size_t_overflow (size);
432 #ifdef EA_UNIX
433 #if __APPLE__
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__ */
438 #else /* EA_UNIX */
440 char *_name;
441 int namespace;
442 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
443 return -1;
444 ret = extattr_get_file (path, namespace, _name, value, (size_t) size);
445 g_free (_name);
447 #endif /* EA_UNIX */
449 return ret;
452 #if !__APPLE__
453 mph_ssize_t
454 Mono_Posix_Syscall_lgetxattr (const char *path, const char *name, void *value, mph_size_t size)
456 mph_ssize_t ret;
457 mph_return_if_size_t_overflow (size);
459 #ifdef EA_UNIX
460 ret = lgetxattr (path, name, value, (size_t) size);
461 #else /* EA_UNIX */
463 char *_name;
464 int namespace;
465 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
466 return -1;
467 ret = extattr_get_link (path, namespace, _name, value, (size_t) size);
468 g_free (_name);
470 #endif /* EA_UNIX */
472 return ret;
474 #endif /* !__APPLE__ */
476 mph_ssize_t
477 Mono_Posix_Syscall_fgetxattr (int fd, const char *name, void *value, mph_size_t size)
479 mph_ssize_t ret;
480 mph_return_if_size_t_overflow (size);
482 #ifdef EA_UNIX
483 #if __APPLE__
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__ */
488 #else /* EA_UNIX */
490 char *_name;
491 int namespace;
492 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
493 return -1;
494 ret = extattr_get_fd (fd, namespace, _name, value, (size_t) size);
495 g_free (_name);
497 #endif /* EA_UNIX */
499 return ret;
502 mph_ssize_t
503 Mono_Posix_Syscall_listxattr (const char *path, void *list, mph_size_t size)
505 mph_return_if_size_t_overflow (size);
507 #ifdef EA_UNIX
508 #if __APPLE__
509 return listxattr (path, list, (size_t) size, 0);
510 #else /* __APPLE__ */
511 return listxattr (path, list, (size_t) size);
512 #endif /* __APPLE__ */
513 #else /* EA_UNIX */
514 return bsd_listxattr (path, list, size);
515 #endif /* EA_UNIX */
518 #if !__APPLE__
519 mph_ssize_t
520 Mono_Posix_Syscall_llistxattr (const char *path, void *list, mph_size_t size)
522 mph_return_if_size_t_overflow (size);
524 #ifdef EA_UNIX
525 return llistxattr (path, list, (size_t) size);
526 #else /* EA_UNIX */
527 return bsd_llistxattr (path, list, size);
528 #endif /* EA_UNIX */
530 #endif /* !__APPLE__ */
532 mph_ssize_t
533 Mono_Posix_Syscall_flistxattr (int fd, void *list, mph_size_t size)
535 mph_return_if_size_t_overflow (size);
537 #ifdef EA_UNIX
538 #if __APPLE__
539 return flistxattr (fd, list, (size_t) size, 0);
540 #else /* __APPLE__ */
541 return flistxattr (fd, list, (size_t) size);
542 #endif /* __APPLE__ */
543 #else /* EA_UNIX */
544 return bsd_flistxattr (fd, list, size);
545 #endif /* EA_UNIX */
548 gint32
549 Mono_Posix_Syscall_removexattr (const char *path, const char *name)
551 gint32 ret;
553 #ifdef EA_UNIX
554 #if __APPLE__
555 ret = removexattr (path, name, 0);
556 #else /* __APPLE__ */
557 ret = removexattr (path, name);
558 #endif /* __APPLE__ */
559 #else /* EA_UNIX */
561 char *_name;
562 int namespace;
563 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
564 return -1;
565 ret = extattr_delete_file (path, namespace, _name);
566 g_free (_name);
568 #endif /* EA_UNIX */
570 return ret;
573 #if !__APPLE__
574 gint32
575 Mono_Posix_Syscall_lremovexattr (const char *path, const char *name)
577 gint32 ret;
579 #ifdef EA_UNIX
580 ret = lremovexattr (path, name);
581 #else /* EA_UNIX */
583 char *_name;
584 int namespace;
585 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
586 return -1;
587 ret = extattr_delete_link (path, namespace, _name);
588 g_free (_name);
590 #endif /* EA_UNIX */
592 return ret;
594 #endif /* !__APPLE__ */
596 gint32
597 Mono_Posix_Syscall_fremovexattr (int fd, const char *name)
599 gint32 ret;
601 #ifdef EA_UNIX
602 #if __APPLE__
603 ret = fremovexattr (fd, name, 0);
604 #else /* __APPLE__ */
605 ret = fremovexattr (fd, name);
606 #endif /* __APPLE__ */
607 #else /* EA_UNIX */
609 char *_name;
610 int namespace;
611 if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
612 return -1;
613 ret = extattr_delete_fd (fd, namespace, _name);
614 g_free (_name);
616 #endif /* EA_UNIX */
618 return ret;
621 G_END_DECLS
623 #endif /* HAVE_SYS_XATTR_H || HAVE_ATTR_ATTR_H || HAVE_SYS_EXTATTR_H */
626 * vim: noexpandtab