[PATCH] getfattr: new applet

LoveSy lovesykun at gmail.com
Sun Jun 18 08:55:49 UTC 2023


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>
---
 miscutils/getfattr.c | 122 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 122 insertions(+)
 create mode 100644 miscutils/getfattr.c

diff --git a/miscutils/getfattr.c b/miscutils/getfattr.c
new file mode 100644
index 000000000..4d4eacb5f
--- /dev/null
+++ b/miscutils/getfattr.c
@@ -0,0 +1,122 @@
+/*
+ * 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|-d ATTR 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 *buflen)
+{
+	ssize_t vallen, gotlen;
+	vallen = (follow ? getxattr: lgetxattr)(file, name, NULL, 0);
+	if (vallen == -1) {
+		return -1;
+	}
+	vallen += 1;
+	if (*buflen < vallen) {
+		*buf = (char *)xrealloc(*buf, vallen);
+		*buflen = vallen;
+	}
+	vallen = (follow ? getxattr : lgetxattr)(file, name, *buf, vallen);
+	if (vallen == -1) {
+		return -1;
+	}
+	(*buf)[vallen] = '\0';
+	printf("%s=\"%s\"\n", name, *buf);
+	return 0;
+}
+
+ssize_t list_attr(const char *file, int follow, char **list, size_t *listlen)
+{
+	ssize_t len;
+	len = (follow ? listxattr : llistxattr)(file, NULL, 0);
+	if (len == -1) {
+		return -1;
+	}
+	if (*listlen < len) {
+		*list = (char *)xrealloc(*list, len);
+		*listlen = len;
+	}
+	len = (follow ? listxattr : llistxattr)(file, *list, len);
+	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 buflen = 0;
+	char *list = NULL;
+	size_t listlen = 0;
+	enum {
+		OPT_h = (1 << 0),
+		OPT_d = (1 << 1),
+	};
+
+	opt = getopt32(argv, "^"
+		"hdn:"
+		/* Min one arg, either -x or -n is a must, -d does not allow -n */
+		"\0" "-1:dn: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, &listlen);
+			ssize_t keylen;
+			char *key = list;
+			while (len > 0 && !r) {
+				r = print_attr(*argv, key, !(opt & OPT_h), &buf, &buflen);
+				keylen = strlen(key) + 1;
+				key += keylen;
+				len -= keylen;
+			}
+		}
+		else {
+			r = print_attr(*argv, name, !(opt & OPT_h), &buf, &buflen);
+		}
+		printf("\n");
+
+		if (r) {
+			bb_simple_perror_msg(*argv);
+			status = EXIT_FAILURE;
+		}
+	} while (*++argv);
+
+	free(buf);
+
+	return status;
+}
-- 
2.34.1



More information about the busybox mailing list