mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Ismael Luceno <ismael@iodev.co.uk>
To: musl@lists.openwall.com
Cc: Rich Felker <dalias@libc.org>, Ismael Luceno <ismael@iodev.co.uk>
Subject: [musl] [PATCH v3 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension)
Date: Sun, 23 Jan 2022 16:59:55 +0100	[thread overview]
Message-ID: <20220123155955.16484-3-ismael@iodev.co.uk> (raw)
In-Reply-To: <20220123155955.16484-1-ismael@iodev.co.uk>

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 7569a657e54e..382169f66b8b 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) {
@@ -107,9 +114,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);
@@ -120,10 +130,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


  parent reply	other threads:[~2022-01-23 15:56 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-23 15:59 [musl] [PATCH v3 0/2] nftw: Implement FTW_CHDIR and FTW_ACTIONRETVAL Ismael Luceno
2022-01-23 15:59 ` [musl] [PATCH v3 1/2] nftw: implement FTW_CHDIR Ismael Luceno
2022-05-12 13:00   ` Rich Felker
2022-01-23 15:59 ` Ismael Luceno [this message]
2022-01-24  0:47   ` [musl] [PATCH v3 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension) Dominique MARTINET
2022-01-24 19:53     ` Ismael Luceno
2022-03-09  4:37       ` Dominique MARTINET
2022-03-09 11:41         ` Rich Felker
2022-04-28  2:02           ` Dominique MARTINET
2022-01-24 22:13 ` [musl] [PATCH v3 0/2] nftw: Implement FTW_CHDIR and FTW_ACTIONRETVAL Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220123155955.16484-3-ismael@iodev.co.uk \
    --to=ismael@iodev.co.uk \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).