[+] Added AuOptional<AuUInt> optMaxRecursion to AuFS::ReadDirRecursive

This commit is contained in:
Reece Wilson 2024-05-19 17:03:59 +01:00
parent 9b26eea886
commit ba36ea4ef1
2 changed files with 30 additions and 20 deletions

View File

@ -37,7 +37,7 @@ namespace Aurora::IO::FS
* @param string
* @return
*/
AUKN_SYM AuSPtr<IReadDir> ReadDirRecursive(const AuROString &string, AuOptional<bool> bTraverseSymlinks = { true });
AUKN_SYM AuSPtr<IReadDir> ReadDirRecursive(const AuROString &string, AuOptional<bool> bTraverseSymlinks = { true }, AuOptional<AuUInt> optMaxRecursion = { 256 });
/**
* @brief Recursively deletes any given path

View File

@ -13,12 +13,14 @@ namespace Aurora::IO::FS
struct RecursiveDirIterator : IReadDir
{
AuSPtr<IReadDir> pDir;
AuList<AuString> nextLevel;
AuList<AuPair<AuUInt, AuString>> nextLevel;
AuString curPath;
AuString curSubDir;
AuUInt uCurrentDepth {};
bool bSymlinkTraversal { true };
AuUInt32 uErrorCount {};
AuList<AuString> failedPaths;
AuOptional<AuUInt> optMaxRecursion {};
virtual AuUInt32 GetErrorCount() override
{
@ -32,15 +34,15 @@ namespace Aurora::IO::FS
bool OpenDir(const AuROString &str)
{
curPath = str;
pDir = ReadDir(str);
this->curPath = str;
this->pDir = ReadDir(str);
return bool(pDir);
}
bool OpenNext(const AuROString &str)
{
curPath = str;
pDir = ReadDir(str);
this->curPath = str;
this->pDir = ReadDir(str);
return bool(pDir);
}
@ -57,15 +59,16 @@ namespace Aurora::IO::FS
this->pDir.reset();
if (!nextLevel.size())
if (!this->nextLevel.size())
{
return;
}
auto a = nextLevel[0];
nextLevel.erase(nextLevel.begin());
pDir = ReadDir(curPath + "/" + a);
curSubDir = a;
auto next = AuMove(this->nextLevel[0]);
this->nextLevel.erase(this->nextLevel.begin());
this->pDir = ReadDir(this->curPath + "/" + AuGet<1>(next));
this->curSubDir = AuGet<1>(next);
this->uCurrentDepth = AuGet<0>(next);
}
virtual StatEx *Next() override
@ -95,9 +98,9 @@ namespace Aurora::IO::FS
pNext = this->pDir->Next();
}
if (curSubDir.size())
if (this->curSubDir.size())
{
pNext->fileName.insert(pNext->fileName.begin(), curSubDir.begin(), curSubDir.end());
pNext->fileName.insert(pNext->fileName.begin(), this->curSubDir.begin(), this->curSubDir.end());
}
if (!this->bSymlinkTraversal && pNext->bSymLink)
@ -107,7 +110,15 @@ namespace Aurora::IO::FS
if (pNext->bExistsDirectory)
{
nextLevel.push_back(pNext->fileName + "/");
if (this->optMaxRecursion &&
this->optMaxRecursion.Value() <= this->uCurrentDepth + 1)
{
this->failedPaths.push_back(pNext->fileName);
}
else
{
this->nextLevel.push_back(AuMakePair(this->uCurrentDepth + 1, pNext->fileName + "/"));
}
}
}
catch (...)
@ -121,16 +132,15 @@ namespace Aurora::IO::FS
}
};
AUKN_SYM AuSPtr<IReadDir> ReadDirRecursive(const AuROString &string, AuOptional<bool> bTraverseSymlinks)
AUKN_SYM AuSPtr<IReadDir> ReadDirRecursive(const AuROString &string, AuOptional<bool> bTraverseSymlinks, AuOptional<AuUInt> optMaxRecursion)
{
SysCheckArgNotNull(string.size(), {});
auto pObj = AuMakeShared<RecursiveDirIterator>();
if (!pObj)
{
SysPushErrorMem();
return {};
}
SysCheckNotNullMemory(pObj, {});
pObj->bSymlinkTraversal = bTraverseSymlinks.ValueOr(true);
pObj->optMaxRecursion = optMaxRecursion;
try
{