15 struct history *chain;
23 #define dirfd(d) (*(int *)d)
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)
27 size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
35 if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
36 if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
38 else if (errno != EACCES) return -1;
40 } else if (S_ISDIR(st.st_mode)) {
41 if (access(path, R_OK) < 0) type = FTW_DNR;
42 else if (flags & FTW_DEPTH) type = FTW_DP;
44 } else if (S_ISLNK(st.st_mode)) {
45 if (flags & FTW_PHYS) type = FTW_SL;
51 if ((flags & FTW_MOUNT) && h
52 && (st.st_dev != h->dev || st.st_ino != h->ino))
58 new.level = h ? h->level+1 : 0;
61 lev.level = new.level;
62 lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0;
64 if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
67 for (; h; h = h->chain)
68 if (h->dev == st.st_dev && h->ino == st.st_ino)
71 if ((type == FTW_D || type == FTW_DP) && fd_limit) {
72 DIR *d = opendir(path);
75 while ((de = readdir(d))) {
76 if (de->d_name[0] == '.'
78 || (de->d_name[1]=='.'
79 && !de->d_name[2]))) continue;
80 if (strlen(de->d_name) >= PATH_MAX-l) {
86 strcpy(path+j+1, de->d_name);
87 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
93 } else if (errno != EACCES) {
99 if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
105 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
109 char pathbuf[PATH_MAX+1];
111 if (fd_limit <= 0) return 0;
115 errno = ENAMETOOLONG;
118 memcpy(pathbuf, path, l+1);
120 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
121 r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
122 pthread_setcancelstate(cs, 0);