1 #include <ftw.h>
2 #include <dirent.h>
3 #ifndef __LITEOS_A__
4 #include <fcntl.h>
5 #endif
6 #include <sys/stat.h>
7 #include <errno.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <limits.h>
11 #include <pthread.h>
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
do_nftw(char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int fd_limit,int flags,struct history * h)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 #ifndef __LITEOS_A__
33 int dfd;
34 int err;
35 #endif
36 struct FTW lev;
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 #ifdef __LITEOS_A__
45 if (access(path, R_OK) < 0) {
46 type = FTW_DNR;
47 } else if (flags & FTW_DEPTH) {
48 type = FTW_DP;
49 }
50 #else
51 if (flags & FTW_DEPTH) type = FTW_DP;
52 #endif
53 else type = FTW_D;
54 } else if (S_ISLNK(st.st_mode)) {
55 if (flags & FTW_PHYS) type = FTW_SL;
56 else type = FTW_SLN;
57 } else {
58 type = FTW_F;
59 }
60
61 if ((flags & FTW_MOUNT) && h && st.st_dev != h->dev)
62 return 0;
63
64 new.chain = h;
65 new.dev = st.st_dev;
66 new.ino = st.st_ino;
67 new.level = h ? h->level+1 : 0;
68 new.base = j+1;
69
70 lev.level = new.level;
71 if (h) {
72 lev.base = h->base;
73 } else {
74 size_t k;
75 for (k=j; k && path[k]=='/'; k--);
76 for (; k && path[k-1]!='/'; k--);
77 lev.base = k;
78 }
79
80 #ifndef __LITEOS_A__
81 if (type == FTW_D || type == FTW_DP) {
82 dfd = open(path, O_RDONLY);
83 err = errno;
84 if (dfd < 0 && err == EACCES) type = FTW_DNR;
85 if (!fd_limit) close(dfd);
86 }
87 #endif
88
89 if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
90 return r;
91
92 for (; h; h = h->chain)
93 if (h->dev == st.st_dev && h->ino == st.st_ino)
94 return 0;
95
96 if ((type == FTW_D || type == FTW_DP) && fd_limit) {
97 #ifdef __LITEOS_A__
98 DIR *d = opendir(path);
99 #else
100 if (dfd < 0) {
101 errno = err;
102 return -1;
103 }
104 DIR *d = fdopendir(dfd);
105 #endif
106 if (d) {
107 struct dirent *de;
108 while ((de = readdir(d))) {
109 if (de->d_name[0] == '.'
110 && (!de->d_name[1]
111 || (de->d_name[1]=='.'
112 && !de->d_name[2]))) continue;
113 if (strlen(de->d_name) >= PATH_MAX-l) {
114 errno = ENAMETOOLONG;
115 closedir(d);
116 return -1;
117 }
118 path[j]='/';
119 strcpy(path+j+1, de->d_name);
120 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
121 closedir(d);
122 return r;
123 }
124 }
125 closedir(d);
126 #ifdef __LITEOS_A__
127 } else if (errno != EACCES) {
128 return -1;
129 }
130 #else
131 } else {
132 close(dfd);
133 return -1;
134 }
135 #endif
136 }
137
138 path[l] = 0;
139 if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
140 return r;
141
142 return 0;
143 }
144
nftw(const char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int fd_limit,int flags)145 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
146 {
147 int r, cs;
148 size_t l;
149 char pathbuf[PATH_MAX+1];
150
151 if (fd_limit <= 0) return 0;
152
153 l = strlen(path);
154 if (l > PATH_MAX) {
155 errno = ENAMETOOLONG;
156 return -1;
157 }
158 memcpy(pathbuf, path, l+1);
159
160 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
161 r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
162 pthread_setcancelstate(cs, 0);
163 return r;
164 }
165
166 weak_alias(nftw, nftw64);
167