initial check-in, version 0.5.0
[musl] / src / misc / nftw.c
1 #include <ftw.h>
2 #include <dirent.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <limits.h>
10 #include "libc.h"
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         char *name;
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 (access(path, R_OK) < 0) type = FTW_DNR;
41                 else if (flags & FTW_DEPTH) type = FTW_DP;
42                 else type = FTW_D;
43         } else if (S_ISLNK(st.st_mode)) {
44                 if (flags & FTW_PHYS) type = FTW_SL;
45                 else type = FTW_SLN;
46         } else {
47                 type = FTW_F;
48         }
49
50         if ((flags & FTW_MOUNT) && h
51          && (st.st_dev != h->dev || st.st_ino != h->ino))
52                 return 0;
53         
54         new.chain = h;
55         new.dev = st.st_dev;
56         new.ino = st.st_ino;
57         new.level = h ? h->level+1 : 0;
58         new.base = l+1;
59         
60         lev.level = new.level;
61         lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0;
62
63         if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
64                 return r;
65
66         for (; h; h = h->chain)
67                 if (h->dev == st.st_dev && h->ino == st.st_ino)
68                         return 0;
69
70         if ((type == FTW_D || type == FTW_DP) && fd_limit) {
71                 DIR *d = opendir(path);
72                 if (d) {
73                         struct dirent *de;
74                         while ((de = readdir(d))) {
75                                 if (de->d_name[0] == '.'
76                                  && (!de->d_name[1]
77                                   || (de->d_name[1]=='.'
78                                    && !de->d_name[2]))) continue;
79                                 if (strlen(de->d_name) >= PATH_MAX-l) {
80                                         errno = ENAMETOOLONG;
81                                         closedir(d);
82                                         return -1;
83                                 }
84                                 path[j]='/';
85                                 strcpy(path+j+1, de->d_name);
86                                 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
87                                         closedir(d);
88                                         return r;
89                                 }
90                         }
91                         closedir(d);
92                 } else if (errno != EACCES) {
93                         return -1;
94                 }
95         }
96
97         path[l] = 0;
98         if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
99                 return r;
100
101         return 0;
102 }
103
104 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
105 {
106         size_t l;
107         char pathbuf[PATH_MAX+1];
108
109         if (fd_limit <= 0) return 0;
110
111         l = strlen(path);
112         if (l > PATH_MAX) {
113                 errno = ENAMETOOLONG;
114                 return -1;
115         }
116         memcpy(pathbuf, path, l+1);
117         
118         return do_nftw(pathbuf, fn, fd_limit, flags, NULL);
119 }
120
121 LFS64(nftw);