Luna/kernel/src/fs/VFS.cpp

65 lines
1.4 KiB
C++
Raw Normal View History

2022-10-08 19:35:19 +00:00
#define MODULE "vfs"
#include "fs/VFS.h"
2022-10-08 19:35:19 +00:00
#include "log/Log.h"
2022-10-09 19:19:22 +00:00
#include "std/string.h"
static VFS::Node* vfs_root;
ssize_t VFS::read(Node* node, size_t offset, size_t length, char* buffer)
{
2022-10-08 19:35:19 +00:00
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;
}
kdbgln("read(): node %s (inode %ld), offset %zd, %zd bytes, into %p", node->name, node->inode, offset, length,
2022-10-09 19:19:22 +00:00
(void*)buffer);
return node->read_func(node, offset, length, buffer);
}
VFS::Node* VFS::open(const char* filename)
{
2022-10-08 19:35:19 +00:00
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;
}
2022-10-09 19:19:22 +00:00
kinfoln("open(): opening %s", filename);
return vfs_root->find_func(vfs_root, filename);
}
void VFS::mount_root(Node* root)
{
2022-10-08 19:35:19 +00:00
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;
2022-10-09 19:19:22 +00:00
}
VFS::Node* VFS::root()
{
return vfs_root;
}