From a1e5fcf0c534f4f35f58e7f315fb2eedf604b1bf Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Mon, 13 Nov 2023 13:05:39 +0200 Subject: [PATCH] added fs.cpp , fs.h from pr #70 --- CMakeLists.txt | 4 +- src/core/file_sys/fs.cpp | 77 ++++++++++++++++++++++ src/core/file_sys/fs.h | 52 +++++++++++++++ src/core/hle/libraries/libc/libc.cpp | 2 + src/core/hle/libraries/libc/libc_stdio.cpp | 27 ++++---- src/core/hle/libraries/libc/libc_stdio.h | 2 + 6 files changed, 152 insertions(+), 12 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 d4d605f41..a2dc24a5c 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 000000000..c51828965 --- /dev/null +++ b/src/core/file_sys/fs.cpp @@ -0,0 +1,77 @@ +#include "fs.h" + +#include + +namespace Core::FileSys { + +constexpr int RESERVED_HANDLES = 3; // First 3 handles are stdin,stdout,stderr + +void MntPoints::mount(const std::string& host_folder, const std::string& guest_folder) { + std::scoped_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::scoped_lock lock{m_mutex}; + m_mnt_pairs.clear(); +} +std::string MntPoints::getHostDirectory(const std::string& guest_directory) { + std::scoped_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::scoped_lock lock{m_mutex}; + auto* file = new File{}; + file->isDirectory = false; + file->isOpened = false; + + int existingFilesNum = m_files.size(); + + for (int index = 0; index < existingFilesNum; index++) { + if (m_files.at(index) == nullptr) { + m_files[index] = file; + return index + RESERVED_HANDLES; + } + } + + m_files.push_back(file); + + return m_files.size() + RESERVED_HANDLES - 1; +} +void HandleTable::deleteHandle(int d) { + std::scoped_lock lock{m_mutex}; + delete m_files.at(d - RESERVED_HANDLES); + m_files[d - RESERVED_HANDLES] = nullptr; +} + +File* HandleTable::getFile(int d) { + std::scoped_lock lock{m_mutex}; + return m_files.at(d - RESERVED_HANDLES); +} +File* HandleTable::getFile(const std::string& host_name) { + std::scoped_lock lock{m_mutex}; + for (auto* file : m_files) { + if (file != nullptr && file->m_host_name == host_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 000000000..0de2cd423 --- /dev/null +++ b/src/core/file_sys/fs.h @@ -0,0 +1,52 @@ +#pragma once +#include +#include +#include + +#include "common/fs_file.h" + +namespace Core::FileSys { + +class MntPoints { + public: + struct MntPair { + std::string host_path; + std::string guest_path; // e.g /app0/ + }; + + MntPoints() = default; + virtual ~MntPoints() = default; + 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_host_name; + std::string m_guest_name; + Common::FS::File f; + //std::vector dirents; + u32 dirents_index; +}; +class HandleTable { + public: + HandleTable() {} + virtual ~HandleTable() {} + int createHandle(); + void deleteHandle(int d); + File* getFile(int d); + File* getFile(const std::string& host_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/libc/libc.cpp b/src/core/hle/libraries/libc/libc.cpp index f3113ecb0..8b1e8dd15 100644 --- a/src/core/hle/libraries/libc/libc.cpp +++ b/src/core/hle/libraries/libc/libc.cpp @@ -442,6 +442,8 @@ void libcSymbolsRegister(Loader::SymbolsResolver* sym) { LIB_FUNCTION("fffwELXNVFA", "libc", 1, "libc", 1, 1, fprintf); LIB_FUNCTION("eLdDw6l0-bU", "libc", 1, "libc", 1, 1, snprintf); LIB_FUNCTION("tcVi5SivF7Q", "libc", 1, "libc", 1, 1, sprintf); + LIB_FUNCTION("xeYO4u7uyJ0", "libc", 1, "libc", 1, 1, fopen); + LIB_FUNCTION("uodLYyUip20", "libc", 1, "libc", 1, 1, fclose); // misc LIB_OBJ("P330P3dFF68", "libc", 1, "libc", 1, 1, &g_need_sceLibc); LIB_OBJ("2sWzhYqFH4E","libc", 1, "libc", 1, 1,stdout); diff --git a/src/core/hle/libraries/libc/libc_stdio.cpp b/src/core/hle/libraries/libc/libc_stdio.cpp index b5374ec0d..c5d4f2468 100644 --- a/src/core/hle/libraries/libc/libc_stdio.cpp +++ b/src/core/hle/libraries/libc/libc_stdio.cpp @@ -1,6 +1,7 @@ +#include "core/hle/libraries/libc/libc_stdio.h" + #include "common/debug.h" #include "common/log.h" -#include "core/hle/libraries/libc/libc_stdio.h" namespace Core::Libraries::LibC { @@ -22,22 +23,26 @@ int PS4_SYSV_ABI fprintf(FILE* file, VA_ARGS) { return 0; } -int PS4_SYSV_ABI snprintf(char* s, size_t n, VA_ARGS) { +int PS4_SYSV_ABI snprintf(char* s, size_t n, VA_ARGS) { VA_CTX(ctx); return snprintf_ctx(s, n, &ctx); } -int PS4_SYSV_ABI sprintf(char* s,VA_ARGS) { +int PS4_SYSV_ABI sprintf(char* s, VA_ARGS) { VA_CTX(ctx); - return sprintf_ctx(s,&ctx); + return sprintf_ctx(s, &ctx); } -int PS4_SYSV_ABI vsnprintf(char* s, size_t n, const char* format, VaList* arg) { - return vsnprintf_ctx(s, n, format, arg); -} +int PS4_SYSV_ABI vsnprintf(char* s, size_t n, const char* format, VaList* arg) { return vsnprintf_ctx(s, n, format, arg); } -int PS4_SYSV_ABI puts(const char* s) { - return std::puts(s); -} +int PS4_SYSV_ABI puts(const char* s) { return std::puts(s); } -} // namespace Core::Libraries::LibC +FILE* PS4_SYSV_ABI fopen(const char* filename, const char* mode) { + LOG_ERROR_IF(log_file_libc, "Unimplemented fopen filename={} , mode ={}\n", filename, mode); + return nullptr; +} +int PS4_SYSV_ABI fclose(FILE* stream) { + LOG_ERROR_IF(log_file_libc, "Unimplemented fclose\n"); + return 0; +} +} // namespace Core::Libraries::LibC diff --git a/src/core/hle/libraries/libc/libc_stdio.h b/src/core/hle/libraries/libc/libc_stdio.h index 4e4f64ff4..227058c5a 100644 --- a/src/core/hle/libraries/libc/libc_stdio.h +++ b/src/core/hle/libraries/libc/libc_stdio.h @@ -11,4 +11,6 @@ int PS4_SYSV_ABI puts(const char* s); int PS4_SYSV_ABI fprintf(FILE* file, VA_ARGS); int PS4_SYSV_ABI snprintf(char* s, size_t n, VA_ARGS); int PS4_SYSV_ABI sprintf(char* s, VA_ARGS); +FILE* PS4_SYSV_ABI fopen(const char* filename, const char* mode); +int PS4_SYSV_ABI fclose(FILE* stream); } // namespace Core::Libraries::LibC