public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3] ftw initial port
@ 2020-10-16  7:46 Eshan dhawan
  2020-11-04 18:20 ` Eshan Dhawan
  0 siblings, 1 reply; 5+ messages in thread
From: Eshan dhawan @ 2020-10-16  7:46 UTC (permalink / raw)
  To: newlib

Signed-off-by: Eshan dhawan <eshandhawan51@gmail.com>
---
 newlib/libc/include/ftw.h     |  69 +++++++++++++++
 newlib/libc/posix/Makefile.am |   2 +-
 newlib/libc/posix/ftw.c       |  34 ++++++++
 newlib/libc/posix/nftw.c      | 156 ++++++++++++++++++++++++++++++++++
 4 files changed, 260 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..400701cf5
--- /dev/null
+++ b/newlib/libc/include/ftw.h
@@ -0,0 +1,69 @@
+/*
+* 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
+
+#ifdef _weak_alias(name, aliasname)
+#  define weak_alias(name, aliasname) \
+  extern __typeof (name) aliasname __attribute__ ((__weak__, __alias__ (#name)));
+#endif
+
+#include <sys/features.h>
+#include <sys/stat.h>
+
+#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.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..fa320e9b0
--- /dev/null
+++ b/newlib/libc/posix/ftw.c
@@ -0,0 +1,34 @@
+/*
+* 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 <ftw.h>
+
+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 © 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 <ftw.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <limits.h>
+#include <pthread.h>
+
+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;
+}
+
+weak_alias(nftw, nftw64);
+
-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] ftw initial port
  2020-10-16  7:46 [PATCH v3] ftw initial port Eshan dhawan
@ 2020-11-04 18:20 ` Eshan Dhawan
  2020-11-04 18:26   ` Jeff Johnston
  0 siblings, 1 reply; 5+ messages in thread
From: Eshan Dhawan @ 2020-11-04 18:20 UTC (permalink / raw)
  To: Newlib

Hello everyone,
I sent this patch earlier but it seems to be missed between other patches.
I wanted it if you all could review it.

-Eshan

On Fri, Oct 16, 2020 at 1:16 PM Eshan dhawan <eshandhawan51@gmail.com>
wrote:

> Signed-off-by: Eshan dhawan <eshandhawan51@gmail.com>
> ---
>  newlib/libc/include/ftw.h     |  69 +++++++++++++++
>  newlib/libc/posix/Makefile.am |   2 +-
>  newlib/libc/posix/ftw.c       |  34 ++++++++
>  newlib/libc/posix/nftw.c      | 156 ++++++++++++++++++++++++++++++++++
>  4 files changed, 260 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..400701cf5
> --- /dev/null
> +++ b/newlib/libc/include/ftw.h
> @@ -0,0 +1,69 @@
> +/*
> +* 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
> +
> +#ifdef _weak_alias(name, aliasname)
> +#  define weak_alias(name, aliasname) \
> +  extern __typeof (name) aliasname __attribute__ ((__weak__, __alias__
> (#name)));
> +#endif
> +
> +#include <sys/features.h>
> +#include <sys/stat.h>
> +
> +#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.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..fa320e9b0
> --- /dev/null
> +++ b/newlib/libc/posix/ftw.c
> @@ -0,0 +1,34 @@
> +/*
> +* 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 <ftw.h>
> +
> +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 © 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 <ftw.h>
> +#include <dirent.h>
> +#include <sys/stat.h>
> +#include <errno.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include <limits.h>
> +#include <pthread.h>
> +
> +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;
> +}
> +
> +weak_alias(nftw, nftw64);
> +
> --
> 2.17.1
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] ftw initial port
  2020-11-04 18:20 ` Eshan Dhawan
