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