summaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
authorDenys Vlasenko2017-09-08 20:55:59 +0200
committerDenys Vlasenko2017-09-08 20:55:59 +0200
commitb278ac0e607aa971c46706be63c580a2fd88dd56 (patch)
tree3d726c79024f77ea9eaa62cf0f68a92f48847386 /miscutils
parentd134aa93414c7d07a938b8076021c62d9987f683 (diff)
downloadbusybox-b278ac0e607aa971c46706be63c580a2fd88dd56.zip
busybox-b278ac0e607aa971c46706be63c580a2fd88dd56.tar.gz
setfattr: new applet
function old new delta setfattr_main - 189 +189 packed_usage 31516 31588 +72 setxattr - 53 +53 lsetxattr - 53 +53 removexattr - 37 +37 lremovexattr - 37 +37 applet_names 2649 2658 +9 find_applet_by_name 124 128 +4 applet_main 1532 1536 +4 ------------------------------------------------------------------------------ (add/remove: 7/0 grow/shrink: 4/0 up/down: 458/0) Total: 458 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils')
-rw-r--r--miscutils/setfattr.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/miscutils/setfattr.c b/miscutils/setfattr.c
new file mode 100644
index 0000000..f0ef227
--- /dev/null
+++ b/miscutils/setfattr.c
@@ -0,0 +1,68 @@
+/*
+ * setfattr - set extended attributes of filesystem objects.
+ *
+ * Copyright (C) 2017 by Denys Vlasenko <vda.linux@googlemail.com>
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
+ */
+//config:config SETFATTR
+//config: bool "setfattr"
+//config: default y
+//config: help
+//config: Set/delete extended attributes on files
+
+//applet:IF_SETFATTR(APPLET_NOEXEC(setfattr, setfattr, BB_DIR_USR_BIN, BB_SUID_DROP, setfattr))
+
+//kbuild:lib-$(CONFIG_SETFATTR) += setfattr.o
+
+#include <sys/xattr.h>
+#include "libbb.h"
+
+//usage:#define setfattr_trivial_usage
+//usage: "[-h] -n|-x ATTR [-v VALUE] FILE..."
+//usage:#define setfattr_full_usage "\n\n"
+//usage: "Set extended attributes"
+//usage: "\n"
+//usage: "\n -h Do not follow symlinks"
+//usage: "\n -x ATTR Remove attribute ATTR"
+//usage: "\n -n ATTR Set attribute ATTR to VALUE"
+//usage: "\n -v VALUE (default: empty)"
+int setfattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int setfattr_main(int argc UNUSED_PARAM, char **argv)
+{
+ const char *name;
+ const char *value = "";
+ int status;
+ int opt;
+ enum {
+ OPT_h = (1 << 0),
+ OPT_x = (1 << 1),
+ };
+
+ opt = getopt32(argv, "^"
+ "hx:n:v:"
+ /* Min one arg, either -x or -n is a must, -x does not allow -v */
+ "\0" "-1:x:n:n--x:x--nv:v--x"
+ , &name, &name, &value
+ );
+ argv += optind;
+
+ status = EXIT_SUCCESS;
+ do {
+ int r;
+ if (opt & OPT_x)
+ r = ((opt & OPT_h) ? lremovexattr : removexattr)(*argv, name);
+ else {
+ r = ((opt & OPT_h) ? lsetxattr : setxattr)(
+ *argv, name,
+ value, strlen(value), /*flags:*/ 0
+ );
+ }
+ if (r) {
+ bb_simple_perror_msg(*argv);
+ status = EXIT_FAILURE;
+ }
+ } while (*++argv);
+
+ return status;
+}