[PATCH 3/3] tools/nolibc: validate directory with O_DIRECTORY in opendir()

From: Thomas Weißschuh

Date: Mon Aug 31 2026 - 15:47:05 EST


fdopendir() requires a call to fstat() to determine if the opened file
is a directory. Currently opendir() inherits this extra syscall.

Switch to O_DIRECTORY and remove the call to fdopendir() in opendir()
to make the directory type check cheaper.

Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
tools/include/nolibc/dirent.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/include/nolibc/dirent.h b/tools/include/nolibc/dirent.h
index 25fbff208998..2dbf4052b85a 100644
--- a/tools/include/nolibc/dirent.h
+++ b/tools/include/nolibc/dirent.h
@@ -55,10 +55,11 @@ DIR *opendir(const char *name)
{
int fd;

- fd = open(name, O_RDONLY);
+ fd = open(name, O_RDONLY | O_DIRECTORY);
if (fd == -1)
return NULL;
- return fdopendir(fd);
+
+ return (DIR *)(intptr_t)~fd;
}

static __attribute__((unused))

--
2.55.0