2022-10-08 19:35:19 +00:00
|
|
|
#define MODULE "vfs"
|
|
|
|
|
2022-10-08 19:22:46 +00:00
|
|
|
#include "fs/VFS.h"
|
2022-10-08 19:35:19 +00:00
|
|
|
#include "log/Log.h"
|
2022-10-08 19:22:46 +00:00
|
|
|
|
|
|
|
static VFS::Node* vfs_root;
|
|
|
|
|
|
|
|
int32_t VFS::read(Node* node, uint32_t offset, uint32_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;
|
|
|
|
}
|
2022-10-08 19:22:46 +00:00
|
|
|
|
|
|
|
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-08 19:22:46 +00:00
|
|
|
|
|
|
|
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);
|
2022-10-08 19:22:46 +00:00
|
|
|
vfs_root = root;
|
|
|
|
}
|