mirror of
https://github.com/EnderIce2/Fennix.git
synced 2025-07-01 18:39:16 +00:00
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:
@ -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
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <task.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <task.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <task.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <acpi.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <task.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <task.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <task.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <task.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <task.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
@ -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);
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
#include "../cmds.hpp"
|
||||
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
|
||||
#include "../../kernel.h"
|
||||
|
||||
|
@ -19,7 +19,7 @@
|
||||
|
||||
#include <interface/driver.h>
|
||||
#include <interface/input.h>
|
||||
#include <filesystem.hpp>
|
||||
#include <fs/vfs.hpp>
|
||||
#include <driver.hpp>
|
||||
#include <lock.hpp>
|
||||
#include <exec.hpp>
|
||||
@ -88,6 +88,7 @@ void KShellThread()
|
||||
|
||||
KPrint("Starting kernel shell...");
|
||||
thisThread->SetPriority(Tasking::TaskPriority::Normal);
|
||||
thisProcess->CWD = fs->GetRoot(0);
|
||||
|
||||
std::string strBuf = "";
|
||||
std::vector<std::string *> history;
|
||||
@ -97,7 +98,8 @@ void KShellThread()
|
||||
bool tabDblPress = false;
|
||||
|
||||
const char *keyDevPath = "/dev/input/keyboard";
|
||||
FileNode *kfd = fs->GetByPath(keyDevPath, fs->GetRoot(0));
|
||||
Node root = fs->GetRoot(0);
|
||||
Node kfd = fs->Lookup(root, keyDevPath);
|
||||
if (kfd == nullptr)
|
||||
{
|
||||
KPrint("Failed to open keyboard device!");
|
||||
@ -125,21 +127,20 @@ void KShellThread()
|
||||
debug("clearing strBuf(\"%s\")", strBuf.c_str());
|
||||
strBuf.clear();
|
||||
|
||||
FileNode *cwd = thisProcess->CWD;
|
||||
Node cwd = thisProcess->CWD;
|
||||
if (!cwd)
|
||||
cwd = fs->GetRoot(0);
|
||||
std::string cwdStr = fs->GetByNode(cwd);
|
||||
debug("cwd: %*s", (int)cwdStr.size(), cwdStr.c_str());
|
||||
debug("cwd: %s", cwd->Path.c_str());
|
||||
|
||||
printf("\x1b[1;34m%s@%s:%s$ \x1b[0m",
|
||||
"kernel", "fennix",
|
||||
cwdStr.c_str());
|
||||
cwd->Path.c_str());
|
||||
|
||||
KeyboardReport scBuf{};
|
||||
ssize_t nBytes;
|
||||
while (true)
|
||||
{
|
||||
nBytes = kfd->Read(&scBuf, sizeof(KeyboardReport), 0);
|
||||
nBytes = fs->Read(kfd, &scBuf, sizeof(KeyboardReport), 0);
|
||||
if (nBytes == 0)
|
||||
{
|
||||
debug("Empty read from keyboard device!");
|
||||
@ -561,14 +562,14 @@ void KShellThread()
|
||||
if (fs->PathIsRelative(cmd_only.c_str()))
|
||||
{
|
||||
path += cmd_only;
|
||||
if (!fs->PathExists(path.c_str(), nullptr))
|
||||
if (!fs->Lookup(root, path.c_str()))
|
||||
path = "/usr/bin/" + cmd_only;
|
||||
}
|
||||
else
|
||||
path = cmd_only;
|
||||
|
||||
debug("path: %s", path.c_str());
|
||||
if (fs->PathExists(path.c_str(), nullptr))
|
||||
if (fs->Lookup(root, path.c_str()))
|
||||
{
|
||||
const char *envp[5] = {
|
||||
"PATH=/bin:/usr/bin",
|
||||
|
Reference in New Issue
Block a user