[PATCH] getfattr: new applet
Denys Vlasenko
vda.linux at googlemail.com
Sun Jul 16 12:55:42 UTC 2023
Applied, thank you.
Please try current git.
I would like to check how we do on errors.
How to easily get getxattr error? Neither sysfs, procfs,
nor devtmpfs throw errors on it.
On Fri, Jul 7, 2023 at 10:45 AM LoveSy <lovesykun at gmail.com> wrote:
>
> function old new delta
> getfattr_main - 380 +380
> print_attr - 204 +204
> list_attr - 152 +152
> .rodata 95358 95401 +43
> applet_names 2766 2775 +9
> e843419 at 0048_000003ed_14 - 8 +8
> e843419 at 0047_000003d1_550 - 8 +8
> applet_main 3216 3224 +8
> packed_usage 34560 34567 +7
> applet_install_loc 201 202 +1
> ------------------------------------------------------------------------------
> (add/remove: 6/0 grow/shrink: 5/0 up/down: 820/0) Total: 820 bytes
> text data bss dec hex filename
> 1127717 16889 1736 1146342 117de6 busybox_old
> 1141124 16937 1736 1159797 11b275 busybox_unstripped
>
> Signed-off-by: LoveSy <lovesykun at gmail.com>
> ---
> Thansk for your review. Updated the patch. Please have a check.
>
> miscutils/getfattr.c | 121 +++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 121 insertions(+)
> create mode 100644 miscutils/getfattr.c
>
> diff --git a/miscutils/getfattr.c b/miscutils/getfattr.c
> new file mode 100644
> index 000000000..44556cb24
> --- /dev/null
> +++ b/miscutils/getfattr.c
> @@ -0,0 +1,121 @@
> +/*
> + * getfattr - get extended attributes of filesystem objects.
> + *
> + * Copyright (C) 2023 by LoveSy <lovesykun at gmail.com>
> + *
> + * 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 <stdio.h>
> +#include <sys/xattr.h>
> +#include "libbb.h"
> +
> +//usage:#define getfattr_trivial_usage
> +//usage: "[-h] -n ATTR FILE...\n"
> +//usage: "or: getfattr [-h] -d FILE..."
> +//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"
> +int getfattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
> +
> +int print_attr(const char *file, const char *name, int follow, char **buf, size_t *bufsize)
> +{
> + ssize_t len;
> + len = (follow ? getxattr: lgetxattr)(file, name, NULL, 0);
> + if (len == -1) {
> + return -1;
> + }
> + if (*bufsize < len) {
> + *buf = xrealloc(*buf, len);
> + *bufsize = len;
> + }
> + len = (follow ? getxattr : lgetxattr)(file, name, *buf, *bufsize);
> + if (len == -1) {
> + return -1;
> + }
> + printf("%s=\"%.*s\"\n", name, len, *buf);
> + return 0;
> +}
> +
> +ssize_t list_attr(const char *file, int follow, char **list, size_t *listsize)
> +{
> + ssize_t len;
> + len = (follow ? listxattr : llistxattr)(file, NULL, 0);
> + if (len == -1) {
> + return -1;
> + }
> + if (*listsize < len) {
> + *list = xrealloc(*list, len);
> + *listsize = len;
> + }
> + len = (follow ? listxattr : llistxattr)(file, *list, *listsize);
> + if (len == -1) {
> + return -1;
> + }
> + return len;
> +}
> +
> +int getfattr_main(int argc UNUSED_PARAM, char **argv)
> +{
> + const char *name = NULL;
> + int status;
> + int opt;
> + char *buf = NULL;
> + size_t bufsize = 0;
> + char *list = NULL;
> + size_t listsize = 0;
> + enum {
> + OPT_h = (1 << 0),
> + OPT_d = (1 << 1),
> + };
> +
> + 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 = 0;
> + printf("# file: %s\n", *argv);
> + if (opt & OPT_d) {
> + ssize_t len = list_attr(*argv, !(opt & OPT_h), &list, &listsize);
> + ssize_t keylen;
> + char *key = list;
> + while (len > 0 && !r) {
> + r = print_attr(*argv, key, !(opt & OPT_h), &buf, &bufsize);
> + keylen = strlen(key) + 1;
> + key += keylen;
> + len -= keylen;
> + }
> + }
> + else {
> + r = print_attr(*argv, name, !(opt & OPT_h), &buf, &bufsize);
> + }
> +
> + if (r) {
> + bb_simple_perror_msg(*argv);
> + status = EXIT_FAILURE;
> + }
> + printf("\n");
> + } while (*++argv);
> +
> + free(buf);
> +
> + return status;
> +}
> --
> 2.34.1
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox
More information about the busybox
mailing list