From 1efe2c0a081b6661d0880048a7a3ff70fe25016f Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Sun, 31 Mar 2024 12:00:19 +0300 Subject: [PATCH] clean-up --- CMakeLists.txt | 6 ++--- inc/Directory.hpp | 10 +++---- inc/FileSystem.hpp | 10 +++---- inc/Link.hpp | 3 +-- inc/Node.hpp | 56 +++++++++++++++++++++++----------------- inc/Path.hpp | 17 ++++++------ src/Directory.cpp | 7 ++--- src/FileSystem.cpp | 4 +-- src/Interpreter.cpp | 20 +++++++------- src/Link.cpp | 2 +- src/Node.cpp | 32 ++++++++++++++++++++--- src/Path.cpp | 35 +++++++++++-------------- test/TestConsistency.cpp | 2 +- 13 files changed, 114 insertions(+), 90 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 997fd07..4237358 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,9 +25,7 @@ target_link_libraries(TestFSE ${PROJECT_NAME} UnitTest++) add_executable(TestConsistency "test/TestConsistency.cpp") target_link_libraries(TestConsistency ${PROJECT_NAME} UnitTest++) -file(COPY "test/consistency_state" DESTINATION "${CMAKE_BINARY_DIR}/") -file(COPY "test/consistency_commands" DESTINATION "${CMAKE_BINARY_DIR}/") -file(COPY "test/checkConsistency" DESTINATION "${CMAKE_BINARY_DIR}/") +file(COPY "test/consistency" DESTINATION "${CMAKE_BINARY_DIR}/") add_test(NAME UnitTests COMMAND valgrind ./TestFSE) -add_test(NAME TestConsistency COMMAND ./checkConsistency) +add_test(NAME TestConsistency COMMAND ./consistency/check) diff --git a/inc/Directory.hpp b/inc/Directory.hpp index fd3275a..54f30bc 100644 --- a/inc/Directory.hpp +++ b/inc/Directory.hpp @@ -10,16 +10,16 @@ public: Directory(const Directory& node); ~Directory() override; - NodeType getType() const override { return DIRECTORY; } + [[nodiscard]] NodeType getType() const override { return DIRECTORY; } [[nodiscard]] std::shared_ptr clone() const override; bool detachNode(const Key& key) override; - bool attachNode(const Key &newKey, std::shared_ptr newNode) override; - std::shared_ptr findNode(const std::vector& path, ui32 currentDepth = 0) override; + bool attachNode(const Key &newKey, const std::shared_ptr& newNode) override; + std::shared_ptr findNode(const std::vector& path, ui32 currentDepth) override; std::shared_ptr findNode(const Key& path) override; [[nodiscard]] ui64 size() const override; void clearFlags(std::shared_ptr& directory) override; - bool isHardNode() const override; + [[nodiscard]] bool isHardNode() const override; void removeIncomingDynamicLinks() override; void removeOutgoingLinks() override; @@ -34,7 +34,7 @@ public: } private: - void getMaxDepthUtil(ui32 depth, ui32& maxDepth) const; + void getMaxDepthUtil(ui32 depth, ui32& maxDepth) const override; void dumpUtil(std::ostream& ss, const Key& key, ui32 currentDepth, std::vector& indents) override; public: diff --git a/inc/FileSystem.hpp b/inc/FileSystem.hpp index 49b19f0..590856c 100644 --- a/inc/FileSystem.hpp +++ b/inc/FileSystem.hpp @@ -5,12 +5,10 @@ #include -// use single vector for incoming links -// move link nodes from link to node class or vise versa -// clean-ups -// links have unique names // copy operator exit if exists, do not rename -// in subtree links should be in-tree after copy operation +// links have unique names +// use single vector for incoming links +// clean-ups // update link if exists class FileSystem { @@ -29,7 +27,7 @@ public: std::ostream& dump(std::ostream& stream) const; void log() const; - ui64 size() const; + [[nodiscard]] ui64 size() const; static const std::string& getLastError(); diff --git a/inc/Link.hpp b/inc/Link.hpp index 4e2167d..89334a2 100644 --- a/inc/Link.hpp +++ b/inc/Link.hpp @@ -8,14 +8,13 @@ public: Link(const Link& node); ~Link() override; - NodeType getType() const override { return LINK; } + [[nodiscard]] NodeType getType() const override { return LINK; } [[nodiscard]] std::shared_ptr clone() const override; [[nodiscard]] std::shared_ptr getLink() const; [[nodiscard]] bool isHardLink() const; std::shared_ptr getTarget() override; - // link on link is not allowed, so no inf looping here std::shared_ptr findNode(const std::vector& path, ui32 currentDepth) override; void dumpUtil(std::ostream& ss, const Key& key, ui32 currentDepth, std::vector& indents) override; diff --git a/inc/Node.hpp b/inc/Node.hpp index 713ecda..bbac105 100644 --- a/inc/Node.hpp +++ b/inc/Node.hpp @@ -1,10 +1,5 @@ #pragma once -typedef unsigned long long ui64; -typedef unsigned long ui32; -typedef long long i64; -typedef long i32; - #include #include #include @@ -12,6 +7,9 @@ typedef long i32; #include #include +typedef unsigned long long ui64; +typedef unsigned long ui32; + class Link; class Directory; @@ -26,32 +24,44 @@ public: Node(const Node& node); virtual ~Node(); - virtual NodeType getType() const { return FILE; } - virtual std::shared_ptr clone() const; - virtual bool detachNode(const Key& key) { return false; } - virtual bool attachNode(const Key &newKey, std::shared_ptr newNode) { return false; } - virtual void getMaxDepthUtil(ui32 depth, ui32& maxDepth) const {} - virtual void dumpUtil(std::ostream& stream, const Key& key, ui32 currentDepth, std::vector& indents); - virtual ui64 size() const { return 0; } - virtual std::shared_ptr getTarget(); - virtual std::shared_ptr findNode(const std::vector& path, ui32 currentDepth = 0); - virtual std::shared_ptr findNode(const Key& path) { return nullptr; } + // modification + virtual bool detachNode(const Key& key); + virtual bool attachNode(const Key &newKey, const std::shared_ptr& newNode); + virtual std::shared_ptr findNode(const std::vector& path, ui32 currentDepth); + virtual std::shared_ptr findNode(const Key& path); + [[nodiscard]] virtual std::shared_ptr clone() const; - virtual void clearFlags(std::shared_ptr& directory); - virtual bool isHardNode() const; - virtual void removeIncomingDynamicLinks(); - virtual void removeOutgoingLinks() {} - - ui32 getMaxDepth() const; + // logging std::ostream& dump(std::ostream &stream); - void getNodeStraightPath(const std::shared_ptr& node, std::vector>& path) const; - bool empty() const { return !size(); } + virtual void dumpUtil(std::ostream& stream, const Key& key, ui32 currentDepth, std::vector& indents); static void indent(std::ostream& ss, ui32 depth, std::vector& indents); + // link support + virtual void clearFlags(std::shared_ptr& directory); + virtual void removeIncomingDynamicLinks(); + virtual void removeOutgoingLinks(); + virtual std::shared_ptr getTarget(); + [[nodiscard]] virtual bool isHardNode() const; + + // general queries + [[nodiscard]] virtual NodeType getType() const; + [[nodiscard]] virtual ui64 size() const; + [[nodiscard]] ui32 getMaxDepth() const; + virtual void getMaxDepthUtil(ui32 depth, ui32& maxDepth) const; + void getNodeStraightPath(const std::shared_ptr& node, std::vector>& path) const; + [[nodiscard]] bool empty() const; + public: + // TODO : remove key duplication Key mKey; + + // to mark currently working subtree recursively. std::weak_ptr mWorkingNodeFlag; + + // to trace path from current node to the root std::weak_ptr mParent; + + // to remove incoming links std::vector> mIncomingHardLinks; std::vector> mIncomingDynamicLinks; }; \ No newline at end of file diff --git a/inc/Path.hpp b/inc/Path.hpp index 9f1a5e8..d0fdc8b 100644 --- a/inc/Path.hpp +++ b/inc/Path.hpp @@ -3,25 +3,26 @@ #include #include +typedef unsigned long long ui64; + extern std::vector transitions; void initializeTransitions(); class Path { public: Path() = default; - Path(const std::string& path); + explicit Path(const std::string& path); void set(const std::string& path); const std::string& operator[](int idx) const; - bool isValid() const; - bool isInvalid() const; - bool isAbsolute() const; - int getDepth() const; + [[nodiscard]] bool isInvalid() const; + [[nodiscard]] bool isAbsolute() const; + [[nodiscard]] ui64 getDepth() const; - const std::vector& getChain() const; - std::vector getParentChain() const; - const std::string& getFilename() const; + [[nodiscard]] const std::vector& getChain() const; + [[nodiscard]] std::vector getParentChain() const; + [[nodiscard]] const std::string& getFilename() const; private: std::vector mChain; diff --git a/src/Directory.cpp b/src/Directory.cpp index 217a09c..005a6ed 100644 --- a/src/Directory.cpp +++ b/src/Directory.cpp @@ -2,16 +2,13 @@ #include "Directory.hpp" #include "Link.hpp" -#include #include #include Directory::Directory() = default; +Directory::~Directory() = default; -Directory::~Directory() { -} - -bool Directory::attachNode(const Key &newKey, std::shared_ptr newNode) { +bool Directory::attachNode(const Key &newKey, const std::shared_ptr& newNode) { assert(mMembers.find(newKey) == mMembers.end()); mMembers.insert({ newKey, newNode }); newNode->mKey = newKey; diff --git a/src/FileSystem.cpp b/src/FileSystem.cpp index 69efc96..dfe38c4 100644 --- a/src/FileSystem.cpp +++ b/src/FileSystem.cpp @@ -22,7 +22,7 @@ FileSystem::~FileSystem() = default; std::shared_ptr FileSystem::getNode(const Path& path, bool parent) { auto pathChain = parent ? path.getParentChain() : path.getChain(); auto currentNode = path.isAbsolute() ? root : currentDirectory; - auto parentNode = pathChain.empty() ? currentNode : currentNode->findNode(pathChain); + auto parentNode = pathChain.empty() ? currentNode : currentNode->findNode(pathChain, 0); if (parent && parentNode) { while (parentNode->getTarget()) { @@ -299,7 +299,7 @@ bool FileSystem::copyNode(const Path& source, const Path& target) { if (!sourceNode) { gError = "Invalid source path"; return false; - }; + } if (parentNodeTarget->getType() != Node::DIRECTORY) { gError = "Target path is not a directory"; diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index da02a0a..4465f45 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -15,7 +15,7 @@ Interpreter::Interpreter() { "change working directory", 1, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.changeCurrent(args[1]); + return filesystem.changeCurrent(Path(args[1])); } }; @@ -23,7 +23,7 @@ Interpreter::Interpreter() { "create directory", 1, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.makeDirectory(args[1]); + return filesystem.makeDirectory(Path(args[1])); } }; @@ -31,7 +31,7 @@ Interpreter::Interpreter() { "remove directory", 1, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.removeDirectory(args[1], false); + return filesystem.removeDirectory(Path(args[1]), false); } }; @@ -39,7 +39,7 @@ Interpreter::Interpreter() { "remove directory recursively", 1, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.removeDirectory(args[1], true); + return filesystem.removeDirectory(Path(args[1]), true); } }; @@ -47,7 +47,7 @@ Interpreter::Interpreter() { "make file", 1, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.makeFile(args[1]); + return filesystem.makeFile(Path(args[1])); } }; @@ -55,7 +55,7 @@ Interpreter::Interpreter() { "remove file or link", 1, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.removeFileOrLink(args[1]); + return filesystem.removeFileOrLink(Path(args[1])); } }; @@ -63,7 +63,7 @@ Interpreter::Interpreter() { "recursive copy of a node", 2, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.copyNode(args[1], args[2]); + return filesystem.copyNode(Path(args[1]), Path(args[2])); } }; @@ -71,7 +71,7 @@ Interpreter::Interpreter() { "move node", 2, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.moveNode(args[1], args[2]); + return filesystem.moveNode(Path(args[1]), Path(args[2])); } }; @@ -79,7 +79,7 @@ Interpreter::Interpreter() { "make hard link", 2, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.makeLink(args[1], args[2], false); + return filesystem.makeLink(Path(args[1]), Path(args[2]), false); } }; @@ -87,7 +87,7 @@ Interpreter::Interpreter() { "make dynamic link", 2, [](FileSystem& filesystem, const std::vector& args){ - return filesystem.makeLink(args[1], args[2], true); + return filesystem.makeLink(Path(args[1]), Path(args[2]), true); } }; } diff --git a/src/Link.cpp b/src/Link.cpp index 6c92f39..00f4689 100644 --- a/src/Link.cpp +++ b/src/Link.cpp @@ -1,7 +1,6 @@ #include "Link.hpp" -#include #include #include #include @@ -63,6 +62,7 @@ std::shared_ptr Link::getTarget() { std::shared_ptr Link::findNode(const std::vector& path, ui32 currentDepth) { assert(path.size() != currentDepth); + // link on link is not allowed, so no inf looping here return getLink()->findNode(path, currentDepth); } diff --git a/src/Node.cpp b/src/Node.cpp index 0f4366a..a9832e1 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -3,7 +3,6 @@ #include "Link.hpp" #include -#include #include #include #include @@ -25,7 +24,6 @@ ui32 Node::getMaxDepth() const { } std::shared_ptr Node::findNode(const std::vector& path, ui32 currentDepth) { - // assert(false); return nullptr; } @@ -94,4 +92,32 @@ void Node::indent(std::ostream& ss, ui32 depth, std::vector& indents) { ss << (indents[i] ? " |" : " "); } ss << " |_"; -} \ No newline at end of file +} + +void Node::removeOutgoingLinks() {} + +bool Node::detachNode(const Key &key) { + return false; +} + +bool Node::attachNode(const Key &newKey, const std::shared_ptr &newNode) { + return false; +} + +std::shared_ptr Node::findNode(const Key &path) { + return nullptr; +} + +bool Node::empty() const { + return !size(); +} + +void Node::getMaxDepthUtil(ui32 depth, ui32 &maxDepth) const {} + +Node::NodeType Node::getType() const { + return Node::FILE; +} + +ui64 Node::size() const { + return 0; +} diff --git a/src/Path.cpp b/src/Path.cpp index bad0ab5..201640a 100644 --- a/src/Path.cpp +++ b/src/Path.cpp @@ -20,10 +20,6 @@ Path::Path(const std::string& path) { set(path); } -bool Path::isValid() const { - return mIsValid; -} - bool Path::isInvalid() const { return !mIsValid; } @@ -32,7 +28,7 @@ bool Path::isAbsolute() const { return mAbsolute; } -int Path::getDepth() const { +ui64 Path::getDepth() const { return mChain.size(); } @@ -62,25 +58,24 @@ void Path::set(const std::string& path) { std::string lowercasePath = path; const auto drivePrefixSize = std::strlen(drivePrefix); - if (drivePrefixSize) { - if ((lowercasePath.size() >= drivePrefixSize) && (std::memcmp(lowercasePath.c_str(), drivePrefix, drivePrefixSize) == 0)) { - lowercasePath.erase(0, drivePrefixSize); - mAbsolute = true; - if (lowercasePath.empty()) lowercasePath = "/"; - else if (lowercasePath[0] != '/') { - mIsValid = false; - return; - } - } else { - if (lowercasePath[0] == '/') { - mIsValid = false; - return; - } + assert(drivePrefixSize); + + if ((lowercasePath.size() >= drivePrefixSize) && (std::memcmp(lowercasePath.c_str(), drivePrefix, drivePrefixSize) == 0)) { + lowercasePath.erase(0, drivePrefixSize); + mAbsolute = true; + if (lowercasePath.empty()) lowercasePath = "/"; + else if (lowercasePath[0] != '/') { + mIsValid = false; + return; } } else { - mAbsolute = path.front() == '/'; + if (lowercasePath[0] == '/') { + mIsValid = false; + return; + } } + mDirectory = path.back() == '/'; mChain.clear(); diff --git a/test/TestConsistency.cpp b/test/TestConsistency.cpp index fc6f2ae..0ba89c3 100644 --- a/test/TestConsistency.cpp +++ b/test/TestConsistency.cpp @@ -5,7 +5,7 @@ // generates and saves final state to check if there are any logic changes to the code -int main(int argc, char* argv[]) { +int main(int, char*[]) { Interpreter interpreter; interpreter.logType = Interpreter::NONE;