From 3ca246383414647540b89606aada4d15c8d98f9f Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Sun, 13 Oct 2024 02:25:29 +0300 Subject: [PATCH] vfs: Implement GetName() & GetPath() in FileNode class --- include/filesystem.hpp | 3 +++ storage/filesystem.cpp | 24 ++++++++++++++++++++++++ syscalls/linux.cpp | 15 ++++++++++----- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/include/filesystem.hpp b/include/filesystem.hpp index 00ba11f..066972b 100644 --- a/include/filesystem.hpp +++ b/include/filesystem.hpp @@ -48,6 +48,9 @@ public: Inode *Node; FileSystemInfo *fsi; + std::string GetName(); + std::string GetPath(); + bool IsDirectory() { return S_ISDIR(Node->Mode); } bool IsCharacterDevice() { return S_ISCHR(Node->Mode); } bool IsBlockDevice() { return S_ISBLK(Node->Mode); } diff --git a/storage/filesystem.cpp b/storage/filesystem.cpp index 6fce9b6..a2c670b 100644 --- a/storage/filesystem.cpp +++ b/storage/filesystem.cpp @@ -287,3 +287,27 @@ namespace vfs return 0; } } + +std::string FileNode::GetName() +{ + return this->Name; +} + +std::string FileNode::GetPath() +{ + const char *path = this->Path.c_str(); + if (strncmp(path, "\x06root-", 6) == 0) /* FIXME: deduce the index */ + { + path += 6; + while (*path != '\0' && *path != '\x06') + path++; + if (*path == '\x06') + path++; + } + else + return this->Path; + + if (path[0] == '\0') + return std::string(this->fsi->RootName); + return std::string(path); +} diff --git a/syscalls/linux.cpp b/syscalls/linux.cpp index 193e3a1..3af91c2 100644 --- a/syscalls/linux.cpp +++ b/syscalls/linux.cpp @@ -1897,14 +1897,19 @@ static long linux_getcwd(SysFrm *, char *buf, size_t size) if (pBuf == nullptr) return -EFAULT; - const char *cwd = pcb->CWD->Path.c_str(); - size_t len = strlen(cwd); - if (len >= size) + std::string cwd = pcb->CWD->GetPath(); + if (cwd.length() >= size) { - warn("Buffer too small (%ld < %ld)", len, size); - return -ERANGE; + warn("Buffer too small (%ld < %ld)", cwd.length(), size); + return -linux_ERANGE; } + strncpy(pBuf, cwd.c_str(), cwd.length()); + pBuf[cwd.length()] = '\0'; + debug("cwd: \"%s\" with %ld bytes", cwd.c_str(), cwd.length()); + return cwd.length(); +} + static int linux_chdir(SysFrm *, const char *path) { PCB *pcb = thisProcess;