2 Copyright (c) 2003 by Joel Baker.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the Author nor the names of any contributors
15 may be used to endorse or promote products derived from this software
16 without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h> /* Because fts(3) says so */
37 #include <unistd.h> /* We want strcpy */
39 #include <cerrno> /* Because errno is our friend */
41 #ifndef __USE_XOPEN_EXTENDED /* We need nftw values, since we implement it */
42 #define __USE_XOPEN_EXTENDED
47 /* I like symbolic values - this is only used in this file. */
54 /* Prototype this so that we can have it later */
56 static int __ftw_core(const char *, void*, int, int, enum __ftw_modes);
59 * The external function calls are really just wrappers around __ftw_core,
60 * since the work they do is 90% the same.
63 int ftw (const char *dir, __ftw_func_t func, int descr) {
64 return __ftw_core(dir, (void*)func, descr, 0, MODE_FTW);
67 int nftw (const char *dir, __nftw_func_t func, int descr, int flags) {
68 return __ftw_core(dir, (void*)func, descr, flags, MODE_NFTW);
72 typedef int (*__ftw_func_t) \
73 (const char *file, const struct stat status, int flag);
74 typedef int (*__nftw_func_t) \
75 (const char *file, const struct stat status, int flag, struct FTW detail);
78 static int __ftw_core(const char *dir, void* func, int descr, int flags,
79 enum __ftw_modes mode) {
84 int ftw_flag = 0, func_ret = 0;
87 __ftw_func_t ftw_func;
88 __nftw_func_t nftw_func;
90 /* We need at least one descriptor to call fts */
97 /* Decide which mode we're running in, and set the FTS options suitably. */
99 if (MODE_NFTW == mode) { /* NFTW mode, with all the bells and whistles. */
100 fts_options = (flags & FTW_PHYS) ? FTS_PHYSICAL : FTS_LOGICAL;
101 fts_options |= (flags & FTW_CHDIR) ? FTS_NOCHDIR : 0;
102 fts_options |= (flags & FTW_MOUNT) ? FTS_XDEV : 0;
103 } else { /* We must be in FTW mode. Nothing else makes sense. */
104 fts_options = FTS_LOGICAL;
107 /* FTW gets a const char *, but FTS expects a null-term array of them. */
109 if (!(paths[0] = (char*) alloca(strlen(dir) + 1))) {
110 errno = ENOMEM; /* This is stack... we probably just died anyway. */
114 strcpy(paths[0], dir);
115 paths[1] = 0; /* null */
117 /* Open the file hierarchy. */
119 if (!(hierarchy = fts_open(paths, fts_options, 0))) {
120 if (EACCES == errno) {
127 /* The main loop. Is it not nifty? Worship the loop. */
130 while ((entry = fts_read(hierarchy))) {
132 std::string path_name = entry->fts_path;
134 switch (entry->fts_info) {
137 if ((MODE_NFTW != mode) || !(flags & FTW_DEPTH)) {
139 path_name = path_name.substr(0, path_name.size() - 1);
164 /* Values that should only occur in nftw mode */
167 if (MODE_NFTW == mode) {
175 if ((MODE_NFTW == mode) && (flags & FTW_DEPTH)) {
183 /* I'm not sure this is right, but we don't have a valid FTW
184 * type to call with, so cowardice seems the better part of
191 if (MODE_FTW == mode) {
192 ftw_func = (__ftw_func_t) func;
193 func_ret = (*ftw_func)
194 (path_name.c_str(), entry->fts_statp, ftw_flag);
195 } else if (MODE_NFTW == mode) {
196 ftw_st.base = (entry->fts_pathlen - entry->fts_namelen);
197 ftw_st.level = entry->fts_level;
199 nftw_func = (__nftw_func_t) func;
200 func_ret = (*nftw_func)
201 (path_name.c_str(), entry->fts_statp, ftw_flag, &ftw_st);
209 if (0 != errno) { /* fts_read returned NULL, and set errno - bail */
213 /* The janitors will be upset if we don't clean up after ourselves. */
215 if (0 != fts_close(hierarchy)) {