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