2
* getfattr - get extended attributes of filesystem objects.
4
* Copyright (C) 2023 by LoveSy <lovesykun@gmail.com>
6
* Licensed under GPLv2, see file LICENSE in this source tree.
8
//config:config GETFATTR
9
//config: bool "getfattr (12.3 kb)"
12
//config: Get extended attributes on files
14
//applet:IF_GETFATTR(APPLET_NOEXEC(getfattr, getfattr, BB_DIR_USR_BIN, BB_SUID_DROP, getfattr))
16
//kbuild:lib-$(CONFIG_GETFATTR) += getfattr.o
19
#include <sys/xattr.h>
22
//usage:#define getfattr_trivial_usage
23
//usage: "[-h] {-d|-n ATTR} FILE...\n"
24
//usage:#define getfattr_full_usage "\n\n"
25
//usage: "Get extended attributes"
27
//usage: "\n -h Do not follow symlinks"
28
//usage: "\n -d Dump all attributes"
29
//usage: "\n -n ATTR Get attribute ATTR"
37
static int print_attr(const char *file, const char *name, char **buf, size_t *bufsize)
44
len = ((option_mask32 & OPT_h) ? lgetxattr: getxattr)(file, name, *buf, *bufsize);
49
*bufsize = (*bufsize * 2) + 1024;
50
*buf = xrealloc(*buf, *bufsize);
53
printf("%s=\"%.*s\"\n", name, len, *buf);
57
static ssize_t list_attr(const char *file, char **list, size_t *listsize)
64
len = ((option_mask32 & OPT_h) ? llistxattr : listxattr)(file, *list, *listsize);
69
*listsize = (*listsize * 2) + 1024;
70
*list = xrealloc(*list, *listsize);
76
int getfattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
77
int getfattr_main(int argc UNUSED_PARAM, char **argv)
87
opt = getopt32(argv, "^"
89
/* Min one arg; -d and -n are exclusive */
91
//getfattr 2.5.1 does not enforce this: ":d:n" /* exactly one of -n or -d is required */
95
status = EXIT_SUCCESS;
99
//getfattr 2.5.1 with no -n/-d defaults to -d
100
if (!(opt & OPT_n)) {
101
ssize_t len = list_attr(*argv, &list, &listsize);
106
printf("# file: %s\n", *argv);
110
r = print_attr(*argv, key, &buf, &bufsize);
113
keylen = strlen(key) + 1;
120
printf("# file: %s\n", *argv);
121
r = print_attr(*argv, name, &buf, &bufsize);
124
bb_simple_perror_msg(*argv);
125
status = EXIT_FAILURE;
132
if (ENABLE_FEATURE_CLEAN_UP)
135
fflush_stdout_and_exit(status);