Refactor filesystem & stl code

This commit is contained in:
EnderIce2
2024-05-18 07:42:01 +03:00
parent 77a291d08b
commit 6801475243
186 changed files with 15784 additions and 9746 deletions

View File

@ -22,130 +22,39 @@
#include <memory.hpp>
vfs::Virtual *fs = nullptr;
vfs::Node *DevFS = nullptr;
vfs::Node *MntFS = nullptr;
vfs::Node *ProcFS = nullptr;
vfs::Node *VarLogFS = nullptr;
vfs::PTMXDevice *ptmx = nullptr;
void SearchForInitrd()
{
for (size_t i = 0; i < MAX_MODULES; i++)
{
uintptr_t initrdAddress = (uintptr_t)bInfo.Modules[i].Address;
if (!initrdAddress)
continue;
if (strcmp(bInfo.Modules[i].CommandLine, "initrd") != 0)
continue;
KPrint("initrd found at %#lx", initrdAddress);
Memory::Virtual vmm;
if (!vmm.Check((void *)initrdAddress))
{
warn("Initrd is not mapped!");
vmm.Map((void *)initrdAddress, (void *)initrdAddress,
bInfo.Modules[i].Size, Memory::RW);
}
if (TestAndInitializeUSTAR(initrdAddress, bInfo.Modules[i].Size))
continue; /* Maybe add another root? */
}
}
EXTERNC NIF void KernelVFS()
{
KPrint("Initializing Virtual File System");
fs = new vfs::Virtual;
vfs::Node *root = fs->GetRootNode();
if (root->Children.size() == 0)
fs->nRoot = new vfs::vfsRoot("/", fs);
for (size_t i = 0; i < MAX_MODULES; i++)
{
if (!bInfo.Modules[i].Address)
continue;
if (strcmp(bInfo.Modules[i].CommandLine, "initrd") == 0)
{
KPrint("initrd found at %#lx", bInfo.Modules[i].Address);
static char initrd = 0;
if (!initrd++)
{
uintptr_t initrdAddress = (uintptr_t)bInfo.Modules[i].Address;
Memory::Virtual vmm;
if (!vmm.Check((void *)initrdAddress))
{
warn("Initrd is not mapped!");
vmm.Map((void *)initrdAddress, (void *)initrdAddress,
bInfo.Modules[i].Size, Memory::RW);
}
vfs::USTAR *ustar = new vfs::USTAR;
if (!ustar->TestArchive(initrdAddress))
{
KPrint("\eE85230USTAR archive is invalid!");
delete ustar;
}
else
ustar->ReadArchive(initrdAddress, fs);
}
}
}
KPrint("Mounting filesystems");
if (!fs->PathExists("/dev"))
DevFS = new vfs::Node(fs->nRoot, "dev", vfs::DIRECTORY);
else
{
vfs::RefNode *dev = fs->Open("/dev");
if (dev->node->Type != vfs::NodeType::DIRECTORY)
{
KPrint("\eE85230/dev is not a directory!");
CPU::Stop();
}
DevFS = dev->node;
delete dev;
}
if (!fs->PathExists("/mnt"))
MntFS = new vfs::Node(fs->nRoot, "mnt", vfs::DIRECTORY);
else
{
vfs::RefNode *mnt = fs->Open("/mnt");
if (mnt->node->Type != vfs::NodeType::DIRECTORY)
{
KPrint("\eE85230/mnt is not a directory!");
CPU::Stop();
}
MntFS = mnt->node;
delete mnt;
}
if (!fs->PathExists("/proc"))
ProcFS = new vfs::Node(fs->nRoot, "proc", vfs::DIRECTORY);
else
{
vfs::RefNode *proc = fs->Open("/proc", nullptr);
if (proc->node->Type != vfs::NodeType::DIRECTORY)
{
KPrint("\eE85230/proc is not a directory!");
CPU::Stop();
}
ProcFS = proc->node;
delete proc;
}
if (!fs->PathExists("/var"))
{
vfs::Node *var = new vfs::Node(fs->nRoot, "var", vfs::DIRECTORY);
VarLogFS = new vfs::Node(var, "log", vfs::DIRECTORY);
}
else
{
vfs::RefNode *var = fs->Open("/var", nullptr);
if (var->node->Type != vfs::NodeType::DIRECTORY)
{
KPrint("\eE85230/var is not a directory!");
CPU::Stop();
}
VarLogFS = var->node;
delete var;
if (!fs->PathExists("/var/log"))
VarLogFS = new vfs::Node(VarLogFS, "log", vfs::DIRECTORY);
else
{
vfs::RefNode *var_log = fs->Open("/var/log", nullptr);
if (var_log->node->Type != vfs::NodeType::DIRECTORY)
{
KPrint("\eE85230/var/log is not a directory!");
CPU::Stop();
}
VarLogFS = var_log->node;
delete var_log;
}
}
new vfs::NullDevice();
new vfs::RandomDevice();
new vfs::ZeroDevice();
new vfs::KConDevice();
ptmx = new vfs::PTMXDevice();
SearchForInitrd();
fs->Initialize();
}