check pwd when removing
This commit is contained in:
parent
83abdea417
commit
f462630d9c
3 changed files with 30 additions and 1 deletions
|
|
@ -32,6 +32,9 @@ public:
|
|||
|
||||
const std::string& getLastError();
|
||||
|
||||
private:
|
||||
bool isPathContainsCurrent(Node* node);
|
||||
|
||||
private:
|
||||
Directory* root = nullptr;
|
||||
Directory* currentDirectory = nullptr;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue