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

@ -20,7 +20,6 @@
#include <types.h>
void cmd_lsof(const char *args);
void cmd_clear(const char *args);
void cmd_echo(const char *args);
void cmd_ls(const char *args);

View File

@ -28,28 +28,29 @@ void cmd_cat(const char *args)
if (args[0] == '\0')
return;
Node *thisNode = fs->GetNodeFromPath(args, thisProcess->CurrentWorkingDirectory);
if (thisNode == nullptr)
{
printf("cat: %s: No such file or directory\n", args);
return;
}
/* FIXME: Reimplement this later */
assert(!"Function not implemented");
// Node *thisNode = fs->GetByPath(args, thisProcess->CWD, true);
// if (thisNode == nullptr)
// {
// printf("cat: %s: No such file or directory\n", args);
// return;
// }
if (thisNode->Type != NodeType::FILE &&
thisNode->Type != NodeType::CHARDEVICE)
{
printf("cat: %s: Not a file\n", args);
return;
}
// if (!thisNode->Stat.IsType(FILE) && !thisNode->Stat.IsType(CHARDEVICE))
// {
// printf("cat: %s: Not a file\n", args);
// return;
// }
vfs::RefNode *fd = fs->Open(thisNode->FullPath);
// vfs::FileHandle *fd = fs->Open(thisNode->FilePath, nullptr, true);
uint8_t *buffer = new uint8_t[fd->Size + 1];
ssize_t rBytes = fd->read(buffer, fd->Size);
if (rBytes > 0)
printf("%s\n", buffer);
else
printf("cat: %s: Could not read file\n", args);
delete[] buffer;
delete fd;
// uint8_t *buffer = new uint8_t[fd->node->Stat.Size + 1];
// ssize_t rBytes = fd->read(buffer, fd->node->Stat.Size);
// if (rBytes > 0)
// printf("%s\n", buffer);
// else
// printf("cat: %s: Could not read file\n", args);
// delete[] buffer;
// delete fd;
}

View File

@ -28,22 +28,24 @@ void cmd_cd(const char *args)
if (args[0] == '\0')
return;
Node *thisNode = fs->GetNodeFromPath(args, thisProcess->CurrentWorkingDirectory);
/* FIXME: Reimplement this later */
assert(!"Function not implemented");
// Node *thisNode = fs->GetByPath(args, thisProcess->CWD, true);
if (thisNode == nullptr)
{
printf("cd: %s: No such file or directory\n", args);
return;
}
// if (thisNode == nullptr)
// {
// printf("cd: %s: No such file or directory\n", args);
// return;
// }
if (thisNode->Type == NodeType::SYMLINK)
thisNode = fs->GetNodeFromPath(thisNode->Symlink);
// if (thisNode->Stat.IsType(SYMLINK))
// thisNode = fs->GetByPath(thisNode->GetSymLink(), nullptr, true);
if (thisNode->Type != NodeType::DIRECTORY)
{
printf("cd: %s: Not a directory\n", args);
return;
}
// if (!thisNode->Stat.IsType(DIRECTORY))
// {
// printf("cd: %s: Not a directory\n", args);
// return;
// }
thisProcess->CurrentWorkingDirectory = thisNode;
// thisProcess->CWD = thisNode;
}

View File

