Virtual File System #10

Merged
apio merged 7 commits from vfs into main 2022-10-10 18:25:43 +00:00
4 changed files with 45 additions and 10 deletions
Showing only changes of commit b38c52f8c7 - Show all commits

View File

@ -12,6 +12,7 @@ namespace VFS
{
char name[64];
uint64_t inode;
uint64_t length;
node_read read_func;
node_finddir find_func;
};

View File

@ -1,24 +1,54 @@
#define MODULE "vfs"
#include "fs/VFS.h"
#include "log/Log.h"
static VFS::Node* vfs_root;
int32_t VFS::read(Node* node, uint32_t offset, uint32_t length, char* buffer)
{
if (!node) return -1;
if (!node->read_func) return -1;
if (!node)
{
kwarnln("read() failed: trying to read from nullptr");
return -1;
}
if (!node->read_func)
{
kwarnln("read() failed: the chosen node doesn't support reading");
return -1;
}
return node->read_func(node, offset, length, buffer);
}
VFS::Node* VFS::open(const char* filename)
{
if (!vfs_root) return 0;
if (!vfs_root->find_func) return 0;
if (!vfs_root)
{
kwarnln("open() failed: root not mounted");
return 0;
}
if (!vfs_root->find_func)
{
kwarnln("open() failed: root doesn't support finding files");
return 0;
}
return vfs_root->find_func(vfs_root, filename);
}
void VFS::mount_root(Node* root)
{
if (!root)
{
kwarnln("mount_root() failed: attempted to mount nullptr");
return;
}
if (vfs_root)
{
kwarnln("mount_root() failed: root filesystem already mounted");
return;
}
kinfoln("mounting node '%s' as vfs root", root->name);
vfs_root = root;
}

View File

@ -152,6 +152,7 @@ void initrd_scan()
VFS::Node& node = nodes[i];
node.inode = i;
node.read_func = initrd_read;
node.length = files[i].size;
strncpy(node.name, files[i].name, sizeof(node.name));
}
}
@ -163,10 +164,13 @@ void InitRD::init()
kdbgln("physical base at %lx, size %lx, mapped to %p", bootboot.initrd_ptr, bootboot.initrd_size, initrd_base);
kdbgln("total blocks: %ld", get_total_blocks());
initrd_scan();
initrd_root.length = 0;
initrd_root.inode = (uint64_t)-1;
strncpy(initrd_root.name, "initrd", 7);
initrd_root.find_func = initrd_scan_root;
VFS::mount_root(&initrd_root);
VFS::mount_root(0);
VFS::mount_root(&initrd_root);
kdbgln("mounted initrd at VFS root, total files: %d", total_files);
initrd_initialized = true;
}

View File

@ -162,17 +162,17 @@ extern "C" void _start()
kinfoln("Trying VFS");
VFS::Node* node = VFS::open("sys/moon.sym");
VFS::Node* node = VFS::open("sys/config");
if (!node) { kerrorln("Unable to find file in VFS"); }
else
{
kinfoln("Found '%s'", node->name);
char buffer[256];
kinfoln("Reading 255 bytes at offset 128...");
int nwritten = VFS::read(node, 128, 255, buffer);
kinfoln("Read a total of %d bytes", nwritten);
buffer[255] = 0;
char* buffer = (char*)kmalloc(node->length + 1);
buffer[node->length] = 0;
int nread = VFS::read(node, 0, (uint32_t)node->length, buffer);
kdbgln("Read %d bytes", nread);
kinfoln("Read: %s", buffer);
kfree(buffer);
}
PCI::scan([](PCI::Device& dev) {