From efd60f780ba66b363dc564b99a09b60163b9edcb Mon Sep 17 00:00:00 2001 From: Amlal EL Mahrouss Date: Sat, 22 Jun 2024 11:02:34 +0200 Subject: IMP: Support fork based operations inside the file manager, update PEF loader to load from forks, we might need a non-fork loader class though. Signed-off-by: Amlal EL Mahrouss --- Kernel/KernelKit/FileManager.hpp | 121 ++++++++++++++++++++++++++++----------- 1 file changed, 88 insertions(+), 33 deletions(-) (limited to 'Kernel/KernelKit/FileManager.hpp') diff --git a/Kernel/KernelKit/FileManager.hpp b/Kernel/KernelKit/FileManager.hpp index b06b926e..c154438e 100644 --- a/Kernel/KernelKit/FileManager.hpp +++ b/Kernel/KernelKit/FileManager.hpp @@ -26,27 +26,35 @@ /// @brief Filesystem abstraction manager. /// Works like the VFS or IFS. -#define kBootFolder "/Boot" -#define kBinFolder "/Applications" -#define kShLibsFolder "/Library" -#define kMountFolder "/Mount" +#define cRestrictR "r" +#define cRestrictRB "rb" +#define cRestrictW "w" +#define cRestrictRW "rw" /// refer to first enum. -#define kFileOpsCount 4 -#define kFileMimeGeneric "application-type/*" +#define cFileOpsCount 4 +#define cFileMimeGeneric "application-type/*" + +/** @brief invalid position. (n-pos) */ +#define kNPos (SizeT)(-1); namespace NewOS { enum { - kFileWriteAll = 100, - kFileReadAll = 101, - kFileReadChunk = 102, - kFileWriteChunk = 103, - kFileIOCnt = (kFileWriteChunk - kFileWriteAll) + 1, + cFileWriteAll = 100, + cFileReadAll = 101, + cFileReadChunk = 102, + cFileWriteChunk = 103, + cFileIOCnt = (cFileWriteChunk - cFileWriteAll) + 1, + }; + + /// @brief filesystem node generic type. + struct PACKED FMNode final { + VoidPtr _Unused; }; - typedef VoidPtr NodePtr; + typedef FMNode* NodePtr; /** @brief Filesystem Manager Interface class @@ -55,7 +63,7 @@ namespace NewOS class FilesystemManagerInterface { public: - FilesystemManagerInterface() = default; + explicit FilesystemManagerInterface() = default; virtual ~FilesystemManagerInterface() = default; public: @@ -87,9 +95,19 @@ namespace NewOS virtual NodePtr Open(_Input const char* path, _Input const char* r) = 0; public: - virtual Void Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT size) = 0; - virtual _Output VoidPtr Read(_Input NodePtr node, _Input Int32 flags, _Input SizeT sz) = 0; - + virtual Void Write(_Input NodePtr node, _Input VoidPtr data, + _Input Int32 flags, _Input SizeT size) = 0; + + virtual _Output VoidPtr Read(_Input NodePtr node, + _Input Int32 flags, _Input SizeT sz) = 0; + + virtual Void Write(_Input const Char* name, + _Input NodePtr node, _Input VoidPtr data, + _Input Int32 flags, _Input SizeT size) = 0; + + virtual _Output VoidPtr Read(_Input const Char* name, + _Input NodePtr node, + _Input Int32 flags, _Input SizeT sz) = 0; public: virtual bool Seek(_Input NodePtr node, _Input SizeT off) = 0; @@ -98,9 +116,6 @@ namespace NewOS virtual bool Rewind(_Input NodePtr node) = 0; }; -/** @brief invalid position. (n-pos) */ -#define kNPos (SizeT)(-1); - #ifdef __FSKIT_NEWFS__ /** * @brief Based of FilesystemManagerInterface, takes care of managing NewFS @@ -128,7 +143,17 @@ namespace NewOS bool Seek(NodePtr node, SizeT off); SizeT Tell(NodePtr node) override; bool Rewind(NodePtr node) override; - + + Void Write(_Input const Char* name, + _Input NodePtr node, _Input VoidPtr data, + _Input Int32 flags, + _Input SizeT size) override; + + _Output VoidPtr Read(_Input const Char* name, + _Input NodePtr node, + _Input Int32 flags, + _Input SizeT sz) override; + public: void SetResourceFork(const char* forkName); void SetDataFork(const char* forkName); @@ -170,7 +195,7 @@ namespace NewOS if (man) { - man->Write(fFile, data, kFileWriteAll); + man->Write(fFile, data, cFileWriteAll); return ErrorOr(0); } @@ -183,7 +208,36 @@ namespace NewOS if (man) { - VoidPtr ret = man->Read(fFile, kFileReadAll, 0); + VoidPtr ret = man->Read(fFile, cFileReadAll, 0); + return ret; + } + + return nullptr; + } + + ErrorOr WriteAll(const char* fName, const VoidPtr data) noexcept + { + if (data == nullptr) + return ErrorOr(kErrorInvalidData); + + auto man = FSClass::GetMounted(); + + if (man) + { + man->Write(fName, fFile, data, cFileWriteAll); + return ErrorOr(0); + } + + return ErrorOr(kErrorInvalidData); + } + + VoidPtr Read(const char* fName) noexcept + { + auto man = FSClass::GetMounted(); + + if (man) + { + VoidPtr ret = man->Read(fName, fFile, cFileReadAll, 0); return ret; } @@ -197,7 +251,7 @@ namespace NewOS if (man) { man->Seek(fFile, offset); - auto ret = man->Read(fFile, kFileReadChunk, sz); + auto ret = man->Read(fFile, cFileReadChunk, sz); return ret; } @@ -212,7 +266,7 @@ namespace NewOS if (man) { man->Seek(fFile, offset); - man->Write(fFile, data, sz, kFileReadChunk); + man->Write(fFile, data, sz, cFileReadChunk); } } @@ -231,28 +285,29 @@ namespace NewOS private: NodePtr fFile; - const Char* fMime{kFileMimeGeneric}; + const Char* fMime{cFileMimeGeneric}; }; -#define kRestrictR "r" -#define kRestrictRB "rb" -#define kRestrictW "w" -#define kRestrictRW "rw" - using FileStreamUTF8 = FileStream; using FileStreamUTF16 = FileStream; typedef UInt64 CursorType; - + + /// @brief constructor template FileStream::FileStream(const Encoding* path, const Encoding* restrict_type) : fFile(Class::GetMounted()->Open(path, restrict_type)) { + kcout << "newoskrnl: new file: " << path << ".\r"; } - template - FileStream::~FileStream() = default; + /// @brief destructor + template + FileStream::~FileStream() + { + delete fFile; + } } // namespace NewOS #define node_cast(PTR) reinterpret_cast(PTR) -- cgit v1.2.3