@ -23,81 +23,84 @@
using namespace vfs;
const char *ColorNodeType(Node *node)
{
switch (node->Type)
{
case NodeType::DIRECTORY:
return "\e3871F5";
case NodeType::BLOCKDEVICE:
return "\eE8CD1E";
case NodeType::CHARDEVICE:
return "\e86E01F";
case NodeType::PIPE:
return "\eE0991F";
case NodeType::SYMLINK:
return "\e1FB9E0";
case NodeType::FILE:
return "\eCCCCCC";
default:
return "\eF72020";
}
}
// const char *ColorNodeType(Node *node)
// {
// switch (node->Stat.GetFileType())
// {
// case DIRECTORY:
// return "\e3871F5";
// case BLOCKDEVICE:
// return "\eE8CD1E";
// case CHARDEVICE:
// return "\e86E01F";
// case PIPE:
// return "\eE0991F";
// case SYMLINK:
// return "\e1FB9E0";
// case FILE:
// return "\eCCCCCC";
// default:
// return "\eF72020";
// }
// }
size_t MaxNameLength(Node *nodes)
{
size_t maxLength = 0;
foreach (auto &node in nodes->Children)
maxLength = std::max(maxLength, strlen(node->Name));
return maxLength;
}
// size_t MaxNameLength(Node *nodes)
// {
// size_t maxLength = 0;
// foreach (auto &node in nodes->GetChildren(true))
// maxLength = std::max(maxLength, strlen(node->FileName));
// return maxLength;
// }
void PrintLS(Node *node)
{
size_t maxNameLength = MaxNameLength(node);
int count = 0;
bool first = true;
foreach (auto &var in node->Children)
{
if (count % 5 == 0 && !first)
printf("\n");
printf(" %s%-*s ", ColorNodeType(var), (int)maxNameLength, var->Name);
count++;
first = false;
}
printf("\eCCCCCC\n");
}
// void PrintLS(Node *node)
// {
// size_t maxNameLength = MaxNameLength(node);
// int count = 0;
// bool first = true;
// foreach (auto &var in node->GetChildren(true))
// {
// if (count % 5 == 0 && !first)
// printf("\n");
// printf(" %s%-*s ", ColorNodeType(var), (int)maxNameLength, var->FileName);
// count++;
// first = false;
// }
// printf("\eCCCCCC\n");
// }
void cmd_ls(const char *args)
{
if (args[0] == '\0')
{
Node *rootNode = thisProcess->CurrentWorkingDirectory;
/* FIXME: Reimplement this later */
assert(!"Function not implemented");
if (rootNode == nullptr)
rootNode = fs->GetRootNode()->Children[0];
// if (args[0] == '\0')
// {
// Node *rootNode = thisProcess->CWD;
PrintLS(rootNode);
}
else
{
Node *thisNode = fs->GetNodeFromPath(args, thisProcess->CurrentWorkingDirectory);
// if (rootNode == nullptr)
// rootNode = fs->FileSystemRoots->GetChildren(true)[0];
if (thisNode == nullptr)
{
printf("ls: %s: No such file or directory\n", args);
return;
}
// PrintLS(rootNode);
// }
// else
// {
// Node *thisNode = fs->GetByPath(args, thisProcess->CWD, true);
if (thisNode->Type == NodeType::SYMLINK)
thisNode = fs->GetNodeFromPath(thisNode->Symlink);
// if (thisNode == nullptr)
// {
// printf("ls: %s: No such file or directory\n", args);
// return;
// }
if (thisNode->Type != NodeType::DIRECTORY)
{
printf("%s%s\n", ColorNodeType(thisNode), thisNode->Name);
return;
}
// if (thisNode->Stat.IsType(SYMLINK))
// thisNode = fs->GetByPath(thisNode->GetSymLink(), nullptr, true);
PrintLS(thisNode);
}
// if (!thisNode->Stat.IsType(DIRECTORY))
// {
// printf("%s%s\n", ColorNodeType(thisNode), thisNode->FileName);
// return;
// }
// PrintLS(thisNode);
// }
}

View File

@ -1,36 +0,0 @@
/*
This file is part of Fennix Kernel.
Fennix Kernel is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
Fennix Kernel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fennix Kernel. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../cmds.hpp"
#include "../../kernel.h"
void cmd_lsof(const char *)
{
printf("PROCESS FD NAME\n");
foreach (auto Proc in TaskManager->GetProcessList())
{
if (!Proc)
continue;
std::vector<vfs::FileDescriptorTable::Fildes> fds_array =
Proc->FileDescriptors->GetFileDescriptors();
foreach (auto fd in fds_array)
printf("%s %d: %s\n", Proc->Name, fd.Descriptor,
fd.Handle->node->FullPath);
}
}

View File

@ -62,7 +62,7 @@ void cmd_modinfo(const char *args)
printf("Resource Info:\n");
printf(" Initialized: %s\n", drv.Initialized ? "yes" : "no");
printf(" Error Code: %i (%s)\n", drv.ErrorCode, strerror(drv.ErrorCode));
printf(" Path: %s\n", drv.Path);
printf(" Path: %s\n", drv.Path.c_str());
printf(" Used Memory: %ld KiB\n", TO_KiB(drv.vma->GetAllocatedMemorySize()));
printf(" Used IRQs:%s\n", drv.InterruptHandlers->empty() ? " none" : "");
foreach (auto var in *drv.InterruptHandlers)

View File

@ -23,53 +23,55 @@
using namespace vfs;
void tree_loop(Node *rootNode, int depth = 0)
{
foreach (auto Child in rootNode->Children)
{
Display->UpdateBuffer();
if (Child->Type == NodeType::DIRECTORY ||
Child->Type == NodeType::MOUNTPOINT)
{
printf("%*s%*s%*s|- %s\n",
depth, "",
depth, "",
depth, "",
Child->Name);
tree_loop(Child, depth + 1);
}
else
printf("%*s%*s%*s|- %s\n",
depth, "",
depth, "",
depth, "",
Child->Name);
}
}
// void tree_loop(Node *rootNode, int depth = 0)
// {
// foreach (auto Child in rootNode->GetChildren(true))
// {
// Display->UpdateBuffer();
// if (Child->Stat.IsType(DIRECTORY) || Child->Stat.IsType(MOUNTPOINT))
// {
// printf("%*s%*s%*s|- %s\n",
// depth, "",
// depth, "",
// depth, "",
// Child->FileName);
// tree_loop(Child, depth + 1);
// }
// else
// printf("%*s%*s%*s|- %s\n",
// depth, "",
// depth, "",
// depth, "",
// Child->FileName);
// }
// }
void cmd_tree(const char *args)
{
Node *rootNode = thisProcess->CurrentWorkingDirectory;
if (args[0] == '\0')
{
if (rootNode == nullptr)
rootNode = fs->GetRootNode()->Children[0];
}
else
{
rootNode = fs->GetNodeFromPath(args, thisProcess->CurrentWorkingDirectory);
if (rootNode == nullptr)
{
printf("ls: %s: No such file or directory\n", args);
return;
}
if (rootNode->Type != NodeType::DIRECTORY)
{
printf("%s\n", rootNode->Name);
return;
}
}
/* FIXME: Reimplement this later */
assert(!"Function not implemented");
printf("%s\n", rootNode->Name);
tree_loop(rootNode);
// Node *rootNode = thisProcess->CWD;
// if (args[0] == '\0')
// {
// if (rootNode == nullptr)
// rootNode = fs->FileSystemRoots->GetChildren(true)[0];
// }
// else
// {
// rootNode = fs->GetByPath(args, thisProcess->CWD, true);
// if (rootNode == nullptr)
// {
// printf("ls: %s: No such file or directory\n", args);
// return;
// }
// if (!rootNode->Stat.IsType(DIRECTORY))
// {
// printf("%s\n", rootNode->FileName);
// return;
// }
// }
// printf("%s\n", rootNode->FileName);
// tree_loop(rootNode);
}

