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 defined(HAVE_SYS_XATTR_H) || defined(HAVE_ATTR_ATTR_H) || defined(HAVE_SYS_EXTATTR_H)
16 #include <sys/types.h>
19 * Where available, we prefer to use the libc implementation of the xattr
20 * syscalls. However, we also support using libattr for this on systems where
21 * libc does not provide this (e.g. glibc-2.2 and older)
22 * (configure-time magic is used to select which library to link to)
24 #ifdef HAVE_SYS_XATTR_H
26 #include <sys/xattr.h>
28 #elif HAVE_ATTR_ATTR_H
30 #include <attr/xattr.h>
32 #endif /* HAVE_SYS_XATTR_H */
34 #ifdef HAVE_SYS_EXTATTR_H
35 #include <sys/extattr.h>
50 * Linux provides extended attributes through the <sys/xattr.h> API.
51 * Any file or link can have attributes assigned to it (provided that they are
52 * supported by the backing filesystem). Each attribute has to be placed in a
53 * namespace, of which "user" is the most common. Namespaces are specified as
54 * a prefix to the attribute name, proceeded by a '.' (e.g. user.myattribute)
56 * FreeBSD provides extended attributes through the <sys/extattr.h> API.
57 * Behaviour is very similar to Linux EA's, but the namespace is specified
58 * through an enum-style parameter rather than as a prefix to an attribute
59 * name. There are also differences in the behaviour of the "list attributes"
62 * This file merges the two implementations into a single API for use by the
63 * Mono.Unix.Syscall.*xattr methods. No matter which OS you are on, things
64 * should "just work" the same as anywhere else.
66 * The API provided here leans more towards the Linux implementation. Attribute
67 * namespaces are provided as prefixes to the attribute name (followed by '.').
68 * There is no limit to the namespaces accepted by the Linux side of this
69 * implementation, but you are obviously limited to the ones available to you
71 * FreeBSD namespaces have to be converted from the textual prefix into their
72 * relevant number so that they can be used in the FreeBSD system calls.
73 * This means that the only namespaces available are the ones known by in this
74 * file (see bsd_extattr_namespaces). However, you can also specify the
75 * numericalnamespace index yourself, by using an attribute name such as
77 * (this will obviously fail on Linux, your code will no longer be 'portable')
79 * Linux {,l,f}setxattr calls have a flags parameter which allow you to control
80 * what should happen if an attribute with the same name does (or doesn't)
81 * already exist. The 'flags' parameter is available here, but because FreeBSD
82 * does not support this kind of refinement, it will fail on FreeBSD if you
83 * specify anything other than XATTR_AUTO (XATTR_AUTO will create the attribute
84 * if it doesn't already exist, and overwrite the existing attribute if it
87 * For usage and behaviour information, see the monodoc documentation on the
88 * Mono.Unix.Syscall class.
99 struct BsdNamespaceInfo
{
104 static struct BsdNamespaceInfo bsd_extattr_namespaces
[] = {
105 {"user" , EXTATTR_NAMESPACE_USER
},
106 {"system" , EXTATTR_NAMESPACE_SYSTEM
}
109 static int bsd_check_flags (gint32 flags
)
111 // BSD doesn't support flags, but always provides the same behaviour as
112 // XATTR_AUTO. So we enforce that here.
113 if (flags
!= Mono_Posix_XattrFlags_XATTR_AUTO
) {
120 // On FreeBSD, we need to convert "user.blah" into namespace 1 and attribute
121 // name "blah", or maybe "6.blah" into namespace 6 attribute "blah"
123 bsd_handle_nsprefix (const char *name
, char **_name
, int *namespace)
126 gchar
**components
= g_strsplit (name
, ".", 2);
128 // Find namespace number from textual representation
129 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++)
130 if (strcmp (bsd_extattr_namespaces
[i
].name
, components
[0]) == 0) {
131 *namespace = bsd_extattr_namespaces
[i
].value
;
135 if (*namespace == 0) {
136 // Perhaps they specified the namespace number themselves..?
138 *namespace = (int) strtol (components
[0], &endptr
, 10);
143 *_name
= g_strdup (components
[1]);
144 g_strfreev (components
);
149 init_attrlists (char *attrlists
[])
151 memset (attrlists
, 0, G_N_ELEMENTS(bsd_extattr_namespaces
) * sizeof(char*));
155 free_attrlists (char *attrlists
[])
158 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++)
159 g_free (attrlists
[i
]);
162 // Counts the number of attributes in the result of a
163 // extattr_list_*() call. Note that the format of the data
164 // is: \3one\3two\6eleven where the leading charaters represent the length
165 // of the following attribute. (the description in the man-page is wrong)
167 count_num_attrs (char *attrs
, size_t size
)
170 unsigned int num_attrs
= 0;
183 // Convert a BSD-style list buffer (see the description for count_num_attrs)
184 // into a Linux-style NULL-terminated list including namespace prefix.
186 *bsd_convert_list (const char *nsprefix
, const char *src
, size_t size
, char *dest
)
189 if (src
== NULL
|| dest
== NULL
|| size
== 0)
194 int attr_len
= (int) src
[i
];
195 int prefix_len
= strlen (nsprefix
);
197 // Add namespace prefix
198 strncpy (dest
, nsprefix
, prefix_len
);
199 dest
[prefix_len
] = '.';
200 dest
+= prefix_len
+ 1;
203 memcpy(dest
, src
+ ++i
, attr_len
);
207 dest
[attr_len
] = '\0';
208 dest
+= attr_len
+ 1;
214 // Combine all the lists of attributes that we know about into a single
215 // Linux-style buffer
217 bsd_combine_lists (char *attrlists
[], char *dest
, size_t dest_size_needed
, size_t dest_size
)
221 return dest_size_needed
;
223 if (dest_size
< dest_size_needed
) {
228 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++)
230 dest
= bsd_convert_list (bsd_extattr_namespaces
[i
].name
, attrlists
[i
], strlen (attrlists
[i
]), dest
);
232 return dest_size_needed
;
236 bsd_listxattr (const char *path
, void *list
, mph_size_t size
)
238 size_t full_size
= 0;
240 char *attrlists
[G_N_ELEMENTS(bsd_extattr_namespaces
)];
242 init_attrlists (attrlists
);
243 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++) {
247 buf_size
= (size_t) extattr_list_file (path
, i
+ 1, NULL
, 0);
251 attrlists
[i
] = g_malloc0 (buf_size
+ 1);
252 buf_size
= (size_t) extattr_list_file (path
, i
+ 1, attrlists
[i
], buf_size
);
256 num_attrs
= count_num_attrs(attrlists
[i
], buf_size
);
257 full_size
+= buf_size
+ (num_attrs
* (strlen (bsd_extattr_namespaces
[i
].name
) + 1));
260 full_size
= bsd_combine_lists (attrlists
, (char *) list
, full_size
, size
);
261 free_attrlists (attrlists
);
266 bsd_llistxattr (const char *path
, void *list
, mph_size_t size
)
268 size_t full_size
= 0;
270 char *attrlists
[G_N_ELEMENTS(bsd_extattr_namespaces
)];
272 init_attrlists (attrlists
);
273 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++) {
277 buf_size
= (size_t) extattr_list_link (path
, i
+ 1, NULL
, 0);
281 attrlists
[i
] = g_malloc0 (buf_size
+ 1);
282 buf_size
= (size_t) extattr_list_link (path
, i
+ 1, attrlists
[i
], buf_size
);
286 num_attrs
= count_num_attrs(attrlists
[i
], buf_size
);
287 full_size
+= buf_size
+ (num_attrs
* (strlen (bsd_extattr_namespaces
[i
].name
) + 1));
290 full_size
= bsd_combine_lists (attrlists
, (char *) list
, full_size
, size
);
291 free_attrlists (attrlists
);
296 bsd_flistxattr (int fd
, void *list
, mph_size_t size
)
298 size_t full_size
= 0;
300 char *attrlists
[G_N_ELEMENTS(bsd_extattr_namespaces
)];
302 init_attrlists (attrlists
);
303 for (i
= 0; i
< G_N_ELEMENTS(bsd_extattr_namespaces
); i
++) {
307 buf_size
= (size_t) extattr_list_fd (fd
, i
+ 1, NULL
, 0);
311 attrlists
[i
] = g_malloc0 (buf_size
+ 1);
312 buf_size
= (size_t) extattr_list_fd (fd
, i
+ 1, attrlists
[i
], buf_size
);
316 num_attrs
= count_num_attrs(attrlists
[i
], buf_size
);
317 full_size
+= buf_size
+ (num_attrs
* (strlen (bsd_extattr_namespaces
[i
].name
) + 1));
320 full_size
= bsd_combine_lists (attrlists
, (char *) list
, full_size
, size
);
321 free_attrlists (attrlists
);
332 Mono_Posix_Syscall_setxattr (const char *path
, const char *name
, unsigned char *value
, mph_size_t size
, gint32 flags
)
335 mph_return_if_size_t_overflow (size
);
340 if (Mono_Posix_FromXattrFlags (flags
, &_flags
) == -1)
343 ret
= setxattr (path
, name
, value
, (size_t) size
, 0, _flags
);
344 #else /* __APPLE__ */
345 ret
= setxattr (path
, name
, value
, (size_t) size
, _flags
);
346 #endif /* __APPLE__ */
352 if (bsd_check_flags (flags
) == -1)
354 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
356 ret
= extattr_set_file (path
, namespace, _name
, value
, (size_t) size
);
366 Mono_Posix_Syscall_lsetxattr (const char *path
, const char *name
, unsigned char *value
, mph_size_t size
, gint32 flags
)
369 mph_return_if_size_t_overflow (size
);
374 if (Mono_Posix_FromXattrFlags (flags
, &_flags
) == -1)
376 ret
= lsetxattr (path
, name
, value
, size
, _flags
);
382 if (bsd_check_flags (flags
) == -1)
384 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
386 ret
= extattr_set_link (path
, namespace, _name
, value
, (size_t) size
);
393 #endif /* !__APPLE__ */
396 Mono_Posix_Syscall_fsetxattr (int fd
, const char *name
, unsigned char *value
, mph_size_t size
, gint32 flags
)
399 mph_return_if_size_t_overflow (size
);
404 if (Mono_Posix_FromXattrFlags (flags
, &_flags
) == -1)
407 ret
= fsetxattr (fd
, name
, value
, (size_t) size
, 0, _flags
);
408 #else /* __APPLE__ */
409 ret
= fsetxattr (fd
, name
, value
, (size_t) size
, _flags
);
410 #endif /* __APPLE__ */
416 if (bsd_check_flags (flags
) == -1)
418 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
420 ret
= extattr_set_fd (fd
, namespace, _name
, value
, (size_t) size
);
429 Mono_Posix_Syscall_getxattr (const char *path
, const char *name
, unsigned char *value
, mph_size_t size
)
432 mph_return_if_size_t_overflow (size
);
436 ret
= getxattr (path
, name
, value
, (size_t) size
, 0, 0);
437 #else /* __APPLE__ */
438 ret
= getxattr (path
, name
, value
, (size_t) size
);
439 #endif /* __APPLE__ */
444 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
446 ret
= extattr_get_file (path
, namespace, _name
, value
, (size_t) size
);
456 Mono_Posix_Syscall_lgetxattr (const char *path
, const char *name
, unsigned char *value
, mph_size_t size
)
459 mph_return_if_size_t_overflow (size
);
462 ret
= lgetxattr (path
, name
, value
, (size_t) size
);
467 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
469 ret
= extattr_get_link (path
, namespace, _name
, value
, (size_t) size
);
476 #endif /* !__APPLE__ */
479 Mono_Posix_Syscall_fgetxattr (int fd
, const char *name
, unsigned char *value
, mph_size_t size
)
482 mph_return_if_size_t_overflow (size
);
486 ret
= fgetxattr (fd
, name
, value
, (size_t) size
, 0, 0);
487 #else /* __APPLE__ */
488 ret
= fgetxattr (fd
, name
, value
, (size_t) size
);
489 #endif /* __APPLE__ */
494 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
496 ret
= extattr_get_fd (fd
, namespace, _name
, value
, (size_t) size
);
505 Mono_Posix_Syscall_listxattr (const char *path
, unsigned char *list
, mph_size_t size
)
507 mph_return_if_size_t_overflow (size
);
511 return listxattr (path
, (char*) list
, (size_t) size
, 0);
512 #else /* __APPLE__ */
513 return listxattr (path
, (char*) list
, (size_t) size
);
514 #endif /* __APPLE__ */
516 return bsd_listxattr (path
, list
, size
);
522 Mono_Posix_Syscall_llistxattr (const char *path
, unsigned char *list
, mph_size_t size
)
524 mph_return_if_size_t_overflow (size
);
527 return llistxattr (path
, (char*) list
, (size_t) size
);
529 return bsd_llistxattr (path
, list
, size
);
532 #endif /* !__APPLE__ */
535 Mono_Posix_Syscall_flistxattr (int fd
, unsigned char *list
, mph_size_t size
)
537 mph_return_if_size_t_overflow (size
);
541 return flistxattr (fd
, (char*) list
, (size_t) size
, 0);
542 #else /* __APPLE__ */
543 return flistxattr (fd
, (char*) list
, (size_t) size
);
544 #endif /* __APPLE__ */
546 return bsd_flistxattr (fd
, list
, size
);
551 Mono_Posix_Syscall_removexattr (const char *path
, const char *name
)
557 ret
= removexattr (path
, name
, 0);
558 #else /* __APPLE__ */
559 ret
= removexattr (path
, name
);
560 #endif /* __APPLE__ */
565 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
567 ret
= extattr_delete_file (path
, namespace, _name
);
577 Mono_Posix_Syscall_lremovexattr (const char *path
, const char *name
)
582 ret
= lremovexattr (path
, name
);
587 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
589 ret
= extattr_delete_link (path
, namespace, _name
);
596 #endif /* !__APPLE__ */
599 Mono_Posix_Syscall_fremovexattr (int fd
, const char *name
)
605 ret
= fremovexattr (fd
, name
, 0);
606 #else /* __APPLE__ */
607 ret
= fremovexattr (fd
, name
);
608 #endif /* __APPLE__ */
613 if (bsd_handle_nsprefix (name
, &_name
, &namespace) == -1)
615 ret
= extattr_delete_fd (fd
, namespace, _name
);
625 #endif /* HAVE_SYS_XATTR_H || HAVE_ATTR_ATTR_H || HAVE_SYS_EXTATTR_H */