From cf809e2f2dbf699035e4841e45070b947374a989 Mon Sep 17 00:00:00 2001 From: YU Jincheng Date: Fri, 7 Jul 2023 16:44:24 +0800 Subject: getfattr: new applet function old new delta getfattr_main - 309 +309 print_attr - 115 +115 packed_usage 34576 34631 +55 .rodata 105349 105395 +46 lgetxattr - 41 +41 getxattr - 41 +41 llistxattr - 35 +35 listxattr - 35 +35 applet_names 2806 2815 +9 applet_main 1620 1624 +4 ------------------------------------------------------------------------------ (add/remove: 7/0 grow/shrink: 4/0 up/down: 690/0) Total: 690 bytes Signed-off-by: YU Jincheng Signed-off-by: Denys Vlasenko --- miscutils/getfattr.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 miscutils/getfattr.c diff --git a/miscutils/getfattr.c b/miscutils/getfattr.c new file mode 100644 index 000000000..59b6f6bca --- /dev/null +++ b/miscutils/getfattr.c @@ -0,0 +1,131 @@ +/* + * getfattr - get extended attributes of filesystem objects. + * + * Copyright (C) 2023 by LoveSy + * + * Licensed under GPLv2, see file LICENSE in this source tree. + */ +//config:config GETFATTR +//config: bool "getfattr (12.3 kb)" +//config: default y +//config: help +//config: Get extended attributes on files + +//applet:IF_GETFATTR(APPLET_NOEXEC(getfattr, getfattr, BB_DIR_USR_BIN, BB_SUID_DROP, getfattr)) + +//kbuild:lib-$(CONFIG_GETFATTR) += getfattr.o + +#include +#include +#include "libbb.h" + +//usage:#define getfattr_trivial_usage +//usage: "[-h] {-d|-n ATTR} FILE...\n" +//usage:#define getfattr_full_usage "\n\n" +//usage: "Get extended attributes" +//usage: "\n" +//usage: "\n -h Do not follow symlinks" +//usage: "\n -d Dump all attributes" +//usage: "\n -n ATTR Get attribute ATTR" + +enum { + OPT_h = (1 << 0), + OPT_d = (1 << 1), +}; + +static int print_attr(const char *file, const char *name, char **buf, size_t *bufsize) +{ + ssize_t len; + + if (*bufsize == 0) + goto grow; + again: + len = ((option_mask32 & OPT_h) ? lgetxattr: getxattr)(file, name, *buf, *bufsize); + if (len < 0) { + if (errno != ERANGE) + return len; + grow: + *bufsize = (*bufsize * 2) + 1024; + *buf = xrealloc(*buf, *bufsize); + goto again; + } + printf("%s=\"%.*s\"\n", name, len, *buf); + return 0; +} + +static ssize_t list_attr(const char *file, char **list, size_t *listsize) +{ + ssize_t len; + + if (*listsize == 0) + goto grow; + again: + len = ((option_mask32 & OPT_h) ? llistxattr : listxattr)(file, *list, *listsize); + if (len < 0) { + if (errno != ERANGE) + return len; + grow: + *listsize = (*listsize * 2) + 1024; + *list = xrealloc(*list, *listsize); + goto again; + } + return len; +} + +int getfattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int getfattr_main(int argc UNUSED_PARAM, char **argv) +{ + const char *name; + int status; + int opt; + char *buf = NULL; + size_t bufsize = 0; + char *list = NULL; + size_t listsize = 0; + + opt = getopt32(argv, "^" + "hdn:" + /* Min one arg; exactly one of -n or -d is required. */ + "\0" "-1:d:n:n--d:d--n" + , &name + ); + argv += optind; + status = EXIT_SUCCESS; + + do { + int r; + if (opt & OPT_d) { + ssize_t len = list_attr(*argv, &list, &listsize); + if (len > 0) { + char *key; + printf("# file: %s\n", *argv); + key = list; + while (len > 0) { + ssize_t keylen; + r = print_attr(*argv, key, &buf, &bufsize); + if (r) + goto err; + keylen = strlen(key) + 1; + key += keylen; + len -= keylen; + } + bb_putchar('\n'); + } + } else { + printf("# file: %s\n", *argv); + r = print_attr(*argv, name, &buf, &bufsize); + if (r) { + err: + bb_simple_perror_msg(*argv); + status = EXIT_FAILURE; + // continue; maybe? + } + bb_putchar('\n'); + } + } while (*++argv); + + if (ENABLE_FEATURE_CLEAN_UP) + free(buf); + + fflush_stdout_and_exit(status); +} -- cgit v1.2.3