1FTW(3) Linux Programmer's Manual FTW(3)
2
3
4
6 ftw, nftw - file tree walk
7
9 #include <ftw.h>
10
11 int nftw(const char *dirpath,
12 int (*fn) (const char *fpath, const struct stat *sb,
13 int typeflag, struct FTW *ftwbuf),
14 int nopenfd, int flags);
15
16 #include <ftw.h>
17
18 int ftw(const char *dirpath,
19 int (*fn) (const char *fpath, const struct stat *sb,
20 int typeflag),
21 int nopenfd);
22
23 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
24
25 nftw(): _XOPEN_SOURCE >= 500
26
28 nftw() walks through the directory tree that is located under the
29 directory dirpath, and calls fn() once for each entry in the tree. By
30 default, directories are handled before the files and subdirectories
31 they contain (preorder traversal).
32
33 To avoid using up all of the calling process's file descriptors,
34 nopenfd specifies the maximum number of directories that nftw() will
35 hold open simultaneously. When the search depth exceeds this, nftw()
36 will become slower because directories have to be closed and reopened.
37 nftw() uses at most one file descriptor for each level in the directory
38 tree.
39
40 For each entry found in the tree, nftw() calls fn() with four argu‐
41 ments: fpath, sb, typeflag, and ftwbuf. fpath is the pathname of the
42 entry, and is expressed either as a pathname relative to the calling
43 process's current working directory at the time of the call to nftw(),
44 if dirpath was expressed as a relative pathname, or as an absolute
45 pathname, if dirpath was expressed as an absolute pathname. sb is a
46 pointer to the stat structure returned by a call to stat(2) for fpath.
47
48 The typeflag argument passed to fn() is an integer that has one of the
49 following values:
50
51 FTW_F fpath is a regular file.
52
53 FTW_D fpath is a directory.
54
55 FTW_DNR
56 fpath is a directory which can't be read.
57
58 FTW_DP fpath is a directory, and FTW_DEPTH was specified in flags. (If
59 FTW_DEPTH was not specified in flags, then directories will
60 always be visited with typeflag set to FTW_D.) All of the files
61 and subdirectories within fpath have been processed.
62
63 FTW_NS The stat(2) call failed on fpath, which is not a symbolic link.
64 The probable cause for this is that the caller had read permis‐
65 sion on the parent directory, so that the filename fpath could
66 be seen, but did not have execute permission, so that the file
67 could not be reached for stat(2). The contents of the buffer
68 pointed to by sb are undefined.
69
70 FTW_SL fpath is a symbolic link, and FTW_PHYS was set in flags.
71
72 FTW_SLN
73 fpath is a symbolic link pointing to a nonexistent file. (This
74 occurs only if FTW_PHYS is not set.) On most implementations,
75 in this case the sb argument passed to fn() contains information
76 returned by performing lstat(2) on the symbolic link. For the
77 details on Linux, see BUGS.
78
79 The fourth argument (ftwbuf) that nftw() supplies when calling fn() is
80 a pointer to a structure of type FTW:
81
82 struct FTW {
83 int base;
84 int level;
85 };
86
87 base is the offset of the filename (i.e., basename component) in the
88 pathname given in fpath. level is the depth of fpath in the directory
89 tree, relative to the root of the tree (dirpath, which has depth 0).
90
91 To stop the tree walk, fn() returns a nonzero value; this value will
92 become the return value of nftw(). As long as fn() returns 0, nftw()
93 will continue either until it has traversed the entire tree, in which
94 case it will return zero, or until it encounters an error (such as a
95 malloc(3) failure), in which case it will return -1.
96
97 Because nftw() uses dynamic data structures, the only safe way to exit
98 out of a tree walk is to return a nonzero value from fn(). To allow a
99 signal to terminate the walk without causing a memory leak, have the
100 handler set a global flag that is checked by fn(). Don't use
101 longjmp(3) unless the program is going to terminate.
102
103 The flags argument of nftw() is formed by ORing zero or more of the
104 following flags:
105
106 FTW_ACTIONRETVAL (since glibc 2.3.3)
107 If this glibc-specific flag is set, then nftw() handles the
108 return value from fn() differently. fn() should return one of
109 the following values:
110
111 FTW_CONTINUE
112 Instructs nftw() to continue normally.
113
114 FTW_SKIP_SIBLINGS
115 If fn() returns this value, then siblings of the current
116 entry will be skipped, and processing continues in the
117 parent.
118
119 FTW_SKIP_SUBTREE
120 If fn() is called with an entry that is a directory
121 (typeflag is FTW_D), this return value will prevent
122 objects within that directory from being passed as argu‐
123 ments to fn(). nftw() continues processing with the next
124 sibling of the directory.
125
126 FTW_STOP
127 Causes nftw() to return immediately with the return value
128 FTW_STOP.
129
130 Other return values could be associated with new actions in the
131 future; fn() should not return values other than those listed
132 above.
133
134 The feature test macro _GNU_SOURCE must be defined (before
135 including any header files) in order to obtain the definition of
136 FTW_ACTIONRETVAL from <ftw.h>.
137
138 FTW_CHDIR
139 If set, do a chdir(2) to each directory before handling its con‐
140 tents. This is useful if the program needs to perform some
141 action in the directory in which fpath resides. (Specifying
142 this flag has no effect on the pathname that is passed in the
143 fpath argument of fn.)
144
145 FTW_DEPTH
146 If set, do a post-order traversal, that is, call fn() for the
147 directory itself after handling the contents of the directory
148 and its subdirectories. (By default, each directory is handled
149 before its contents.)
150
151 FTW_MOUNT
152 If set, stay within the same filesystem (i.e., do not cross
153 mount points).
154
155 FTW_PHYS
156 If set, do not follow symbolic links. (This is what you want.)
157 If not set, symbolic links are followed, but no file is reported
158 twice.
159
160 If FTW_PHYS is not set, but FTW_DEPTH is set, then the function
161 fn() is never called for a directory that would be a descendant
162 of itself.
163
164 ftw()
165 ftw() is an older function that offers a subset of the functionality of
166 nftw(). The notable differences are as follows:
167
168 * ftw() has no flags argument. It behaves the same as when nftw() is
169 called with flags specified as zero.
170
171 * The callback function, fn(), is not supplied with a fourth argument.
172
173 * The range of values that is passed via the typeflag argument sup‐
174 plied to fn() is smaller: just FTW_F, FTW_D, FTW_DNR, FTW_NS, and
175 (possibly) FTW_SL.
176
178 These functions return 0 on success, and -1 if an error occurs.
179
180 If fn() returns nonzero, then the tree walk is terminated and the value
181 returned by fn() is returned as the result of ftw() or nftw().
182
183 If nftw() is called with the FTW_ACTIONRETVAL flag, then the only
184 nonzero value that should be used by fn() to terminate the tree walk is
185 FTW_STOP, and that value is returned as the result of nftw().
186
188 nftw() is available under glibc since version 2.1.
189
191 For an explanation of the terms used in this section, see
192 attributes(7).
193
194 ┌──────────┬───────────────┬─────────────┐
195 │Interface │ Attribute │ Value │
196 ├──────────┼───────────────┼─────────────┤
197 │nftw() │ Thread safety │ MT-Safe cwd │
198 ├──────────┼───────────────┼─────────────┤
199 │ftw() │ Thread safety │ MT-Safe │
200 └──────────┴───────────────┴─────────────┘
201
203 POSIX.1-2001, POSIX.1-2008, SVr4, SUSv1. POSIX.1-2008 marks ftw() as
204 obsolete.
205
207 POSIX.1-2008 notes that the results are unspecified if fn does not pre‐
208 serve the current working directory.
209
210 The function nftw() and the use of FTW_SL with ftw() were introduced in
211 SUSv1.
212
213 In some implementations (e.g., glibc), ftw() will never use FTW_SL, on
214 other systems FTW_SL occurs only for symbolic links that do not point
215 to an existing file, and again on other systems ftw() will use FTW_SL
216 for each symbolic link. If fpath is a symbolic link and stat(2)
217 failed, POSIX.1-2008 states that it is undefined whether FTW_NS or
218 FTW_SL is passed in typeflag. For predictable results, use nftw().
219
221 In the specification of nftw(), POSIX.1 notes that when FTW_NS is
222 passed as the typeflag argument of fn(), then the contents of the buf‐
223 fer pointed to by the sb argument are undefined. The standard makes no
224 such statement for the case where FTW_SLN is passed in typeflag, with
225 the implication that the contents of the buffer pointed to by sb are
226 defined. And indeed this is the case on most implementations: the buf‐
227 fer pointed to by sb contains the results produced by applying lstat(2)
228 to the symbolic link. In early glibc, the behavior was the same. How‐
229 ever, since glibc 2.4, the contents of the buffer pointed to by sb are
230 undefined when FTW_SLN is passed in typeflag. This change appears to
231 be an unintended regression, but it is not (yet) clear if the behavior
232 will be restored to that provided in the original glibc implementation
233 (and on other implementations).
234
236 The following program traverses the directory tree under the path named
237 in its first command-line argument, or under the current directory if
238 no argument is supplied. It displays various information about each
239 file. The second command-line argument can be used to specify charac‐
240 ters that control the value assigned to the flags argument when calling
241 nftw().
242
243 Program source
244
245 #define _XOPEN_SOURCE 500
246 #include <ftw.h>
247 #include <stdio.h>
248 #include <stdlib.h>
249 #include <string.h>
250 #include <stdint.h>
251
252 static int
253 display_info(const char *fpath, const struct stat *sb,
254 int tflag, struct FTW *ftwbuf)
255 {
256 printf("%-3s %2d ",
257 (tflag == FTW_D) ? "d" : (tflag == FTW_DNR) ? "dnr" :
258 (tflag == FTW_DP) ? "dp" : (tflag == FTW_F) ? "f" :
259 (tflag == FTW_NS) ? "ns" : (tflag == FTW_SL) ? "sl" :
260 (tflag == FTW_SLN) ? "sln" : "???",
261 ftwbuf->level);
262
263 if (tflag == FTW_NS)
264 printf("-------");
265 else
266 printf("%7jd", (intmax_t) sb->st_size);
267
268 printf(" %-40s %d %s\n",
269 fpath, ftwbuf->base, fpath + ftwbuf->base);
270
271 return 0; /* To tell nftw() to continue */
272 }
273
274 int
275 main(int argc, char *argv[])
276 {
277 int flags = 0;
278
279 if (argc > 2 && strchr(argv[2], 'd') != NULL)
280 flags |= FTW_DEPTH;
281 if (argc > 2 && strchr(argv[2], 'p') != NULL)
282 flags |= FTW_PHYS;
283
284 if (nftw((argc < 2) ? "." : argv[1], display_info, 20, flags)
285 == -1) {
286 perror("nftw");
287 exit(EXIT_FAILURE);
288 }
289
290 exit(EXIT_SUCCESS);
291 }
292
294 stat(2), fts(3), readdir(3)
295
297 This page is part of release 5.04 of the Linux man-pages project. A
298 description of the project, information about reporting bugs, and the
299 latest version of this page, can be found at
300 https://www.kernel.org/doc/man-pages/.
301
302
303
304Linux 2019-03-06 FTW(3)