sys-xattr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Wrapper functions for <sys/xattr.h> (or <attr/xattr.h>) and <sys/extattr.h>
  3. *
  4. * Authors:
  5. * Daniel Drake ([email protected])
  6. * Jonathan Pryor ([email protected])
  7. *
  8. * Copyright (C) 2005 Daniel Drake
  9. * Copyright (C) 2006 Jonathan Pryor
  10. */
  11. #include <config.h>
  12. #if defined(HAVE_SYS_XATTR_H) || defined(HAVE_ATTR_ATTR_H) || defined(HAVE_SYS_EXTATTR_H)
  13. #include <sys/types.h>
  14. /*
  15. * Where available, we prefer to use the libc implementation of the xattr
  16. * syscalls. However, we also support using libattr for this on systems where
  17. * libc does not provide this (e.g. glibc-2.2 and older)
  18. * (configure-time magic is used to select which library to link to)
  19. */
  20. #ifdef HAVE_SYS_XATTR_H
  21. // libc
  22. #include <sys/xattr.h>
  23. #define EA_UNIX
  24. #elif HAVE_ATTR_ATTR_H
  25. // libattr
  26. #include <attr/xattr.h>
  27. #define EA_UNIX
  28. #endif /* HAVE_SYS_XATTR_H */
  29. #ifdef HAVE_SYS_EXTATTR_H
  30. #include <sys/extattr.h>
  31. #include <sys/uio.h>
  32. #define EA_BSD
  33. #endif
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include "map.h"
  40. #include "mph.h"
  41. /*
  42. * Linux provides extended attributes through the <sys/xattr.h> API.
  43. * Any file or link can have attributes assigned to it (provided that they are
  44. * supported by the backing filesystem). Each attribute has to be placed in a
  45. * namespace, of which "user" is the most common. Namespaces are specified as
  46. * a prefix to the attribute name, proceeded by a '.' (e.g. user.myattribute)
  47. *
  48. * FreeBSD provides extended attributes through the <sys/extattr.h> API.
  49. * Behaviour is very similar to Linux EA's, but the namespace is specified
  50. * through an enum-style parameter rather than as a prefix to an attribute
  51. * name. There are also differences in the behaviour of the "list attributes"
  52. * system calls.
  53. *
  54. * This file merges the two implementations into a single API for use by the
  55. * Mono.Unix.Syscall.*xattr methods. No matter which OS you are on, things
  56. * should "just work" the same as anywhere else.
  57. *
  58. * The API provided here leans more towards the Linux implementation. Attribute
  59. * namespaces are provided as prefixes to the attribute name (followed by '.').
  60. * There is no limit to the namespaces accepted by the Linux side of this
  61. * implementation, but you are obviously limited to the ones available to you
  62. * on the system.
  63. * FreeBSD namespaces have to be converted from the textual prefix into their
  64. * relevant number so that they can be used in the FreeBSD system calls.
  65. * This means that the only namespaces available are the ones known by in this
  66. * file (see bsd_extattr_namespaces). However, you can also specify the
  67. * numericalnamespace index yourself, by using an attribute name such as
  68. * "5.myattr".
  69. * (this will obviously fail on Linux, your code will no longer be 'portable')
  70. *
  71. * Linux {,l,f}setxattr calls have a flags parameter which allow you to control
  72. * what should happen if an attribute with the same name does (or doesn't)
  73. * already exist. The 'flags' parameter is available here, but because FreeBSD
  74. * does not support this kind of refinement, it will fail on FreeBSD if you
  75. * specify anything other than XATTR_AUTO (XATTR_AUTO will create the attribute
  76. * if it doesn't already exist, and overwrite the existing attribute if it
  77. * already set).
  78. *
  79. * For usage and behaviour information, see the monodoc documentation on the
  80. * Mono.Unix.Syscall class.
  81. */
  82. G_BEGIN_DECLS
  83. //
  84. // HELPER FUNCTIONS
  85. //
  86. #ifdef EA_BSD
  87. struct BsdNamespaceInfo {
  88. const char *name;
  89. int value;
  90. };
  91. static struct BsdNamespaceInfo bsd_extattr_namespaces[] = {
  92. {"user" , EXTATTR_NAMESPACE_USER},
  93. {"system" , EXTATTR_NAMESPACE_SYSTEM}
  94. };
  95. static int bsd_check_flags (gint32 flags)
  96. {
  97. // BSD doesn't support flags, but always provides the same behaviour as
  98. // XATTR_AUTO. So we enforce that here.
  99. if (flags != Mono_Posix_XattrFlags_XATTR_AUTO) {
  100. errno = EINVAL;
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. // On FreeBSD, we need to convert "user.blah" into namespace 1 and attribute
  106. // name "blah", or maybe "6.blah" into namespace 6 attribute "blah"
  107. static int
  108. bsd_handle_nsprefix (const char *name, char **_name, int *namespace)
  109. {
  110. int i;
  111. gchar **components = g_strsplit (name, ".", 2);
  112. // Find namespace number from textual representation
  113. for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
  114. if (strcmp (bsd_extattr_namespaces[i].name, components[0]) == 0) {
  115. *namespace = bsd_extattr_namespaces[i].value;
  116. break;
  117. }
  118. if (*namespace == 0) {
  119. // Perhaps they specified the namespace number themselves..?
  120. char *endptr;
  121. *namespace = (int) strtol (components[0], &endptr, 10);
  122. if (*endptr != '\0')
  123. return -1;
  124. }
  125. *_name = g_strdup (components[1]);
  126. g_strfreev (components);
  127. return 0;
  128. }
  129. static void
  130. init_attrlists (char *attrlists[])
  131. {
  132. memset (attrlists, 0, G_N_ELEMENTS(bsd_extattr_namespaces) * sizeof(char*));
  133. }
  134. static void
  135. free_attrlists (char *attrlists[])
  136. {
  137. int i;
  138. for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
  139. g_free (attrlists[i]);
  140. }
  141. // Counts the number of attributes in the result of a
  142. // extattr_list_*() call. Note that the format of the data
  143. // is: \3one\3two\6eleven where the leading charaters represent the length
  144. // of the following attribute. (the description in the man-page is wrong)
  145. static unsigned int
  146. count_num_attrs (char *attrs, size_t size)
  147. {
  148. size_t i = 0;
  149. unsigned int num_attrs = 0;
  150. if (!attrs || !size)
  151. return 0;
  152. while (i < size) {
  153. num_attrs++;
  154. i += attrs[i] + 1;
  155. }
  156. return num_attrs;
  157. }
  158. // Convert a BSD-style list buffer (see the description for count_num_attrs)
  159. // into a Linux-style NULL-terminated list including namespace prefix.
  160. static char
  161. *bsd_convert_list (const char *nsprefix, const char *src, size_t size, char *dest)
  162. {
  163. size_t i = 0;
  164. if (src == NULL || dest == NULL || size == 0)
  165. return NULL;
  166. while (i < size) {
  167. // Read length
  168. int attr_len = (int) src[i];
  169. int prefix_len = strlen (nsprefix);
  170. // Add namespace prefix
  171. strncpy (dest, nsprefix, prefix_len);
  172. dest[prefix_len] = '.';
  173. dest += prefix_len + 1;
  174. // Copy attribute
  175. memcpy(dest, src + ++i, attr_len);
  176. // NULL-terminate
  177. i += attr_len;
  178. dest[attr_len] = '\0';
  179. dest += attr_len + 1;
  180. }
  181. return dest;
  182. }
  183. // Combine all the lists of attributes that we know about into a single
  184. // Linux-style buffer
  185. static ssize_t
  186. bsd_combine_lists (char *attrlists[], char *dest, size_t dest_size_needed, size_t dest_size)
  187. {
  188. int i;
  189. if (!dest)
  190. return dest_size_needed;
  191. if (dest_size < dest_size_needed) {
  192. errno = ERANGE;
  193. return -1;
  194. }
  195. for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++)
  196. if (attrlists[i])
  197. dest = bsd_convert_list (bsd_extattr_namespaces[i].name, attrlists[i], strlen (attrlists[i]), dest);
  198. return dest_size_needed;
  199. }
  200. static mph_ssize_t
  201. bsd_listxattr (const char *path, void *list, mph_size_t size)
  202. {
  203. size_t full_size = 0;
  204. int i;
  205. char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
  206. init_attrlists (attrlists);
  207. for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
  208. size_t buf_size;
  209. int num_attrs;
  210. buf_size = (size_t) extattr_list_file (path, i + 1, NULL, 0);
  211. if (buf_size == -1)
  212. continue;
  213. attrlists[i] = g_malloc0 (buf_size + 1);
  214. buf_size = (size_t) extattr_list_file (path, i + 1, attrlists[i], buf_size);
  215. if (buf_size == -1)
  216. continue;
  217. num_attrs = count_num_attrs(attrlists[i], buf_size);
  218. full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
  219. }
  220. full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
  221. free_attrlists (attrlists);
  222. return full_size;
  223. }
  224. static mph_ssize_t
  225. bsd_llistxattr (const char *path, void *list, mph_size_t size)
  226. {
  227. size_t full_size = 0;
  228. int i;
  229. char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
  230. init_attrlists (attrlists);
  231. for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
  232. size_t buf_size;
  233. int num_attrs;
  234. buf_size = (size_t) extattr_list_link (path, i + 1, NULL, 0);
  235. if (buf_size == -1)
  236. continue;
  237. attrlists[i] = g_malloc0 (buf_size + 1);
  238. buf_size = (size_t) extattr_list_link (path, i + 1, attrlists[i], buf_size);
  239. if (buf_size == -1)
  240. continue;
  241. num_attrs = count_num_attrs(attrlists[i], buf_size);
  242. full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
  243. }
  244. full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
  245. free_attrlists (attrlists);
  246. return full_size;
  247. }
  248. static mph_ssize_t
  249. bsd_flistxattr (int fd, void *list, mph_size_t size)
  250. {
  251. size_t full_size = 0;
  252. int i;
  253. char *attrlists[G_N_ELEMENTS(bsd_extattr_namespaces)];
  254. init_attrlists (attrlists);
  255. for (i = 0; i < G_N_ELEMENTS(bsd_extattr_namespaces); i++) {
  256. size_t buf_size;
  257. int num_attrs;
  258. buf_size = (size_t) extattr_list_fd (fd, i + 1, NULL, 0);
  259. if (buf_size == -1)
  260. continue;
  261. attrlists[i] = g_malloc0 (buf_size + 1);
  262. buf_size = (size_t) extattr_list_fd (fd, i + 1, attrlists[i], buf_size);
  263. if (buf_size == -1)
  264. continue;
  265. num_attrs = count_num_attrs(attrlists[i], buf_size);
  266. full_size += buf_size + (num_attrs * (strlen (bsd_extattr_namespaces[i].name) + 1));
  267. }
  268. full_size = bsd_combine_lists (attrlists, (char *) list, full_size, size);
  269. free_attrlists (attrlists);
  270. return full_size;
  271. }
  272. #endif /* EA_BSD */
  273. //
  274. // THE PROVIDED API
  275. //
  276. gint32
  277. Mono_Posix_Syscall_setxattr (const char *path, const char *name, unsigned char *value, mph_size_t size, gint32 flags)
  278. {
  279. gint32 ret;
  280. mph_return_if_size_t_overflow (size);
  281. #ifdef EA_UNIX
  282. {
  283. int _flags;
  284. if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
  285. return -1;
  286. #if __APPLE__
  287. ret = setxattr (path, name, value, (size_t) size, 0, _flags);
  288. #else /* __APPLE__ */
  289. ret = setxattr (path, name, value, (size_t) size, _flags);
  290. #endif /* __APPLE__ */
  291. }
  292. #else /* EA_UNIX */
  293. {
  294. char *_name;
  295. int namespace;
  296. if (bsd_check_flags (flags) == -1)
  297. return -1;
  298. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  299. return -1;
  300. ret = extattr_set_file (path, namespace, _name, value, (size_t) size);
  301. g_free (_name);
  302. }
  303. #endif /* EA_UNIX */
  304. return ret;
  305. }
  306. #if !__APPLE__
  307. gint32
  308. Mono_Posix_Syscall_lsetxattr (const char *path, const char *name, unsigned char *value, mph_size_t size, gint32 flags)
  309. {
  310. gint32 ret;
  311. mph_return_if_size_t_overflow (size);
  312. #ifdef EA_UNIX
  313. {
  314. int _flags;
  315. if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
  316. return -1;
  317. ret = lsetxattr (path, name, value, size, _flags);
  318. }
  319. #else /* EA_UNIX */
  320. {
  321. char *_name;
  322. int namespace;
  323. if (bsd_check_flags (flags) == -1)
  324. return -1;
  325. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  326. return -1;
  327. ret = extattr_set_link (path, namespace, _name, value, (size_t) size);
  328. g_free (_name);
  329. }
  330. #endif /* EA_UNIX */
  331. return ret;
  332. }
  333. #endif /* !__APPLE__ */
  334. gint32
  335. Mono_Posix_Syscall_fsetxattr (int fd, const char *name, unsigned char *value, mph_size_t size, gint32 flags)
  336. {
  337. gint32 ret;
  338. mph_return_if_size_t_overflow (size);
  339. #ifdef EA_UNIX
  340. {
  341. int _flags;
  342. if (Mono_Posix_FromXattrFlags (flags, &_flags) == -1)
  343. return -1;
  344. #if __APPLE__
  345. ret = fsetxattr (fd, name, value, (size_t) size, 0, _flags);
  346. #else /* __APPLE__ */
  347. ret = fsetxattr (fd, name, value, (size_t) size, _flags);
  348. #endif /* __APPLE__ */
  349. }
  350. #else /* EA_UNIX */
  351. {
  352. char *_name;
  353. int namespace;
  354. if (bsd_check_flags (flags) == -1)
  355. return -1;
  356. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  357. return -1;
  358. ret = extattr_set_fd (fd, namespace, _name, value, (size_t) size);
  359. g_free (_name);
  360. }
  361. #endif /* EA_UNIX */
  362. return ret;
  363. }
  364. mph_ssize_t
  365. Mono_Posix_Syscall_getxattr (const char *path, const char *name, unsigned char *value, mph_size_t size)
  366. {
  367. mph_ssize_t ret;
  368. mph_return_if_size_t_overflow (size);
  369. #ifdef EA_UNIX
  370. #if __APPLE__
  371. ret = getxattr (path, name, value, (size_t) size, 0, 0);
  372. #else /* __APPLE__ */
  373. ret = getxattr (path, name, value, (size_t) size);
  374. #endif /* __APPLE__ */
  375. #else /* EA_UNIX */
  376. {
  377. char *_name;
  378. int namespace;
  379. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  380. return -1;
  381. ret = extattr_get_file (path, namespace, _name, value, (size_t) size);
  382. g_free (_name);
  383. }
  384. #endif /* EA_UNIX */
  385. return ret;
  386. }
  387. #if !__APPLE__
  388. mph_ssize_t
  389. Mono_Posix_Syscall_lgetxattr (const char *path, const char *name, unsigned char *value, mph_size_t size)
  390. {
  391. mph_ssize_t ret;
  392. mph_return_if_size_t_overflow (size);
  393. #ifdef EA_UNIX
  394. ret = lgetxattr (path, name, value, (size_t) size);
  395. #else /* EA_UNIX */
  396. {
  397. char *_name;
  398. int namespace;
  399. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  400. return -1;
  401. ret = extattr_get_link (path, namespace, _name, value, (size_t) size);
  402. g_free (_name);
  403. }
  404. #endif /* EA_UNIX */
  405. return ret;
  406. }
  407. #endif /* !__APPLE__ */
  408. mph_ssize_t
  409. Mono_Posix_Syscall_fgetxattr (int fd, const char *name, unsigned char *value, mph_size_t size)
  410. {
  411. mph_ssize_t ret;
  412. mph_return_if_size_t_overflow (size);
  413. #ifdef EA_UNIX
  414. #if __APPLE__
  415. ret = fgetxattr (fd, name, value, (size_t) size, 0, 0);
  416. #else /* __APPLE__ */
  417. ret = fgetxattr (fd, name, value, (size_t) size);
  418. #endif /* __APPLE__ */
  419. #else /* EA_UNIX */
  420. {
  421. char *_name;
  422. int namespace;
  423. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  424. return -1;
  425. ret = extattr_get_fd (fd, namespace, _name, value, (size_t) size);
  426. g_free (_name);
  427. }
  428. #endif /* EA_UNIX */
  429. return ret;
  430. }
  431. mph_ssize_t
  432. Mono_Posix_Syscall_listxattr (const char *path, unsigned char *list, mph_size_t size)
  433. {
  434. mph_return_if_size_t_overflow (size);
  435. #ifdef EA_UNIX
  436. #if __APPLE__
  437. return listxattr (path, (char*) list, (size_t) size, 0);
  438. #else /* __APPLE__ */
  439. return listxattr (path, (char*) list, (size_t) size);
  440. #endif /* __APPLE__ */
  441. #else /* EA_UNIX */
  442. return bsd_listxattr (path, list, size);
  443. #endif /* EA_UNIX */
  444. }
  445. #if !__APPLE__
  446. mph_ssize_t
  447. Mono_Posix_Syscall_llistxattr (const char *path, unsigned char *list, mph_size_t size)
  448. {
  449. mph_return_if_size_t_overflow (size);
  450. #ifdef EA_UNIX
  451. return llistxattr (path, (char*) list, (size_t) size);
  452. #else /* EA_UNIX */
  453. return bsd_llistxattr (path, list, size);
  454. #endif /* EA_UNIX */
  455. }
  456. #endif /* !__APPLE__ */
  457. mph_ssize_t
  458. Mono_Posix_Syscall_flistxattr (int fd, unsigned char *list, mph_size_t size)
  459. {
  460. mph_return_if_size_t_overflow (size);
  461. #ifdef EA_UNIX
  462. #if __APPLE__
  463. return flistxattr (fd, (char*) list, (size_t) size, 0);
  464. #else /* __APPLE__ */
  465. return flistxattr (fd, (char*) list, (size_t) size);
  466. #endif /* __APPLE__ */
  467. #else /* EA_UNIX */
  468. return bsd_flistxattr (fd, list, size);
  469. #endif /* EA_UNIX */
  470. }
  471. gint32
  472. Mono_Posix_Syscall_removexattr (const char *path, const char *name)
  473. {
  474. gint32 ret;
  475. #ifdef EA_UNIX
  476. #if __APPLE__
  477. ret = removexattr (path, name, 0);
  478. #else /* __APPLE__ */
  479. ret = removexattr (path, name);
  480. #endif /* __APPLE__ */
  481. #else /* EA_UNIX */
  482. {
  483. char *_name;
  484. int namespace;
  485. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  486. return -1;
  487. ret = extattr_delete_file (path, namespace, _name);
  488. g_free (_name);
  489. }
  490. #endif /* EA_UNIX */
  491. return ret;
  492. }
  493. #if !__APPLE__
  494. gint32
  495. Mono_Posix_Syscall_lremovexattr (const char *path, const char *name)
  496. {
  497. gint32 ret;
  498. #ifdef EA_UNIX
  499. ret = lremovexattr (path, name);
  500. #else /* EA_UNIX */
  501. {
  502. char *_name;
  503. int namespace;
  504. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  505. return -1;
  506. ret = extattr_delete_link (path, namespace, _name);
  507. g_free (_name);
  508. }
  509. #endif /* EA_UNIX */
  510. return ret;
  511. }
  512. #endif /* !__APPLE__ */
  513. gint32
  514. Mono_Posix_Syscall_fremovexattr (int fd, const char *name)
  515. {
  516. gint32 ret;
  517. #ifdef EA_UNIX
  518. #if __APPLE__
  519. ret = fremovexattr (fd, name, 0);
  520. #else /* __APPLE__ */
  521. ret = fremovexattr (fd, name);
  522. #endif /* __APPLE__ */
  523. #else /* EA_UNIX */
  524. {
  525. char *_name;
  526. int namespace;
  527. if (bsd_handle_nsprefix (name, &_name, &namespace) == -1)
  528. return -1;
  529. ret = extattr_delete_fd (fd, namespace, _name);
  530. g_free (_name);
  531. }
  532. #endif /* EA_UNIX */
  533. return ret;
  534. }
  535. G_END_DECLS
  536. #endif /* HAVE_SYS_XATTR_H || HAVE_ATTR_ATTR_H || HAVE_SYS_EXTATTR_H */
  537. /*
  538. * vim: noexpandtab
  539. */