From ec362948f458ac82db0cf184ff55530d2d04f53f Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Thu, 19 Oct 2023 17:52:49 +0300 Subject: [PATCH] more draft fs --- .../FileSystem/generic_file_system.h | 2 + .../LibKernel/FileSystem/meta_file_system.cpp | 44 ++++++++++++++++--- .../LibKernel/FileSystem/meta_file_system.h | 14 ++++-- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/generic_file_system.h b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/generic_file_system.h index c43f15a5..424f1498 100644 --- a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/generic_file_system.h +++ b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/generic_file_system.h @@ -19,6 +19,8 @@ class GenericHandleAllocator { }; class AbstractFileSystem { + public: + virtual bool ownsHandle(u32 handle) = 0; virtual u32 openFile(std::string filename, FileAccess access) = 0; virtual void closeFile(u32 handle) = 0; }; diff --git a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/meta_file_system.cpp b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/meta_file_system.cpp index bcf17192..9750ee51 100644 --- a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/meta_file_system.cpp +++ b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/meta_file_system.cpp @@ -2,19 +2,51 @@ namespace Emulator::Host::GenericFS { -void MetaFileSystem::mount(std::string prefix, AbstractFileSystem* system) {} +void MetaFileSystem::mount(std::string prefix, AbstractFileSystem* system) { + System x; + x.prefix = prefix; + x.system = system; + fileSystems.push_back(x); +} void MetaFileSystem::unMount(AbstractFileSystem* system) {} -void MetaFileSystem::unMountAll() {} +void MetaFileSystem::unMountAll() { fileSystems.clear(); } -bool MetaFileSystem::mapFilePath(std::string inpath, std::string* outpath, AbstractFileSystem** system) { return false; } +AbstractFileSystem* MetaFileSystem::getHandleOwner(u32 handle) { + for (u32 i = 0; i < fileSystems.size(); i++) { + if (fileSystems[i].system->ownsHandle(handle)) return fileSystems[i].system; // got it! + } + return nullptr; -void MetaFileSystem::releaseHandle(u32 handle) {} +} -u32 MetaFileSystem::openFile(std::string filename, FileAccess access) { return u32(); } +bool MetaFileSystem::mapFilePath(std::string inpath, std::string* outpath, AbstractFileSystem** system) { + for (unsigned int i = 0; i < fileSystems.size(); i++) { + int prefLen = fileSystems[i].prefix.size(); + if (fileSystems[i].prefix == inpath.substr(0, prefLen)) + { + *outpath = inpath.substr(prefLen); + *system = fileSystems[i].system; + return true; + } + } + return false; +} -void MetaFileSystem::closeFile(u32 handle) {} +u32 MetaFileSystem::openFile(std::string filename, FileAccess access) { + AbstractFileSystem* system; + std::string of; + if (mapFilePath(filename, &of, &system)) { + return system->openFile(of, access); + } + return 0; +} + +void MetaFileSystem::closeFile(u32 handle) { + AbstractFileSystem* sys = getHandleOwner(handle); + if (sys) sys->closeFile(handle); +} } // namespace Emulator::Host::GenericFS \ No newline at end of file diff --git a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/meta_file_system.h b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/meta_file_system.h index 17b959c9..83b04744 100644 --- a/src/Emulator/HLE/Libraries/LibKernel/FileSystem/meta_file_system.h +++ b/src/Emulator/HLE/Libraries/LibKernel/FileSystem/meta_file_system.h @@ -1,7 +1,9 @@ #pragma once #include + #include #include + #include "generic_file_system.h" namespace Emulator::Host::GenericFS { @@ -15,6 +17,7 @@ class MetaFileSystem : public GenericHandleAllocator, AbstractFileSystem { u32 current; std::vector fileSystems; std::string currentDirectory; + std::vector handler; public: MetaFileSystem() : current(0) {} @@ -24,9 +27,14 @@ class MetaFileSystem : public GenericHandleAllocator, AbstractFileSystem { void unMountAll(); AbstractFileSystem *getHandleOwner(u32 handle); bool mapFilePath(std::string inpath, std::string *outpath, AbstractFileSystem **system); - u32 requestHandle() { return ++current; } - void releaseHandle(u32 handle); + u32 requestHandle() { + handler.push_back(current); + current++; + return handler.back(); + } + void releaseHandle(u32 handle) { handler.erase(std::remove(handler.begin(), handler.end(), handle), handler.end()); } + bool ownsHandle(u32 handle) { return false; } u32 openFile(std::string filename, FileAccess access); void closeFile(u32 handle); }; -} \ No newline at end of file +} // namespace Emulator::Host::GenericFS \ No newline at end of file