From 75722822f6bf92d63b94b5b42804e849348c42cd Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Thu, 28 Mar 2024 00:05:07 +0300 Subject: [PATCH] rd added --- inc/DirectoryTree.hpp | 2 +- inc/FileSystem.hpp | 9 ++------- src/DirectoryTree.cpp | 40 ++++++++++++++++++++++++++++------------ src/FileSystem.cpp | 20 ++++++++++++++++++++ src/Interpreter.cpp | 8 ++++++++ 5 files changed, 59 insertions(+), 20 deletions(-) diff --git a/inc/DirectoryTree.hpp b/inc/DirectoryTree.hpp index 5e9dc09..624ca23 100644 --- a/inc/DirectoryTree.hpp +++ b/inc/DirectoryTree.hpp @@ -69,8 +69,8 @@ public: void dump(std::stringstream& ss); bool attachNode(const std::vector& directoryPath, const Key& newKey, Node* newNode); + bool detachNode(const std::vector& directoryPath, const Key& key); Node* findNode(const std::vector& path, ui32 currentDepth = 0); - void detachNode(Node* node); [[nodiscard]] ui32 getMaxDepth() const; diff --git a/inc/FileSystem.hpp b/inc/FileSystem.hpp index 61593cd..449349c 100644 --- a/inc/FileSystem.hpp +++ b/inc/FileSystem.hpp @@ -12,17 +12,12 @@ public: bool makeDirectory(const Path& path); bool changeCurrent(const Path& path); + bool removeDirectory(const Path& path); + void log() const; const std::string& getLastError(); -private: - void logNode(std::stringstream& ss, const Node* node, int depth, std::vector& indents) const; - void indent(std::stringstream & ss, int depth, std::vector& indents) const; - void logDirectory(std::stringstream & ss, const Directory* node, int depth, std::vector& indents) const; - void logFile(std::stringstream& ss, const File* node, int depth, std::vector& indents) const; - void logLink(std::stringstream & ss, const Link* node, int depth, std::vector& indents) const; - private: Directory* root = nullptr; Directory* currentDirectory = nullptr; diff --git a/src/DirectoryTree.cpp b/src/DirectoryTree.cpp index 7047e27..12cd5d4 100644 --- a/src/DirectoryTree.cpp +++ b/src/DirectoryTree.cpp @@ -23,20 +23,19 @@ Directory::Directory() { mType = DIRECTORY; } -Directory::~Directory() = default; +Directory::~Directory() { + mMembers.traverseInorder(mMembers.getRoot(), [](DirectoryTree::Node* node){ + delete node->data; + }); +} bool Directory::attachNode(const std::vector& directoryPath, const Key& newKey, Node* newNode) { Node* node = findNode(directoryPath, 0); - if (!node) { + if (!node || node->mType != DIRECTORY) { gError = "Invalid path"; return false; } - if (node->mType != DIRECTORY) { - gError = "given path is not a directory"; - return false; - } - auto directory = ((Directory*) node); DirectoryTree::Node* iterNode = directory->mMembers.find(DirectoryKey(newKey)); @@ -47,8 +46,29 @@ bool Directory::attachNode(const std::vector& directoryPath, const Key& new } directory->mMembers.insert(DirectoryKey(newKey), newNode); - updateTreeLinkCount(newNode); + updateTreeLinkCount(directory); + return true; +} + +bool Directory::detachNode(const std::vector& directoryPath, const Key& key) { + Node* node = findNode(directoryPath, 0); + if (!node || node->mType != DIRECTORY) { + gError = "Invalid path"; + return false; + } + + auto directory = ((Directory*) node); + + DirectoryTree::Node* removeNode = directory->mMembers.find(DirectoryKey(key)); + if (!removeNode) { + gError = "Invalid path"; + return false; + } + + directory->mMembers.remove(DirectoryKey(key)); + + updateTreeLinkCount(directory); return true; } @@ -90,10 +110,6 @@ void Directory::updateTreeLinkCount(Node* node) { // TODO : update all caches all the way up to '/' } -void Directory::detachNode(Node* node) { - // mMembers.remove(); -} - void Directory::getMaxDepthUtil(ui32 depth, ui32& maxDepth) const { if (!mMembers.getRoot()) return; maxDepth = std::max(depth, maxDepth); diff --git a/src/FileSystem.cpp b/src/FileSystem.cpp index fa680c0..300e0b5 100644 --- a/src/FileSystem.cpp +++ b/src/FileSystem.cpp @@ -38,6 +38,26 @@ bool FileSystem::makeDirectory(const Path& path) { return true; } +bool FileSystem::removeDirectory(const Path& path) { + if (path.getDepth() < 1 || path.isInvalid()) { + gError = "Invalid path"; + return false; + } + + Directory* parentDirectory = path.isAbsolute() ? root : currentDirectory; + + auto existingNode = parentDirectory->findNode(path.getChain()); + if (!existingNode) { + gError = "Cant remove, such node doesnt exists"; + return false; + } + + assert(parentDirectory->detachNode(path.getParentChain(), path.getFilename())); + + delete existingNode; + return true; +} + bool FileSystem::changeCurrent(const Path& path) { if (path.isInvalid()) { gError = "Invalid path"; diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index 0afee4c..8bc2b05 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -25,6 +25,14 @@ Interpreter::Interpreter() { return filesystem.makeDirectory(args[1]); } }; + + mCommands["rd"] = { + "remove directory", + 1, + [](FileSystem& filesystem, const std::vector& args){ + return filesystem.removeDirectory(args[1]); + } + }; } void Interpreter::reportError(const std::string& description) {