From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 14792 invoked from network); 23 Jan 2022 15:56:42 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 23 Jan 2022 15:56:42 -0000 Received: (qmail 1241 invoked by uid 550); 23 Jan 2022 15:56:35 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 1117 invoked from network); 23 Jan 2022 15:56:35 -0000 From: Ismael Luceno To: musl@lists.openwall.com Cc: Rich Felker , Ismael Luceno Date: Sun, 23 Jan 2022 16:59:54 +0100 Message-Id: <20220123155955.16484-2-ismael@iodev.co.uk> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20220123155955.16484-1-ismael@iodev.co.uk> References: <20220123155955.16484-1-ismael@iodev.co.uk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH v3 1/2] nftw: implement FTW_CHDIR Signed-off-by: Ismael Luceno --- src/misc/nftw.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/misc/nftw.c b/src/misc/nftw.c index 5b233b2b8e77..7569a657e54e 100644 --- a/src/misc/nftw.c +++ b/src/misc/nftw.c @@ -87,6 +87,14 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, DIR *d = fdopendir(dfd); if (d) { struct dirent *de; + if (flags & FTW_CHDIR) { + if (!fchdir(dfd)) { + err = errno; + closedir(d); + errno = err; + return -1; + } + } while ((de = readdir(d))) { if (de->d_name[0] == '.' && (!de->d_name[1] @@ -123,6 +131,7 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str int r, cs; size_t l; char pathbuf[PATH_MAX+1]; + int orig_dfd; if (fd_limit <= 0) return 0; @@ -133,9 +142,22 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str } memcpy(pathbuf, path, l+1); + if (flags & FTW_CHDIR) { + orig_dfd = open(".", O_CLOEXEC | O_PATH); + if (orig_dfd < 0) + return -1; + } + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); r = do_nftw(pathbuf, fn, fd_limit, flags, NULL); pthread_setcancelstate(cs, 0); + if (flags & FTW_CHDIR) { + if (!fchdir(orig_dfd)) + r = -1; + int err = errno; + close(orig_dfd); + errno = err; + } return r; } -- 2.33.0