1 #ifndef HAVE_OPENDIR
2
3 /*
4 * Copyright © 2005-2020 Rich Felker, et al.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <ftw.h>
27 #include <dirent.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <pthread.h>
34 /* Pull in _STDIO_WITH_THREAD_CANCELLATION_SUPPORT */
35 #include "../stdio/local.h"
36
37 struct history
38 {
39 struct history *chain;
40 dev_t dev;
41 ino_t ino;
42 int level;
43 int base;
44 };
45
46 #undef dirfd
47 #define dirfd(d) (*(int *)d)
48
do_nftw(char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int fd_limit,int flags,struct history * h)49 static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h)
50 {
51 size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
52 struct stat st;
53 struct history new;
54 int type;
55 int r;
56 struct FTW lev;
57
58 if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
59 if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
60 type = FTW_SLN;
61 else if (errno != EACCES) return -1;
62 else type = FTW_NS;
63 } else if (S_ISDIR(st.st_mode)) {
64 if (access(path, R_OK) < 0) type = FTW_DNR;
65 else if (flags & FTW_DEPTH) type = FTW_DP;
66 else type = FTW_D;
67 } else if (S_ISLNK(st.st_mode)) {
68 if (flags & FTW_PHYS) type = FTW_SL;
69 else type = FTW_SLN;
70 } else {
71 type = FTW_F;
72 }
73
74 if ((flags & FTW_MOUNT) && h && st.st_dev != h->dev)
75 return 0;
76
77 new.chain = h;
78 new.dev = st.st_dev;
79 new.ino = st.st_ino;
80 new.level = h ? h->level+1 : 0;
81 new.base = j+1;
82
83 lev.level = new.level;
84 if (h) {
85 lev.base = h->base;
86 } else {
87 size_t k;
88 for (k=j; k && path[k]=='/'; k--);
89 for (; k && path[k-1]!='/'; k--);
90 lev.base = k;
91 }
92
93 if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
94 return r;
95
96 for (; h; h = h->chain)
97 if (h->dev == st.st_dev && h->ino == st.st_ino)
98 return 0;
99
100 if ((type == FTW_D || type == FTW_DP) && fd_limit) {
101 DIR *d = opendir(path);
102 if (d) {
103 struct dirent *de;
104 while ((de = readdir(d))) {
105 if (de->d_name[0] == '.'
106 && (!de->d_name[1]
107 || (de->d_name[1]=='.'
108 && !de->d_name[2]))) continue;
109 if (strlen(de->d_name) >= PATH_MAX-l) {
110 errno = ENAMETOOLONG;
111 closedir(d);
112 return -1;
113 }
114 path[j]='/';
115 strcpy(path+j+1, de->d_name);
116 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
117 closedir(d);
118 return r;
119 }
120 }
121 closedir(d);
122 } else if (errno != EACCES) {
123 return -1;
124 }
125 }
126
127 path[l] = 0;
128 if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
129 return r;
130
131 return 0;
132 }
133
nftw(const char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int fd_limit,int flags)134 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
135 {
136 int r, cs;
137 size_t l;
138 char pathbuf[PATH_MAX+1];
139
140 if (fd_limit <= 0) return 0;
141
142 l = strlen(path);
143 if (l > PATH_MAX) {
144 errno = ENAMETOOLONG;
145 return -1;
146 }
147 memcpy(pathbuf, path, l+1);
148
149 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
150 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
151 #endif
152 r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
153 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
154 pthread_setcancelstate(cs, 0);
155 #endif
156 return r;
157 }
158
159 #endif /* ! HAVE_OPENDIR */
160