• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <pthread.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 
do_nftw(char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int fd_limit,int flags,struct history * h)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 	int dfd;
31 	int err;
32 	struct FTW lev;
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 (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 (type == FTW_D || type == FTW_DP) {
69 		dfd = open(path, O_RDONLY);
70 		err = errno;
71 		if (dfd < 0 && err == EACCES) type = FTW_DNR;
72 		if (!fd_limit) close(dfd);
73 	}
74 
75 	if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
76 		return r;
77 
78 	for (; h; h = h->chain)
79 		if (h->dev == st.st_dev && h->ino == st.st_ino)
80 			return 0;
81 
82 	if ((type == FTW_D || type == FTW_DP) && fd_limit) {
83 		if (dfd < 0) {
84 			errno = err;
85 			return -1;
86 		}
87 		DIR *d = fdopendir(dfd);
88 		if (d) {
89 			struct dirent *de;
90 			while ((de = readdir(d))) {
91 				if (de->d_name[0] == '.'
92 				 && (!de->d_name[1]
93 				  || (de->d_name[1]=='.'
94 				   && !de->d_name[2]))) continue;
95 				if (strlen(de->d_name) >= PATH_MAX-l) {
96 					errno = ENAMETOOLONG;
97 					closedir(d);
98 					return -1;
99 				}
100 				path[j]='/';
101 				strcpy(path+j+1, de->d_name);
102 				if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
103 					closedir(d);
104 					return r;
105 				}
106 			}
107 			closedir(d);
108 		} else {
109 			close(dfd);
110 			return -1;
111 		}
112 	}
113 
114 	path[l] = 0;
115 	if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
116 		return r;
117 
118 	return 0;
119 }
120 
nftw(const char * path,int (* fn)(const char *,const struct stat *,int,struct FTW *),int fd_limit,int flags)121 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
122 {
123 	int r, cs;
124 	size_t l;
125 	char pathbuf[PATH_MAX+1];
126 
127 	if (fd_limit <= 0) return 0;
128 
129 	l = strlen(path);
130 	if (l > PATH_MAX) {
131 		errno = ENAMETOOLONG;
132 		return -1;
133 	}
134 	memcpy(pathbuf, path, l+1);
135 
136 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
137 	r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
138 	pthread_setcancelstate(cs, 0);
139 	return r;
140 }
141 
142 weak_alias(nftw, nftw64);
143