mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Tony Ambardar <tony.ambardar@gmail.com>
To: musl@lists.openwall.com
Cc: Tony Ambardar <Tony.Ambardar@gmail.com>
Subject: [musl] [PATCH v1] nftw: support FTW_ACTIONRETVAL extension
Date: Fri, 25 Sep 2020 02:18:28 -0700	[thread overview]
Message-ID: <20200925091828.306644-1-Tony.Ambardar@gmail.com> (raw)

Add the FTW_ACTIONRETVAL mode for walking file trees, a commonly used glibc
extension. This updates the existing musl nftw/ftw code to support the new
mode and include options e.g. to prune trees and skip tree siblings.

This patch is based on one originally submitted to musl's mailing list [1],
which was reviewed with interest but received no follow-up. It has since
been cleaned up, and tested on OpenWRT for building Linux's 'bpftool'
utility (the primary tool for BPF introspection and manipulation).

Including this extension should also further reduce any need for fts(3)
support.

[1] https://www.openwall.com/lists/musl/2018/12/16/1

Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
---
 include/ftw.h   |  8 ++++++++
 src/misc/nftw.c | 35 ++++++++++++++++++++++++++++++-----
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/include/ftw.h b/include/ftw.h
index b15c062a..ce85deac 100644
--- a/include/ftw.h
+++ b/include/ftw.h
@@ -21,6 +21,14 @@ extern "C" {
 #define FTW_CHDIR 4
 #define FTW_DEPTH 8
 
+#ifdef _GNU_SOURCE
+#define FTW_ACTIONRETVAL 16
+#define FTW_CONTINUE 0
+#define FTW_STOP 1
+#define FTW_SKIP_SUBTREE 2
+#define FTW_SKIP_SIBLINGS 3
+#endif
+
 struct FTW {
 	int base;
 	int level;
diff --git a/src/misc/nftw.c b/src/misc/nftw.c
index 8dcff7fe..0bb7b601 100644
--- a/src/misc/nftw.c
+++ b/src/misc/nftw.c
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
 #include <ftw.h>
 #include <dirent.h>
 #include <fcntl.h>
@@ -72,8 +73,20 @@ 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;
+	if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
+		if (flags & FTW_ACTIONRETVAL)
+			switch (r) {
+			case FTW_SKIP_SUBTREE:
+				h = NULL;
+			case FTW_CONTINUE:
+				break;
+			case FTW_SKIP_SIBLINGS:
+			case FTW_STOP:
+				return r;
+			}
+		else
+			return r;
+	}
 
 	for (; h; h = h->chain)
 		if (h->dev == st.st_dev && h->ino == st.st_ino)
@@ -101,7 +114,10 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
 				strcpy(path+j+1, de->d_name);
 				if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
 					closedir(d);
-					return r;
+					if ((flags & FTW_ACTIONRETVAL) && r == FTW_SKIP_SIBLINGS)
+						break;
+					else
+						return r;
 				}
 			}
 			closedir(d);
@@ -112,8 +128,16 @@ 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;
+	if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
+		if (flags & FTW_ACTIONRETVAL)
+			switch (r) {
+				case FTW_SKIP_SIBLINGS:
+				case FTW_STOP:
+					return r;
+			}
+		else
+			return r;
+	}
 
 	return 0;
 }
@@ -139,4 +163,5 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str
 	return r;
 }
 
+#undef nftw64
 weak_alias(nftw, nftw64);
-- 
2.25.1


             reply	other threads:[~2020-09-25  9:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-25  9:18 Tony Ambardar [this message]
2020-09-25 19:06 ` 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=20200925091828.306644-1-Tony.Ambardar@gmail.com \
    --to=tony.ambardar@gmail.com \
    --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).