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