fix(kernel/vfs): 🎉 a complete rewrite of the vfs

This is the fourth time re-writing the VFS, hope this will be the last. Tried to make it as modular as possible so this won't be necessary in the future. 🙏

This change required the entire kernel code to be modified.
This commit is contained in:
2025-05-13 15:59:12 +00:00
parent 83a7f83f81
commit 557c7e6235
83 changed files with 3252 additions and 2487 deletions

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"
@ -28,7 +28,7 @@ void cmd_cat(const char *args)
if (args[0] == '\0')
return;
FileNode *node = fs->GetByPath(args, nullptr);
Node node = fs->Lookup(thisProcess->CWD, args);
if (node == nullptr)
{
@ -48,11 +48,11 @@ void cmd_cat(const char *args)
return;
}
kstat stat = {};
node->Stat(&stat);
kstat stat;
fs->Stat(node, &stat);
uint8_t *buffer = new uint8_t[stat.Size + 1];
ssize_t rBytes = node->Read(buffer, stat.Size, 0);
ssize_t rBytes = fs->Read(node, buffer, stat.Size, 0);
if (rBytes > 0)
printf("%s\n", buffer);
else

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"
@ -28,8 +28,7 @@ void cmd_cd(const char *args)
if (args[0] == '\0')
return;
FileNode *node = fs->GetByPath(args, nullptr);
Node node = fs->Lookup(thisProcess->CWD, args);
if (node == nullptr)
{
printf("cd: %s: No such file or directory\n", args);
@ -43,4 +42,5 @@ void cmd_cd(const char *args)
}
thisProcess->CWD = node;
debug("changed cwd to %s", node->Path.c_str());
}

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <acpi.hpp>
#include "../../kernel.h"
@ -38,9 +38,41 @@ void cmd_dump(const char *args)
*strLen = '\0';
strLen++;
void *Address = (void *)strtoul(strAddr, nullptr, 16);
void *Address;
unsigned long Length = strtoul(strLen, nullptr, 10);
Node root = fs->GetRoot(0);
Node fileNode = fs->Lookup(root, strAddr);
if (fileNode && !fileNode->IsDirectory() && !fileNode->IsFIFO() && !fileNode->IsSocket())
{
kstat stat;
int status = fs->Stat(fileNode, &stat);
if (status != 0)
{
printf("cannot get stat: %s\n", strerror(status));
return;
}
size_t size = stat.Size > (off_t)Length ? Length : stat.Size;
Address = new char[size]; /* FIXME: memory leak */
size_t read = fs->Read(fileNode, Address, size, 0);
if (read < Length)
{
debug("clamp %lu to %lu", Length, read);
Length = read;
}
}
else
{
if (fileNode)
{
printf("file %s cannot be dumped\n", strAddr);
return;
}
Address = (void *)strtoul(strAddr, nullptr, 16);
debug("address %s", strAddr);
}
{
unsigned char *AddressChar = (unsigned char *)Address;
unsigned char Buffer[17];
@ -74,4 +106,4 @@ void cmd_dump(const char *args)
}
putchar('\n');
}
}
}

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <task.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <task.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <task.hpp>
#include "../../kernel.h"

View File

