summaryrefslogtreecommitdiff
path: root/miscutils/getfattr.c
blob: cb42fdac0a5a9ef17d80eb1f1647962896948121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * 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] {-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),
	OPT_n = (1 << 2),
};

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;
	exitcode_t status;
	int opt;
	char *buf = NULL;
	size_t bufsize = 0;
	char *list = NULL;
	size_t listsize = 0;

	opt = getopt32(argv, "^"
		"hdn:"
		/* Min one arg; -d and -n are exclusive */
		"\0" "-1:n--d:d--n"
			//getfattr 2.5.1 does not enforce this: ":d:n" /* exactly one of -n or -d is required */
		, &name
	);
	argv += optind;
	status = EXIT_SUCCESS;

	do {
		int r;
//getfattr 2.5.1 with no -n/-d defaults to -d
		if (!(opt & OPT_n)) {
			ssize_t len = list_attr(*argv, &list, &listsize);
			if (len < 0)
				goto err;
			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;
			}
			bb_putchar('\n');
		}
	} while (*++argv);

	if (ENABLE_FEATURE_CLEAN_UP)
		free(buf);

	fflush_stdout_and_exit(status);
}