xattr_ext.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2022, Jaidyn Levesque <jadedctrl@posteo.at>
  3. *
  4. * This program is free software: you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation, either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. #include <stdlib.h>
  18. #include <sys/xattr.h>
  19. #include <errno.h>
  20. char*
  21. get_xattr(const char* path, const char* attr, int* error_code)
  22. {
  23. ssize_t value_size = getxattr(path, attr, NULL, 0);
  24. if (value_size == -1) {
  25. *error_code = errno;
  26. return NULL;
  27. }
  28. char* value = (char*) malloc(value_size + 1);
  29. ssize_t new_size = getxattr(path, attr, value, value_size + 1);
  30. *error_code = (new_size == -1) ? errno : 0;
  31. value[value_size] = '\0';
  32. return value;
  33. }
  34. int
  35. set_xattr(const char* path, const char* attr, const char* value, int* error_code)
  36. {
  37. int retcode = lsetxattr(path, attr, value, strlen(value), 0);
  38. *error_code = (retcode == 0) ? 0 : errno;
  39. return retcode;
  40. }
  41. char*
  42. list_xattr(const char* path, ssize_t* size, int* error_code)
  43. {
  44. ssize_t value_size = llistxattr(path, NULL, 0);
  45. if (value_size == -1) {
  46. *error_code = errno;
  47. return NULL;
  48. }
  49. char* value = (char*) malloc(value_size + 1);
  50. *size = llistxattr(path, value, value_size + 1);
  51. *error_code = (*size == -1) ? errno : 0;
  52. value[value_size] = '\0';
  53. return value;
  54. }
  55. int
  56. remove_xattr(const char* path, const char* attr)
  57. {
  58. if (lremovexattr(path, attr) == -1)
  59. return errno;
  60. else
  61. return 0;
  62. }