446 lines
16 KiB
HTML
446 lines
16 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of LISTXATTR</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>LISTXATTR</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
listxattr, llistxattr, flistxattr - list extended attribute names
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/sys/xattr.h">sys/xattr.h</A>></B>
|
|
|
|
<B>ssize_t listxattr(const char *</B><I>path</I><B>, char *</B><I>list</I><B>, size_t </B><I>size</I><B>);</B>
|
|
<B>ssize_t llistxattr(const char *</B><I>path</I><B>, char *</B><I>list</I><B>, size_t </B><I>size</I><B>);</B>
|
|
<B>ssize_t flistxattr(int </B><I>fd</I><B>, char *</B><I>list</I><B>, size_t </B><I>size</I><B>);</B>
|
|
</PRE>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
Extended attributes are
|
|
<I>name</I>:<I>value</I>
|
|
|
|
pairs associated with inodes (files, directories, symbolic links, etc.).
|
|
They are extensions to the normal attributes which are associated
|
|
with all inodes in the system (i.e., the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2)
|
|
|
|
data).
|
|
A complete overview of extended attributes concepts can be found in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+xattr">xattr</A></B>(7).
|
|
|
|
<P>
|
|
|
|
<B>listxattr</B>()
|
|
|
|
retrieves the list
|
|
of extended attribute names associated with the given
|
|
<I>path</I>
|
|
|
|
in the filesystem.
|
|
The retrieved list is placed in
|
|
<I>list</I>,
|
|
|
|
a caller-allocated buffer whose size (in bytes) is specified in the argument
|
|
<I>size</I>.
|
|
|
|
The list is the set of (null-terminated) names, one after the other.
|
|
Names of extended attributes to which the calling process does not
|
|
have access may be omitted from the list.
|
|
The length of the attribute name
|
|
<I>list</I>
|
|
|
|
is returned.
|
|
<P>
|
|
|
|
<B>llistxattr</B>()
|
|
|
|
is identical to
|
|
<B>listxattr</B>(),
|
|
|
|
except in the case of a symbolic link, where the list of names of
|
|
extended attributes associated with the link itself is retrieved,
|
|
not the file that it refers to.
|
|
<P>
|
|
|
|
<B>flistxattr</B>()
|
|
|
|
is identical to
|
|
<B>listxattr</B>(),
|
|
|
|
only the open file referred to by
|
|
<I>fd</I>
|
|
|
|
(as returned by
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2))
|
|
|
|
is interrogated in place of
|
|
<I>path</I>.
|
|
|
|
<P>
|
|
|
|
A single extended attribute
|
|
<I>name</I>
|
|
|
|
is a null-terminated string.
|
|
The name includes a namespace prefix; there may be several, disjoint
|
|
namespaces associated with an individual inode.
|
|
<P>
|
|
|
|
If
|
|
<I>size</I>
|
|
|
|
is specified as zero, these calls return the current size of the
|
|
list of extended attribute names (and leave
|
|
<I>list</I>
|
|
|
|
unchanged).
|
|
This can be used to determine the size of the buffer that
|
|
should be supplied in a subsequent call.
|
|
(But, bear in mind that there is a possibility that the
|
|
set of extended attributes may change between the two calls,
|
|
so that it is still necessary to check the return status
|
|
from the second call.)
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Example</H3>
|
|
|
|
The
|
|
<I>list</I>
|
|
|
|
of names is returned as an unordered array of null-terminated character
|
|
strings (attribute names are separated by null bytes ('\0')), like this:
|
|
<P>
|
|
|
|
|
|
|
|
user.name1\0system.name1\0user.name2\0
|
|
|
|
|
|
<P>
|
|
|
|
Filesystems that implement POSIX ACLs using
|
|
extended attributes might return a
|
|
<I>list</I>
|
|
|
|
like this:
|
|
<P>
|
|
|
|
|
|
|
|
system.posix_acl_access\0system.posix_acl_default\0
|
|
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, a nonnegative number is returned indicating the size of the
|
|
extended attribute name list.
|
|
On failure, -1 is returned and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="1"><B>E2BIG</B>
|
|
|
|
<DD>
|
|
The size of the list of extended attribute names is larger than the maximum
|
|
size allowed; the list cannot be retrieved.
|
|
This can happen on filesystems that support an unlimited number of
|
|
extended attributes per file such as XFS, for example.
|
|
See BUGS.
|
|
<DT id="2"><B>ENOTSUP</B>
|
|
|
|
<DD>
|
|
Extended attributes are not supported by the filesystem, or are disabled.
|
|
<DT id="3"><B>ERANGE</B>
|
|
|
|
<DD>
|
|
The
|
|
<I>size</I>
|
|
|
|
of the
|
|
<I>list</I>
|
|
|
|
buffer is too small to hold the result.
|
|
</DL>
|
|
<P>
|
|
|
|
In addition, the errors documented in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2)
|
|
|
|
can also occur.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
These system calls have been available on Linux since kernel 2.4;
|
|
glibc support is provided since version 2.3.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
These system calls are Linux-specific.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
|
|
As noted in
|
|
<B><A HREF="/cgi-bin/man/man2html?7+xattr">xattr</A></B>(7),
|
|
|
|
the VFS imposes a limit of 64 kB on the size of the extended
|
|
attribute name list returned by
|
|
<B><A HREF="/cgi-bin/man/man2html?7+listxattr">listxattr</A></B>(7).
|
|
|
|
If the total size of attribute names attached to a file exceeds this limit,
|
|
it is no longer possible to retrieve the list of attribute names.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The following program demonstrates the usage of
|
|
<B>listxattr</B>()
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getxattr">getxattr</A></B>(2).
|
|
|
|
For the file whose pathname is provided as a command-line argument,
|
|
it lists all extended file attributes and their values.
|
|
<P>
|
|
|
|
To keep the code simple, the program assumes that attribute keys and
|
|
values are constant during the execution of the program.
|
|
A production program should expect and handle changes during
|
|
execution of the program.
|
|
For example,
|
|
the number of bytes required for attribute keys
|
|
might increase between the two calls to
|
|
<B>listxattr</B>().
|
|
|
|
An application could handle this possibility using
|
|
a loop that retries the call
|
|
(perhaps up to a predetermined maximum number of attempts)
|
|
with a larger buffer each time it fails with the error
|
|
<B>ERANGE</B>.
|
|
|
|
Calls to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getxattr">getxattr</A></B>(2)
|
|
|
|
could be handled similarly.
|
|
<P>
|
|
|
|
The following output was recorded by first creating a file, setting
|
|
some extended file attributes,
|
|
and then listing the attributes with the example program.
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Example output</H3>
|
|
|
|
|
|
|
|
$ <B>touch /tmp/foo</B>
|
|
$ <B>setfattr -n user.fred -v chocolate /tmp/foo</B>
|
|
$ <B>setfattr -n user.frieda -v bar /tmp/foo</B>
|
|
$ <B>setfattr -n user.empty /tmp/foo</B>
|
|
$ <B>./listxattr /tmp/foo</B>
|
|
user.fred: chocolate
|
|
user.frieda: bar
|
|
user.empty: <no value>
|
|
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H3>Program source (listxattr.c)</H3>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/malloc.h">malloc.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/xattr.h">sys/xattr.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> ssize_t buflen, keylen, vallen;
|
|
<BR> char *buf, *key, *val;
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s path\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Determine the length of the buffer needed.
|
|
<BR> */
|
|
<BR> buflen = listxattr(argv[1], NULL, 0);
|
|
<BR> if (buflen == -1) {
|
|
<BR> perror("listxattr");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<BR> if (buflen == 0) {
|
|
<BR> printf("%s has no attributes.\n", argv[1]);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
<BR> }
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Allocate the buffer.
|
|
<BR> */
|
|
<BR> buf = malloc(buflen);
|
|
<BR> if (buf == NULL) {
|
|
<BR> perror("malloc");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Copy the list of attribute keys to the buffer.
|
|
<BR> */
|
|
<BR> buflen = listxattr(argv[1], buf, buflen);
|
|
<BR> if (buflen == -1) {
|
|
<BR> perror("listxattr");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Loop over the list of zero terminated strings with the
|
|
<BR> * attribute keys. Use the remaining buffer length to determine
|
|
<BR> * the end of the list.
|
|
<BR> */
|
|
<BR> key = buf;
|
|
<BR> while (buflen > 0) {
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Output attribute key.
|
|
<BR> */
|
|
<BR> printf("%s: ", key);
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Determine length of the value.
|
|
<BR> */
|
|
<BR> vallen = getxattr(argv[1], key, NULL, 0);
|
|
<BR> if (vallen == -1)
|
|
<BR> perror("getxattr");
|
|
<P>
|
|
<BR> if (vallen > 0) {
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Allocate value buffer.
|
|
<BR> * One extra byte is needed to append 0x00.
|
|
<BR> */
|
|
<BR> val = malloc(vallen + 1);
|
|
<BR> if (val == NULL) {
|
|
<BR> perror("malloc");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Copy value to buffer.
|
|
<BR> */
|
|
<BR> vallen = getxattr(argv[1], key, val, vallen);
|
|
<BR> if (vallen == -1)
|
|
<BR> perror("getxattr");
|
|
<BR> else {
|
|
<BR> /*
|
|
<BR> * Output attribute value.
|
|
<BR> */
|
|
<BR> val[vallen] = 0;
|
|
<BR> printf("%s", val);
|
|
<BR> }
|
|
<P>
|
|
<BR> free(val);
|
|
<BR> } else if (vallen == 0)
|
|
<BR> printf("<no value>");
|
|
<P>
|
|
<BR> printf("\n");
|
|
<P>
|
|
<BR> /*
|
|
<BR> * Forward to next attribute key.
|
|
<BR> */
|
|
<BR> keylen = strlen(key) + 1;
|
|
<BR> buflen -= keylen;
|
|
<BR> key += keylen;
|
|
<BR> }
|
|
<P>
|
|
<BR> free(buf);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+getfattr">getfattr</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+setfattr">setfattr</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getxattr">getxattr</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+removexattr">removexattr</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setxattr">setxattr</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+symlink">symlink</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+xattr">xattr</A></B>(7)
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="4"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="5"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="6"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="7"><A HREF="#lbAE">Example</A><DD>
|
|
</DL>
|
|
<DT id="8"><A HREF="#lbAF">RETURN VALUE</A><DD>
|
|
<DT id="9"><A HREF="#lbAG">ERRORS</A><DD>
|
|
<DT id="10"><A HREF="#lbAH">VERSIONS</A><DD>
|
|
<DT id="11"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="12"><A HREF="#lbAJ">BUGS</A><DD>
|
|
<DT id="13"><A HREF="#lbAK">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="14"><A HREF="#lbAL">Example output</A><DD>
|
|
<DT id="15"><A HREF="#lbAM">Program source (listxattr.c)</A><DD>
|
|
</DL>
|
|
<DT id="16"><A HREF="#lbAN">SEE ALSO</A><DD>
|
|
<DT id="17"><A HREF="#lbAO">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:33 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|