diff mbox series

[pseudo,v3,1/3] Move ftw and ftw64 to calling ntfw and nftw64

Message ID 1746302395-8723-2-git-send-email-mark.hatle@kernel.crashing.org
State New
Headers show
Series nftw, ftw: add wrappers | expand

Commit Message

Mark Hatle May 3, 2025, 7:59 p.m. UTC
From: Mark Hatle <mark.hatle@amd.com>

The 4 functions are very similar to each other: nftw-nftw64 and ftw-ftw64 are
identical to their pair, except for the stat struct they use (stat vs stat64).

nftw is a "superset" of ftw: nftw is backwards compatible with ftw, but it
also accepts a number of extra flags to modify its behavior.

Since all the 4 functions are so similar, the same codebase is used to
implement all (which is also fairly similar to their implementation in
glibc).

[1]: https://linux.die.net/man/3/nftw

Re-implemented, but based on the ideas from Gyorgy Sarvari <skandigraun@gmail.com>

Signed-off-by: Mark Hatle <mark.hatle@amd.com>
---
 guts/README              |  4 ++--
 ports/linux/guts/ftw64.c |  9 ++++++++-
 ports/unix/guts/ftw.c    | 10 +++++++++-
 3 files changed, 19 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/guts/README b/guts/README
index 0a1fe5f..bb2e573 100644
--- a/guts/README
+++ b/guts/README
@@ -54,6 +54,8 @@  other functions:
 	__xmknod:		__xmknodat
 	__xstat64:		__fxstatat64
 	__xstat:		__fxstatat
+	ftw:			nftw
+	ftw64:			nftw64
 
 The following functions are full implementations:
 
@@ -129,8 +131,6 @@  calling the underlying routine.
 	eaccess
 	euidaccess
 	fts_open
-	ftw64
-	ftw
 	glob64
 	glob
 	lutimes
diff --git a/ports/linux/guts/ftw64.c b/ports/linux/guts/ftw64.c
index 48adb80..72e80f1 100644
--- a/ports/linux/guts/ftw64.c
+++ b/ports/linux/guts/ftw64.c
@@ -9,7 +9,14 @@ 
  *	int rc = -1;
  */
 
-	rc = real_ftw64(path, fn, nopenfd);
+	// 1. Set the flag argument to 0, just like glibc does.
+	// 2. The difference between ftw and nftw callback
+	//    is only the last parameter: struct FTW is only used
+	//    by nftw(), and it is missing from ftw().
+	//    However since otherwise the stacklayout for the
+	//    functions is the same, this cast should work just the
+	//    way we want it. This is also borrowed from glibc.
+	rc = wrap_nftw64(path, (void *)fn, nopenfd, 0);
 
 /*	return rc;
  * }
diff --git a/ports/unix/guts/ftw.c b/ports/unix/guts/ftw.c
index 58945a1..12a17f4 100644
--- a/ports/unix/guts/ftw.c
+++ b/ports/unix/guts/ftw.c
@@ -9,7 +9,15 @@ 
  *	int rc = -1;
  */
 
-	rc = real_ftw(path, fn, nopenfd);
+	// 1. Set the flag argument to 0, just like glibc does.
+	// 2. The difference between ftw and nftw callback
+	//    is only the last parameter: struct FTW is only used
+	//    by nftw(), and it is missing from ftw().
+	//    However since otherwise the stacklayout for the
+	//    functions is the same, this cast should work just the
+	//    way we want it. This is also borrowed from glibc.
+
+	rc = wrap_nftw(path, (void *)fn, nopenfd, 0);
 
 /*	return rc;
  * }