mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH v2 0/2] nftw: Implement FTW_CHDIR and FTW_ACTIONRETVAL
@ 2022-01-22 14:51 Ismael Luceno
  2022-01-22 14:51 ` [musl] [PATCH v2 1/2] nftw: implement FTW_CHDIR Ismael Luceno
  2022-01-22 14:51 ` [musl] [PATCH v2 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension) Ismael Luceno
  0 siblings, 2 replies; 5+ messages in thread
From: Ismael Luceno @ 2022-01-22 14:51 UTC (permalink / raw)
  To: musl; +Cc: Rich Felker, Ismael Luceno

FTW_CHDIR is specified by POSIX. While the spec doesn't clarify, nftw
restores the starting directory in conformance with other implementations.

FTW_ACTIONRETVAL is a GNU extension added by glibc 2.3.3.

Changes since v1:
- Fixed handling of FTW_STOP when FTW_DEPTH isn't set.

Ismael Luceno (2):
  nftw: implement FTW_CHDIR
  nftw: implement FTW_ACTIONRETVAL (GNU extension)

 include/ftw.h   |  7 +++++++
 src/misc/nftw.c | 43 +++++++++++++++++++++++++++++++++----------
 2 files changed, 40 insertions(+), 10 deletions(-)

-- 
2.33.0


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

* [musl] [PATCH v2 1/2] nftw: implement FTW_CHDIR
  2022-01-22 14:51 [musl] [PATCH v2 0/2] nftw: Implement FTW_CHDIR and FTW_ACTIONRETVAL Ismael Luceno
@ 2022-01-22 14:51 ` Ismael Luceno
  2022-01-22 15:50   ` Markus Wichmann
  2022-01-22 14:51 ` [musl] [PATCH v2 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension) Ismael Luceno
  1 sibling, 1 reply; 5+ messages in thread
From: Ismael Luceno @ 2022-01-22 14:51 UTC (permalink / raw)
  To: musl; +Cc: Rich Felker, Ismael Luceno

Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
---
 src/misc/nftw.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index 8dcff7fefd2a..0a35686e663d 100644
--- a/src/misc/nftw.c
+++ b/src/misc/nftw.c
@@ -87,6 +87,8 @@ 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)
+				fchdir(dfd);
 			while ((de = readdir(d))) {
 				if (de->d_name[0] == '.'
 				 && (!de->d_name[1]
@@ -123,6 +125,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 +136,16 @@ 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);
+
 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 	r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
 	pthread_setcancelstate(cs, 0);
+	if (flags & FTW_CHDIR) {
+		fchdir(orig_dfd);
+		close(orig_dfd);
+	}
 	return r;
 }
 
-- 
2.33.0


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

* [musl] [PATCH v2 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension)
  2022-01-22 14:51 [musl] [PATCH v2 0/2] nftw: Implement FTW_CHDIR and FTW_ACTIONRETVAL Ismael Luceno
  2022-01-22 14:51 ` [musl] [PATCH v2 1/2] nftw: implement FTW_CHDIR Ismael Luceno
