[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] getfattr: new applet
From:       LoveSy <lovesykun () gmail ! com>
Date:       2023-06-18 9:18:20
Message-ID: 20230618091820.87553-1-lovesykun () gmail ! com
[Download RAW message or body]

function                                             old     new   delta
getfattr_main                                          -     388    +388
print_attr                                             -     204    +204
list_attr                                              -     152    +152
.rodata                                            95358   95401     +43
applet_names                                        2766    2775      +9
e843419@0048_000003ed_14                               -       8      +8
e843419@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: 828/0)             Total: 828 bytes
   text    data     bss     dec     hex filename
1127717   16889    1736 1146342  117de6 busybox_old
1157516   16937    1736 1176189  11f27d busybox_unstripped

Signed-off-by: LoveSy <lovesykun@gmail.com>
---
Sorry for my duplicate emails. Please ignore my previous email with
the same title, as I accidentially sent it without discription.

I raise the patch because I notice there's setfattr in busybox but
there's no getfattr. This patch adds the missing getfattr
counterpart for setfattr. Note that this is a very simple
implementation.

 miscutils/getfattr.c | 120 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 120 insertions(+)
 create mode 100644 miscutils/getfattr.c

diff --git a/miscutils/getfattr.c b/miscutils/getfattr.c
new file mode 100644
index 000000000..556241fb5
--- /dev/null
+++ b/miscutils/getfattr.c
@@ -0,0 +1,120 @@
+/*
+ * getfattr - get extended attributes of filesystem objects.
+ *
+ * Copyright (C) 2023 by LoveSy <lovesykun@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);
+	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);
+	free(list);
+
+	return status;
+}
-- 
2.34.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic