From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id CEB5438708A1 for ; Fri, 18 Sep 2020 21:59:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CEB5438708A1 Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-411-CuQQRg22PbilfNSHA03m1Q-1; Fri, 18 Sep 2020 17:59:24 -0400 X-MC-Unique: CuQQRg22PbilfNSHA03m1Q-1 Received: by mail-pf1-f200.google.com with SMTP id c18so4517514pfi.21 for ; Fri, 18 Sep 2020 14:59:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=CQ5a911aDo8Aidt+uVDnSoKaY57N0QoddqssZVefffw=; b=ahtjL5TMCEXUChO+CzaRkmjv8SkLQlsmpykv5HtEDBzZK9oUhFaSAHC2pwNQ8g+foG 4l6iZRtrgzMDYsCTFoKvyDpqH7mNkoY4fjxdBi0NOijGq8PPNHq29KAAwNsthDL4YiFc LLd+hBtE8v5uRBNrtzf7VwV01jefbD4gZIowRPP/sS6yy9QYkszYkmOhmhm1TPCSUDwB pUO2r9Xb9fX8J1VDmjmJVsILvNpErekz9JM6P9tqzAiOlsvX6IRQLq6MgXPKY6yeX8wn +g9MASQJCvS7cWptA/aAmtXyq6G5bACaQD4H/+jcxw5YrxaoUp267IJoez+sfz5+a3fS HsSA== X-Gm-Message-State: AOAM531kCPtheQ0RlRASdf9tEALn41eaNrxLc8OTauk2qOR/A5v6+ziW SaF+lpoCYj0rrHzcyejQ1/doXOBXLiEziEw56BrQZMgLPglMRIwFoAfcEdPPMukRBONTiDpCxgW ExInI0oMS5kkxsstD5h/eIpRDAgRTua8= X-Received: by 2002:a63:c1c:: with SMTP id b28mr28789611pgl.275.1600466362957; Fri, 18 Sep 2020 14:59:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOjLDtPByAnt+hmeXI297DTPvFteVCL+0gJLYoIXcxyeWvrLrv2iRI221rRuQ+nEQY1jmOo/ih6bTvP1RWlqM= X-Received: by 2002:a63:c1c:: with SMTP id b28mr28789599pgl.275.1600466362620; Fri, 18 Sep 2020 14:59:22 -0700 (PDT) MIME-Version: 1.0 References: <20200912211939.31559-1-eshandhawan51@gmail.com> In-Reply-To: <20200912211939.31559-1-eshandhawan51@gmail.com> From: Jeff Johnston Date: Fri, 18 Sep 2020 17:59:11 -0400 Message-ID: Subject: Re: [PATCH] FTW Port from musl To: Eshan dhawan Cc: Newlib X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.29 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: Fri, 18 Sep 2020 21:59:31 -0000 Hi Eshan, There is an issue. This code is in the shared part of newlib but weak_alias is not defined in shared headers. You could spell out the weak_alias using __attribute__ as you can see in libc/sys/linux/linuxthreads/libc-symbols.h: # define _weak_alias(name, aliasname) \ extern __typeof (name) aliasname __attribute__ ((__weak__, __alias__ (#name))); or you could define a similar macro and reuse it. -- Jeff J. On Sat, Sep 12, 2020 at 5:20 PM Eshan dhawan via Newlib < newlib@sourceware.org> wrote: > Signed-off-by: Eshan dhawan > --- > newlib/libc/include/ftw.h | 64 ++++++++++++++ > newlib/libc/posix/Makefile.am | 2 +- > newlib/libc/posix/ftw.c | 34 ++++++++ > newlib/libc/posix/nftw.c | 156 ++++++++++++++++++++++++++++++++++ > 4 files changed, 255 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..f88ce87b2 > --- /dev/null > +++ b/newlib/libc/include/ftw.h > @@ -0,0 +1,64 @@ > +/* > +* Copyright =C2=A9 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 > + > +#define FTW_F 1 > +#define FTW_D 2 > +#define FTW_DNR 3 > +#define FTW_NS 4 > +#define FTW_SL 5 > +#define FTW_DP 6 > +#define FTW_SLN 7 > + > +#define FTW_PHYS 1 > +#define FTW_MOUNT 2 > +#define FTW_CHDIR 4 > +#define FTW_DEPTH 8 > + > +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); > + > +#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) > +#define ftw64 ftw > +#define nftw64 nftw > +#endif > + > +#ifdef __cplusplus > +} > +#endif > + > +#endif > diff --git a/newlib/libc/posix/Makefile.am b/newlib/libc/posix/Makefile.a= m > index 6cdee1df0..5a358f782 100644 > --- a/newlib/libc/posix/Makefile.am > +++ b/newlib/libc/posix/Makefile.am > @@ -10,7 +10,7 @@ GENERAL_SOURCES =3D \ > 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 =3D \ > scandir.c seekdir.c > diff --git a/newlib/libc/posix/ftw.c b/newlib/libc/posix/ftw.c > new file mode 100644 > index 000000000..fa320e9b0 > --- /dev/null > +++ b/newlib/libc/posix/ftw.c > @@ -0,0 +1,34 @@ > +/* > +* Copyright =C2=A9 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); > +} > + > +weak_alias(ftw, ftw64); > diff --git a/newlib/libc/posix/nftw.c b/newlib/libc/posix/nftw.c > new file mode 100644 > index 000000000..0ffca9f67 > --- /dev/null > +++ b/newlib/libc/posix/nftw.c > @@ -0,0 +1,156 @@ > +/* > +* Copyright =C2=A9 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 =3D strlen(path), j =3D l && path[l-1]=3D=3D'/' ? 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=3D=3DENOENT && !lstat(pa= th, > &st)) > + type =3D FTW_SLN; > + else if (errno !=3D EACCES) return -1; > + else type =3D FTW_NS; > + } else if (S_ISDIR(st.st_mode)) { > + if (access(path, R_OK) < 0) type =3D FTW_DNR; > + else if (flags & FTW_DEPTH) type =3D FTW_DP; > + else type =3D FTW_D; > + } else if (S_ISLNK(st.st_mode)) { > + if (flags & FTW_PHYS) type =3D FTW_SL; > + else type =3D FTW_SLN; > + } else { > + type =3D FTW_F; > + } > + > + if ((flags & FTW_MOUNT) && h && st.st_dev !=3D h->dev) > + return 0; > + > + new.chain =3D h; > + new.dev =3D st.st_dev; > + new.ino =3D st.st_ino; > + new.level =3D h ? h->level+1 : 0; > + new.base =3D j+1; > + > + lev.level =3D new.level; > + if (h) { > + lev.base =3D h->base; > + } else { > + size_t k; > + for (k=3Dj; k && path[k]=3D=3D'/'; k--); > + for (; k && path[k-1]!=3D'/'; k--); > + lev.base =3D k; > + } > + > + if (!(flags & FTW_DEPTH) && (r=3Dfn(path, &st, type, &lev))) > + return r; > + > + for (; h; h =3D h->chain) > + if (h->dev =3D=3D st.st_dev && h->ino =3D=3D st.st_ino) > + return 0; > + > + if ((type =3D=3D FTW_D || type =3D=3D FTW_DP) && fd_limit) { > + DIR *d =3D opendir(path); > + if (d) { > + struct dirent *de; > + while ((de =3D readdir(d))) { > + if (de->d_name[0] =3D=3D '.' > + && (!de->d_name[1] > + || (de->d_name[1]=3D=3D'.' > + && !de->d_name[2]))) continue; > + if (strlen(de->d_name) >=3D PATH_MAX-l) { > + errno =3D ENAMETOOLONG; > + closedir(d); > + return -1; > + } > + path[j]=3D'/'; > + strcpy(path+j+1, de->d_name); > + if ((r=3Ddo_nftw(path, fn, fd_limit-1, > flags, &new))) { > + closedir(d); > + return r; > + } > + } > + closedir(d); > + } else if (errno !=3D EACCES) { > + return -1; > + } > + } > + > + path[l] =3D 0; > + if ((flags & FTW_DEPTH) && (r=3Dfn(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 <=3D 0) return 0; > + > + l =3D strlen(path); > + if (l > PATH_MAX) { > + errno =3D ENAMETOOLONG; > + return -1; > + } > + memcpy(pathbuf, path, l+1); > + > +#ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT > + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); > +#endif > + r =3D do_nftw(pathbuf, fn, fd_limit, flags, NULL); > +#ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT > + pthread_setcancelstate(cs, 0); > +#endif > + return r; > +} > + > +weak_alias(nftw, nftw64); > + > -- > 2.17.1 > >