>From 9896c027a6b261e09dff68d61993bb60313e9166 Mon Sep 17 00:00:00 2001 From: Joakim Sindholt Date: Tue, 12 Jul 2016 10:14:34 +0200 Subject: [PATCH 1/2] ignore trailing slashes in nftw path --- src/misc/nftw.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/misc/nftw.c b/src/misc/nftw.c index efb2b89..7df7226 100644 --- a/src/misc/nftw.c +++ b/src/misc/nftw.c @@ -46,17 +46,17 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, type = FTW_F; } - if ((flags & FTW_MOUNT) && h && st.st_dev != h->dev) + if ((flags & FTW_MOUNT) && st.st_dev != h->dev) return 0; new.chain = h; new.dev = st.st_dev; new.ino = st.st_ino; - new.level = h ? h->level+1 : 0; - new.base = l+1; + new.level = h->level+1; + new.base = j+1; lev.level = new.level; - lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0; + lev.base = h->base; if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) return r; @@ -101,6 +101,7 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags) { + struct history h; int r, cs; size_t l; char pathbuf[PATH_MAX+1]; @@ -108,14 +109,25 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str if (fd_limit <= 0) return 0; l = strlen(path); + while (l && path[l-1]=='/') + --l; if (l > PATH_MAX) { errno = ENAMETOOLONG; return -1; } - memcpy(pathbuf, path, l+1); + memcpy(pathbuf, path, l); + pathbuf[l] = 0; + h.chain = NULL; + h.dev = (dev_t)-1; + h.ino = (ino_t)-1; + h.level = -1; + h.base = l; + while (h.base && pathbuf[h.base-1]!='/') + --h.base; + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); - r = do_nftw(pathbuf, fn, fd_limit, flags, NULL); + r = do_nftw(pathbuf, fn, fd_limit, flags, &h); pthread_setcancelstate(cs, 0); return r; } -- 2.7.3