man-pages/man3/ftw.3.html
2021-03-31 01:06:50 +01:00

718 lines
17 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of FTW</TITLE>
</HEAD><BODY>
<H1>FTW</H1>
Section: Linux Programmer's Manual (3)<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">&nbsp;</A>
<H2>NAME</H2>
ftw, nftw - file tree walk
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/ftw.h">ftw.h</A>&gt;</B>
<B>int nftw(const char *</B><I>dirpath</I><B>,</B>
<B> int (*</B><I>fn</I><B>) (const char *</B><I>fpath</I><B>, const struct stat *</B><I>sb</I><B>,</B>
<B> int </B><I>typeflag</I><B>, struct FTW *</B><I>ftwbuf</I><B>),</B>
<B> int </B><I>nopenfd</I><B>, int </B><I>flags</I><B>);</B>
<B>#include &lt;<A HREF="file:///usr/include/ftw.h">ftw.h</A>&gt;</B>
<B>int ftw(const char *</B><I>dirpath</I><B>,</B>
<B> int (*</B><I>fn</I><B>) (const char *</B><I>fpath</I><B>, const struct stat *</B><I>sb</I><B>,</B>
<B> int </B><I>typeflag</I><B>),</B>
<B> int </B><I>nopenfd</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>nftw</B>():
_XOPEN_SOURCE &gt;= 500
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>nftw</B>()
walks through the directory tree that is
located under the directory <I>dirpath</I>,
and calls <I>fn</I>() once for each entry in the tree.
By default, directories are handled before the files and
subdirectories they contain (preorder traversal).
<P>
To avoid using up all of the calling process's file descriptors,
<I>nopenfd</I> specifies the maximum number of directories that
<B>nftw</B>()
will hold open simultaneously.
When
the search depth exceeds this,
<B>nftw</B>()
will become slower because
directories have to be closed and reopened.
<B>nftw</B>()
uses at most
one file descriptor for each level in the directory tree.
<P>
For each entry found in the tree,
<B>nftw</B>()
calls
<I>fn</I>() with four arguments:
<I>fpath</I>,
<I>sb</I>,
<I>typeflag</I>,
and
<I>ftwbuf</I>.
<I>fpath</I>
is the pathname of the entry,
and is expressed either as a pathname relative to the calling process's
current working directory at the time of the call to
<B>nftw</B>(),
if
<I>dirpath</I>
was expressed as a relative pathname,
or as an absolute pathname, if
<I>dirpath</I>
was expressed as an absolute pathname.
<I>sb</I>
is a pointer to the
<I>stat</I>
structure returned by a call to
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2)
for
<I>fpath</I>.
<P>
The
<I>typeflag</I>
argument passed to
<I>fn</I>()
is an integer that has one of the following values:
<DL COMPACT>
<DT id="1"><B>FTW_F</B>
<DD>
<I>fpath</I>
is a regular file.
<DT id="2"><B>FTW_D</B>
<DD>
<I>fpath</I>
is a directory.
<DT id="3"><B>FTW_DNR</B>
<DD>
<I>fpath</I>
is a directory which can't be read.
<DT id="4"><B>FTW_DP</B>
<DD>
<I>fpath</I>
is a directory, and <B>FTW_DEPTH</B> was specified in <I>flags</I>.
(If
<B>FTW_DEPTH</B>
was not specified in
<I>flags</I>,
then directories will always be visited with
<I>typeflag</I>
set to
<B>FTW_D</B>.)
All of the files
and subdirectories within <I>fpath</I> have been processed.
<DT id="5"><B>FTW_NS</B>
<DD>
The
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2)
call failed on
<I>fpath</I>,
which is not a symbolic link.
The probable cause for this is that the caller had read permission
on the parent directory, so that the filename
<I>fpath</I>
could be seen,
but did not have execute permission,
so that the file could not be reached for
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2).
The contents of the buffer pointed to by
<I>sb</I>
are undefined.
<DT id="6"><B>FTW_SL</B>
<DD>
<I>fpath</I>
is a symbolic link, and <B>FTW_PHYS</B> was set in <I>flags</I>.
<DT id="7"><B>FTW_SLN</B>
<DD>
<I>fpath</I>
is a symbolic link pointing to a nonexistent file.
(This occurs only if <B>FTW_PHYS</B> is not set.)
On most implementations, in this case the
<I>sb</I>
argument passed to
<I>fn</I>()
contains information returned by performing
<B><A HREF="/cgi-bin/man/man2html?2+lstat">lstat</A></B>(2)
on the symbolic link.
For the details on Linux, see BUGS.
</DL>
<P>
The fourth argument
(<I>ftwbuf</I>)
that
<B>nftw</B>()
supplies when calling
<I>fn</I>()
is a pointer to a structure of type <I>FTW</I>:
<P>
struct FTW {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;base;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;level;
};
<P>
<I>base</I>
is the offset of the filename (i.e., basename component)
in the pathname given in
<I>fpath</I>.
<I>level</I>
is the depth of
<I>fpath</I>
in the directory tree, relative to the root of the tree
(<I>dirpath</I>,
which has depth 0).
<P>
To stop the tree walk, <I>fn</I>() returns a nonzero value; this
value will become the return value of
<B>nftw</B>().
As long as <I>fn</I>() returns 0,
<B>nftw</B>()
will continue either until it has traversed the entire tree,
in which case it will return zero,
or until it encounters an error (such as a
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
failure), in which case it will return -1.
<P>
Because
<B>nftw</B>()
uses dynamic data structures, the only safe way to
exit out of a tree walk is to return a nonzero value from <I>fn</I>().
To allow a signal to terminate the walk without causing a memory leak,
have the handler set a global flag that is checked by <I>fn</I>().
<I>Don't</I> use
<B><A HREF="/cgi-bin/man/man2html?3+longjmp">longjmp</A></B>(3)
unless the program is going to terminate.
<P>
The <I>flags</I> argument of
<B>nftw</B>()
is formed by ORing zero or more of the
following flags:
<DL COMPACT>
<DT id="8"><B>FTW_ACTIONRETVAL</B> (since glibc 2.3.3)
<DD>
If this glibc-specific flag is set, then
<B>nftw</B>()
handles the return value from
<I>fn</I>()
differently.
<I>fn</I>()
should return one of the following values:
<DL COMPACT><DT id="9"><DD>
<DL COMPACT>
<DT id="10"><B>FTW_CONTINUE</B>
<DD>
Instructs
<B>nftw</B>()
to continue normally.
<DT id="11"><B>FTW_SKIP_SIBLINGS</B>
<DD>
If <I>fn</I>() returns this value, then
siblings of the current entry will be skipped,
and processing continues in the parent.
<DT id="12"><B>FTW_SKIP_SUBTREE</B>
<DD>
If <I>fn</I>() is called with an entry that is a directory
(<I>typeflag</I> is <B>FTW_D</B>), this return
value will prevent objects within that directory from being passed as
arguments to <I>fn</I>().
<B>nftw</B>()
continues processing with the next sibling of the directory.
<DT id="13"><B>FTW_STOP</B>
<DD>
Causes
<B>nftw</B>()
to return immediately with the return value
<B>FTW_STOP</B>.
</DL>
<P>
Other return values could be associated with new actions in the future;
<I>fn</I>() should not return values other than those listed above.
<P>
The feature test macro
<B>_GNU_SOURCE</B>
must be defined
(before including
<I>any</I>
header files)
in order to
obtain the definition of <B>FTW_ACTIONRETVAL</B> from <I>&lt;<A HREF="file:///usr/include/ftw.h">ftw.h</A>&gt;</I>.
</DL>
<DT id="14"><B>FTW_CHDIR</B>
<DD>
If set, do a
<B><A HREF="/cgi-bin/man/man2html?2+chdir">chdir</A></B>(2)
to each directory before handling its contents.
This is useful if the program needs to perform some action
in the directory in which <I>fpath</I> resides.
(Specifying this flag has no effect on the pathname that is passed in the
<I>fpath</I>
argument of
<I>fn</I>.)
<DT id="15"><B>FTW_DEPTH</B>
<DD>
If set, do a post-order traversal, that is, call <I>fn</I>() for
the directory itself <I>after</I> handling the contents of the directory
and its subdirectories.
(By default, each directory is handled <I>before</I> its contents.)
<DT id="16"><B>FTW_MOUNT</B>
<DD>
If set, stay within the same filesystem
(i.e., do not cross mount points).
<DT id="17"><B>FTW_PHYS</B>
<DD>
If set, do not follow symbolic links.
(This is what you want.)
If not set, symbolic links are followed, but no file is reported twice.
<DT id="18"><DD>
If <B>FTW_PHYS</B> is not set, but <B>FTW_DEPTH</B> is set,
then the function
<I>fn</I>()
is never called for a directory that would be a descendant of itself.
</DL>
<A NAME="lbAE">&nbsp;</A>
<H3>ftw()</H3>
<B>ftw</B>()
is an older function that offers a subset of the functionality of
<B>nftw</B>().
The notable differences are as follows:
<DL COMPACT>
<DT id="19">*<DD>
<B>ftw</B>()
has no
<I>flags</I>
argument.
It behaves the same as when
<B>nftw</B>()
is called with
<I>flags</I>
specified as zero.
<DT id="20">*<DD>
The callback function,
<I>fn</I>(),
is not supplied with a fourth argument.
<DT id="21">*<DD>
The range of values that is passed via the
<I>typeflag</I>
argument supplied to
<I>fn</I>()
is smaller: just
<B>FTW_F</B>,
<B>FTW_D</B>,
<B>FTW_DNR</B>,
<B>FTW_NS</B>,
and (possibly)
<B>FTW_SL</B>.
</DL>
<A NAME="lbAF">&nbsp;</A>
<H2>RETURN VALUE</H2>
These functions return 0 on success, and -1 if an error occurs.
<P>
If <I>fn</I>() returns nonzero,
then the tree walk is terminated and the value returned by <I>fn</I>()
is returned as the result of
<B>ftw</B>()
or
<B>nftw</B>().
<P>
If
<B>nftw</B>()
is called with the <B>FTW_ACTIONRETVAL</B> flag,
then the only nonzero value that should be used by <I>fn</I>()
to terminate the tree walk is <B>FTW_STOP</B>,
and that value is returned as the result of
<B>nftw</B>().
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
<B>nftw</B>()
is available under glibc since version 2.1.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>nftw</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe cwd<BR></TD></TR>
<TR VALIGN=top><TD>
<B>ftw</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, SVr4, SUSv1.
POSIX.1-2008 marks
<B>ftw</B>()
as obsolete.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
POSIX.1-2008 notes that the results are unspecified if
<I>fn</I>
does not preserve the current working directory.
<P>
The function
<B>nftw</B>()
and the use of <B>FTW_SL</B> with
<B>ftw</B>()
were introduced in SUSv1.
<P>
In some implementations (e.g., glibc),
<B>ftw</B>()
will never use <B>FTW_SL</B>, on other systems <B>FTW_SL</B> occurs only
for symbolic links that do not point to an existing file,
and again on other systems
<B>ftw</B>()
will use <B>FTW_SL</B> for each symbolic link.
If
<I>fpath</I>
is a symbolic link and
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2)
failed, POSIX.1-2008 states
that it is undefined whether <B>FTW_NS</B> or <B>FTW_SL</B>
is passed in
<I>typeflag</I>.
For predictable results, use
<B>nftw</B>().
<A NAME="lbAK">&nbsp;</A>
<H2>BUGS</H2>
In the specification of
<B>nftw</B>(),
POSIX.1 notes that when
<B>FTW_NS</B>
is passed as the
<I>typeflag</I>
argument of
<I>fn</I>(),
then the contents of the buffer pointed to by the
<I>sb</I>
argument are undefined.
The standard makes no such statement for the case where
<B>FTW_SLN</B>
is passed in
<I>typeflag</I>,
with the implication that the contents of the buffer pointed to by
<I>sb</I>
are defined.
And indeed this is the case on most implementations: the buffer pointed to by
<I>sb</I>
contains the results produced by applying
<B><A HREF="/cgi-bin/man/man2html?2+lstat">lstat</A></B>(2)
to the symbolic link.
In early glibc, the behavior was the same.
However, since glibc 2.4, the contents of the buffer pointed to by
<I>sb</I>
are undefined when
<B>FTW_SLN</B>
is passed in
<I>typeflag</I>.
This change
<I>appears</I>
to be an unintended regression,
but it is not (yet) clear if the behavior will be restored to that
provided in the original glibc implementation (and on other implementations).
<A NAME="lbAL">&nbsp;</A>
<H2>EXAMPLE</H2>
The following program traverses the directory tree under the path named
in its first command-line argument, or under the current directory
if no argument is supplied.
It displays various information about each file.
The second command-line argument can be used to specify characters that
control the value assigned to the <I>flags</I>
argument when calling
<B>nftw</B>().
<A NAME="lbAM">&nbsp;</A>
<H3>Program source</H3>
#define _XOPEN_SOURCE 500
#include &lt;<A HREF="file:///usr/include/ftw.h">ftw.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdint.h">stdint.h</A>&gt;
<P>
static int
display_info(const char *fpath, const struct stat *sb,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;tflag,&nbsp;struct&nbsp;FTW&nbsp;*ftwbuf)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%-3s&nbsp;%2d&nbsp;&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(tflag&nbsp;==&nbsp;FTW_D)&nbsp;?&nbsp;&nbsp;&nbsp;&quot;d&quot;&nbsp;&nbsp;&nbsp;:&nbsp;(tflag&nbsp;==&nbsp;FTW_DNR)&nbsp;?&nbsp;&quot;dnr&quot;&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(tflag&nbsp;==&nbsp;FTW_DP)&nbsp;?&nbsp;&nbsp;&quot;dp&quot;&nbsp;&nbsp;:&nbsp;(tflag&nbsp;==&nbsp;FTW_F)&nbsp;?&nbsp;&nbsp;&nbsp;&quot;f&quot;&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(tflag&nbsp;==&nbsp;FTW_NS)&nbsp;?&nbsp;&nbsp;&quot;ns&quot;&nbsp;&nbsp;:&nbsp;(tflag&nbsp;==&nbsp;FTW_SL)&nbsp;?&nbsp;&nbsp;&quot;sl&quot;&nbsp;:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(tflag&nbsp;==&nbsp;FTW_SLN)&nbsp;?&nbsp;&quot;sln&quot;&nbsp;:&nbsp;&quot;???&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ftwbuf-&gt;level);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(tflag&nbsp;==&nbsp;FTW_NS)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;-------&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%7jd&quot;,&nbsp;(intmax_t)&nbsp;sb-&gt;st_size);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;&nbsp;&nbsp;&nbsp;%-40s&nbsp;%d&nbsp;%s\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fpath,&nbsp;ftwbuf-&gt;base,&nbsp;fpath&nbsp;+&nbsp;ftwbuf-&gt;base);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;To&nbsp;tell&nbsp;nftw()&nbsp;to&nbsp;continue&nbsp;*/
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;flags&nbsp;=&nbsp;0;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&gt;&nbsp;2&nbsp;&amp;&amp;&nbsp;strchr(argv[2],&nbsp;'d')&nbsp;!=&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flags&nbsp;|=&nbsp;FTW_DEPTH;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&gt;&nbsp;2&nbsp;&amp;&amp;&nbsp;strchr(argv[2],&nbsp;'p')&nbsp;!=&nbsp;NULL)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flags&nbsp;|=&nbsp;FTW_PHYS;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(nftw((argc&nbsp;&lt;&nbsp;2)&nbsp;?&nbsp;&quot;.&quot;&nbsp;:&nbsp;argv[1],&nbsp;display_info,&nbsp;20,&nbsp;flags)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;nftw&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAN">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+fts">fts</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+readdir">readdir</A></B>(3)
<A NAME="lbAO">&nbsp;</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">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="22"><A HREF="#lbAB">NAME</A><DD>
<DT id="23"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="24"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="25"><A HREF="#lbAE">ftw()</A><DD>
</DL>
<DT id="26"><A HREF="#lbAF">RETURN VALUE</A><DD>
<DT id="27"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="28"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="29"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="30"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="31"><A HREF="#lbAK">BUGS</A><DD>
<DT id="32"><A HREF="#lbAL">EXAMPLE</A><DD>
<DL>
<DT id="33"><A HREF="#lbAM">Program source</A><DD>
</DL>
<DT id="34"><A HREF="#lbAN">SEE ALSO</A><DD>
<DT id="35"><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:43 GMT, March 31, 2021
</BODY>
</HTML>