diff --git a/CMakeLists.txt b/CMakeLists.txt index d4d605f4..adede79d 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..18884b74 --- /dev/null +++ b/src/core/file_sys/fs.cpp @@ -0,0 +1,91 @@ +#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 ""; +} +std::string MntPoints::getHostFile(const std::string& guest_file) { + std::scoped_lock lock{m_mutex}; + + for (auto& pair : m_mnt_pairs) { + //horrible code but it works :D + int find = guest_file.find(pair.guest_path); + if (find == 0) { + std::string npath = guest_file.substr(pair.guest_path.size(), guest_file.size()-1); + std::replace(pair.host_path.begin(), pair.host_path.end(), '\\', '/'); + return pair.host_path + npath; + } + } + 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 00000000..75ea174c --- /dev/null +++ b/src/core/file_sys/fs.h @@ -0,0 +1,54 @@ +#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); + std::string MntPoints::getHostFile(const std::string& guest_file); + + 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; + std::mutex m_mutex; +}; +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/main.cpp b/src/main.cpp index d9d56865..b7723ae7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include "emuTimer.h" #include "emulator.h" #include +#include "core/file_sys/fs.h" int main(int argc, char* argv[]) { if (argc == 1) { @@ -35,6 +36,10 @@ int main(int argc, char* argv[]) { // Argument 1 is the path of self file to boot const char* const path = argv[1]; + auto* mnt = Common::Singleton::Instance(); + std::filesystem::path p = std::string(path); + mnt->mount(p.parent_path().string(), "/app0"); + auto linker = Common::Singleton::Instance(); Core::Libraries::InitHLELibs(&linker->getHLESymbols()); linker->LoadModule(path);