From dc7b1fc4c9dc4776ad527f0566b2520fbc732582 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 21 Apr 2023 18:32:20 +0300 Subject: [PATCH] Rework virtual filesystem implementation --- Core/Driver/Driver.cpp | 8 +-- Execute/Elf/BaseLoad.cpp | 16 ++--- Execute/Elf/Dyn.cpp | 2 +- Execute/Elf/Exec.cpp | 10 +-- Execute/Elf/Parse.cpp | 16 ++--- Execute/Elf/Rel.cpp | 2 +- Execute/Parse.cpp | 6 +- Execute/Spawn.cpp | 16 ++--- FileSystem/Filesystem.cpp | 128 ++++++++++++++++++-------------------- Kernel.cpp | 18 +++--- KernelThread.cpp | 10 +-- Recovery/RecoveryMain.cpp | 12 ++-- include/exec.hpp | 6 +- include/filesystem.hpp | 24 ++++--- 14 files changed, 138 insertions(+), 136 deletions(-) diff --git a/Core/Driver/Driver.cpp b/Core/Driver/Driver.cpp index 94f9009..0f392e3 100644 --- a/Core/Driver/Driver.cpp +++ b/Core/Driver/Driver.cpp @@ -219,10 +219,10 @@ namespace Driver DriverConfigFile << "/config.ini"; fixme("Loading driver config file: %s", DriverConfigFile.c_str()); - std::shared_ptr DriverDirectory = vfs->Open(Config.DriverDirectory); - if (DriverDirectory->Status == VirtualFileSystem::FileStatus::OK) + VirtualFileSystem::File DriverDirectory = vfs->Open(Config.DriverDirectory); + if (DriverDirectory.IsOK()) { - foreach (auto driver in DriverDirectory->node->Children) + foreach (auto driver in DriverDirectory.node->Children) if (driver->Flags == VirtualFileSystem::NodeFlags::FILE) if (cwk_path_has_extension(driver->Name)) { @@ -246,7 +246,7 @@ namespace Driver } else { - KPrint("\eE85230Failed to open driver directory: %s! (Status: %#lx)", Config.DriverDirectory, DriverDirectory->Status); + KPrint("\eE85230Failed to open driver directory: %s! (Status: %#lx)", Config.DriverDirectory, DriverDirectory.Status); CPU::Stop(); } vfs->Close(DriverDirectory); diff --git a/Execute/Elf/BaseLoad.cpp b/Execute/Elf/BaseLoad.cpp index 8007701..8e7e7bb 100644 --- a/Execute/Elf/BaseLoad.cpp +++ b/Execute/Elf/BaseLoad.cpp @@ -131,9 +131,9 @@ namespace Execute cwk_path_get_basename(Path, &BaseName, nullptr); TaskArchitecture Arch = TaskArchitecture::UnknownArchitecture; - std::shared_ptr ExFile = vfs->Open(Path); + File ExFile = vfs->Open(Path); - if (ExFile->Status != FileStatus::OK) + if (ExFile.Status != FileStatus::OK) { vfs->Close(ExFile); error("Failed to open file: %s", Path); @@ -141,7 +141,7 @@ namespace Execute } else { - if (ExFile->node->Flags != NodeFlags::FILE) + if (ExFile.node->Flags != NodeFlags::FILE) { vfs->Close(ExFile); error("Invalid file path: %s", Path); @@ -155,12 +155,12 @@ namespace Execute } } - size_t ExFileSize = ExFile->node->Length; + size_t ExFileSize = ExFile.node->Length; /* Allocate elf in memory */ void *ElfFile = KernelAllocator.RequestPages(TO_PAGES(ExFileSize + 1)); /* Copy the file to the allocated memory */ - memcpy(ElfFile, (void *)ExFile->node->Address, ExFileSize); + memcpy(ElfFile, (void *)ExFile.node->Address, ExFileSize); debug("Image Size: %#lx - %#lx (length: %ld)", ElfFile, (uintptr_t)ElfFile + ExFileSize, ExFileSize); Elf64_Ehdr *ELFHeader = (Elf64_Ehdr *)ElfFile; @@ -220,13 +220,13 @@ namespace Execute switch (ELFHeader->e_type) { case ET_REL: - bl = ELFLoadRel(ElfFile, ExFile.get(), Process); + bl = ELFLoadRel(ElfFile, ExFile, Process); break; case ET_EXEC: - bl = ELFLoadExec(ElfFile, ExFile.get(), Process); + bl = ELFLoadExec(ElfFile, ExFile, Process); break; case ET_DYN: - bl = ELFLoadDyn(ElfFile, ExFile.get(), Process); + bl = ELFLoadDyn(ElfFile, ExFile, Process); break; case ET_CORE: { diff --git a/Execute/Elf/Dyn.cpp b/Execute/Elf/Dyn.cpp index e489cab..c5739d5 100644 --- a/Execute/Elf/Dyn.cpp +++ b/Execute/Elf/Dyn.cpp @@ -32,7 +32,7 @@ using namespace Tasking; namespace Execute { ELFBaseLoad ELFLoadDyn(void *BaseImage, - VirtualFileSystem::File *ExFile, + VirtualFileSystem::File &ExFile, Tasking::PCB *Process) { fixme("Not implemented"); diff --git a/Execute/Elf/Exec.cpp b/Execute/Elf/Exec.cpp index 556addf..d9d8a9f 100644 --- a/Execute/Elf/Exec.cpp +++ b/Execute/Elf/Exec.cpp @@ -32,7 +32,7 @@ using namespace Tasking; namespace Execute { ELFBaseLoad ELFLoadExec(void *ElfFile, - VirtualFileSystem::File *ExFile, + VirtualFileSystem::File &ExFile, Tasking::PCB *Process) { debug("Executable"); @@ -46,7 +46,7 @@ namespace Execute uintptr_t BaseAddress = UINTPTR_MAX; uint64_t ElfAppSize = 0; uintptr_t EntryPoint = ELFHeader->e_entry; - debug("%s's entry point is %#lx", ExFile->Name, EntryPoint); + debug("%s's entry point is %#lx", ExFile.Name, EntryPoint); Elf64_Phdr ItrPhdr; @@ -171,8 +171,8 @@ namespace Execute memcpy((void *)InterpreterPath, (uint8_t *)ElfFile + ItrPhdr.p_offset, 256); debug("Interpreter: %s", InterpreterPath); - std::shared_ptr InterpreterFile = vfs->Open(InterpreterPath); - if (InterpreterFile->Status != VirtualFileSystem::FileStatus::OK) + VirtualFileSystem::File InterpreterFile = vfs->Open(InterpreterPath); + if (!InterpreterFile.IsOK()) warn("Failed to open interpreter file: %s", InterpreterPath); vfs->Close(InterpreterFile); @@ -206,7 +206,7 @@ namespace Execute strcpy(aux_platform, "x86_64"); ELFBase.auxv.push_back({.archaux = {.a_type = AT_NULL, .a_un = {.a_val = 0}}}); - ELFBase.auxv.push_back({.archaux = {.a_type = AT_EXECFN, .a_un = {.a_val = (uint64_t)vfs->GetPathFromNode(ExFile->node).get()}}}); + ELFBase.auxv.push_back({.archaux = {.a_type = AT_EXECFN, .a_un = {.a_val = (uint64_t)vfs->GetPathFromNode(ExFile.node).get()}}}); ELFBase.auxv.push_back({.archaux = {.a_type = AT_PLATFORM, .a_un = {.a_val = (uint64_t)aux_platform}}}); ELFBase.auxv.push_back({.archaux = {.a_type = AT_ENTRY, .a_un = {.a_val = (uint64_t)EntryPoint}}}); ELFBase.auxv.push_back({.archaux = {.a_type = AT_BASE, .a_un = {.a_val = (uint64_t)MemoryImage.Virtual}}}); diff --git a/Execute/Elf/Parse.cpp b/Execute/Elf/Parse.cpp index 6356277..d9ffa49 100644 --- a/Execute/Elf/Parse.cpp +++ b/Execute/Elf/Parse.cpp @@ -232,9 +232,9 @@ namespace Execute } /* No need to check if it's valid, the GetBinaryType() call above does that. */ - std::shared_ptr File = vfs->Open(Interpreter); + VirtualFileSystem::File File = vfs->Open(Interpreter); - Elf64_Ehdr *ELFHeader = (Elf64_Ehdr *)File->node->Address; + Elf64_Ehdr *ELFHeader = (Elf64_Ehdr *)File.node->Address; #ifdef DEBUG const char *InterpreterType[6] = { @@ -259,7 +259,7 @@ namespace Execute for (Elf64_Half i = 0; i < ELFHeader->e_phnum; i++) { memcpy(&ItrPhdr, - (uint8_t *)File->node->Address + ELFHeader->e_phoff + ELFHeader->e_phentsize * i, + (uint8_t *)File.node->Address + ELFHeader->e_phoff + ELFHeader->e_phentsize * i, sizeof(Elf64_Phdr)); BaseAddress = MIN(BaseAddress, ItrPhdr.p_vaddr); @@ -269,7 +269,7 @@ namespace Execute for (Elf64_Half i = 0; i < ELFHeader->e_phnum; i++) { memcpy(&ItrPhdr, - (uint8_t *)File->node->Address + ELFHeader->e_phoff + ELFHeader->e_phentsize * i, + (uint8_t *)File.node->Address + ELFHeader->e_phoff + ELFHeader->e_phentsize * i, sizeof(Elf64_Phdr)); uintptr_t SegmentEnd; @@ -277,12 +277,12 @@ namespace Execute ElfAppSize = MAX(ElfAppSize, SegmentEnd); } - MmImage MemoryImage = ELFCreateMemoryImage(mem, pV, (void *)File->node->Address, ElfAppSize); + MmImage MemoryImage = ELFCreateMemoryImage(mem, pV, (void *)File.node->Address, ElfAppSize); for (Elf64_Half i = 0; i < ELFHeader->e_phnum; i++) { memcpy(&ItrPhdr, - (uint8_t *)File->node->Address + ELFHeader->e_phoff + ELFHeader->e_phentsize * i, + (uint8_t *)File.node->Address + ELFHeader->e_phoff + ELFHeader->e_phentsize * i, sizeof(Elf64_Phdr)); if (ItrPhdr.p_type == PT_LOAD) @@ -296,8 +296,8 @@ namespace Execute (ItrPhdr.p_flags & PF_W) ? "W" : "", (ItrPhdr.p_flags & PF_X) ? "X" : ""); - memcpy((void *)MAddr, (uint8_t *)File->node->Address + ItrPhdr.p_offset, ItrPhdr.p_filesz); - debug("memcpy: %#lx => %#lx (%ld bytes)", (uint8_t *)File->node->Address + ItrPhdr.p_offset, MAddr, ItrPhdr.p_filesz); + memcpy((void *)MAddr, (uint8_t *)File.node->Address + ItrPhdr.p_offset, ItrPhdr.p_filesz); + debug("memcpy: %#lx => %#lx (%ld bytes)", (uint8_t *)File.node->Address + ItrPhdr.p_offset, MAddr, ItrPhdr.p_filesz); } } diff --git a/Execute/Elf/Rel.cpp b/Execute/Elf/Rel.cpp index 8fdab7f..e14ce2f 100644 --- a/Execute/Elf/Rel.cpp +++ b/Execute/Elf/Rel.cpp @@ -27,7 +27,7 @@ namespace Execute /* Originally from https://wiki.osdev.org/ELF_Tutorial */ ELFBaseLoad ELFLoadRel(void *BaseImage, - VirtualFileSystem::File *ExFile, + VirtualFileSystem::File &ExFile, Tasking::PCB *Process) { debug("Relocatable"); diff --git a/Execute/Parse.cpp b/Execute/Parse.cpp index f7b2105..c37f7c7 100644 --- a/Execute/Parse.cpp +++ b/Execute/Parse.cpp @@ -86,12 +86,12 @@ namespace Execute BinaryType GetBinaryType(char *Path) { BinaryType Type = BinaryType::BinTypeInvalid; - std::shared_ptr ExFile = vfs->Open(Path); + VirtualFileSystem::File ExFile = vfs->Open(Path); - if (ExFile->Status == VirtualFileSystem::FileStatus::OK) + if (ExFile.IsOK()) { debug("File opened: %s", Path); - Type = GetBinaryType((void *)ExFile->node->Address); + Type = GetBinaryType((void *)ExFile.node->Address); } vfs->Close(ExFile); diff --git a/Execute/Spawn.cpp b/Execute/Spawn.cpp index 90a4070..fcd5617 100644 --- a/Execute/Spawn.cpp +++ b/Execute/Spawn.cpp @@ -37,11 +37,11 @@ namespace Execute .Process = nullptr, .Thread = nullptr}; - std::shared_ptr ExFile = vfs->Open(Path); + VirtualFileSystem::File ExFile = vfs->Open(Path); - if (ExFile->Status == VirtualFileSystem::FileStatus::OK) + if (ExFile.IsOK()) { - if (ExFile->node->Flags != VirtualFileSystem::NodeFlags::FILE) + if (ExFile.node->Flags != VirtualFileSystem::NodeFlags::FILE) { ret.Status = ExStatus::InvalidFilePath; goto Exit; @@ -51,17 +51,17 @@ namespace Execute { case BinaryType::BinTypeFex: { - Fex *FexHdr = (Fex *)ExFile->node->Address; + Fex *FexHdr = (Fex *)ExFile.node->Address; if (FexHdr->Type == FexFormatType::FexFormatType_Executable) { const char *BaseName; cwk_path_get_basename(Path, &BaseName, nullptr); PCB *Process = TaskManager->CreateProcess(TaskManager->GetCurrentProcess(), BaseName, TaskTrustLevel::User); - void *BaseImage = KernelAllocator.RequestPages(TO_PAGES(ExFile->node->Length + 1)); - memcpy(BaseImage, (void *)ExFile->node->Address, ExFile->node->Length); + void *BaseImage = KernelAllocator.RequestPages(TO_PAGES(ExFile.node->Length + 1)); + memcpy(BaseImage, (void *)ExFile.node->Address, ExFile.node->Length); - Memory::Virtual(Process->PageTable).Map((void *)BaseImage, (void *)BaseImage, ExFile->node->Length, Memory::PTFlag::RW | Memory::PTFlag::US); + Memory::Virtual(Process->PageTable).Map((void *)BaseImage, (void *)BaseImage, ExFile.node->Length, Memory::PTFlag::RW | Memory::PTFlag::US); std::vector auxv; // TODO! @@ -97,7 +97,7 @@ namespace Execute } } } - else if (ExFile->Status == VirtualFileSystem::FileStatus::NotFound) + else if (ExFile.Status == VirtualFileSystem::FileStatus::NotFound) ret.Status = ExStatus::InvalidFilePath; else ret.Status = ExStatus::InvalidFile; diff --git a/FileSystem/Filesystem.cpp b/FileSystem/Filesystem.cpp index c0a45ae..335112e 100644 --- a/FileSystem/Filesystem.cpp +++ b/FileSystem/Filesystem.cpp @@ -168,11 +168,11 @@ namespace VirtualFileSystem return nullptr; } - std::shared_ptr Virtual::ConvertNodeToFILE(Node *node) + File Virtual::ConvertNodeToFILE(Node *node) { - std::shared_ptr file = std::make_shared(); - file->Status = FileStatus::OK; - file->node = node; + File file{}; + file.Status = FileStatus::OK; + file.node = node; return file; } @@ -454,92 +454,86 @@ namespace VirtualFileSystem FileStatus Virtual::Delete(Node *Path, bool Recursive, Node *Parent) { return Delete(GetPathFromNode(Path).get(), Recursive, Parent); } /* TODO: REWORK */ - std::shared_ptr Virtual::Mount(const char *Path, FileSystemOperations *Operator) + File Virtual::Mount(const char *Path, FileSystemOperations *Operator) { SmartLock(VFSLock); - std::shared_ptr file = std::make_shared(); + File file{}; if (unlikely(!Operator)) { - file->Status = FileStatus::InvalidOperator; + file.Status = FileStatus::InvalidOperator; return file; } if (unlikely(isempty((char *)Path))) { - file->Status = FileStatus::InvalidParameter; + file.Status = FileStatus::InvalidParameter; return file; } vfsdbg("Mounting %s", Path); const char *PathCopy; cwk_path_get_basename(Path, &PathCopy, 0); - strcpy(file->Name, PathCopy); - file->Status = FileStatus::OK; - file->node = Create(Path, NodeFlags::MOUNTPOINT); - file->node->Operator = Operator; + strcpy(file.Name, PathCopy); + file.Status = FileStatus::OK; + file.node = Create(Path, NodeFlags::MOUNTPOINT); + file.node->Operator = Operator; return file; } - FileStatus Virtual::Unmount(std::shared_ptr File) + FileStatus Virtual::Unmount(File &File) { SmartLock(VFSLock); - if (unlikely(File.get())) + if (unlikely(!File.node)) return FileStatus::InvalidParameter; - fixme("Unmounting %s", File->Name); + fixme("Unmounting %s", File.Name); return FileStatus::OK; } - size_t Virtual::Read(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size) + size_t Virtual::Read(File &File, size_t Offset, uint8_t *Buffer, size_t Size) { SmartLock(VFSLock); - if (unlikely(!File.get())) - return 0; - - if (unlikely(!File->node)) + if (unlikely(!File.node)) { - File->Status = FileStatus::InvalidNode; + File.Status = FileStatus::InvalidNode; return 0; } - if (unlikely(!File->node->Operator)) + if (unlikely(!File.node->Operator)) { - File->Status = FileStatus::InvalidOperator; + File.Status = FileStatus::InvalidOperator; return 0; } - File->Status = FileStatus::OK; + File.Status = FileStatus::OK; - vfsdbg("Reading %s out->%016x", File->Name, Buffer); - return File->node->Operator->Read(File->node, Offset, Size, Buffer); + vfsdbg("Reading %s out->%016x", File.Name, Buffer); + return File.node->Operator->Read(File.node, Offset, Size, Buffer); } - size_t Virtual::Write(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size) + size_t Virtual::Write(File &File, size_t Offset, uint8_t *Buffer, size_t Size) { SmartLock(VFSLock); - if (unlikely(!File.get())) - return 0; - - if (unlikely(!File->node)) + if (unlikely(!File.node)) { - File->Status = FileStatus::InvalidNode; + File.Status = FileStatus::InvalidNode; return 0; } - if (unlikely(!File->node->Operator)) + if (unlikely(!File.node->Operator)) { - File->Status = FileStatus::InvalidOperator; + File.Status = FileStatus::InvalidOperator; return 0; } - File->Status = FileStatus::OK; + File.Status = FileStatus::OK; - vfsdbg("Writing %s out->%016x", File->Name, Buffer); - return File->node->Operator->Write(File->node, Offset, Size, Buffer); + vfsdbg("Writing %s out->%016x", File.Name, Buffer); + return File.node->Operator->Write(File.node, Offset, Size, Buffer); } /* TODO: CHECK Open */ - std::shared_ptr Virtual::Open(const char *Path, Node *Parent) + File Virtual::Open(const char *Path, Node *Parent) { SmartLock(VFSLock); vfsdbg("Opening %s with parent %s", Path, Parent ? Parent->Name : "(null)"); @@ -547,41 +541,41 @@ namespace VirtualFileSystem if (strcmp(Path, "/") == 0) { - std::shared_ptr file = std::make_shared(); - file->node = FileSystemRoot; - strcpy(file->Name, "/"); + File file{}; + file.node = FileSystemRoot; + strcpy(file.Name, "/"); return file; } if (strcmp(Path, ".") == 0) { - std::shared_ptr file = std::make_shared(); - file->node = Parent; - if (unlikely(!file->node)) - file->Status = FileStatus::NotFound; + File file{}; + file.node = Parent; + if (unlikely(!file.node)) + file.Status = FileStatus::NotFound; cwk_path_get_basename(GetPathFromNode(Parent).get(), &basename, nullptr); - strcpy(file->Name, basename); + strcpy(file.Name, basename); return file; } if (strcmp(Path, "..") == 0) { - std::shared_ptr file = std::make_shared(); + File file{}; if (Parent->Parent != nullptr) - file->node = Parent->Parent; + file.node = Parent->Parent; - if (!file->node) - file->Status = FileStatus::NotFound; + if (!file.node) + file.Status = FileStatus::NotFound; cwk_path_get_basename(GetPathFromNode(Parent).get(), &basename, nullptr); - strcpy(file->Name, basename); + strcpy(file.Name, basename); return file; } Node *CurrentParent = this->GetParent(Path, Parent); std::shared_ptr CleanPath = NormalizePath(Path, CurrentParent); - std::shared_ptr file = std::make_shared(); + File file{}; /* TODO: Check for other errors */ if (!PathExists(CleanPath.get(), CurrentParent)) @@ -590,45 +584,45 @@ namespace VirtualFileSystem { if (strcmp(Child->Name, CleanPath.get()) == 0) { - file->node = Child; - if (file->node == nullptr) + file.node = Child; + if (file.node == nullptr) { - file->Status = FileStatus::UnknownFileStatusError; - file->node = nullptr; + file.Status = FileStatus::UnknownFileStatusError; + file.node = nullptr; return file; } cwk_path_get_basename(GetPathFromNode(Child).get(), &basename, nullptr); - strcpy(file->Name, basename); + strcpy(file.Name, basename); return file; } } - file->node = GetNodeFromPath(CleanPath.get(), FileSystemRoot->Children[0]); - if (file->node) + file.node = GetNodeFromPath(CleanPath.get(), FileSystemRoot->Children[0]); + if (file.node) { - cwk_path_get_basename(GetPathFromNode(file->node).get(), &basename, nullptr); - strcpy(file->Name, basename); + cwk_path_get_basename(GetPathFromNode(file.node).get(), &basename, nullptr); + strcpy(file.Name, basename); return file; } } else { - file->node = GetNodeFromPath(CleanPath.get(), CurrentParent); + file.node = GetNodeFromPath(CleanPath.get(), CurrentParent); cwk_path_get_basename(CleanPath.get(), &basename, nullptr); - strcpy(file->Name, basename); + strcpy(file.Name, basename); return file; } - file->Status = FileStatus::NotFound; + file.Status = FileStatus::NotFound; return file; } - FileStatus Virtual::Close(std::shared_ptr File) + FileStatus Virtual::Close(File &File) { SmartLock(VFSLock); - if (unlikely(!File.get())) + if (unlikely(!File.node)) return FileStatus::InvalidHandle; - vfsdbg("Closing %s", File->Name); + vfsdbg("Closing %s", File.Name); return FileStatus::OK; } diff --git a/Kernel.cpp b/Kernel.cpp index 1fcfe62..4666734 100644 --- a/Kernel.cpp +++ b/Kernel.cpp @@ -426,42 +426,42 @@ EXTERNC NIF void Main(BootInfo *Info) DevFS = vfs->Create("/system/dev", NodeFlags::DIRECTORY); else { - std::shared_ptr dev = vfs->Open("/system/dev"); - if (dev->node->Flags != NodeFlags::DIRECTORY) + File dev = vfs->Open("/system/dev"); + if (dev.node->Flags != NodeFlags::DIRECTORY) { KPrint("\eE85230/system/dev is not a directory! Halting..."); CPU::Stop(); } vfs->Close(dev); - DevFS = dev->node; + DevFS = dev.node; } if (!vfs->PathExists("/system/mnt")) MntFS = vfs->Create("/system/mnt", NodeFlags::DIRECTORY); else { - std::shared_ptr mnt = vfs->Open("/system/mnt"); - if (mnt->node->Flags != NodeFlags::DIRECTORY) + File mnt = vfs->Open("/system/mnt"); + if (mnt.node->Flags != NodeFlags::DIRECTORY) { KPrint("\eE85230/system/mnt is not a directory! Halting..."); CPU::Stop(); } vfs->Close(mnt); - MntFS = mnt->node; + MntFS = mnt.node; } if (!vfs->PathExists("/system/proc")) ProcFS = vfs->Create("/system/proc", NodeFlags::DIRECTORY); else { - std::shared_ptr proc = vfs->Open("/system/proc", nullptr); - if (proc->node->Flags != NodeFlags::DIRECTORY) + File proc = vfs->Open("/system/proc", nullptr); + if (proc.node->Flags != NodeFlags::DIRECTORY) { KPrint("\eE85230/system/proc is not a directory! Halting..."); CPU::Stop(); } vfs->Close(proc); - ProcFS = proc->node; + ProcFS = proc.node; } KPrint("\e058C19################################"); diff --git a/KernelThread.cpp b/KernelThread.cpp index 3e37cb1..da9b75e 100644 --- a/KernelThread.cpp +++ b/KernelThread.cpp @@ -176,17 +176,17 @@ void BootLogoAnimationThread() while (FrameCount < 27) { sprintf(BootAnimPath, "%d.tga", FrameCount); - std::shared_ptr ba = bootanim_vfs->Open(BootAnimPath); - if (ba->Status != FileStatus::OK) + File ba = bootanim_vfs->Open(BootAnimPath); + if (!ba.IsOK()) { bootanim_vfs->Close(ba); debug("Failed to load boot animation frame %s", BootAnimPath); break; } - FrameSizes[FrameCount] = s_cst(uint32_t, ba->node->Length); - Frames[FrameCount] = new uint8_t[ba->node->Length]; - memcpy((void *)Frames[FrameCount], (void *)ba->node->Address, ba->node->Length); + FrameSizes[FrameCount] = s_cst(uint32_t, ba.node->Length); + Frames[FrameCount] = new uint8_t[ba.node->Length]; + memcpy((void *)Frames[FrameCount], (void *)ba.node->Address, ba.node->Length); bootanim_vfs->Close(ba); FrameCount++; } diff --git a/Recovery/RecoveryMain.cpp b/Recovery/RecoveryMain.cpp index 833e5a3..75e88e3 100644 --- a/Recovery/RecoveryMain.cpp +++ b/Recovery/RecoveryMain.cpp @@ -70,25 +70,25 @@ namespace Recovery return; } - std::shared_ptr pcm = vfs->Open(AudioFile); + VirtualFileSystem::File pcm = vfs->Open(AudioFile); - if (pcm->Status != FileStatus::OK) + if (!pcm.IsOK()) { error("Cannot open audio file! Cannot play audio!"); return; } - void *PCMRaw = KernelAllocator.RequestPages(TO_PAGES(pcm->node->Length + 1)); - memcpy(PCMRaw, (void *)pcm->node->Address, pcm->node->Length); + void *PCMRaw = KernelAllocator.RequestPages(TO_PAGES(pcm.node->Length + 1)); + memcpy(PCMRaw, (void *)pcm.node->Address, pcm.node->Length); KernelCallback callback{}; callback.Reason = SendReason; callback.AudioCallback.Send.Data = (uint8_t *)PCMRaw; - callback.AudioCallback.Send.Length = pcm->node->Length; + callback.AudioCallback.Send.Length = pcm.node->Length; debug("Playing audio..."); int status = DriverManager->IOCB(AudioDrv.DriverUID, &callback); debug("Audio played! %d", status); - KernelAllocator.FreePages((void *)PCMRaw, TO_PAGES(pcm->node->Length + 1)); + KernelAllocator.FreePages((void *)PCMRaw, TO_PAGES(pcm.node->Length + 1)); vfs->Close(pcm); TEXIT(0); } diff --git a/include/exec.hpp b/include/exec.hpp index 25b3ca7..2de1808 100644 --- a/include/exec.hpp +++ b/include/exec.hpp @@ -125,15 +125,15 @@ namespace Execute uintptr_t LoadELFInterpreter(Memory::MemMgr *mem, Memory::Virtual &pV, const char *Interpreter); ELFBaseLoad ELFLoadRel(void *ElfFile, - VirtualFileSystem::File *ExFile, + VirtualFileSystem::File &ExFile, Tasking::PCB *Process); ELFBaseLoad ELFLoadExec(void *ElfFile, - VirtualFileSystem::File *ExFile, + VirtualFileSystem::File &ExFile, Tasking::PCB *Process); ELFBaseLoad ELFLoadDyn(void *ElfFile, - VirtualFileSystem::File *ExFile, + VirtualFileSystem::File &ExFile, Tasking::PCB *Process); void StartExecuteService(); diff --git a/include/filesystem.hpp b/include/filesystem.hpp index 33de81f..8b135ff 100644 --- a/include/filesystem.hpp +++ b/include/filesystem.hpp @@ -19,7 +19,6 @@ #define __FENNIX_KERNEL_FILESYSTEM_H__ #include - #include #include @@ -129,6 +128,8 @@ namespace VirtualFileSystem char Name[FILENAME_LENGTH]; FileStatus Status; Node *node; + + bool IsOK() { return Status == FileStatus::OK; } }; /* Manage / etc.. */ @@ -140,7 +141,14 @@ namespace VirtualFileSystem public: std::shared_ptr GetPathFromNode(Node *node); Node *GetNodeFromPath(const char *Path, Node *Parent = nullptr); - std::shared_ptr ConvertNodeToFILE(Node *node); + + /** + * @brief Convert a Node to a File + * + * @param node Node to convert + * @return Converted node + */ + File ConvertNodeToFILE(Node *node); Node *GetParent(const char *Path, Node *Parent); Node *GetRootNode() { return FileSystemRoot; } @@ -157,14 +165,14 @@ namespace VirtualFileSystem FileStatus Delete(const char *Path, bool Recursive = false, Node *Parent = nullptr); FileStatus Delete(Node *Path, bool Recursive = false, Node *Parent = nullptr); - std::shared_ptr Mount(const char *Path, FileSystemOperations *Operator); - FileStatus Unmount(std::shared_ptr File); + File Mount(const char *Path, FileSystemOperations *Operator); + FileStatus Unmount(File &File); - size_t Read(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size); - size_t Write(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size); + size_t Read(File &File, size_t Offset, uint8_t *Buffer, size_t Size); + size_t Write(File &File, size_t Offset, uint8_t *Buffer, size_t Size); - std::shared_ptr Open(const char *Path, Node *Parent = nullptr); - FileStatus Close(std::shared_ptr File); + File Open(const char *Path, Node *Parent = nullptr); + FileStatus Close(File &File); Virtual(); ~Virtual();