xref: /third_party/musl/src/misc/nftw.c (revision 570af302)
1#include <ftw.h>
2#include <dirent.h>
3#ifndef __LITEOS_A__
4#include <fcntl.h>
5#endif
6#include <sys/stat.h>
7#include <errno.h>
8#include <unistd.h>
9#include <string.h>
10#include <limits.h>
11#include <pthread.h>
12
13struct history
14{
15	struct history *chain;
16	dev_t dev;
17	ino_t ino;
18	int level;
19	int base;
20};
21
22#undef dirfd
23#define dirfd(d) (*(int *)d)
24
25static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h)
26{
27	size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
28	struct stat st;
29	struct history new;
30	int type;
31	int r;
32#ifndef __LITEOS_A__
33	int dfd;
34	int err;
35#endif
36	struct FTW lev;
37
38	st.st_dev = st.st_ino = 0;
39
40	if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
41		if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
42			type = FTW_SLN;
43		else if (errno != EACCES) return -1;
44		else type = FTW_NS;
45	} else if (S_ISDIR(st.st_mode)) {
46#ifdef __LITEOS_A__
47		if (access(path, R_OK) < 0) {
48			type = FTW_DNR;
49		} else if (flags & FTW_DEPTH) {
50			type = FTW_DP;
51		}
52#else
53		if (flags & FTW_DEPTH) type = FTW_DP;
54#endif
55		else type = FTW_D;
56	} else if (S_ISLNK(st.st_mode)) {
57		if (flags & FTW_PHYS) type = FTW_SL;
58		else type = FTW_SLN;
59	} else {
60		type = FTW_F;
61	}
62
63	if ((flags & FTW_MOUNT) && h && type != FTW_NS && st.st_dev != h->dev)
64		return 0;
65
66	new.chain = h;
67	new.dev = st.st_dev;
68	new.ino = st.st_ino;
69	new.level = h ? h->level+1 : 0;
70	new.base = j+1;
71
72	lev.level = new.level;
73	if (h) {
74		lev.base = h->base;
75	} else {
76		size_t k;
77		for (k=j; k && path[k]=='/'; k--);
78		for (; k && path[k-1]!='/'; k--);
79		lev.base = k;
80	}
81
82#ifndef __LITEOS_A__
83	if (type == FTW_D || type == FTW_DP) {
84		dfd = open(path, O_RDONLY);
85		err = errno;
86		if (dfd < 0 && err == EACCES) type = FTW_DNR;
87		if (!fd_limit) close(dfd);
88	}
89#endif
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#ifdef __LITEOS_A__
100		DIR *d = opendir(path);
101#else
102		if (dfd < 0) {
103			errno = err;
104			return -1;
105		}
106		DIR *d = fdopendir(dfd);
107#endif
108		if (d) {
109			struct dirent *de;
110			while ((de = readdir(d))) {
111				if (de->d_name[0] == '.'
112				 && (!de->d_name[1]
113				  || (de->d_name[1]=='.'
114				   && !de->d_name[2]))) continue;
115				if (strlen(de->d_name) >= PATH_MAX-l) {
116					errno = ENAMETOOLONG;
117					closedir(d);
118					return -1;
119				}
120				path[j]='/';
121				strcpy(path+j+1, de->d_name);
122				if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
123					closedir(d);
124					return r;
125				}
126			}
127			closedir(d);
128#ifdef __LITEOS_A__
129		} else if (errno != EACCES) {
130			return -1;
131		}
132#else
133		} else {
134			close(dfd);
135			return -1;
136		}
137#endif
138	}
139
140	path[l] = 0;
141	if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
142		return r;
143
144	return 0;
145}
146
147int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
148{
149	int r, cs;
150	size_t l;
151	char pathbuf[PATH_MAX+1];
152
153	if (fd_limit <= 0) return 0;
154
155	l = strlen(path);
156	if (l > PATH_MAX) {
157		errno = ENAMETOOLONG;
158		return -1;
159	}
160	memcpy(pathbuf, path, l+1);
161
162	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
163	r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
164	pthread_setcancelstate(cs, 0);
165	return r;
166}
167
168weak_alias(nftw, nftw64);
169