View File

@ -17,15 +17,16 @@
#include <kshell.hpp>
#include <interface/driver.h>
#include <filesystem.hpp>
#include <driver.hpp>
#include <lock.hpp>
#include <exec.hpp>
#include <debug.h>
#include <thread>
#include <cctype>
#include "../kernel.h"
#include "../driver.h"
#include "cmds.hpp"
NewLock(ShellLock);
@ -47,7 +48,6 @@ void __cmd_builtin(const char *)
}
static Command commands[] = {
{"lsof", cmd_lsof},
{"ls", cmd_ls},
{"tree", cmd_tree},
{"cd", cmd_cd},
@ -167,7 +167,7 @@ void StartKernelShell()
bool upperCase = false;
bool tabDblPress = false;
vfs::RefNode *kfd = fs->Open("/dev/key");
FileNode *kfd = fs->GetByPath("/dev/key", nullptr);
if (kfd == nullptr)
{
KPrint("Failed to open keyboard device!");
@ -192,14 +192,14 @@ void StartKernelShell()
size_t seekCount = 0;
strBuf.clear();
vfs::Node *cwd = thisProcess->CurrentWorkingDirectory;
FileNode *cwd = thisProcess->CWD;
if (!cwd)
cwd = fs->GetNodeFromPath("/");
cwd = fs->GetByPath("/", nullptr);
printf("\e34C6EB%s@%s:%s$ \eCCCCCC",
"kernel",
"fennix",
cwd->FullPath);
cwd->Path.c_str());
Display->UpdateBuffer();
Display->GetBufferCursor(&homeX, &homeY);
@ -215,7 +215,7 @@ void StartKernelShell()
CurY.store(__cy);
CurHalt.store(false);
nBytes = kfd->read(scBuf, 2);
nBytes = kfd->Read(scBuf, 2, 0);
if (nBytes == 0)
continue;
if (nBytes < 0)
@ -330,7 +330,7 @@ void StartKernelShell()
size_t strSeek = seekCount ? seekCount - 1 : 0;
seekCount = strSeek;
debug("strSeek: %d: %s", strSeek, strBuf.c_str());
strBuf.erase((int)strSeek);
strBuf.erase(strSeek);
Display->PrintString(strBuf.c_str());
debug("after strBuf: %s", strBuf.c_str());
@ -363,7 +363,7 @@ void StartKernelShell()
Display->SetBufferCursor(unseekX, unseekY);
strBufBck();
debug("seekCount: %d: %s", seekCount, strBuf.c_str());
strBuf.erase((int)seekCount);
strBuf.erase(seekCount);
Display->PrintString(strBuf.c_str());
debug("after strBuf: %s", strBuf.c_str());
@ -666,7 +666,7 @@ void StartKernelShell()
// size_t strSeek = seekCount ? seekCount - 1 : 0;
debug("seekCount: %d; \"%s\"", seekCount, strBuf.c_str());
strBuf.insert(seekCount, 1, c);
strBuf.insert(seekCount, (size_t)1, c);
Display->PrintString(strBuf.c_str());
debug("after strBuf: %s (seek and bs is +1 [seek: %d; bs: %d])",
strBuf.c_str(), seekCount + 1, bsCount + 1);
@ -750,12 +750,12 @@ void StartKernelShell()
std::string path = "/bin/";
if (!fs->PathExists("/bin"))
if (!fs->PathExists(path.c_str(), nullptr))
path = "/usr/bin/";
path += cmd_only;
debug("path: %s", path.c_str());
if (fs->PathExists(path.c_str()))
if (fs->PathExists(path.c_str(), nullptr))
{
const char *envp[5] = {
"PATH=/bin:/usr/bin",