diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index 8d1829781..300d24263 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -67,6 +67,12 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { bool write = (flags & 0x3) == ORBIS_KERNEL_O_WRONLY; bool rdwr = (flags & 0x3) == ORBIS_KERNEL_O_RDWR; + if (!read && !write && !rdwr) { + // Start by checking for invalid flags. + *__Error() = POSIX_EINVAL; + return -1; + } + bool nonblock = (flags & ORBIS_KERNEL_O_NONBLOCK) != 0; bool append = (flags & ORBIS_KERNEL_O_APPEND) != 0; bool fsync = (flags & ORBIS_KERNEL_O_FSYNC) != 0; @@ -114,10 +120,10 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { *__Error() = POSIX_EROFS; return -1; } - // Create file if it doesn't exist + // Create a file if it doesn't exist Common::FS::IOFile out(file->m_host_name, Common::FS::FileAccessMode::Write); } else if (!exists) { - // File to open doesn't exist, return ENOENT + // If we're not creating a file, and it doesn't exist, return ENOENT h->DeleteHandle(handle); *__Error() = POSIX_ENOENT; return -1; @@ -126,10 +132,7 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { if (std::filesystem::is_directory(file->m_host_name) || directory) { // Directories can be opened even if the directory flag isn't set. // In these cases, error behavior is identical to the directory code path. - directory = true; - file->type = Core::FileSys::FileType::Directory; - } else { - file->type = Core::FileSys::FileType::Regular; + directory = true; } if (directory) { @@ -141,6 +144,8 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { return -1; } + file->type = Core::FileSys::FileType::Directory; + // Populate directory contents mnt->IterateDirectory(file->m_guest_name, [&file](const auto& ent_path, const auto ent_is_file) { @@ -157,11 +162,6 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { h->DeleteHandle(handle); *__Error() = POSIX_EISDIR; return -1; - } else { - // Invalid flags - h->DeleteHandle(handle); - *__Error() = POSIX_EINVAL; - return -1; } if (e == EACCES) { @@ -181,6 +181,8 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { // Since open starts by closing the file, this won't interfere with later open calls. e = file->f.Open(file->m_host_name, Common::FS::FileAccessMode::ReadWrite); + file->type = Core::FileSys::FileType::Regular; + if (truncate && read_only) { // Can't open files with truncate flag in a read only directory h->DeleteHandle(handle); @@ -208,11 +210,6 @@ s32 PS4_SYSV_ABI open(const char* raw_path, s32 flags, u16 mode) { } else if (rdwr) { // Read and write e = file->f.Open(file->m_host_name, Common::FS::FileAccessMode::ReadWrite); - } else { - // Invalid flags - h->DeleteHandle(handle); - *__Error() = POSIX_EINVAL; - return -1; } }