Move command
This commit is contained in:
parent
446aa5adbc
commit
3138b0aad3
5 changed files with 59 additions and 7 deletions
|
|
@ -89,7 +89,9 @@ public:
|
|||
bool attachNode(const std::vector<Key>& directoryPath, const Key& newKey, Node* newNode);
|
||||
bool detachNode(const std::vector<Key>& directoryPath, const Key& key);
|
||||
|
||||
bool detachNode(const Key& key);
|
||||
bool attachNode(const Key &newKey, Node *newNode);
|
||||
|
||||
Node* findNode(const std::vector<Key>& path, ui32 currentDepth = 0);
|
||||
Node* findNode(const Key& path);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,14 +6,12 @@
|
|||
#include <sstream>
|
||||
|
||||
// Functionality:
|
||||
// - add node to path conversion
|
||||
// - check for current node deletion
|
||||
// - print current node
|
||||
// - print links and link counts
|
||||
// - update link counts
|
||||
// - add link creation commands
|
||||
|
||||
// Refactor
|
||||
// - remove code duplication
|
||||
// - Move DirectoryKey inside Node (no need to store pointers from Node to DirectoryTree::Node)
|
||||
// - Merge DirectoryTree::Node and Node (no pointer overhead, requires tree nodes to be consistent, check insertNodeInstead)
|
||||
// - introduce smart pointers
|
||||
|
|
@ -30,6 +28,7 @@ public:
|
|||
bool removeDirectory(const Path& path, bool recursively);
|
||||
bool removeFileOrLink(const Path& path);
|
||||
bool copyNode(const Path& source, const Path& to);
|
||||
bool moveNode(const Path& source, const Path& to);
|
||||
|
||||
void log() const;
|
||||
|
||||
|
|
|
|||
|
|
@ -86,18 +86,20 @@ bool Directory::detachNode(const std::vector<Key>& directoryPath, const Key& key
|
|||
return false;
|
||||
}
|
||||
|
||||
auto directory = ((Directory*) node);
|
||||
return ((Directory*) node)->detachNode(key);
|
||||
}
|
||||
|
||||
DirectoryTree::Node* removeNode = directory->mMembers.find(DirectoryKey(key));
|
||||
bool Directory::detachNode(const Key& key) {
|
||||
DirectoryTree::Node* removeNode = mMembers.find(DirectoryKey(key));
|
||||
if (!removeNode) {
|
||||
gError = "Invalid path";
|
||||
return false;
|
||||
}
|
||||
|
||||
removeNode->mParent = nullptr;
|
||||
directory->mMembers.remove(DirectoryKey(key));
|
||||
mMembers.remove(DirectoryKey(key));
|
||||
|
||||
updateTreeLinkCount(directory);
|
||||
updateTreeLinkCount(this);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -104,6 +104,7 @@ bool FileSystem::copyNode(const Path& source, const Path& target) {
|
|||
return false;
|
||||
}
|
||||
|
||||
// TODO : remove code duplication
|
||||
Directory* workingDirectorySource = source.isAbsolute() ? root : currentDirectory;
|
||||
Directory* workingDirectoryTarget = target.isAbsolute() ? root : currentDirectory;
|
||||
|
||||
|
|
@ -135,6 +136,46 @@ bool FileSystem::copyNode(const Path& source, const Path& target) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool FileSystem::moveNode(const Path &source, const Path &target) {
|
||||
if (source.getDepth() < 1 || source.isInvalid()) {
|
||||
gError = "Invalid source path";
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO : remove code duplication
|
||||
Directory* workingDirectorySource = source.isAbsolute() ? root : currentDirectory;
|
||||
Directory* workingDirectoryTarget = target.isAbsolute() ? root : currentDirectory;
|
||||
|
||||
auto sourceParentNode = workingDirectorySource->findNode(source.getParentChain());
|
||||
assert(sourceParentNode->mType == Node::DIRECTORY);
|
||||
auto sourceNode = workingDirectorySource->findNode(source.getChain());
|
||||
if (!sourceNode) {
|
||||
gError = "Invalid source path";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto targetNode = workingDirectoryTarget->findNode(target.getChain());
|
||||
if (!targetNode || targetNode->mType != Node::DIRECTORY) {
|
||||
gError = "Invalid target directory";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto sourceParentDirectory = (Directory*)sourceParentNode;
|
||||
auto targetDirectory = (Directory*)targetNode;
|
||||
|
||||
Key key = sourceNode->mTreeNode->key.val;
|
||||
|
||||
if (targetDirectory->findNode(key)) {
|
||||
gError = "Node with such name already exists in the target directory";
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(sourceParentDirectory->detachNode(key));
|
||||
assert(targetDirectory->attachNode(key, sourceNode));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileSystem::removeFileOrLink(const Path &path) {
|
||||
if (path.getDepth() < 1 || path.isInvalid()) {
|
||||
gError = "Invalid path";
|
||||
|
|
|
|||
|
|
@ -65,6 +65,14 @@ Interpreter::Interpreter() {
|
|||
return filesystem.copyNode(args[1], args[2]);
|
||||
}
|
||||
};
|
||||
|
||||
mCommands["move"] = {
|
||||
"move node",
|
||||
2,
|
||||
[](FileSystem& filesystem, const std::vector<std::string>& args){
|
||||
return filesystem.moveNode(args[1], args[2]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void Interpreter::reportError(const std::string& description) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue