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-11 15:48:11 +00:00
|
|
|
#include "errno.h"
|
2022-10-08 19:35:19 +00:00
|
|
|
#include "log/Log.h"
|
2022-10-16 12:45:25 +00:00
|
|
|
#include "std/libgen.h"
|
2022-10-10 16:44:43 +00:00
|
|
|
#include "std/stdlib.h"
|
2022-10-09 19:19:22 +00:00
|
|
|
#include "std/string.h"
|
2022-10-08 19:22:46 +00:00
|
|
|
|
|
|
|
static VFS::Node* vfs_root;
|
|
|
|
|
2022-10-09 19:30:38 +00:00
|
|
|
ssize_t VFS::read(Node* node, size_t offset, size_t length, char* buffer)
|
2022-10-08 19:22:46 +00:00
|
|
|
{
|
2022-10-08 19:35:19 +00:00
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
kwarnln("read() failed: trying to read from nullptr");
|
|
|
|
return -1;
|
|
|
|
}
|
2022-10-11 15:48:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-10-08 19:22:46 +00:00
|
|
|
|
|
|
|
return node->read_func(node, offset, length, buffer);
|
|
|
|
}
|
|
|
|
|
2022-10-11 19:03:30 +00:00
|
|
|
ssize_t VFS::write(Node* node, size_t offset, size_t length, const char* buffer)
|
|
|
|
{
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
kwarnln("write() failed: trying to write to nullptr");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (node->type == VFS_DIRECTORY)
|
|
|
|
{
|
|
|
|
kwarnln("write() failed: is a directory");
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
|
|
|
if (!node->write_func)
|
|
|
|
{
|
|
|
|
kwarnln("write() failed: the chosen node doesn't support writing");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node->write_func(node, offset, length, buffer);
|
|
|
|
}
|
|
|
|
|
2022-10-08 19:22:46 +00:00
|
|
|
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;
|
2022-10-09 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VFS::Node* VFS::root()
|
|
|
|
{
|
|
|
|
return vfs_root;
|
2022-10-10 16:44:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-10-11 16:23:00 +00:00
|
|
|
if (child->flags & VFS_MOUNTPOINT)
|
|
|
|
{
|
|
|
|
if (!child->link)
|
|
|
|
{
|
|
|
|
kwarnln("Current node's link is null");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
child = child->link;
|
|
|
|
}
|
2022-10-10 16:44:43 +00:00
|
|
|
current_node = child;
|
|
|
|
kfree(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
filename += path_section_size;
|
|
|
|
}
|
2022-10-11 16:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-16 12:45:25 +00:00
|
|
|
int VFS::mkdir(const char* pathname)
|
|
|
|
{
|
|
|
|
char* bstr = strdup(pathname);
|
|
|
|
char* dstr = strdup(pathname);
|
|
|
|
|
|
|
|
char* base = basename(bstr);
|
|
|
|
char* dir = dirname(dstr);
|
|
|
|
|
|
|
|
kdbgln("mkdir(): creating %s in directory %s", base, dir);
|
|
|
|
|
|
|
|
int result = mkdir(dir, base);
|
|
|
|
|
|
|
|
kfree(bstr);
|
|
|
|
kfree(dstr);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-10-16 14:58:18 +00:00
|
|
|
bool VFS::exists(const char* pathname)
|
|
|
|
{
|
|
|
|
return resolve_path(pathname) != nullptr;
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:23:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-11 17:21:16 +00:00
|
|
|
void VFS::mount(const char* pathname, Node* mounted)
|
|
|
|
{
|
|
|
|
return mount(resolve_path(pathname), mounted);
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:23:00 +00:00
|
|
|
void VFS::unmount(Node* mountpoint)
|
|
|
|
{
|
|
|
|
if (!mountpoint) return;
|
|
|
|
if (!(mountpoint->flags & VFS_MOUNTPOINT)) return;
|
|
|
|
mountpoint->flags &= ~VFS_MOUNTPOINT;
|
2022-10-08 19:22:46 +00:00
|
|
|
}
|