@ 2020-11-04 18:26   ` Jeff Johnston
  2020-11-04 18:38     ` Eshan Dhawan
  2020-11-04 19:02     ` Corinna Vinschen
  0 siblings, 2 replies; 5+ messages in thread
From: Jeff Johnston @ 2020-11-04 18:26 UTC (permalink / raw)
  To: Eshan Dhawan, Newlib

Hi Eshan,

I did review this.  I had questions about your weak_alias macro.  You check
for _weak_alias, then define weak_alias.  If _weak_alias is defined,
why not just use it?  As well, if _weak_alias is not defined, then you are
referencing weak_alias in your code without it being defined.

-- Jeff J.

On Wed, Nov 4, 2020 at 1:21 PM Eshan Dhawan via Newlib <
newlib@sourceware.org> wrote:

> Hello everyone,
> I sent this patch earlier but it seems to be missed between other patches.
> I wanted it if you all could review it.
>
> -Eshan
>
> On Fri, Oct 16, 2020 at 1:16 PM Eshan dhawan <eshandhawan51@gmail.com>
> wrote:
>
> > Signed-off-by: Eshan dhawan <eshandhawan51@gmail.com>
> > ---
> >  newlib/libc/include/ftw.h     |  69 +++++++++++++++
> >  newlib/libc/posix/Makefile.am |   2 +-
> >  newlib/libc/posix/ftw.c       |  34 ++++++++
> >  newlib/libc/posix/nftw.c      | 156 ++++++++++++++++++++++++++++++++++
> >  4 files changed, 260 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..400701cf5
> > --- /dev/null
> > +++ b/newlib/libc/include/ftw.h
> > @@ -0,0 +1,69 @@
> > +/*
> > +* 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
> > +
> > +#ifdef _weak_alias(name, aliasname)
> > +#  define weak_alias(name, aliasname) \
> > +  extern __typeof (name) aliasname __attribute__ ((__weak__, __alias__
> > (#name)));
> > +#endif
> > +
> > +#include <sys/features.h>
> > +#include <sys/stat.h>
> > +
> > +#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.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..fa320e9b0
> > --- /dev/null
> > +++ b/newlib/libc/posix/ftw.c
> > @@ -0,0 +1,34 @@
> > +/*
> > +* 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 <ftw.h>
> > +
> > +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 © 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 <ftw.h>
> > +#include <dirent.h>
> > +#include <sys/stat.h>
> > +#include <errno.h>
> > +#include <unistd.h>
> > +#include <string.h>
> > +#include <limits.h>
> > +#include <pthread.h>
> > +
> > +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;
> > +}
> > +
> > +weak_alias(nftw, nftw64);
> > +
> > --
> > 2.17.1
> >
> >
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] ftw initial port
  2020-11-04 18:26   ` Jeff Johnston
@ 2020-11-04 18:38     ` Eshan Dhawan
  2020-11-04 19:02     ` Corinna Vinschen
  1 sibling, 0 replies; 5+ messages in thread
From: Eshan Dhawan @ 2020-11-04 18:38 UTC (permalink / raw)
  To: Jeff Johnston; +Cc: Newlib

Hi jeff,
Apologies I missed I will update it soon.

Thanks
-Eshan


On Wed, Nov 4, 2020 at 11:56 PM Jeff Johnston <jjohnstn@redhat.com> wrote:

