check pwd when removing

This commit is contained in:
IlyaShurupov 2024-03-28 11:16:14 +03:00
parent 83abdea417
commit f462630d9c
3 changed files with 30 additions and 1 deletions

View file

@ -32,6 +32,9 @@ public:
const std::string& getLastError();
private:
bool isPathContainsCurrent(Node* node);
private:
Directory* root = nullptr;
Directory* currentDirectory = nullptr;

View file

@ -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<const Key*> currentPath;
std::vector<const Key*> 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;
}

View file

@ -6,7 +6,7 @@ static double randomFloat() {
return static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX);
}
const auto size = 10000;
const auto size = 1000;
class TestClass {
ui64 val1 = 0;