1 /*
2  * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  * Sponsored in part by the Defense Advanced Research Projects
17  * Agency (DARPA) and Air Force Research Laboratory, Air Force
18  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
19  */
20 
21 #include <errno.h>
22 #include <fts.h>
23 #include <ftw.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 
28 extern "C" FTS* __fts_open(char* const*, int, int (*)(const FTSENT**, const FTSENT**));
29 
do_nftw(const char * path,int (* ftw_fn)(const char *,const struct stat *,int),int (* nftw_fn)(const char *,const struct stat *,int,FTW *),int nfds,int nftw_flags)30 static int do_nftw(const char* path,
31                    int (*ftw_fn)(const char*, const struct stat*, int),
32                    int (*nftw_fn)(const char*, const struct stat*, int, FTW*),
33                    int nfds,
34                    int nftw_flags) {
35   // TODO: nfds is currently unused.
36   if (nfds < 1) {
37     errno = EINVAL;
38     return -1;
39   }
40 
41   // Translate to fts_open options.
42   int fts_options = FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR;
43   if (nftw_fn) {
44     fts_options = FTS_COMFOLLOW | ((nftw_flags & FTW_PHYS) ? FTS_PHYSICAL : FTS_LOGICAL);
45     if (!(nftw_flags & FTW_CHDIR)) fts_options |= FTS_NOCHDIR;
46     if (nftw_flags & FTW_MOUNT) fts_options |= FTS_XDEV;
47   }
48   bool postorder = (nftw_flags & FTW_DEPTH) != 0;
49 
50   // Call fts_open.
51   char* const paths[2] = { const_cast<char*>(path), nullptr };
52   FTS* fts = __fts_open(paths, fts_options | FTS_FOR_FTW, nullptr);
53   if (fts == nullptr) {
54     return -1;
55   }
56 
57   // Translate fts_read results into ftw/nftw callbacks.
58   int error = 0;
59   FTSENT* cur;
60   while (error == 0 && (cur = fts_read(fts)) != nullptr) {
61     int fn_flag;
62     switch (cur->fts_info) {
63       case FTS_D:
64         // In the postorder case, we'll translate FTS_DP to FTW_DP later.
65         // In the can't-access case, we'll translate FTS_DNR to FTW_DNR later.
66         if (postorder || access(cur->fts_path, R_OK) == -1) continue;
67         fn_flag = FTW_D;
68         break;
69       case FTS_DC:
70         // POSIX says nftw "shall not report" directories causing loops (http://b/31152735).
71         continue;
72       case FTS_DNR:
73         fn_flag = FTW_DNR;
74         break;
75       case FTS_DP:
76         if (!postorder) continue;
77         fn_flag = FTW_DP;
78         break;
79       case FTS_F:
80       case FTS_DEFAULT:
81         fn_flag = FTW_F;
82         break;
83       case FTS_NS:
84       case FTS_NSOK:
85         fn_flag = FTW_NS;
86         break;
87       case FTS_SL:
88         fn_flag = FTW_SL;
89         break;
90       case FTS_SLNONE:
91         fn_flag = (nftw_fn != nullptr) ? FTW_SLN : FTW_NS;
92         break;
93       default:
94         error = -1;
95         continue;
96     }
97 
98     // Call the appropriate function.
99     if (nftw_fn != nullptr) {
100       FTW ftw;
101       ftw.base = cur->fts_pathlen - cur->fts_namelen;
102       ftw.level = cur->fts_level;
103       error = nftw_fn(cur->fts_path, cur->fts_statp, fn_flag, &ftw);
104     } else {
105       error = ftw_fn(cur->fts_path, cur->fts_statp, fn_flag);
106     }
107   }
108 
109   int saved_errno = errno;
110   if (fts_close(fts) != 0 && error == 0) {
111     error = -1;
112   } else {
113     errno = saved_errno;
114   }
115   return error;
116 }
117 
ftw(const char * path,int (* ftw_fn)(const char *,const struct stat *,int),int nfds)118 int ftw(const char* path, int (*ftw_fn)(const char*, const struct stat*, int), int nfds) {
119   return do_nftw(path, ftw_fn, nullptr, nfds, 0);
120 }
121 
nftw(const char * path,int (* nftw_fn)(const char *,const struct stat *,int,FTW *),int nfds,int nftw_flags)122 int nftw(const char* path, int (*nftw_fn)(const char*, const struct stat*, int, FTW*),
123          int nfds, int nftw_flags) {
124   return do_nftw(path, nullptr, nftw_fn, nfds, nftw_flags);
125 }
126