From f48032658f9f063e7a415678b5588671140f37ec Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Sun, 13 Oct 2024 02:33:46 +0300 Subject: [PATCH] vfs: Fix GetRoot(), CacheRecursiveSearch() and root identifier --- include/filesystem.hpp | 2 +- storage/cache.cpp | 40 ++++++++++++++++++++++++++-------------- storage/filesystem.cpp | 21 +++++++++------------ storage/virtual.cpp | 8 ++++---- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/include/filesystem.hpp b/include/filesystem.hpp index 066972b..759c3c6 100644 --- a/include/filesystem.hpp +++ b/include/filesystem.hpp @@ -37,7 +37,7 @@ static_assert(IFTODT(S_IFCHR) == DT_CHR); else \ return fsi->Ops.op(this->Node, ##__VA_ARGS__) -#define FSROOT(num) "\002root-" #num "\003" +#define FSROOT(num) "\x06root-" #num "\x06" class FileNode { diff --git a/storage/cache.cpp b/storage/cache.cpp index 59ca107..12932a0 100644 --- a/storage/cache.cpp +++ b/storage/cache.cpp @@ -32,7 +32,12 @@ namespace vfs struct cwk_segment segment; if (!cwk_path_get_first_segment(*Path, &segment)) + { + if (strcmp(*Path, Parent->fsi->RootName) == 0) + return Parent; + ReturnLogError(nullptr, "Failed to get first segment of path"); + } size_t segments = 0; while (cwk_path_get_next_segment(&segment)) @@ -41,26 +46,26 @@ namespace vfs if (segments == 0) return Parent; - const char *path = *Path; - if (strncmp(path, "\002root-", 6) == 0) /* FIXME: deduce the index */ + const char *tmpPath = *Path; + if (strncmp(tmpPath, "\x06root-", 6) == 0) /* FIXME: deduce the index */ { - path += 6; - while (*path != '\0' && *path != '\003') - path++; - if (*path == '\003') - path++; + tmpPath += 6; + while (*tmpPath != '\0' && *tmpPath != '\x06') + tmpPath++; + if (*tmpPath == '\x06') + tmpPath++; } else - path = *Path; + tmpPath = *Path; FileNode *__Parent = Parent; - if (this->PathIsAbsolute(path)) + if (this->PathIsAbsolute(tmpPath)) { while (__Parent->Parent) __Parent = __Parent->Parent; } - cwk_path_get_first_segment(path, &segment); + cwk_path_get_first_segment(tmpPath, &segment); do { std::string segmentName(segment.begin, segment.size); @@ -94,11 +99,18 @@ namespace vfs if (Root == nullptr) return nullptr; - debug("%s cache search for \"%s\" in \"%s\"", IsName ? "Relative" : "Absolute", NameOrPath, Root->Path.c_str()); + debug("%s cache search for \"%s\" in \"%s\"", + IsName ? "Relative" : "Absolute", + NameOrPath, + Root->Path.c_str()); struct cwk_segment segment; if (!cwk_path_get_first_segment(NameOrPath, &segment)) + { + if (strcmp(NameOrPath, Root->fsi->RootName) == 0) + return Root; ReturnLogError(nullptr, "Failed to get first segment of path"); + } size_t segments = 0; while (cwk_path_get_next_segment(&segment)) @@ -116,12 +128,12 @@ namespace vfs } const char *path = NameOrPath; - if (strncmp(path, "\002root-", 6) == 0) /* FIXME: deduce the index */ + if (strncmp(path, "\x06root-", 6) == 0) /* FIXME: deduce the index */ { path += 6; - while (*path != '\0' && *path != '\003') + while (*path != '\0' && *path != '\x06') path++; - if (*path == '\003') + if (*path == '\x06') path++; } else diff --git a/storage/filesystem.cpp b/storage/filesystem.cpp index a2c670b..5fd5f9e 100644 --- a/storage/filesystem.cpp +++ b/storage/filesystem.cpp @@ -39,19 +39,16 @@ namespace vfs FileNode *Virtual::GetRoot(size_t Index) { - if (Index >= FileSystemRoots->Children.size()) - return nullptr; - - Inode *RootNode = FileSystemRoots->Children[Index]; - - char rootName[128]{}; - snprintf(rootName, sizeof(rootName), "\002root-%ld\003", Index); + assert(Index < FileSystemRoots->Children.size()); auto it = FileRoots.find(Index); if (it != FileRoots.end()) return it->second; - FileNode *ret = this->CreateCacheNode(nullptr, RootNode, rootName, 0); + Inode *rootNode = FileSystemRoots->Children[Index]; + char rootName[128]{}; + snprintf(rootName, sizeof(rootName), "\x06root-%ld\x06", Index); + FileNode *ret = this->CreateCacheNode(nullptr, rootNode, rootName, 0); FileRoots.insert({Index, ret}); return ret; } @@ -116,7 +113,7 @@ namespace vfs FileNode *Virtual::GetByPath(const char *Path, FileNode *Parent) { debug("GetByPath: %s", Path); - if (Parent == nullptr) + if (Parent == nullptr || this->PathIsAbsolute(Path)) Parent = thisProcess ? thisProcess->Info.RootNode : this->GetRoot(0); if (strcmp(Path, ".") == 0) @@ -129,12 +126,12 @@ namespace vfs if (fn) return fn; - if (strncmp(Path, "\002root-", 6) == 0) /* FIXME: deduce the index */ + if (strncmp(Path, "\x06root-", 6) == 0) /* FIXME: deduce the index */ { Path += 7; - while (*Path != '\0' && *Path != '\003') + while (*Path != '\0' && *Path != '\x06') Path++; - if (*Path == '\003') + if (*Path == '\x06') Path++; } diff --git a/storage/virtual.cpp b/storage/virtual.cpp index 5bb9898..c2d31d1 100644 --- a/storage/virtual.cpp +++ b/storage/virtual.cpp @@ -45,10 +45,10 @@ namespace vfs return -ENOENT; off_t offset = 0; - foreach (const auto &Root in Parent->Children) + for (const auto &Root : Parent->Children) { char rootName[128]{}; - snprintf(rootName, sizeof(rootName), "\x02root-%ld\x03", offset); + snprintf(rootName, sizeof(rootName), "\x06root-%ld\x06", offset); if (strcmp(rootName, Name) == 0) { @@ -159,8 +159,8 @@ namespace vfs S_IRWXO | S_IFDIR; FileNode *proc = this->ForceCreate(this->GetRoot(0), "proc", mode); - FileNode *log = this->ForceCreate(this->GetRoot(0), "var", mode); - log = this->ForceCreate(log, "log", mode); + FileNode *var = this->ForceCreate(this->GetRoot(0), "var", mode); + FileNode *log = this->ForceCreate(var, "log", mode); proc->Node->Flags = iFlags; log->Node->Flags = iFlags;