]>
Commit | Line | Data |
---|---|---|
bf5a7247 MK |
1 | .\" Copyright (C) Andreas Gruenbacher, February 2001 |
2 | .\" Copyright (C) Silicon Graphics Inc, September 2001 | |
636a3877 | 3 | .\" Copyright (C) 2015 Heinrich Schuchardt <xypron.glpk@gmx.de> |
fea681da | 4 | .\" |
1dd72f9c | 5 | .\" %%%LICENSE_START(GPLv2+_DOC_FULL) |
fea681da MK |
6 | .\" This is free documentation; you can redistribute it and/or |
7 | .\" modify it under the terms of the GNU General Public License as | |
8 | .\" published by the Free Software Foundation; either version 2 of | |
9 | .\" the License, or (at your option) any later version. | |
10 | .\" | |
11 | .\" The GNU General Public License's references to "object code" | |
12 | .\" and "executables" are to be interpreted as the output of any | |
13 | .\" document formatting or typesetting system, including | |
14 | .\" intermediate and printed output. | |
15 | .\" | |
16 | .\" This manual is distributed in the hope that it will be useful, | |
17 | .\" but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | .\" GNU General Public License for more details. | |
20 | .\" | |
21 | .\" You should have received a copy of the GNU General Public | |
c715f741 MK |
22 | .\" License along with this manual; if not, see |
23 | .\" <http://www.gnu.org/licenses/>. | |
6a8d8745 | 24 | .\" %%%LICENSE_END |
fea681da | 25 | .\" |
9ba01802 | 26 | .TH LISTXATTR 2 2019-03-06 "Linux" "Linux Programmer's Manual" |
fea681da MK |
27 | .SH NAME |
28 | listxattr, llistxattr, flistxattr \- list extended attribute names | |
29 | .SH SYNOPSIS | |
30 | .fam C | |
31 | .nf | |
32 | .B #include <sys/types.h> | |
4f72d935 | 33 | .B #include <sys/xattr.h> |
68e4db0a | 34 | .PP |
c10859eb MK |
35 | .BI "ssize_t listxattr(const char\ *" path ", char\ *" list \ |
36 | ", size_t " size ); | |
37 | .BI "ssize_t llistxattr(const char\ *" path ", char\ *" list \ | |
38 | ", size_t " size ); | |
d3b03141 | 39 | .BI "ssize_t flistxattr(int " fd ", char\ *" list ", size_t " size ); |
fea681da MK |
40 | .fi |
41 | .fam T | |
42 | .SH DESCRIPTION | |
2d10343d MK |
43 | Extended attributes are |
44 | .IR name : value | |
0967c11f | 45 | pairs associated with inodes (files, directories, symbolic links, etc.). |
fea681da | 46 | They are extensions to the normal attributes which are associated |
75b94dc3 | 47 | with all inodes in the system (i.e., the |
fea681da MK |
48 | .BR stat (2) |
49 | data). | |
50 | A complete overview of extended attributes concepts can be found in | |
89fabe2e | 51 | .BR xattr (7). |
fea681da | 52 | .PP |
e511ffb6 | 53 | .BR listxattr () |
cc66e8b4 | 54 | retrieves the list |
fea681da MK |
55 | of extended attribute names associated with the given |
56 | .I path | |
9ee4a2b6 | 57 | in the filesystem. |
cc66e8b4 MK |
58 | The retrieved list is placed in |
59 | .IR list , | |
60 | a caller-allocated buffer whose size (in bytes) is specified in the argument | |
61 | .IR size . | |
836f07c1 | 62 | The list is the set of (null-terminated) names, one after the other. |
fea681da MK |
63 | Names of extended attributes to which the calling process does not |
64 | have access may be omitted from the list. | |
65 | The length of the attribute name | |
66 | .I list | |
67 | is returned. | |
68 | .PP | |
e511ffb6 | 69 | .BR llistxattr () |
c13182ef | 70 | is identical to |
e511ffb6 | 71 | .BR listxattr (), |
fea681da MK |
72 | except in the case of a symbolic link, where the list of names of |
73 | extended attributes associated with the link itself is retrieved, | |
74 | not the file that it refers to. | |
75 | .PP | |
e511ffb6 | 76 | .BR flistxattr () |
fea681da | 77 | is identical to |
e511ffb6 | 78 | .BR listxattr (), |
d3b03141 MK |
79 | only the open file referred to by |
80 | .I fd | |
fea681da MK |
81 | (as returned by |
82 | .BR open (2)) | |
83 | is interrogated in place of | |
84 | .IR path . | |
85 | .PP | |
86 | A single extended attribute | |
87 | .I name | |
f1ec5ad1 | 88 | is a null-terminated string. |
df8a3cac | 89 | The name includes a namespace prefix; there may be several, disjoint |
fea681da | 90 | namespaces associated with an individual inode. |
efeece04 | 91 | .PP |
a4526c2f | 92 | If |
fea681da | 93 | .I size |
a4526c2f MK |
94 | is specified as zero, these calls return the current size of the |
95 | list of extended attribute names (and leave | |
96 | .I list | |
97 | unchanged). | |
98 | This can be used to determine the size of the buffer that | |
99 | should be supplied in a subsequent call. | |
100 | (But, bear in mind that there is a possibility that the | |
101 | set of extended attributes may change between the two calls, | |
102 | so that it is still necessary to check the return status | |
103 | from the second call.) | |
2b2581ee | 104 | .SS Example |
fea681da MK |
105 | The |
106 | .I list | |
836f07c1 | 107 | of names is returned as an unordered array of null-terminated character |
d1a71985 | 108 | strings (attribute names are separated by null bytes (\(aq\e0\(aq)), like this: |
408731d4 | 109 | .PP |
d6c8cd78 MK |
110 | .in +4n |
111 | .EX | |
d1a71985 | 112 | user.name1\e0system.name1\e0user.name2\e0 |
d6c8cd78 MK |
113 | .EE |
114 | .in | |
11ac5b51 | 115 | .PP |
f1ec5ad1 MK |
116 | Filesystems that implement POSIX ACLs using |
117 | extended attributes might return a | |
fea681da MK |
118 | .I list |
119 | like this: | |
408731d4 | 120 | .PP |
d6c8cd78 MK |
121 | .in +4n |
122 | .EX | |
d1a71985 | 123 | system.posix_acl_access\e0system.posix_acl_default\e0 |
d6c8cd78 MK |
124 | .EE |
125 | .in | |
fea681da | 126 | .SH RETURN VALUE |
2507f0aa | 127 | On success, a nonnegative number is returned indicating the size of the |
fea681da MK |
128 | extended attribute name list. |
129 | On failure, \-1 is returned and | |
130 | .I errno | |
131 | is set appropriately. | |
92d67001 MK |
132 | .SH ERRORS |
133 | .TP | |
92b1a2cc | 134 | .B E2BIG |
f124c136 | 135 | The size of the list of extended attribute names is larger than the maximum |
efb3a65a MK |
136 | size allowed; the list cannot be retrieved. |
137 | This can happen on filesystems that support an unlimited number of | |
138 | extended attributes per file such as XFS, for example. | |
7353fc54 | 139 | See BUGS. |
92b1a2cc | 140 | .TP |
92d67001 | 141 | .B ENOTSUP |
9ee4a2b6 | 142 | Extended attributes are not supported by the filesystem, or are disabled. |
92d67001 MK |
143 | .TP |
144 | .B ERANGE | |
145 | The | |
fea681da MK |
146 | .I size |
147 | of the | |
148 | .I list | |
92d67001 | 149 | buffer is too small to hold the result. |
fea681da | 150 | .PP |
92d67001 | 151 | In addition, the errors documented in |
fea681da | 152 | .BR stat (2) |
92d67001 | 153 | can also occur. |
570dd978 MK |
154 | .SH VERSIONS |
155 | These system calls have been available on Linux since kernel 2.4; | |
156 | glibc support is provided since version 2.3. | |
47297adb | 157 | .SH CONFORMING TO |
8382f16d | 158 | These system calls are Linux-specific. |
dc919d09 MK |
159 | .\" .SH AUTHORS |
160 | .\" Andreas Gruenbacher, | |
161 | .\" .RI < a.gruenbacher@computer.org > | |
162 | .\" and the SGI XFS development team, | |
163 | .\" .RI < linux-xfs@oss.sgi.com >. | |
164 | .\" Please send any bug reports or comments to these addresses. | |
7993869b MK |
165 | .SH BUGS |
166 | .\" The xattr(7) page refers to this text: | |
167 | As noted in | |
168 | .BR xattr (7), | |
c4b7e5ac | 169 | the VFS imposes a limit of 64\ kB on the size of the extended |
7993869b MK |
170 | attribute name list returned by |
171 | .BR listxattr (7). | |
172 | If the total size of attribute names attached to a file exceeds this limit, | |
173 | it is no longer possible to retrieve the list of attribute names. | |
636a3877 HS |
174 | .SH EXAMPLE |
175 | The following program demonstrates the usage of | |
176 | .BR listxattr () | |
177 | and | |
178 | .BR getxattr (2). | |
9c321ba2 MK |
179 | For the file whose pathname is provided as a command-line argument, |
180 | it lists all extended file attributes and their values. | |
efeece04 | 181 | .PP |
9c321ba2 MK |
182 | To keep the code simple, the program assumes that attribute keys and |
183 | values are constant during the execution of the program. | |
184 | A production program should expect and handle changes during | |
185 | execution of the program. | |
a8215524 MK |
186 | For example, |
187 | the number of bytes required for attribute keys | |
188 | might increase between the two calls to | |
189 | .BR listxattr (). | |
190 | An application could handle this possibility using | |
191 | a loop that retries the call | |
192 | (perhaps up to a predetermined maximum number of attempts) | |
193 | with a larger buffer each time it fails with the error | |
194 | .BR ERANGE . | |
195 | Calls to | |
196 | .BR getxattr (2) | |
197 | could be handled similarly. | |
efeece04 | 198 | .PP |
9c321ba2 MK |
199 | The following output was recorded by first creating a file, setting |
200 | some extended file attributes, | |
201 | and then listing the attributes with the example program. | |
636a3877 HS |
202 | .SS Example output |
203 | .in +4n | |
b8302363 | 204 | .EX |
9c321ba2 MK |
205 | $ \fBtouch /tmp/foo\fP |
206 | $ \fBsetfattr -n user.fred -v chocolate /tmp/foo\fP | |
207 | $ \fBsetfattr -n user.frieda -v bar /tmp/foo\fP | |
208 | $ \fBsetfattr -n user.empty /tmp/foo\fP | |
209 | $ \fB./listxattr /tmp/foo\fP | |
636a3877 HS |
210 | user.fred: chocolate |
211 | user.frieda: bar | |
212 | user.empty: <no value> | |
b8302363 | 213 | .EE |
636a3877 | 214 | .in |
9c321ba2 | 215 | .SS Program source (listxattr.c) |
408731d4 | 216 | .EX |
636a3877 HS |
217 | #include <malloc.h> |
218 | #include <stdio.h> | |
219 | #include <stdlib.h> | |
220 | #include <string.h> | |
221 | #include <sys/types.h> | |
222 | #include <sys/xattr.h> | |
223 | ||
224 | int | |
225 | main(int argc, char *argv[]) | |
226 | { | |
227 | ssize_t buflen, keylen, vallen; | |
228 | char *buf, *key, *val; | |
229 | ||
230 | if (argc != 2) { | |
d1a71985 | 231 | fprintf(stderr, "Usage: %s path\en", argv[0]); |
636a3877 HS |
232 | exit(EXIT_FAILURE); |
233 | } | |
234 | ||
235 | /* | |
236 | * Determine the length of the buffer needed. | |
237 | */ | |
238 | buflen = listxattr(argv[1], NULL, 0); | |
239 | if (buflen == \-1) { | |
240 | perror("listxattr"); | |
241 | exit(EXIT_FAILURE); | |
242 | } | |
243 | if (buflen == 0) { | |
d1a71985 | 244 | printf("%s has no attributes.\en", argv[1]); |
636a3877 HS |
245 | exit(EXIT_SUCCESS); |
246 | } | |
247 | ||
248 | /* | |
249 | * Allocate the buffer. | |
250 | */ | |
251 | buf = malloc(buflen); | |
252 | if (buf == NULL) { | |
253 | perror("malloc"); | |
254 | exit(EXIT_FAILURE); | |
255 | } | |
256 | ||
257 | /* | |
258 | * Copy the list of attribute keys to the buffer. | |
259 | */ | |
260 | buflen = listxattr(argv[1], buf, buflen); | |
261 | if (buflen == \-1) { | |
262 | perror("listxattr"); | |
263 | exit(EXIT_FAILURE); | |
264 | } | |
265 | ||
266 | /* | |
9c321ba2 MK |
267 | * Loop over the list of zero terminated strings with the |
268 | * attribute keys. Use the remaining buffer length to determine | |
269 | * the end of the list. | |
636a3877 HS |
270 | */ |
271 | key = buf; | |
272 | while (buflen > 0) { | |
273 | ||
274 | /* | |
275 | * Output attribute key. | |
276 | */ | |
277 | printf("%s: ", key); | |
278 | ||
279 | /* | |
280 | * Determine length of the value. | |
281 | */ | |
282 | vallen = getxattr(argv[1], key, NULL, 0); | |
283 | if (vallen == \-1) | |
284 | perror("getxattr"); | |
285 | ||
286 | if (vallen > 0) { | |
287 | ||
288 | /* | |
289 | * Allocate value buffer. | |
290 | * One extra byte is needed to append 0x00. | |
291 | */ | |
292 | val = malloc(vallen + 1); | |
293 | if (val == NULL) { | |
294 | perror("malloc"); | |
295 | exit(EXIT_FAILURE); | |
296 | } | |
297 | ||
298 | /* | |
299 | * Copy value to buffer. | |
300 | */ | |
301 | vallen = getxattr(argv[1], key, val, vallen); | |
302 | if (vallen == \-1) | |
303 | perror("getxattr"); | |
304 | else { | |
305 | /* | |
306 | * Output attribute value. | |
307 | */ | |
308 | val[vallen] = 0; | |
309 | printf("%s", val); | |
310 | } | |
311 | ||
312 | free(val); | |
313 | } else if (vallen == 0) | |
314 | printf("<no value>"); | |
315 | ||
d1a71985 | 316 | printf("\en"); |
636a3877 HS |
317 | |
318 | /* | |
319 | * Forward to next attribute key. | |
320 | */ | |
321 | keylen = strlen(key) + 1; | |
322 | buflen \-= keylen; | |
323 | key += keylen; | |
324 | } | |
325 | ||
326 | free(buf); | |
327 | exit(EXIT_SUCCESS); | |
328 | } | |
408731d4 | 329 | .EE |
fea681da MK |
330 | .SH SEE ALSO |
331 | .BR getfattr (1), | |
332 | .BR setfattr (1), | |
333 | .BR getxattr (2), | |
334 | .BR open (2), | |
335 | .BR removexattr (2), | |
336 | .BR setxattr (2), | |
337 | .BR stat (2), | |
89fabe2e MK |
338 | .BR symlink (7), |
339 | .BR xattr (7) |