From 2fbc6105d73fe9550f8a7d1f36e5db80a17a1531 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 29 Mar 2023 22:23:52 +0200 Subject: [PATCH] kernel+libc: Add O_DIRECTORY and use it in opendir() --- kernel/src/sys/open.cpp | 2 ++ libc/include/bits/open-flags.h | 1 + libc/src/dirent.cpp | 3 +-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel/src/sys/open.cpp b/kernel/src/sys/open.cpp index 4456bec9..4513a5a8 100644 --- a/kernel/src/sys/open.cpp +++ b/kernel/src/sys/open.cpp @@ -44,6 +44,8 @@ Result sys_open(Registers*, SyscallArgs args) if ((flags & O_WRONLY) && (inode->mode() & S_IWUSR) == 0) return err(EACCES); } + if(inode->type() != VFS::InodeType::Directory && (flags & O_DIRECTORY)) return err(ENOTDIR); + if ((flags & O_WRONLY) && (flags & O_TRUNC)) inode->truncate(0); int fd = TRY(current->allocate_fd(0)); diff --git a/libc/include/bits/open-flags.h b/libc/include/bits/open-flags.h index 2b9e3e78..58179a8c 100644 --- a/libc/include/bits/open-flags.h +++ b/libc/include/bits/open-flags.h @@ -12,6 +12,7 @@ #define O_TRUNC 32 #define O_NONBLOCK 64 #define O_CLOEXEC 128 +#define O_DIRECTORY 256 #define O_ACCMODE O_RDWR diff --git a/libc/src/dirent.cpp b/libc/src/dirent.cpp index 650026fe..48dcf7e1 100644 --- a/libc/src/dirent.cpp +++ b/libc/src/dirent.cpp @@ -13,8 +13,7 @@ extern "C" DIR* dp = (DIR*)malloc(sizeof(DIR)); if (!dp) { return nullptr; } - // FIXME: Use O_DIRECTORY (validate that path is actually a directory) - int fd = open(path, O_RDONLY); + int fd = open(path, O_RDONLY | O_DIRECTORY); if (fd < 0) { free(dp);