> Hi Eshan,
>
> I did review this.  I had questions about your weak_alias macro.  You
> check for _weak_alias, then define weak_alias.  If _weak_alias is defined,
> why not just use it?  As well, if _weak_alias is not defined, then you are
> referencing weak_alias in your code without it being defined.
>
> -- Jeff J.
>
> On Wed, Nov 4, 2020 at 1:21 PM Eshan Dhawan via Newlib <
> newlib@sourceware.org> wrote:
>
>> Hello everyone,
>> I sent this patch earlier but it seems to be missed between other patches.
>> I wanted it if you all could review it.
>>
>> -Eshan
>>
>> On Fri, Oct 16, 2020 at 1:16 PM Eshan dhawan <eshandhawan51@gmail.com>
>> wrote:
>>
>> > Signed-off-by: Eshan dhawan <eshandhawan51@gmail.com>
>> > ---
>> >  newlib/libc/include/ftw.h     |  69 +++++++++++++++
>> >  newlib/libc/posix/Makefile.am |   2 +-
>> >  newlib/libc/posix/ftw.c       |  34 ++++++++
>> >  newlib/libc/posix/nftw.c      | 156 ++++++++++++++++++++++++++++++++++
>> >  4 files changed, 260 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..400701cf5
>> > --- /dev/null
>> > +++ b/newlib/libc/include/ftw.h
>> > @@ -0,0 +1,69 @@
>> > +/*
>> > +* 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
>> > +
>> > +#ifdef _weak_alias(name, aliasname)
>> > +#  define weak_alias(name, aliasname) \
>> > +  extern __typeof (name) aliasname __attribute__ ((__weak__, __alias__
>> > (#name)));
>> > +#endif
>> > +
>> > +#include <sys/features.h>
>> > +#include <sys/stat.h>
>> > +
>> > +#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.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..fa320e9b0
>> > --- /dev/null
>> > +++ b/newlib/libc/posix/ftw.c
>> > @@ -0,0 +1,34 @@
>> > +/*
>> > +* 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 <ftw.h>
>> > +
>> > +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 © 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 <ftw.h>
>> > +#include <dirent.h>
>> > +#include <sys/stat.h>
>> > +#include <errno.h>
>> > +#include <unistd.h>
>> > +#include <string.h>
>> > +#include <limits.h>
>> > +#include <pthread.h>
>> > +
>> > +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;
>> > +}
>> > +
>> > +weak_alias(nftw, nftw64);
>> > +
>> > --
>> > 2.17.1
>> >
>> >
>>
>>

-- 
Thanks
- Eshan

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] ftw initial port
  2020-11-04 18:26   ` Jeff Johnston
  2020-11-04 18:38     ` Eshan Dhawan
@ 2020-11-04 19:02     ` Corinna Vinschen
  1 sibling, 0 replies; 5+ messages in thread
From: Corinna Vinschen @ 2020-11-04 19:02 UTC (permalink / raw)
  To: newlib

On Nov  4 13:26, Jeff Johnston via Newlib wrote:
> Hi Eshan,
> 
> I did review this.  I had questions about your weak_alias macro.  You check
> for _weak_alias, then define weak_alias.  If _weak_alias is defined,
> why not just use it?  As well, if _weak_alias is not defined, then you are
> referencing weak_alias in your code without it being defined.

There's also still no header file compatibility between this ftw and
Cygwin's, which I'd asked for when you sent the first patch.  The flags
for the 3rd argument (FTW_F, etc) uses different values, which Cygwin
can't follow for backward compatibility.

I also asked if we really need these ftw64 and nftw64 entry points.  If
we actually need them, the implementation as weak aliases is wrong,
afaics.  ftw64/nftw64 would have to use the equivalent 64 bit types from
stdio64, like struct stat64.  Otherwise the 64 bit functions will be
broken, won't they?


Corinna


> 
> -- Jeff J.
> 
> On Wed, Nov 4, 2020 at 1:21 PM Eshan Dhawan via Newlib <
> newlib@sourceware.org> wrote:
> 
> > Hello everyone,
> > I sent this patch earlier but it seems to be missed between other patches.
> > I wanted it if you all could review it.
> >
> > -Eshan
> >
> > On Fri, Oct 16, 2020 at 1:16 PM Eshan dhawan <eshandhawan51@gmail.com>
> > wrote:
> >
> > > Signed-off-by: Eshan dhawan <eshandhawan51@gmail.com>
> > > ---
> > >  newlib/libc/include/ftw.h     |  69 +++++++++++++++
> > >  newlib/libc/posix/Makefile.am |   2 +-
> > >  newlib/libc/posix/ftw.c       |  34 ++++++++
> > >  newlib/libc/posix/nftw.c      | 156 ++++++++++++++++++++++++++++++++++
> > >  4 files changed, 260 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..400701cf5
> > > --- /dev/null
> > > +++ b/newlib/libc/include/ftw.h
> > > @@ -0,0 +1,69 @@
> > > +/*
> > > +* 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
> > > +
> > > +#ifdef _weak_alias(name, aliasname)
> > > +#  define weak_alias(name, aliasname) \
> > > +  extern __typeof (name) aliasname __attribute__ ((__weak__, __alias__
> > > (#name)));
> > > +#endif
> > > +
> > > +#include <sys/features.h>
> > > +#include <sys/stat.h>
> > > +
> > > +#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.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..fa320e9b0
> > > --- /dev/null
> > > +++ b/newlib/libc/posix/ftw.c
> > > @@ -0,0 +1,34 @@
> > > +/*
> > > +* 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 <ftw.h>
> > > +
> > > +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 © 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 <ftw.h>
> > > +#include <dirent.h>
> > > +#include <sys/stat.h>
> > > +#include <errno.h>
> > > +#include <unistd.h>
> > > +#include <string.h>
> > > +#include <limits.h>
> > > +#include <pthread.h>
> > > +
> > > +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;
> > > +}
> > > +
> > > +weak_alias(nftw, nftw64);
> > > +
> > > --
> > > 2.17.1
> > >
> > >
> >
> >


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-11-04 19:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-16  7:46 [PATCH v3] ftw initial port Eshan dhawan
2020-11-04 18:20 ` Eshan Dhawan
2020-11-04 18:26   ` Jeff Johnston
2020-11-04 18:38     ` Eshan Dhawan
2020-11-04 19:02     ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).