#include #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; return node; } std::string get_full_path(vfs_node* node) { if(node->parent == nullptr) { return "/"; } if(node->parent->parent == nullptr) { /* If parent is root. */ return "/" + node->name; } return get_full_path(node->parent) + "/" + node->name; } void delete_vfs_tree(vfs_node* node) { if(!node) { return; } if(node->type == DIR_NODE) { for(auto const& [key, val] : node->children) { delete_vfs_tree(val); } } delete node; } /* Find a VFS node by it's absolute path. */ vfs_node* find_node_by_path(vfs_node* root, const std::string& path) { if(path == "/") { return root; } vfs_node* current = root; std::stringstream ss(path); std::string segment; /* Discard the first empty segment that comes form the leading '/'. */ if(path[0] == '/') { std::getline(ss, segment, '/'); } while(std::getline(ss, segment, '/')) { if(current->type == DIR_NODE && current->children.count(segment)) { current = current->children[segment]; } else { return nullptr; /* Path segmenet not found. */ } } return current; }