blob: 1aa8e14cf4c42efbc5ab9727cdd46a9f36f6affe [file] [log] [blame]
Ethan Yonker91758442017-02-03 13:24:31 -06001/****************************************************************************
2 | (C) Copyright 2008 Novell, Inc. All Rights Reserved.
3 |
4 | GPLv2: This program is free software; you can redistribute it
5 | and/or modify it under the terms of version 2 of the GNU General
6 | Public License as published by the Free Software Foundation.
7 |
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU General Public License for more details.
12 +-------------------------------------------------------------------------*/
13/*
14 * NOTE from Dees_Troy: modified source to display values along with xattr names
15 * Below code comments about usage are no longer accurate but came from the
16 * original source code from the chromium project and combine features of
17 * listxattr and getfattr:
18 * https://chromium.googlesource.com/chromiumos/platform/punybench/+/factory-1235.B/file.m/listxattr.c
19 * https://chromium.googlesource.com/chromiumos/platform/punybench/+/factory-1235.B/file.m/getxattr.c
20 */
21/*
22 * LISTXATTR(2) Linux Programmer's Manual LISTXATTR(2)
23 *
24 *
25 *
26 * NAME
27 * listxattr, llistxattr, flistxattr - list extended attribute names
28 *
29 * SYNOPSIS
30 * #include <sys/types.h>
31 * #include <attr/xattr.h>
32 *
33 * ssize_t listxattr (const char *path,
34 * char *list, size_t size);
35 * ssize_t llistxattr (const char *path,
36 * char *list, size_t size);
37 * ssize_t flistxattr (int filedes,
38 * char *list, size_t size);
39 *
40 * DESCRIPTION
41 * Extended attributes are name:value pairs associated with inodes
42 * (files, directories, symlinks, etc). They are extensions to the
43 * normal attributes which are associated with all inodes in the sys-
44 * tem (i.e. the stat(2) data). A complete overview of extended
45 * attributes concepts can be found in attr(5).
46 *
47 * listxattr retrieves the list of extended attribute names associated
48 * with the given path in the filesystem. The list is the set of
49 * (NULL-terminated) names, one after the other. Names of extended
50 * attributes to which the calling process does not have access may be
51 * omitted from the list. The length of the attribute name list is
52 * returned.
53 *
54 * llistxattr is identical to listxattr, except in the case of a sym-
55 * bolic link, where the list of names of extended attributes associ-
56 * ated with the link itself is retrieved, not the file that it refers
57 * to.
58 *
59 * flistxattr is identical to listxattr, only the open file pointed to
60 * by filedes (as returned by open(2)) is interrogated in place of
61 * path.
62 *
63 * A single extended attribute name is a simple NULL-terminated
64 * string. The name includes a namespace prefix; there may be sev-
65 * eral, disjoint namespaces associated with an individual inode.
66 *
67 * An empty buffer of size zero can be passed into these calls to
68 * return the current size of the list of extended attribute names,
69 * which can be used to estimate the size of a buffer which is suffi-
70 * ciently large to hold the list of names.
71 *
72 * EXAMPLES
73 * The list of names is returned as an unordered array of NULL-termi-
74 * nated character strings (attribute names are separated by NULL
75 * characters), like this:
76 * user.name1\0system.name1\0user.name2\0
77 *
78 * Filesystems like ext2, ext3 and XFS which implement POSIX ACLs
79 * using extended attributes, might return a list like this:
80 * system.posix_acl_access\0system.posix_acl_default\0
81 *
82 * RETURN VALUE
83 * On success, a positive number is returned indicating the size of
84 * the extended attribute name list. On failure, -1 is returned and
85 * errno is set appropriately.
86 *
87 * If the size of the list buffer is too small to hold the result,
88 * errno is set to ERANGE.
89 *
90 * If extended attributes are not supported by the filesystem, or are
91 * disabled, errno is set to ENOTSUP.
92 *
93 * The errors documented for the stat(2) system call are also applica-
94 * ble here.
95 *
96 * AUTHORS
97 * Andreas Gruenbacher, <a.gruenbacher@computer.org> and the SGI XFS
98 * development team, <linux-xfs@oss.sgi.com>. Please send any bug
99 * reports or comments to these addresses.
100 *
101 * SEE ALSO
102 * getfattr(1), setfattr(1), getxattr(2), open(2), removexattr(2),
103 * setxattr(2), stat(2), attr(5)
104 *
105 *
106 *
107 * Dec 2001 Extended Attributes LISTXATTR(2)
108 */
109#include <stdio.h>
110#include <stdlib.h>
111#include <ctype.h>
112#include <sys/types.h>
113#include <sys/xattr.h>
114//#include <eprintf.h>
115//#include <puny.h>
116
117/* dumpmem: dumps an n byte area of memory to screen */
118void dumpmem (const void *mem, unsigned int n)
119{
120 const char *c = mem;
121 unsigned i;
122 int all_text = 1;
123 if (n == 0) {
124 printf("<empty>");
125 return;
126 }
127 for (i = 0; i < n - 1; i++, c++) {
128 if (!isprint(*c)) {
129 all_text = 0;
130 break;
131 }
132 }
133 c = mem;
134 if (all_text) {
135 for (i = 0; i < n - 1; i++, c++) {
136 putchar(*c);
137 }
138 return;
139 } else {
140 char hex[(n * 2) + 1];
141 for(i = 0; i < n; i++, c++)
142 sprintf(hex + (i * 2), "%02X", *c);
143 hex[n] = 0;
144 printf("0x%s", hex);
145 return;
146 }
147}
148
149void dump_list (char *file, char *list, ssize_t size)
150{
151 int c;
152 int i;
153 int first = 1;
154 int j = 0;
155 char xattr[1024];
156 char value[1024];
157 ssize_t size2;
158 for (i = 0; i < size; i++) {
159 c = list[i];
160 if (c) {
161 if (first) {
162 putchar('\t');
163 first = 0;
164 j = 0;
165 }
166 putchar(c);
167 xattr[j++] = list[i];
168 } else {
169 xattr[j] = '\0';
170 size2 = getxattr(file, xattr, value, sizeof(value));
171 if (size2 < 0) {
172 printf("file=%s xattr=%s returned:", file, xattr);
173 } else {
174 putchar('=');
175 dumpmem(value, size2);
176 }
177 putchar('\n');
178 first = 1;
179 }
180 }
181}
182void usage (void)
183{
184 printf("listxattr <file>");
185}
186char List[1<<17];
187int main (int argc, char *argv[])
188{
189 ssize_t size;
190 if (argc < 2) {
191 usage();
192 exit(2);
193 }
194 size = listxattr(argv[1], List, sizeof(List));
195 if (size == -1) {
196 perror(argv[1]);
197 exit(2);
198 }
199 printf("xattrs for %s:\n", argv[1]);
200 dump_list(argv[1], List, size);
201 return 0;
202}