diff --git a/client/src/terminal.cpp b/client/src/terminal.cpp index 9293de0..1fdff70 100644 --- a/client/src/terminal.cpp +++ b/client/src/terminal.cpp @@ -20,7 +20,8 @@ Terminal::Terminal(void) { _should_close = false; /* Create local file system for the client. */ - _local_vfs = vfs_manager::create_root_system("local"); + VFSManager vfs_manager; + _local_vfs = vfs_manager.create_vfs("local"); _local_cmd_processor = new CommandProcessor(_local_vfs); _current_path = get_full_path(_local_cmd_processor->get_current_dir()); _scroll_offset = 0; diff --git a/common/src/vfs_manager.cpp b/common/src/vfs_manager.cpp index e379b5b..db431d4 100644 --- a/common/src/vfs_manager.cpp +++ b/common/src/vfs_manager.cpp @@ -10,25 +10,16 @@ vfs_node* new_node(std::string name, vfs_node_type type, vfs_node* parent) { return node; } -vfs_node* vfs_manager::create_root_system(const std::string& system_type) { - /* Rood directory. */ - vfs_node* root = new_node("/", DIR_NODE, nullptr); - - /* Subdirectories. */ - vfs_node* home = new_node("home", DIR_NODE, root); - vfs_node* bin = new_node("bin", DIR_NODE, root); - root->children["home"] = home; - root->children["bin"] = bin; - - /* User diractory. */ - vfs_node* user = new_node("user", DIR_NODE, home); - home->children["user"] = user; - - /* Create file. */ - vfs_node* readme = new_node("readme.txt", FILE_NODE, user); - readme->content = "Welcome to your new virtual machine."; - user->children["readme.txt"] = readme; +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; + /* 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++. @@ -46,6 +37,26 @@ vfs_node* vfs_manager::create_root_system(const std::string& system_type) { )lua"; bin->children["ls.lua"] = ls_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); diff --git a/common/src/vfs_manager.h b/common/src/vfs_manager.h index 0175c40..ec328e1 100644 --- a/common/src/vfs_manager.h +++ b/common/src/vfs_manager.h @@ -1,7 +1,16 @@ #pragma once +#include + #include "vfs.h" -namespace vfs_manager { - vfs_node* create_root_system(const std::string& system_type); -} +class VFSManager { +public: + VFSManager(void); + ~VFSManager(void); /* TODO: Implement recursive VFS deletion. */ + + vfs_node* create_vfs(const std::string& system_type); +private: + vfs_node* _vfs_root; +}; + diff --git a/server/src/main.cpp b/server/src/main.cpp index 6e513c1..8a4d0cd 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -9,10 +9,12 @@ int main(int argc, char** argv) { printf("=== Server starting ===\n"); + VFSManager vfs_manager; + /* Create the world map of networks. */ std::map world_vfs; - world_vfs["8.8.8.8"] = vfs_manager::create_root_system("npc"); /* Basic npc system. */ - world_vfs["10.0.2.15"] = vfs_manager::create_root_system("npc"); /* Another one. */ + world_vfs["8.8.8.8"] = vfs_manager.create_vfs("npc"); /* Basic npc system. */ + world_vfs["10.0.2.15"] = vfs_manager.create_vfs("npc"); /* Another one. */ printf("Created world with %zu networks.\n", world_vfs.size()); /* Network manager now managed the whole world. */