From f462630d9c42a47469715a10bc110fd0a329488a Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Thu, 28 Mar 2024 11:16:14 +0300 Subject: [PATCH] check pwd when removing --- inc/FileSystem.hpp | 3 +++ src/FileSystem.cpp | 26 ++++++++++++++++++++++++++ test/TestTree.cpp | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/inc/FileSystem.hpp b/inc/FileSystem.hpp index 5f03702..2d6f172 100644 --- a/inc/FileSystem.hpp +++ b/inc/FileSystem.hpp @@ -32,6 +32,9 @@ public: const std::string& getLastError(); +private: + bool isPathContainsCurrent(Node* node); + private: Directory* root = nullptr; Directory* currentDirectory = nullptr; diff --git a/src/FileSystem.cpp b/src/FileSystem.cpp index df42180..a7b7eda 100644 --- a/src/FileSystem.cpp +++ b/src/FileSystem.cpp @@ -52,6 +52,11 @@ bool FileSystem::removeDirectory(const Path& path) { return false; } + if (isPathContainsCurrent(existingNode)) { + gError = "Can not remove directory you are currently working in"; + return false; + } + assert(parentDirectory->detachNode(path.getParentChain(), path.getFilename())); delete existingNode; @@ -94,3 +99,24 @@ void FileSystem::log() const { const std::string& FileSystem::getLastError() { return gError; } + +bool FileSystem::isPathContainsCurrent(Node* node) { + std::vector currentPath; + std::vector path; + + root->getNodePath(currentDirectory, currentPath); + root->getNodePath(node, path); + + std::reverse(currentPath.begin(), currentPath.end()); + std::reverse(path.begin(), path.end()); + + if (path.size() > currentPath.size()) { + return false; + } + + for (ui32 i = 0; i < path.size(); i++) { + if (path[i] != currentPath[i]) return false; + } + + return true; +} \ No newline at end of file diff --git a/test/TestTree.cpp b/test/TestTree.cpp index 3011abf..5ff8afa 100644 --- a/test/TestTree.cpp +++ b/test/TestTree.cpp @@ -6,7 +6,7 @@ static double randomFloat() { return static_cast(std::rand()) / static_cast(RAND_MAX); } -const auto size = 10000; +const auto size = 1000; class TestClass { ui64 val1 = 0;