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