From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id CE2E4396ECB9 for ; Wed, 20 Jan 2021 22:14:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CE2E4396ECB9 Received: by mail-pl1-x636.google.com with SMTP id 31so960488plb.10 for ; Wed, 20 Jan 2021 14:14:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=cyMjh82op/27hdzMmMJw5kPT6C9grwU6zwHt2XKe+6o=; b=beg/MGMYqSqG/Jr7aPnfn+W+ySiUQnDH9VRFuc5CWYtdmhry7ESrJwK10W6ekxrD3t mujkdGI4mKYC+cFn3Xi3xt5YmnjWRqzlJNy1Moh+ZXNX1oKgzi5x0XusgdixebezO1qS tct6OoRlHnA6L0WHZDjor5LhCSkEGpUxHiPpUIxRMZmzDu7WdBpfBulvFyTpG6Jq+JlH hP+QHYOouNVt3eyWkhKYVfwHvJs9yURCL/7IX+hRQYj4fnvZN1lCPFVkljvzCetlZz0u p69Bq6Ag/TvPRBELAcXgH3FD0DWtI3cTo0eYUhGn0pGT7pC8zsWshdbnDX2MATNNkXxI C35A== X-Gm-Message-State: AOAM5328DWrHrfZEUB0YfrcTUSd2iBo4nqJ47qfnMD7sC3PdXkIdPZW0 ps2N3P7/e2mdgDLHJ5f/16dDEmQRNC3P2w== X-Google-Smtp-Source: ABdhPJyseRr2HFiPsEwK6NJNOG1ULL+CRgAF/Y6Y6dY/VIBeyPgPHI7FgfvE/88b4yJ6uLfO5SYCvA== X-Received: by 2002:a17:90a:4dc5:: with SMTP id r5mr660592pjl.114.1611180879582; Wed, 20 Jan 2021 14:14:39 -0800 (PST) Received: from localhost.localdomain ([117.212.111.149]) by smtp.gmail.com with ESMTPSA id a19sm3253554pfi.130.2021.01.20.14.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jan 2021 14:14:38 -0800 (PST) From: Eshan Dhawan To: newlib@sourceware.org Subject: [PATCH] FTW Port for Newlib Date: Thu, 21 Jan 2021 03:44:27 +0530 Message-Id: <20210120221427.214833-1-eshandhawan51@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jan 2021 22:14:42 -0000 From: Eshan dhawan Signed-off-by: Eshan Dhawan --- newlib/libc/include/ftw.h | 66 +++++++++++++++ newlib/libc/posix/Makefile.am | 2 +- newlib/libc/posix/ftw.c | 33 ++++++++ newlib/libc/posix/nftw.c | 154 ++++++++++++++++++++++++++++++++++ 4 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 newlib/libc/include/ftw.h create mode 100644 newlib/libc/posix/ftw.c create mode 100644 newlib/libc/posix/nftw.c diff --git a/newlib/libc/include/ftw.h b/newlib/libc/include/ftw.h new file mode 100644 index 000000000..8de1e1e5c --- /dev/null +++ b/newlib/libc/include/ftw.h @@ -0,0 +1,66 @@ +/* +* Copyright © 2005-2020 Rich Felker, et al. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef _FTW_H +#define _FTW_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +/* + * Valid flags for the 3rd argument to the function that is passed as the + * second argument to ftw(3) and nftw(3). Say it three times fast! + */ +#define FTW_F 0 /* File. */ +#define FTW_D 1 /* Directory. */ +#define FTW_DNR 2 /* Directory without read permission. */ +#define FTW_DP 3 /* Directory with subdirectories visited. */ +#define FTW_NS 4 /* Unknown type; stat() failed. */ +#define FTW_SL 5 /* Symbolic link. */ +#define FTW_SLN 6 /* Sym link that names a nonexistent file. */ + +/* + * Flags for use as the 4th argument to nftw(3). These may be ORed together. + */ +#define FTW_PHYS 0x01 /* Physical walk, don't follow sym links. */ +#define FTW_MOUNT 0x02 /* The walk does not cross a mount point. */ +#define FTW_DEPTH 0x04 /* Subdirs visited before the dir itself. */ +#define FTW_CHDIR 0x08 /* Change to a directory before reading it. */ + +struct FTW { + int base; + int level; +}; + +int ftw(const char *, int (*)(const char *, const struct stat *, int), int); +int nftw(const char *, int (*)(const char *, const struct stat *, int, struct FTW *), int, int); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/newlib/libc/posix/Makefile.am b/newlib/libc/posix/Makefile.am index 6cdee1df0..5a358f782 100644 --- a/newlib/libc/posix/Makefile.am +++ b/newlib/libc/posix/Makefile.am @@ -10,7 +10,7 @@ GENERAL_SOURCES = \ opendir.c readdir.c readdir_r.c \ regcomp.c regerror.c regexec.c regfree.c \ rewinddir.c sleep.c usleep.c \ - telldir.c + telldir.c ftw.c nftw.c ELIX_2_SOURCES = \ scandir.c seekdir.c diff --git a/newlib/libc/posix/ftw.c b/newlib/libc/posix/ftw.c new file mode 100644 index 000000000..18cfc24cb --- /dev/null +++ b/newlib/libc/posix/ftw.c @@ -0,0 +1,33 @@ +/* +* Copyright © 2005-2020 Rich Felker, et al. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include + +int ftw(const char *path, int (*fn)(const char *, const struct stat *, int), int fd_limit) +{ + /* The following cast assumes that calling a function with one + * argument more than it needs behaves as expected. This is + * actually undefined, but works on all real-world machines. */ + return nftw(path, (int (*)())fn, fd_limit, FTW_PHYS); +} + diff --git a/newlib/libc/posix/nftw.c b/newlib/libc/posix/nftw.c new file mode 100644 index 000000000..f4f55f341 --- /dev/null +++ b/newlib/libc/posix/nftw.c @@ -0,0 +1,154 @@ +/* +* Copyright © 2005-2020 Rich Felker, et al. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +struct history +{ + struct history *chain; + dev_t dev; + ino_t ino; + int level; + int base; +}; + +#undef dirfd +#define dirfd(d) (*(int *)d) + +static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags, struct history *h) +{ + size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l; + struct stat st; + struct history new; + int type; + int r; + struct FTW lev; + + if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) { + if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st)) + type = FTW_SLN; + else if (errno != EACCES) return -1; + else type = FTW_NS; + } else if (S_ISDIR(st.st_mode)) { + if (access(path, R_OK) < 0) type = FTW_DNR; + else if (flags & FTW_DEPTH) type = FTW_DP; + else type = FTW_D; + } else if (S_ISLNK(st.st_mode)) { + if (flags & FTW_PHYS) type = FTW_SL; + else type = FTW_SLN; + } else { + type = FTW_F; + } + + if ((flags & FTW_MOUNT) && h && 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 = j+1; + + lev.level = new.level; + if (h) { + lev.base = h->base; + } else { + size_t k; + for (k=j; k && path[k]=='/'; k--); + for (; k && path[k-1]!='/'; k--); + lev.base = k; + } + + if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) + return r; + + for (; h; h = h->chain) + if (h->dev == st.st_dev && h->ino == st.st_ino) + return 0; + + if ((type == FTW_D || type == FTW_DP) && fd_limit) { + DIR *d = opendir(path); + if (d) { + struct dirent *de; + while ((de = readdir(d))) { + if (de->d_name[0] == '.' + && (!de->d_name[1] + || (de->d_name[1]=='.' + && !de->d_name[2]))) continue; + if (strlen(de->d_name) >= PATH_MAX-l) { + errno = ENAMETOOLONG; + closedir(d); + return -1; + } + path[j]='/'; + strcpy(path+j+1, de->d_name); + if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) { + closedir(d); + return r; + } + } + closedir(d); + } else if (errno != EACCES) { + return -1; + } + } + + path[l] = 0; + if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) + return r; + + return 0; +} + +int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags) +{ + int r, cs; + size_t l; + char pathbuf[PATH_MAX+1]; + + if (fd_limit <= 0) return 0; + + l = strlen(path); + if (l > PATH_MAX) { + errno = ENAMETOOLONG; + return -1; + } + memcpy(pathbuf, path, l+1); + +#ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); +#endif + r = do_nftw(pathbuf, fn, fd_limit, flags, NULL); +#ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT + pthread_setcancelstate(cs, 0); +#endif + return r; +} + -- 2.27.0