From bfbe67bc42ba74e9b919e436e7e3c62e1fb77091 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Mon, 6 Nov 2023 08:32:27 +0200 Subject: [PATCH] fs mount points and handles --- CMakeLists.txt | 4 +- src/core/file_sys/fs.cpp | 66 +++++++++++++++++++ src/core/file_sys/fs.h | 45 +++++++++++++ .../hle/libraries/libkernel/file_system.cpp | 4 +- .../hle/libraries/libkernel/file_system.h | 2 +- 5 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 src/core/file_sys/fs.cpp create mode 100644 src/core/file_sys/fs.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c1483383..139ef57e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,7 +58,9 @@ set(SYSTEMSERVICE_SOURCES src/core/hle/libraries/libsystemservice/system_service ) set(FILESYSTEM_SOURCES src/core/hle/libraries/libkernel/file_system.cpp - src/core/hle/libraries/libkernel/file_system.h + src/core/hle/libraries/libkernel/file_system.h + src/core/file_sys/fs.cpp + src/core/file_sys/fs.h ) set(HOST_SOURCES src/Emulator/Host/controller.cpp diff --git a/src/core/file_sys/fs.cpp b/src/core/file_sys/fs.cpp new file mode 100644 index 00000000..29c35179 --- /dev/null +++ b/src/core/file_sys/fs.cpp @@ -0,0 +1,66 @@ +#include "fs.h" + +#include + +namespace Core::FileSys { +void MntPoints::mount(const std::string& host_folder, const std::string& guest_folder) { + std::unique_lock lock{m_mutex}; + + MntPair pair; + pair.host_path = host_folder; + pair.guest_path = guest_folder; + + m_mnt_pairs.push_back(pair); +} +void MntPoints::unmount(const std::string& path) {} // TODO! +void MntPoints::unmountAll() { + std::unique_lock lock{m_mutex}; + m_mnt_pairs.clear(); +} +std::string MntPoints::getHostDirectory(const std::string& guest_directory) { + std::unique_lock 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 + 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 ""; +} +int HandleTable::createHandle() { + std::unique_lock lock{m_mutex}; + auto* file = new File{}; + file->isDirectory = false; + file->isOpened = false; + + int existingFilesNum = m_files.size(); + // TODO when i close a file m_files probably have a open pos , so we can fill this + m_files.push_back(file); + + return existingFilesNum - 1; +} +void HandleTable::deleteHandle(int d) { + std::unique_lock lock{m_mutex}; + delete m_files.at(d); + m_files[d] = nullptr; +} +File* HandleTable::getFile(int d) { + std::unique_lock lock{m_mutex}; + return m_files.at(d); +} +File* HandleTable::getFile(const std::string& real_name) { + std::unique_lock lock{m_mutex}; + for (auto* file : m_files) { + if (file != nullptr && file->m_real_name == real_name) { + return file; + } + } + return nullptr; +} +} // namespace Core::FileSys \ No newline at end of file diff --git a/src/core/file_sys/fs.h b/src/core/file_sys/fs.h new file mode 100644 index 00000000..462093ba --- /dev/null +++ b/src/core/file_sys/fs.h @@ -0,0 +1,45 @@ +#pragma once +#include +#include +#include + +namespace Core::FileSys { + +class MntPoints { + public: + struct MntPair { + std::string host_path; + std::string guest_path; // e.g /app0/ + }; + + MntPoints() {} + virtual ~MntPoints() {} + void mount(const std::string& host_folder, const std::string& guest_folder); + void unmount(const std::string& path); + void unmountAll(); + std::string getHostDirectory(const std::string& guest_directory); + + private: + std::vector m_mnt_pairs; + std::mutex m_mutex; +}; + +struct File { + std::atomic_bool isOpened; + std::atomic_bool isDirectory; + std::string m_real_name; +}; +class HandleTable { + HandleTable() {} + virtual ~HandleTable() {} + int createHandle(); + void deleteHandle(int d); + File* getFile(int d); + File* getFile(const std::string& real_name); + + private: + std::vector m_files; + std::mutex m_mutex; +}; + +} // namespace Core::FileSys \ No newline at end of file diff --git a/src/core/hle/libraries/libkernel/file_system.cpp b/src/core/hle/libraries/libkernel/file_system.cpp index 271cf363..85461025 100644 --- a/src/core/hle/libraries/libkernel/file_system.cpp +++ b/src/core/hle/libraries/libkernel/file_system.cpp @@ -12,7 +12,7 @@ int PS4_SYSV_ABI sceKernelOpen(const char* path, int flags, u16 mode) { return 0; } -int PS4_SYSV_ABI open(const char* path, int flags, /* SceKernelMode*/ u16 mode) { +int PS4_SYSV_ABI posix_open(const char* path, int flags, /* SceKernelMode*/ u16 mode) { LOG_INFO_IF(log_file_fs, "posix open redirect to sceKernelOpen\n"); int result = sceKernelOpen(path, flags, mode); if (result < 0) { @@ -23,7 +23,7 @@ int PS4_SYSV_ABI open(const char* path, int flags, /* SceKernelMode*/ u16 mode) void fileSystemSymbolsRegister(Loader::SymbolsResolver* sym) { LIB_FUNCTION("1G3lF1Gg1k8", "libkernel", 1, "libkernel", 1, 1, sceKernelOpen); - LIB_FUNCTION("wuCroIGjt2g", "libScePosix", 1, "libkernel", 1, 1, open); + LIB_FUNCTION("wuCroIGjt2g", "libScePosix", 1, "libkernel", 1, 1, posix_open); } } // namespace Core::Libraries::LibKernel diff --git a/src/core/hle/libraries/libkernel/file_system.h b/src/core/hle/libraries/libkernel/file_system.h index f5be1d1e..010e952a 100644 --- a/src/core/hle/libraries/libkernel/file_system.h +++ b/src/core/hle/libraries/libkernel/file_system.h @@ -10,7 +10,7 @@ namespace Core::Libraries::LibKernel { int PS4_SYSV_ABI sceKernelOpen(const char *path, int flags, /* SceKernelMode*/ u16 mode); -int PS4_SYSV_ABI open(const char *path, int flags, /* SceKernelMode*/ u16 mode); +int PS4_SYSV_ABI posix_open(const char *path, int flags, /* SceKernelMode*/ u16 mode); void fileSystemSymbolsRegister(Loader::SymbolsResolver *sym);