From 77d205571d297b0948e0896d6c49ebc2194c06e4 Mon Sep 17 00:00:00 2001 From: stasoid Date: Thu, 24 Oct 2024 00:25:09 +0500 Subject: [PATCH] LibCore/System: Add mkdir, openat (stub), fstatat (stub) for Windows Also support directories in open(). --- Libraries/LibCore/SystemWindows.cpp | 41 ++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/Libraries/LibCore/SystemWindows.cpp b/Libraries/LibCore/SystemWindows.cpp index e1a8dae9614..9587ec9d10f 100644 --- a/Libraries/LibCore/SystemWindows.cpp +++ b/Libraries/LibCore/SystemWindows.cpp @@ -12,7 +12,8 @@ #include #include #include -#include +#include +#include #include namespace Core::System { @@ -20,9 +21,21 @@ namespace Core::System { ErrorOr open(StringView path, int options, mode_t mode) { ByteString string_path = path; - int rc = _open(string_path.characters(), options, mode); - if (rc < 0) - return Error::from_syscall("open"sv, -errno); + auto sz_path = string_path.characters(); + int rc = _open(sz_path, options, mode); + if (rc < 0) { + int error = errno; + struct stat st = {}; + if (::stat(sz_path, &st) == 0 && (st.st_mode & S_IFDIR)) { + HANDLE dir_handle = CreateFile(sz_path, GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (dir_handle == INVALID_HANDLE_VALUE) + return Error::from_windows_error(GetLastError()); + int dir_fd = _open_osfhandle((intptr_t)dir_handle, 0); + if (dir_fd != -1) + return dir_fd; + } + return Error::from_syscall("open"sv, -error); + } return rc; } @@ -133,4 +146,24 @@ ErrorOr unlink(StringView path) return {}; } +ErrorOr mkdir(StringView path, mode_t) +{ + ByteString str = path; + if (_mkdir(str.characters()) < 0) + return Error::from_syscall("mkdir"sv, -errno); + return {}; +} + +ErrorOr openat(int, StringView, int, mode_t) +{ + dbgln("Core::System::openat() is not implemented"); + VERIFY_NOT_REACHED(); +} + +ErrorOr fstatat(int, StringView, int) +{ + dbgln("Core::System::fstatat() is not implemented"); + VERIFY_NOT_REACHED(); +} + }