]> git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/misc/nftw.c
Define _ALL_SOURCE when generating predefined-macros.txt.
[wasi-libc.git] / libc-top-half / musl / src / misc / nftw.c
1 #include <ftw.h>
2 #include <dirent.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <limits.h>
8 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
9 #include <pthread.h>
10 #endif
11
12 struct history
13 {
14 struct history *chain;
15 dev_t dev;
16 ino_t ino;
17 int level;
18 int base;
19 };
20
21 #undef dirfd
22 #define dirfd(d) (*(int *)d)
23
24 static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h)
25 {
26 size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
27 struct stat st;
28 struct history new;
29 int type;
30 int r;
31 struct FTW lev;
32
33 if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
34 if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
35 type = FTW_SLN;
36 else if (errno != EACCES) return -1;
37 else type = FTW_NS;
38 } else if (S_ISDIR(st.st_mode)) {
39 if (access(path, R_OK) < 0) type = FTW_DNR;
40 else 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 (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
69 return r;
70
71 for (; h; h = h->chain)
72 if (h->dev == st.st_dev && h->ino == st.st_ino)
73 return 0;
74
75 if ((type == FTW_D || type == FTW_DP) && fd_limit) {
76 DIR *d = opendir(path);
77 if (d) {
78 struct dirent *de;
79 while ((de = readdir(d))) {
80 if (de->d_name[0] == '.'
81 && (!de->d_name[1]
82 || (de->d_name[1]=='.'
83 && !de->d_name[2]))) continue;
84 if (strlen(de->d_name) >= PATH_MAX-l) {
85 errno = ENAMETOOLONG;
86 closedir(d);
87 return -1;
88 }
89 path[j]='/';
90 strcpy(path+j+1, de->d_name);
91 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
92 closedir(d);
93 return r;
94 }
95 }
96 closedir(d);
97 } else if (errno != EACCES) {
98 return -1;
99 }
100 }
101
102 path[l] = 0;
103 if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
104 return r;
105
106 return 0;
107 }
108
109 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
110 {
111 int r, cs;
112 size_t l;
113 char pathbuf[PATH_MAX+1];
114
115 if (fd_limit <= 0) return 0;
116
117 l = strlen(path);
118 if (l > PATH_MAX) {
119 errno = ENAMETOOLONG;
120 return -1;
121 }
122 memcpy(pathbuf, path, l+1);
123
124 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
125 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
126 #endif
127 r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
128 #if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
129 pthread_setcancelstate(cs, 0);
130 #endif
131 return r;
132 }
133
134 weak_alias(nftw, nftw64);