@ 2022-01-22 14:51 ` Ismael Luceno
  1 sibling, 0 replies; 5+ messages in thread
From: Ismael Luceno @ 2022-01-22 14:51 UTC (permalink / raw)
  To: musl; +Cc: Rich Felker, Ismael Luceno

Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
---
 include/ftw.h   |  7 +++++++
 src/misc/nftw.c | 33 +++++++++++++++++++++++----------
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/include/ftw.h b/include/ftw.h
index b15c062a8389..2e77dc76e11b 100644
--- a/include/ftw.h
+++ b/include/ftw.h
@@ -21,6 +21,13 @@ extern "C" {
 #define FTW_CHDIR 4
 #define FTW_DEPTH 8
 
+#define FTW_ACTIONRETVAL  16
+/* return values for callback */
+#define FTW_CONTINUE	  0
+#define FTW_STOP          1
+#define FTW_SKIP_SUBTREE  2
+#define FTW_SKIP_SIBLINGS 3
+
 struct FTW {
 	int base;
 	int level;
diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index 0a35686e663d..17e287c4c091 100644
--- a/src/misc/nftw.c
+++ b/src/misc/nftw.c
@@ -26,7 +26,7 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
 	struct stat st;
 	struct history new;
 	int type;
-	int r;
+	int r, r2;
 	int dfd;
 	int err;
 	struct FTW lev;
@@ -72,12 +72,19 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
 		if (!fd_limit) close(dfd);
 	}
 
-	if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
-		return r;
+	r = 0;
+	if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
+		if ((flags & FTW_ACTIONRETVAL)) {
+			if (r == FTW_STOP) return FTW_STOP;
+			if (r == FTW_SKIP_SUBTREE) return 0;
+			/* other values are saved for when returning */
+		} else
+			return r;
+	}
 
 	for (; h; h = h->chain)
 		if (h->dev == st.st_dev && h->ino == st.st_ino)
-			return 0;
+			return r;
 
 	if ((type == FTW_D || type == FTW_DP) && fd_limit) {
 		if (dfd < 0) {
@@ -101,9 +108,12 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
 				}
 				path[j]='/';
 				strcpy(path+j+1, de->d_name);
-				if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
+				if ((r2=do_nftw(path, fn, fd_limit-1, flags, &new))) {
+					if ((flags & FTW_ACTIONRETVAL)
+					    && r2 == FTW_SKIP_SIBLINGS)
+						break;
 					closedir(d);
-					return r;
+					return r2;
 				}
 			}
 			closedir(d);
@@ -114,10 +124,13 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
 	}
 
 	path[l] = 0;
-	if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
-		return r;
-
-	return 0;
+	if (flags & FTW_DEPTH) {
+		r = fn(path, &st, type, &lev);
+		/* ignore FTW_SKIP_SUBTREE (too late), the caller is broken */
+		if ((flags & FTW_ACTIONRETVAL) && r == FTW_SKIP_SUBTREE)
+			return 0;
+	}
+	return r;
 }
 
 int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags)
-- 
2.33.0


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

* Re: [musl] [PATCH v2 1/2] nftw: implement FTW_CHDIR
  2022-01-22 14:51 ` [musl] [PATCH v2 1/2] nftw: implement FTW_CHDIR Ismael Luceno
@ 2022-01-22 15:50   ` Markus Wichmann
  2022-01-23 15:47     ` Ismael Luceno
  0 siblings, 1 reply; 5+ messages in thread
From: Markus Wichmann @ 2022-01-22 15:50 UTC (permalink / raw)
  To: musl

On Sat, Jan 22, 2022 at 03:51:58PM +0100, Ismael Luceno wrote:
> @@ -133,9 +136,16 @@ 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);
> +
>  	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
>  	r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
>  	pthread_setcancelstate(cs, 0);
> +	if (flags & FTW_CHDIR) {
> +		fchdir(orig_dfd);
> +		close(orig_dfd);
> +	}
>  	return r;
>  }
>
> --
> 2.33.0
>

Erm... maybe a dumb question, but what if either the open() or the
fchdir() fails? Is anything specified for that case? Is effectively
ignoring FTW_CHDIR OK in that case or would you have to signal failure?
I mean, I see that there is not much you can do in either case, but to
just fail silently seems wrong to me.

Ciao,
Markus

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

* Re: [musl] [PATCH v2 1/2] nftw: implement FTW_CHDIR
  2022-01-22 15:50   ` Markus Wichmann
@ 2022-01-23 15:47     ` Ismael Luceno
  0 siblings, 0 replies; 5+ messages in thread
From: Ismael Luceno @ 2022-01-23 15:47 UTC (permalink / raw)
  To: musl

On 22/Jan/2022 16:50, Markus Wichmann wrote:
> On Sat, Jan 22, 2022 at 03:51:58PM +0100, Ismael Luceno wrote:
> > @@ -133,9 +136,16 @@ 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);
> > +
> >  	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
> >  	r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
> >  	pthread_setcancelstate(cs, 0);
> > +	if (flags & FTW_CHDIR) {
> > +		fchdir(orig_dfd);
> > +		close(orig_dfd);
> > +	}
> >  	return r;
> >  }
> >
> > --
> > 2.33.0
> >
> 
> Erm... maybe a dumb question, but what if either the open() or the
> fchdir() fails? Is anything specified for that case? Is effectively
> ignoring FTW_CHDIR OK in that case or would you have to signal failure?
> I mean, I see that there is not much you can do in either case, but to
> just fail silently seems wrong to me.

Ah, indeed. I'll try to address that.

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

end of thread, other threads:[~2022-01-23 15:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-22 14:51 [musl] [PATCH v2 0/2] nftw: Implement FTW_CHDIR and FTW_ACTIONRETVAL Ismael Luceno
2022-01-22 14:51 ` [musl] [PATCH v2 1/2] nftw: implement FTW_CHDIR Ismael Luceno
2022-01-22 15:50   ` Markus Wichmann
2022-01-23 15:47     ` Ismael Luceno
2022-01-22 14:51 ` [musl] [PATCH v2 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension) Ismael Luceno

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).