bettola/common/src/vfs_manager.cpp
Ritchie Cunningham b30c497769 [Add] CoW foundation and scriptable rm command.
Putting down the nessassary steps for the Copy-on-Write described in the
previous commit. Implemented the first script based write command, 'rm'
2025-09-21 22:39:16 +01:00

82 lines
2.9 KiB
C++

#include "vfs_manager.h"
#include "vfs.h"
/* Create a new node. */
vfs_node* new_node(std::string name, vfs_node_type type, vfs_node* parent) {
vfs_node* node = new vfs_node();
node->name = name;
node->type = type;
node->parent = parent;
node->read_only = false; /* Writable by default. */
return node;
}
VFSManager::VFSManager(void) {
/* Create template VFS that holds shared, read-only directories. */
_vfs_root = new_node("/", DIR_NODE, nullptr);
vfs_node* bin = new_node("bin", DIR_NODE, _vfs_root);
_vfs_root->children["bin"] = bin;
_vfs_root->read_only = true;
bin->read_only = true;
/* TODO:
* Load all scripts from assets/scripts/bin into the bind node.
* We'll create ls.lua manually for now.
*/
vfs_node* ls_script = new_node("ls.lua", FILE_NODE, bin);
ls_script->content = R"lua(-- /bin/ls.lua - Lists files in a directory.
local dir = current_dir -- Get directory object from C++.
local output = ""
-- Iterate over the 'children' map exposed from c++.
for name, node in pairs(dir.children) do
output = output .. name
if node.type == 1 then
output = output .. "/"
end
output = output .. " "
end
return output
)lua";
bin->children["ls.lua"] = ls_script;
vfs_node* rm_script = new_node("rm.lua", FILE_NODE, bin);
rm_script->content = R"lua(-- /bin/rm.lua - Removes a file.
local file_to_remove = arg[1]
if not file_to_remove then return "rm: missing operand" end
return vfs.rm(file_to_remove, current_dir)
)lua";
bin->children["rm.lua"] = rm_script;
}
VFSManager::~VFSManager(void) {
/* TODO: Implement recursive deletion of all created VFS nodes.*/
//delete _vfs_root;
}
vfs_node* VFSManager::create_vfs(const std::string& system_type) {
vfs_node* root = new_node("/", DIR_NODE, nullptr);
/* Create directories for this specific VFS. */
vfs_node* home = new_node("home", DIR_NODE, root);
vfs_node* user = new_node("user", DIR_NODE, home);
home->children["user"] = user;
vfs_node* readme = new_node("readme.txt", FILE_NODE, user);
readme->content = "Welcome to your new virtual machine.";
user->children["readme.txt"] = readme;
/* Link to the shared directories from the template. */
root->children["bin"] = _vfs_root->children["bin"];
if(system_type == "npc") {
vfs_node* npc_file = new_node("npc_system.txt", FILE_NODE, root);
npc_file->content = "This guy sucks nuts!";
root->children["npc_system.txt"] = npc_file;
}
return root;
}