more draft fs

This commit is contained in:
georgemoralis 2023-10-19 17:52:49 +03:00
parent eb307b9cd9
commit ec362948f4
3 changed files with 51 additions and 9 deletions

View File

@ -19,6 +19,8 @@ class GenericHandleAllocator {
}; };
class AbstractFileSystem { class AbstractFileSystem {
public:
virtual bool ownsHandle(u32 handle) = 0;
virtual u32 openFile(std::string filename, FileAccess access) = 0; virtual u32 openFile(std::string filename, FileAccess access) = 0;
virtual void closeFile(u32 handle) = 0; virtual void closeFile(u32 handle) = 0;
}; };

View File

@ -2,19 +2,51 @@
namespace Emulator::Host::GenericFS { 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::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 } // namespace Emulator::Host::GenericFS

View File

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <types.h> #include <types.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include "generic_file_system.h" #include "generic_file_system.h"
namespace Emulator::Host::GenericFS { namespace Emulator::Host::GenericFS {
@ -15,6 +17,7 @@ class MetaFileSystem : public GenericHandleAllocator, AbstractFileSystem {
u32 current; u32 current;
std::vector<System> fileSystems; std::vector<System> fileSystems;
std::string currentDirectory; std::string currentDirectory;
std::vector<u32> handler;
public: public:
MetaFileSystem() : current(0) {} MetaFileSystem() : current(0) {}
@ -24,9 +27,14 @@ class MetaFileSystem : public GenericHandleAllocator, AbstractFileSystem {
void unMountAll(); void unMountAll();
AbstractFileSystem *getHandleOwner(u32 handle); AbstractFileSystem *getHandleOwner(u32 handle);
bool mapFilePath(std::string inpath, std::string *outpath, AbstractFileSystem **system); bool mapFilePath(std::string inpath, std::string *outpath, AbstractFileSystem **system);
u32 requestHandle() { return ++current; } u32 requestHandle() {
void releaseHandle(u32 handle); 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); u32 openFile(std::string filename, FileAccess access);
void closeFile(u32 handle); void closeFile(u32 handle);
}; };
} } // namespace Emulator::Host::GenericFS