AuroraRuntime/Source/IO/FS/DirDeleter.cpp

121 lines
2.6 KiB
C++
Raw Normal View History

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: DirDeleter.cpp
Date: 2022-11-06
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
namespace Aurora::IO::FS
{
struct RecursiveDirDeleter : IReadDir
{
AuSPtr<IReadDir> pDir;
AuList<AuString> nextLevel;
AuList<AuString> nextLevel2;
AuString curPath;
AuString curSubDir;
bool OpenDir(const AuString &str)
{
curPath = str;
pDir = ReadDir(str);
return bool(pDir);
}
bool OpenNext(const AuString &str)
{
curPath = str;
pDir = ReadDir(str);
return bool(pDir);
}
void DoNext()
{
this->pDir.reset();
if (!nextLevel.size())
{
return;
}
auto a = nextLevel[0];
nextLevel.erase(nextLevel.begin());
this->pDir = ReadDir(curPath + "/" + a);
curSubDir = a;
}
virtual StatEx *Next() override
{
if (!this->pDir)
{
return {};
}
auto pNext = this->pDir->Next();
while (!pNext)
{
DoNext();
if (!this->pDir)
{
return {};
}
pNext = this->pDir->Next();
}
if (curSubDir.size())
{
pNext->fileName.insert(pNext->fileName.begin(), curSubDir.begin(), curSubDir.end());
}
if (pNext->bExistsDirectory)
{
nextLevel.push_back(pNext->fileName + "/");
nextLevel2.push_back(pNext->fileName + "/");
}
else
{
AuFS::Remove(pNext->path);
}
return pNext;
}
void RemoveDirs()
{
for (auto itr = nextLevel2.rbegin(); itr != nextLevel2.rend(); itr++)
{
AuFS::Remove(curPath + "/" + itr->c_str());
}
}
};
AUKN_SYM bool DirDeleter(const AuString &string)
{
auto pObj = AuMakeShared<RecursiveDirDeleter>();
if (!pObj)
{
SysPushErrorMem();
return {};
}
if (!pObj->OpenDir(string))
{
return {};
}
while (pObj->Next())
{
}
pObj->RemoveDirs();
AuFS::Remove(string);
return !AuFS::DirExists(string);
}
}