Denys Vlasenko | b278ac0 | 2017-09-08 20:55:59 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "setfattr (3.7 kb)" |
Denys Vlasenko | b278ac0 | 2017-09-08 20:55:59 +0200 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: help |
| 12 | //config: Set/delete extended attributes on files |
| 13 | |
| 14 | //applet:IF_SETFATTR(APPLET_NOEXEC(setfattr, setfattr, BB_DIR_USR_BIN, BB_SUID_DROP, setfattr)) |
| 15 | |
| 16 | //kbuild:lib-$(CONFIG_SETFATTR) += setfattr.o |
| 17 | |
| 18 | #include <sys/xattr.h> |
| 19 | #include "libbb.h" |
| 20 | |
| 21 | //usage:#define setfattr_trivial_usage |
| 22 | //usage: "[-h] -n|-x ATTR [-v VALUE] FILE..." |
| 23 | //usage:#define setfattr_full_usage "\n\n" |
| 24 | //usage: "Set extended attributes" |
| 25 | //usage: "\n" |
| 26 | //usage: "\n -h Do not follow symlinks" |
| 27 | //usage: "\n -x ATTR Remove attribute ATTR" |
| 28 | //usage: "\n -n ATTR Set attribute ATTR to VALUE" |
| 29 | //usage: "\n -v VALUE (default: empty)" |
| 30 | int setfattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 31 | int setfattr_main(int argc UNUSED_PARAM, char **argv) |
| 32 | { |
| 33 | const char *name; |
| 34 | const char *value = ""; |
| 35 | int status; |
| 36 | int opt; |
| 37 | enum { |
| 38 | OPT_h = (1 << 0), |
| 39 | OPT_x = (1 << 1), |
| 40 | }; |
| 41 | |
| 42 | opt = getopt32(argv, "^" |
| 43 | "hx:n:v:" |
| 44 | /* Min one arg, either -x or -n is a must, -x does not allow -v */ |
| 45 | "\0" "-1:x:n:n--x:x--nv:v--x" |
| 46 | , &name, &name, &value |
| 47 | ); |
| 48 | argv += optind; |
| 49 | |
| 50 | status = EXIT_SUCCESS; |
| 51 | do { |
| 52 | int r; |
| 53 | if (opt & OPT_x) |
| 54 | r = ((opt & OPT_h) ? lremovexattr : removexattr)(*argv, name); |
| 55 | else { |
| 56 | r = ((opt & OPT_h) ? lsetxattr : setxattr)( |
| 57 | *argv, name, |
| 58 | value, strlen(value), /*flags:*/ 0 |
| 59 | ); |
| 60 | } |
| 61 | if (r) { |
| 62 | bb_simple_perror_msg(*argv); |
| 63 | status = EXIT_FAILURE; |
| 64 | } |
| 65 | } while (*++argv); |
| 66 | |
| 67 | return status; |
| 68 | } |