@ -17,98 +17,60 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"
using namespace vfs;
const char *ColorNodeType(FileNode *node)
const char *ColorNodeType(Node node)
{
if (node->IsRegularFile())
return "\x1b[32m";
return "\x1b[0m";
else if (node->IsDirectory())
return "\x1b[34m";
return "\x1b[1;34m";
else if (node->IsBlockDevice())
return "\x1b[33m";
return "\x1b[1;33m";
else if (node->IsCharacterDevice())
return "\x1b[33m";
return "\x1b[1;33m";
else if (node->IsFIFO())
return "\x1b[33m";
return "\x1b[0;33m";
else if (node->IsSymbolicLink())
return "\x1b[35m";
return "\x1b[1;36m";
else
return "\x1b[0m";
}
__no_sanitize("alignment") size_t MaxNameLength(FileNode *nodes)
__no_sanitize("alignment") void PrintLS(Node node)
{
size_t maxLength = 0;
kdirent *dirBuffer = new kdirent[16];
ssize_t read = 0;
off_t offset = 0;
while ((read = nodes->ReadDir(dirBuffer, sizeof(kdirent) * 16, offset, LONG_MAX)) > 0)
{
if (read / sizeof(kdirent) == 0)
break;
off_t bufOffset = 0;
debug("There are %ld entries in this directory", read / sizeof(kdirent));
for (size_t i = 0; i < read / sizeof(kdirent); i++)
{
kdirent *dirent = (kdirent *)((uintptr_t)dirBuffer + bufOffset);
if (dirent->d_reclen == 0)
break;
bufOffset += dirent->d_reclen;
maxLength = std::max(maxLength, strlen(dirent->d_name));
debug("dirent->d_name: %s (max length: %ld)", dirent->d_name, maxLength);
}
offset += read / sizeof(kdirent);
}
delete[] dirBuffer;
return maxLength;
}
__no_sanitize("alignment") void PrintLS(FileNode *node)
{
size_t maxNameLength = MaxNameLength(node);
size_t maxNameLength = 0;
int count = 0;
bool first = true;
kdirent *dirBuffer = new kdirent[16];
ssize_t read = 0;
off_t offset = 0;
while ((read = node->ReadDir(dirBuffer, sizeof(kdirent) * 16, offset, LONG_MAX)) > 0)
{
if (read / sizeof(kdirent) == 0)
break;
std::list<Node> children = fs->ReadDirectory(node);
off_t bufOffset = 0;
for (size_t i = 0; i < read / sizeof(kdirent); i++)
{
if (count % 5 == 0 && !first)
printf("\n");
kdirent *dirent = (kdirent *)((uintptr_t)dirBuffer + bufOffset);
if (dirent->d_reclen == 0)
break;
bufOffset += dirent->d_reclen;
printf(" %s%-*s ", ColorNodeType(node), (int)maxNameLength, dirent->d_name);
count++;
first = false;
}
offset += read / sizeof(kdirent);
for (auto &&i : children)
std::max(maxNameLength, i->Name.length());
for (auto &&i : children)
{
if (count % 5 == 0 && !first)
printf("\n");
printf(" %s%-*s ", ColorNodeType(i), (int)maxNameLength, i->Name.c_str());
count++;
first = false;
}
printf("\x1b[0m\n");
delete[] dirBuffer;
}
void cmd_ls(const char *args)
{
if (args[0] == '\0')
{
FileNode *rootNode = thisProcess->CWD;
Node rootNode = thisProcess->CWD;
if (rootNode == nullptr)
rootNode = fs->GetRoot(0);
@ -117,7 +79,7 @@ void cmd_ls(const char *args)
return;
}
FileNode *thisNode = fs->GetByPath(args, nullptr);
Node thisNode = fs->Lookup(thisProcess->CWD, args);
if (thisNode == nullptr)
{

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <acpi.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <task.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <task.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <task.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <task.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include <task.hpp>
#include "../../kernel.h"

View File

@ -17,70 +17,38 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"
void tree_loop(FileNode *rootNode, int depth = 0)
// Enhancing the tree_loop function to display a fancier tree structure
__no_sanitize("alignment") void tree_loop(Node rootNode, int depth = 0, std::string prefix = "")
{
// for (auto Child : 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);
// }
std::list<Node> children = fs->ReadDirectory(rootNode);
size_t count = children.size();
size_t index = 0;
kdirent *dirBuffer = new kdirent[16];
ssize_t read = 0;
off_t offset = 0;
while ((read = rootNode->ReadDir(dirBuffer, sizeof(kdirent) * 16, offset, LONG_MAX)) > 0)
{
if (read / sizeof(kdirent) == 0)
break;
for (auto &&child : children)
{
if (child->Name == "." || child->Name == "..")
continue;
off_t bufOffset = 0;
for (size_t i = 0; i < read / sizeof(kdirent); i++)
{
kdirent *dirent = (kdirent *)((uintptr_t)dirBuffer + bufOffset);
if (dirent->d_reclen == 0)
break;
bufOffset += dirent->d_reclen;
bool isLast = (++index == count);
if (strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
continue;
printf("%s%s- %s\n", prefix.c_str(), isLast ? "\\" : "|-", child->Name.c_str());
FileNode *node = fs->GetByPath(dirent->d_name, rootNode);
if (node == nullptr)
continue;
for (int i = 0; i < depth; i++)
printf(" ");
printf("|- %s\n", dirent->d_name);
if (node->IsDirectory())
tree_loop(node, depth + 1);
}
offset += read;
}
delete[] dirBuffer;
if (child->IsDirectory())
{
std::string newPrefix = prefix + (isLast ? " " : "| ");
tree_loop(child, depth + 1, newPrefix);
}
}
}
void cmd_tree(const char *args)
{
FileNode *rootNode = thisProcess->CWD;
Node rootNode = thisProcess->CWD;
if (args[0] == '\0')
{
if (rootNode == nullptr)
@ -88,7 +56,7 @@ void cmd_tree(const char *args)
}
else
{
rootNode = fs->GetByPath(args, nullptr);
rootNode = fs->Lookup(thisProcess->CWD, args);
if (rootNode == nullptr)
{
printf("ls: %s: No such file or directory\n", args);

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"

View File

@ -17,7 +17,7 @@
#include "../cmds.hpp"
#include <filesystem.hpp>
#include <fs/vfs.hpp>
#include "../../kernel.h"