1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00

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
This commit is contained in:
stasoid 2025-01-05 12:36:03 +05:00 committed by Andrew Kaster
parent 870cce9d11
commit 259cd70c1b
Notes: github-actions[bot] 2025-02-06 02:28:51 +00:00

View file

@ -21,23 +21,10 @@ namespace Core::System {
ErrorOr<int> 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<void> close(int fd)