Luna/kernel/src/fs/VFS.cpp

142 lines
3.7 KiB
C++
Raw Normal View History

2022-10-08 19:35:19 +00:00
#define MODULE "vfs"
#include "fs/VFS.h"
#include "errno.h"
2022-10-08 19:35:19 +00:00
#include "log/Log.h"
#include "std/stdlib.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->type == VFS_DIRECTORY)
{
kwarnln("read() failed: is a directory");
return -EISDIR;
}
2022-10-08 19:35:19 +00:00
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);
}
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;
}
VFS::Node* VFS::resolve_path(const char* filename, Node* root)
{
if (!root) root = vfs_root;
if (strlen(filename) == 0) return 0;
if (*filename == '/') // Absolute path.
{
filename++;
root = vfs_root;
}
Node* current_node = root;
while (true)
{
while (*filename == '/') { filename++; }
if (*filename == 0) { return current_node; }
size_t path_section_size = 0;
while (filename[path_section_size] && filename[path_section_size] != '/') { path_section_size++; }
if (strncmp(filename, ".", path_section_size) != 0) // The current path section is not '.'
{
char* buffer = (char*)kmalloc(path_section_size + 1);
memcpy(buffer, filename, path_section_size);
buffer[path_section_size] = 0;
if (!current_node->find_func)
{
kwarnln("Current node has no way to find child nodes");
return 0;
}
Node* child = current_node->find_func(current_node, buffer);
if (!child)
{
kwarnln("Current node did not find our target node");
return 0;
}
if (child->flags & VFS_MOUNTPOINT)
{
kdbgln("Current node (%s, inode=%ld) is a mountpoint, resolving actual node", child->name,
child->inode);
if (!child->link)
{
kwarnln("Current node's link is null");
return 0;
}
child = child->link;
kdbgln("Resolved to %s (inode %ld)", child->name, child->inode);
}
current_node = child;
kfree(buffer);
}
filename += path_section_size;
}
}
int VFS::mkdir(const char* path, const char* name)
{
Node* node = resolve_path(path, vfs_root);
if (!node)
{
kwarnln("Attempting to mkdir in %s, which does not exist", path);
return -1;
}
if (!node->mkdir_func)
{
kwarnln("Chosen node does not support mkdir()");
return -1;
}
return node->mkdir_func(node, name);
}
void VFS::mount(Node* mountpoint, Node* mounted)
{
if (!mountpoint || !mounted) return;
if (mountpoint->flags & VFS_MOUNTPOINT || mounted->flags & VFS_MOUNTPOINT) return;
mountpoint->link = mounted;
mountpoint->flags |= VFS_MOUNTPOINT;
}
void VFS::unmount(Node* mountpoint)
{
if (!mountpoint) return;
if (!(mountpoint->flags & VFS_MOUNTPOINT)) return;
mountpoint->flags &= ~VFS_MOUNTPOINT;
}