| 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 |
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) |
| 10 |
#include <pthread.h> |
| 11 |
#endif |
| 12 |
|
| 13 |
struct 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 |
|
| 25 |
static 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 |
int dfd; |
| 33 |
int err; |
| 34 |
struct FTW lev; |
| 35 |
|
| 36 |
st.st_dev = st.st_ino = 0; |
| 37 |
|
| 38 |
if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) { |
| 39 |
if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st)) |
| 40 |
type = FTW_SLN; |
| 41 |
else if (errno != EACCES) return -1; |
| 42 |
else type = FTW_NS; |
| 43 |
} else if (S_ISDIR(st.st_mode)) { |
| 44 |
if (flags & FTW_DEPTH) type = FTW_DP; |
| 45 |
else type = FTW_D; |
| 46 |
} else if (S_ISLNK(st.st_mode)) { |
| 47 |
if (flags & FTW_PHYS) type = FTW_SL; |
| 48 |
else type = FTW_SLN; |
| 49 |
} else { |
| 50 |
type = FTW_F; |
| 51 |
} |
| 52 |
|
| 53 |
if ((flags & FTW_MOUNT) && h && type != FTW_NS && st.st_dev != h->dev) |
| 54 |
return 0; |
| 55 |
|
| 56 |
new.chain = h; |
| 57 |
new.dev = st.st_dev; |
| 58 |
new.ino = st.st_ino; |
| 59 |
new.level = h ? h->level+1 : 0; |
| 60 |
new.base = j+1; |
| 61 |
|
| 62 |
lev.level = new.level; |
| 63 |
if (h) { |
| 64 |
lev.base = h->base; |
| 65 |
} else { |
| 66 |
size_t k; |
| 67 |
for (k=j; k && path[k]=='/'; k--); |
| 68 |
for (; k && path[k-1]!='/'; k--); |
| 69 |
lev.base = k; |
| 70 |
} |
| 71 |
|
| 72 |
if (type == FTW_D || type == FTW_DP) { |
| 73 |
dfd = open(path, O_RDONLY); |
| 74 |
err = errno; |
| 75 |
if (dfd < 0 && err == EACCES) type = FTW_DNR; |
| 76 |
if (!fd_limit) close(dfd); |
| 77 |
} |
| 78 |
|
| 79 |
if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) |
| 80 |
return r; |
| 81 |
|
| 82 |
for (; h; h = h->chain) |
| 83 |
if (h->dev == st.st_dev && h->ino == st.st_ino) |
| 84 |
return 0; |
| 85 |
|
| 86 |
if ((type == FTW_D || type == FTW_DP) && fd_limit) { |
| 87 |
if (dfd < 0) { |
| 88 |
errno = err; |
| 89 |
return -1; |
| 90 |
} |
| 91 |
DIR *d = fdopendir(dfd); |
| 92 |
if (d) { |
| 93 |
struct dirent *de; |
| 94 |
while ((de = readdir(d))) { |
| 95 |
if (de->d_name[0] == '.' |
| 96 |
&& (!de->d_name[1] |
| 97 |
|| (de->d_name[1]=='.' |
| 98 |
&& !de->d_name[2]))) continue; |
| 99 |
if (strlen(de->d_name) >= PATH_MAX-l) { |
| 100 |
errno = ENAMETOOLONG; |
| 101 |
closedir(d); |
| 102 |
return -1; |
| 103 |
} |
| 104 |
path[j]='/'; |
| 105 |
strcpy(path+j+1, de->d_name); |
| 106 |
if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) { |
| 107 |
closedir(d); |
| 108 |
return r; |
| 109 |
} |
| 110 |
} |
| 111 |
closedir(d); |
| 112 |
} else { |
| 113 |
close(dfd); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
path[l] = 0; |
| 119 |
if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) |
| 120 |
return r; |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags) |
| 126 |
{ |
| 127 |
int r, cs; |
| 128 |
size_t l; |
| 129 |
char pathbuf[PATH_MAX+1]; |
| 130 |
|
| 131 |
if (fd_limit <= 0) return 0; |
| 132 |
|
| 133 |
l = strlen(path); |
| 134 |
if (l > PATH_MAX) { |
| 135 |
errno = ENAMETOOLONG; |
| 136 |
return -1; |
| 137 |
} |
| 138 |
memcpy(pathbuf, path, l+1); |
| 139 |
|
| 140 |
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) |
| 141 |
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); |
| 142 |
#endif |
| 143 |
r = do_nftw(pathbuf, fn, fd_limit, flags, NULL); |
| 144 |
#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT) |
| 145 |
pthread_setcancelstate(cs, 0); |
| 146 |
#endif |
| 147 |
return r; |
| 148 |
} |
| 149 |
|
| 150 |
weak_alias(nftw, nftw64); |