From bd471f67cc6eff4abf47f027edbace707065bc6b Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Sun, 17 Jul 2016 10:42:31 -0700 Subject: Add getfattr(1) and setfattr(1). Plus basic tests. --- toys/pending/setfattr.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 toys/pending/setfattr.c (limited to 'toys/pending/setfattr.c') diff --git a/toys/pending/setfattr.c b/toys/pending/setfattr.c new file mode 100644 index 00000000..9f17ce05 --- /dev/null +++ b/toys/pending/setfattr.c @@ -0,0 +1,56 @@ +/* setfattr.c - Write POSIX extended attributes. + * + * Copyright 2016 Android Open Source Project. + * + * No standard + +USE_SETFATTR(NEWTOY(setfattr, "hn:v:x:[!xv]", TOYFLAG_USR|TOYFLAG_BIN)) + +config SETFATTR + bool "setfattr" + default y + help + usage: setfattr [-h] -n NAME [-v VALUE] FILE... + usage: setfattr [-h] -x NAME FILE... + + Write POSIX extended attributes. + + -h Do not dereference symbolic links. + -n Set value of given attribute. + -x Remove value of given attribute. + -v Value to use with -n (default is empty). +*/ + +#define FOR_setfattr +#include "toys.h" + +GLOBALS( + char *x, *v, *n; +) + +static void do_setfattr(char *file) +{ + int (*setter)(const char *, const char *, const void *, size_t, int) = + setxattr; + int (*remover)(const char *, const char *) = removexattr; + + if (toys.optflags&FLAG_h) { + setter = lsetxattr; + remover = lremovexattr; + } + + if (toys.optflags&FLAG_x) { + if (remover(file, TT.x)) perror_msg("removexattr failed"); + } else { + if (setter(file, TT.n, TT.v, TT.v ? strlen(TT.v) : 0, 0)) + perror_msg("setxattr failed"); + } +} + +void setfattr_main(void) +{ + char **s; + + if (!(toys.optflags&(FLAG_n|FLAG_x))) error_exit("need 'n' or 'x'"); + for (s=toys.optargs; *s; s++) do_setfattr(*s); +} -- cgit v1.2.3