From 6cf627be8a472a05e05ada8cf62e56dffbeea328 Mon Sep 17 00:00:00 2001 From: stasoid Date: Sun, 5 Jan 2025 12:36:03 +0500 Subject: [PATCH] LibCore: Simplify System::open _O_OBTAIN_DIR flag makes _open use FILE_FLAG_BACKUP_SEMANTICS in CreateFile call. FILE_FLAG_BACKUP_SEMANTICS is required to open directory handles. For ordinary files FILE_FLAG_BACKUP_SEMANTICS overrides file security checks when the process has SE_BACKUP_NAME and SE_RESTORE_NAME privileges, see https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea --- Libraries/LibCore/SystemWindows.cpp | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/Libraries/LibCore/SystemWindows.cpp b/Libraries/LibCore/SystemWindows.cpp index ceb45498294..7b7deda69c6 100644 --- a/Libraries/LibCore/SystemWindows.cpp +++ b/Libraries/LibCore/SystemWindows.cpp @@ -21,23 +21,10 @@ namespace Core::System { ErrorOr open(StringView path, int options, mode_t mode) { - ByteString string_path = path; - auto sz_path = string_path.characters(); - int rc = _open(sz_path, options | O_BINARY, 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(); - 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; + int fd = _open(ByteString(path).characters(), options | O_BINARY | _O_OBTAIN_DIR, mode); + if (fd < 0) + return Error::from_syscall("open"sv, -errno); + return fd; } ErrorOr close(int fd)