From 0a689270b395b92f3130780de98ae479a8a5694e Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sat, 21 Oct 2023 10:31:30 +0300 Subject: [PATCH] fs fixes and clean --- src/Core/PS4/HLE/ErrorCodes.h | 5 +- .../LibKernel/FileSystem/file_system.cpp | 58 ++++++++++--------- .../LibKernel/FileSystem/file_system.h | 2 +- src/Emulator/Host/fs.cpp | 22 +++---- src/Emulator/Host/fs.h | 4 +- 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/Core/PS4/HLE/ErrorCodes.h b/src/Core/PS4/HLE/ErrorCodes.h index 0e6d3c19..37d334d5 100644 --- a/src/Core/PS4/HLE/ErrorCodes.h +++ b/src/Core/PS4/HLE/ErrorCodes.h @@ -2,10 +2,11 @@ constexpr int SCE_OK = 0; constexpr int SCE_KERNEL_ERROR_EBADF = 0x80020009; -constexpr int SCE_KERNEL_ERROR_ENOMEM = 0x8002000c; // Insufficient memory -constexpr int SCE_KERNEL_ERROR_EFAULT = 0x8002000e; // Invalid address pointer +constexpr int SCE_KERNEL_ERROR_ENOMEM = 0x8002000c; // Insufficient memory +constexpr int SCE_KERNEL_ERROR_EFAULT = 0x8002000e; // Invalid address pointer constexpr int SCE_KERNEL_ERROR_ENOTDIR = 0x80020014; constexpr int SCE_KERNEL_ERROR_EINVAL = 0x80020016; // null or invalid states +constexpr int SCE_KERNEL_ERROR_EMFILE = 0x80020018; // Limit on the number of file descriptors that can be open has been reached constexpr int SCE_KERNEL_ERROR_EAGAIN = 0x80020023; // Memory cannot be allocated constexpr int SCE_KERNEL_ERROR_ENAMETOOLONG = 0x8002003f; // character strings exceeds valid size diff --git a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.cpp b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.cpp index f25d1ec8..6fab0ce2 100644 --- a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.cpp +++ b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.cpp @@ -1,51 +1,55 @@ #include "file_system.h" -#include -#include -#include -#include "Emulator/Host/fs.h" -#include +#include +#include +#include +#include + +#include "Emulator/Host/fs.h" namespace Emulator::HLE::Libraries::LibKernel::FileSystem { constexpr bool log_file_fs = true; // disable it to disable logging int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) { - LOG_INFO_IF(log_file_fs, "sceKernelOpen path = {} flags = {} mode = {}\n", path, log_hex_full(flags), log_hex_full(mode)); + LOG_INFO_IF(log_file_fs, "sceKernelOpen path = {} flags = {} mode = {}\n", path, log_hex_full(flags), log_hex_full(mode)); - bool isDirectory = (flags & SCE_KERNEL_O_DIRECTORY) != 0; - bool createFileOrDir = (flags & SCE_KERNEL_O_CREAT) != 0; + bool isDirectory = (flags & SCE_KERNEL_O_DIRECTORY) != 0; + bool create = (flags & SCE_KERNEL_O_CREAT) != 0; - auto* h = singleton::instance(); + auto* h = singleton::instance(); auto* mnt = singleton::instance(); + u32 handle = 0; + if (h->createHandle().has_value()) { + handle = h->createHandle().value(); + } else { + return SCE_KERNEL_ERROR_EMFILE; // file descriptors is full error(?) + } + Emulator::Host::Fs::File* file = h->getFile(handle); - u32 handle = h->createHandle().value();//TODO check if overflows - - Emulator::Host::Fs::File file = h->getFile(handle); - - file.guest_path = path; + file->guest_path = path; if (isDirectory) { - file.host_path = mnt->getHostDirectory(path); - if (!std::filesystem::exists(file.host_path)) { //directory doesn't exist - if (createFileOrDir) { //if we have a create flag create it - if (std::filesystem::create_directories(file.host_path)) { + file->host_path = mnt->getHostDirectory(path); + if (!std::filesystem::is_directory(file->host_path)) { // directory doesn't exist + if (create) { // if we have a create flag create it + if (std::filesystem::create_directories(file->host_path)) { return handle; } else { return SCE_KERNEL_ERROR_ENOTDIR; } - } else { + } else { return SCE_KERNEL_ERROR_ENOTDIR; - } + } } - } - return handle; + } + return handle; } -int PS4_SYSV_ABI sceKernelClose(int d) { - LOG_INFO_IF(log_file_fs, "sceKernelClose descriptor = {}\n", d); +int PS4_SYSV_ABI sceKernelClose(int handle) { + LOG_INFO_IF(log_file_fs, "sceKernelClose descriptor = {}\n", handle); auto* h = singleton::instance(); - Emulator::Host::Fs::File file = h->getFile(d); - file.valid = false; - h->freeHandle(d); + Emulator::Host::Fs::File* file = h->getFile(handle); + file->valid = false; + h->freeHandle(handle); return SCE_OK; } } // namespace Emulator::HLE::Libraries::LibKernel::FileSystem \ No newline at end of file diff --git a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.h b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.h index 06680b06..069241ba 100644 --- a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.h +++ b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/file_system.h @@ -7,6 +7,6 @@ constexpr u32 SCE_KERNEL_O_CREAT = 0x0200; constexpr u32 SCE_KERNEL_O_DIRECTORY = 0x00020000; int PS4_SYSV_ABI sceKernelOpen(const char *path, int flags, /* SceKernelMode*/ u16 mode); -int PS4_SYSV_ABI sceKernelClose(int d); +int PS4_SYSV_ABI sceKernelClose(int handle); } // namespace Emulator::HLE::Libraries::LibKernel::FileSystem \ No newline at end of file diff --git a/src/Emulator/Host/fs.cpp b/src/Emulator/Host/fs.cpp index b03e941f..be36a35e 100644 --- a/src/Emulator/Host/fs.cpp +++ b/src/Emulator/Host/fs.cpp @@ -1,33 +1,33 @@ #include "fs.h" + #include namespace Emulator::Host::Fs { -void MntPoints::mount(const std::string& host_folder, const std::string& guest_folder) { - Lib::LockMutexGuard lock(m_mutex); +void MntPoints::mount(const std::string& host_folder, const std::string& guest_folder) { + Lib::LockMutexGuard lock(m_mutex); - MntPair pair; + MntPair pair; pair.host_path = host_folder; pair.guest_path = guest_folder; - m_mnt_pairs.push_back(pair); + m_mnt_pairs.push_back(pair); } void MntPoints::unMount(const std::string& path) {} -void MntPoints::unMountAll() {} -std::string MntPoints::getHostDirectory(const std::string& guest_directory) { - Lib::LockMutexGuard lock(m_mutex); - for (auto& pair : m_mnt_pairs) - { +void MntPoints::unmountAll() {} +std::string MntPoints::getHostDirectory(const std::string& guest_directory) { + Lib::LockMutexGuard lock(m_mutex); + for (auto& pair : m_mnt_pairs) { if (pair.guest_path.starts_with(guest_directory)) { return pair.host_path + guest_directory; } } - //hack for relative path , get app0 and assuming it goes from there + // hack for relative path , get app0 and assuming it goes from there for (auto& pair : m_mnt_pairs) { if (pair.guest_path.starts_with("/app0")) { std::replace(pair.host_path.begin(), pair.host_path.end(), '\\', '/'); return pair.host_path + guest_directory; } } - return std::string(); + return ""; } } // namespace Emulator::Host::Fs \ No newline at end of file diff --git a/src/Emulator/Host/fs.h b/src/Emulator/Host/fs.h index 7fcf33a7..3f62122b 100644 --- a/src/Emulator/Host/fs.h +++ b/src/Emulator/Host/fs.h @@ -23,7 +23,7 @@ class MntPoints { virtual ~MntPoints() {} void mount(const std::string& host_folder, const std::string& guest_folder); void unMount(const std::string& path); - void unMountAll(); + void unmountAll(); std::string getHostDirectory(const std::string& guest_directory); private: @@ -88,7 +88,7 @@ class HandleTable { return handle; } - File getFile(u32 handle) { return files[handle]; } + File* getFile(u32 handle) { return &files[handle]; } void freeHandle(u32 handle) { if (handle >= files.size()) {