3
0

setfattr.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * setfattr - set extended attributes of filesystem objects.
  3. *
  4. * Copyright (C) 2017 by Denys Vlasenko <vda.linux@googlemail.com>
  5. *
  6. * Licensed under GPLv2, see file LICENSE in this source tree.
  7. */
  8. //config:config SETFATTR
  9. //config: bool "setfattr (3.9 kb)"
  10. //config: default y
  11. //config: help
  12. //config: Set/delete extended attributes on files
  13. //applet:IF_SETFATTR(APPLET_NOEXEC(setfattr, setfattr, BB_DIR_USR_BIN, BB_SUID_DROP, setfattr))
  14. //kbuild:lib-$(CONFIG_SETFATTR) += setfattr.o
  15. #include <sys/xattr.h>
  16. #include "libbb.h"
  17. //usage:#define setfattr_trivial_usage
  18. //usage: "[-h] -n|-x ATTR [-v VALUE] FILE..."
  19. //usage:#define setfattr_full_usage "\n\n"
  20. //usage: "Set extended attributes"
  21. //usage: "\n"
  22. //usage: "\n -h Do not follow symlinks"
  23. //usage: "\n -x ATTR Remove attribute ATTR"
  24. //usage: "\n -n ATTR Set attribute ATTR to VALUE"
  25. //usage: "\n -v VALUE (default: empty)"
  26. int setfattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int setfattr_main(int argc UNUSED_PARAM, char **argv)
  28. {
  29. const char *name;
  30. const char *value = "";
  31. exitcode_t status;
  32. int opt;
  33. enum {
  34. OPT_h = (1 << 0),
  35. OPT_x = (1 << 1),
  36. };
  37. opt = getopt32(argv, "^"
  38. "hx:n:v:"
  39. /* Min one arg, either -x or -n is a must, -x does not allow -v */
  40. "\0" "-1:x:n:n--x:x--nv:v--x"
  41. , &name, &name, &value
  42. );
  43. argv += optind;
  44. status = EXIT_SUCCESS;
  45. do {
  46. int r;
  47. if (opt & OPT_x)
  48. r = ((opt & OPT_h) ? lremovexattr : removexattr)(*argv, name);
  49. else {
  50. r = ((opt & OPT_h) ? lsetxattr : setxattr)(
  51. *argv, name,
  52. value, strlen(value), /*flags:*/ 0
  53. );
  54. }
  55. if (r) {
  56. bb_simple_perror_msg(*argv);
  57. status = EXIT_FAILURE;
  58. }
  59. } while (*++argv);
  60. return status;
  61. }