13 struct history *chain;
21 #define dirfd(d) (*(int *)d)
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)
25 size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
33 if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
34 if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
36 else if (errno != EACCES) return -1;
38 } else if (S_ISDIR(st.st_mode)) {
39 if (access(path, R_OK) < 0) type = FTW_DNR;
40 else if (flags & FTW_DEPTH) type = FTW_DP;
42 } else if (S_ISLNK(st.st_mode)) {
43 if (flags & FTW_PHYS) type = FTW_SL;
49 if ((flags & FTW_MOUNT) && h
50 && (st.st_dev != h->dev || st.st_ino != h->ino))
56 new.level = h ? h->level+1 : 0;
59 lev.level = new.level;
60 lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0;
62 if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
65 for (; h; h = h->chain)
66 if (h->dev == st.st_dev && h->ino == st.st_ino)
69 if ((type == FTW_D || type == FTW_DP) && fd_limit) {
70 DIR *d = opendir(path);
73 while ((de = readdir(d))) {
74 if (de->d_name[0] == '.'
76 || (de->d_name[1]=='.'
77 && !de->d_name[2]))) continue;
78 if (strlen(de->d_name) >= PATH_MAX-l) {
84 strcpy(path+j+1, de->d_name);
85 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
91 } else if (errno != EACCES) {
97 if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
103 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
107 char pathbuf[PATH_MAX+1];
109 if (fd_limit <= 0) return 0;
113 errno = ENAMETOOLONG;
116 memcpy(pathbuf, path, l+1);
118 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
119 r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
120 pthread_setcancelstate(cs, 0);