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

@ -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);
}