New test.
[mono.git] / support / sys-xattr.c
1 /*
2  * Wrapper functions for <sys/xattr.h> (or <attr/xattr.h>) and <sys/extattr.h>
3  *
4  * Authors:
5  *   Daniel Drake (dsd@gentoo.org)
6  *
7  * Copyright (C) 2005 Daniel Drake
8  */
9
10 #include <config.h>
11
12 #if defined(HAVE_SYS_XATTR_H) || defined(HAVE_ATTR_ATTR_H) || defined(HAVE_SYS_EXTATTR_H)
13
14 #include <sys/types.h>
15
16 /*
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)
21  */
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 */
31
32 #ifdef HAVE_SYS_EXTATTR_H
33 #include <sys/extattr.h>
34 #include <sys/uio.h>
35 #define EA_BSD
36 #endif
37
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 #include "map.h"
45 #include "mph.h"
46
47 /*
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)
53  *
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.
59  *
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.
63  *
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')
76  *
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).
84  * 
85  * For usage and behaviour information, see the monodoc documentation on the
86  * Mono.Unix.Syscall class.
87  */
88
89 G_BEGIN_DECLS
90
91 //
92 // HELPER FUNCTIONS
93 //
94
95 #ifdef EA_BSD
96
97 struct BsdNamespaceInfo {
98         const char *name;
99         int value;
100 };
101
102 static struct BsdNamespaceInfo bsd_extattr_namespaces[] = {
103         {"user"         , EXTATTR_NAMESPACE_USER},
104         {"system"       , EXTATTR_NAMESPACE_SYSTEM}
105 };
106
107 static int bsd_check_flags (gint32 flags)
108 {
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;
114         }
115         return 0;
116 }
117
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)
122 {
123         int i;
124         gchar **components = g_strsplit (name, ".", 2);
125
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;
131                 }
132
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;
139         }
140
141         *_name = g_strdup (components[1]);
142         g_strfreev (components);
143         return 0;
144 }
145
146 static void
147 init_attrlists (char *attrlists[])
148 {
149         memset (attrlists, 0, G_N_ELEMENTS(bsd_extattr_namespaces) * sizeof(char*));
150 }
151
152 static void
153 free_attrlists (char *attrlists[])
154 {
155         int i;
156         for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
157                 g_free (attrlists[i]);
158 }
159
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)
166 {
167         size_t i = 0;
168         unsigned int num_attrs = 0;
169
170         if (!attrs || !size)
171                 return 0;
172
173         while (i < size) {
174                 num_attrs++;
175                 i += attrs[i] + 1;
176         }
177
178         return num_attrs;
179 }
180
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)
185 {
186         size_t i = 0;
187         if (src == NULL || dest == NULL || size == 0)
188                 return NULL;
189
190         while (i < size) {
191                 // Read length
192                 int attr_len = (int) src[i];
193                 int prefix_len = strlen (nsprefix);
194
195                 // Add namespace prefix
196                 strncpy (dest, nsprefix, prefix_len);
197                 dest[prefix_len] = '.';
198                 dest += prefix_len + 1;
199
200                 // Copy attribute
201                 memcpy(dest, src + ++i, attr_len);
202
203                 // NULL-terminate
204                 i += attr_len;
205                 dest[attr_len] = '\0';
206                 dest += attr_len + 1;
207         }
208
209         return dest;
210 }
211
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)
216 {
217         int i;
218         if (!dest)
219                 return dest_size_needed;
220
221         if (dest_size < dest_size_needed) {
222                 errno = ERANGE;
223                 return -1;
224         }
225
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);
229
230         return dest_size_needed;
231 }
232
233 static mph_ssize_t
234 bsd_listxattr (const char *path, void *list, mph_size_t size)
235 {
236         size_t full_size = 0;
237         int i;
238         char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
239
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;
244
245                 buf_size = (size_t) extattr_list_file (path, i + 1, NULL, 0);
246                 if (buf_size == -1)
247                         continue;
248
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;
253
254                 num_attrs = count_num_attrs(attrlists[i], buf_size);
255                 full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
256         }
257
258         full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
259         free_attrlists (attrlists);
260         return full_size;
261 }
262
263 static mph_ssize_t
264 bsd_llistxattr (const char *path, void *list, mph_size_t size)
265 {
266         size_t full_size = 0;
267         int i;
268         char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
269
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;
274
275                 buf_size = (size_t) extattr_list_link (path, i + 1, NULL, 0);
276                 if (buf_size == -1)
277                         continue;
278
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;
283
284                 num_attrs = count_num_attrs(attrlists[i], buf_size);
285                 full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
286         }
287
288         full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
289         free_attrlists (attrlists);
290         return full_size;
291 }
292
293 static mph_ssize_t
294 bsd_flistxattr (int fd, void *list, mph_size_t size)
295 {
296         size_t full_size = 0;
297         int i;
298         char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
299
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;
304
305                 buf_size = (size_t) extattr_list_fd (fd, i + 1, NULL, 0);
306                 if (buf_size == -1)
307                         continue;
308
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;
313
314                 num_attrs = count_num_attrs(attrlists[i], buf_size);
315                 full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
316         }
317
318         full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
319         free_attrlists (attrlists);
320         return full_size;
321 }
322
323 #endif /* EA_BSD */
324
325 //
326 // THE PROVIDED API
327 //
328
329 gint32
330 Mono_Posix_Syscall_setxattr (const char *path, const char *name, void *value, mph_size_t size, gint32 flags)
331 {
332         gint32 ret;
333         mph_return_if_size_t_overflow (size);
334
335 #ifdef EA_UNIX
336         {
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__ */
345         }
346 #else /* EA_UNIX */
347         {
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);
356         }
357 #endif /* EA_UNIX */
358
359         return ret;
360 }
361
362 #if !__APPLE__
363 gint32
364 Mono_Posix_Syscall_lsetxattr (const char *path, const char *name, void *value, mph_size_t size, gint32 flags)
365 {
366         gint32 ret;
367         mph_return_if_size_t_overflow (size);
368
369 #ifdef EA_UNIX
370         {
371                 int _flags;
372                 if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
373                         return -1;
374                 ret = lsetxattr (path, name, value, size, _flags);
375         }
376 #else /* EA_UNIX */
377         {
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);
386         }
387 #endif /* EA_UNIX */
388
389         return ret;
390 }
391 #endif /* !__APPLE__ */
392
393 gint32
394 Mono_Posix_Syscall_fsetxattr (int fd, const char *name, void *value, mph_size_t size, gint32 flags)
395 {
396         gint32 ret;
397         mph_return_if_size_t_overflow (size);
398
399 #ifdef EA_UNIX
400         {
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__ */
409         }
410 #else /* EA_UNIX */
411         {
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);
420         }
421 #endif /* EA_UNIX */
422
423         return ret;
424 }
425
426 mph_ssize_t
427 Mono_Posix_Syscall_getxattr (const char *path, const char *name, void *value, mph_size_t size)
428 {
429         mph_ssize_t ret;
430         mph_return_if_size_t_overflow (size);
431
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 */
439         {
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);
446         }
447 #endif /* EA_UNIX */
448
449         return ret;
450 }
451
452 #if !__APPLE__
453 mph_ssize_t
454 Mono_Posix_Syscall_lgetxattr (const char *path, const char *name, void *value, mph_size_t size)
455 {
456         mph_ssize_t ret;
457         mph_return_if_size_t_overflow (size);
458
459 #ifdef EA_UNIX
460         ret = lgetxattr (path, name, value, (size_t) size);
461 #else /* EA_UNIX */
462         {
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);
469         }
470 #endif /* EA_UNIX */
471
472         return ret;
473 }
474 #endif /* !__APPLE__ */
475
476 mph_ssize_t
477 Mono_Posix_Syscall_fgetxattr (int fd, const char *name, void *value, mph_size_t size)
478 {
479         mph_ssize_t ret;
480         mph_return_if_size_t_overflow (size);
481
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 */
489         {
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);
496         }
497 #endif /* EA_UNIX */
498
499         return ret;
500 }
501
502 mph_ssize_t
503 Mono_Posix_Syscall_listxattr (const char *path, void *list, mph_size_t size)
504 {
505         mph_return_if_size_t_overflow (size);
506
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 */
516 }
517
518 #if !__APPLE__
519 mph_ssize_t
520 Mono_Posix_Syscall_llistxattr (const char *path, void *list, mph_size_t size)
521 {
522         mph_return_if_size_t_overflow (size);
523
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 */
529 }
530 #endif /* !__APPLE__ */
531
532 mph_ssize_t
533 Mono_Posix_Syscall_flistxattr (int fd, void *list, mph_size_t size)
534 {
535         mph_return_if_size_t_overflow (size);
536
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 */
546 }
547
548 gint32
549 Mono_Posix_Syscall_removexattr (const char *path, const char *name)
550 {
551         gint32 ret;
552
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 */
560         {
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);
567         }
568 #endif /* EA_UNIX */
569
570         return ret;
571 }
572
573 #if !__APPLE__
574 gint32
575 Mono_Posix_Syscall_lremovexattr (const char *path, const char *name)
576 {
577         gint32 ret;
578
579 #ifdef EA_UNIX
580         ret = lremovexattr (path, name);
581 #else /* EA_UNIX */
582         {
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);
589         }
590 #endif /* EA_UNIX */
591
592         return ret;
593 }
594 #endif /* !__APPLE__ */
595
596 gint32
597 Mono_Posix_Syscall_fremovexattr (int fd, const char *name)
598 {
599         gint32 ret;
600
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 */
608         {
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);
615         }
616 #endif /* EA_UNIX */
617
618         return ret;
619 }
620
621 G_END_DECLS
622
623 #endif /* HAVE_SYS_XATTR_H || HAVE_ATTR_ATTR_H || HAVE_SYS_EXTATTR_H */
624
625 /*
626  * vim: noexpandtab
627  */