This commit is contained in:
IlyaShurupov 2024-03-28 00:05:07 +03:00
parent fae5a39053
commit 75722822f6
5 changed files with 59 additions and 20 deletions

View file

@ -69,8 +69,8 @@ public:
void dump(std::stringstream& ss);
bool attachNode(const std::vector<Key>& directoryPath, const Key& newKey, Node* newNode);
bool detachNode(const std::vector<Key>& directoryPath, const Key& key);
Node* findNode(const std::vector<Key>& path, ui32 currentDepth = 0);
void detachNode(Node* node);
[[nodiscard]] ui32 getMaxDepth() const;

View file

@ -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<bool>& indents) const;
void indent(std::stringstream & ss, int depth, std::vector<bool>& indents) const;
void logDirectory(std::stringstream & ss, const Directory* node, int depth, std::vector<bool>& indents) const;
void logFile(std::stringstream& ss, const File* node, int depth, std::vector<bool>& indents) const;
void logLink(std::stringstream & ss, const Link* node, int depth, std::vector<bool>& indents) const;
private:
Directory* root = nullptr;
Directory* currentDirectory = nullptr;

View file

@ -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<Key>& 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<Key>& directoryPath, const Key& new
}
directory->mMembers.insert(DirectoryKey(newKey), newNode);
updateTreeLinkCount(newNode);
updateTreeLinkCount(directory);
return true;
}
bool Directory::detachNode(const std::vector<Key>& 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);

View file

@ -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";

View file

@ -25,6 +25,14 @@ Interpreter::Interpreter() {
return filesystem.makeDirectory(args[1]);
}
};
mCommands["rd"] = {
"remove directory",
1,
[](FileSystem& filesystem, const std::vector<std::string>& args){
return filesystem.removeDirectory(args[1]);
}
};
}
void Interpreter::reportError(const std::string& description) {