2022-11-06 12:30:06 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: FSRecursion.cpp
|
|
|
|
Date: 2022-11-06
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "FS.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::IO::FS
|
|
|
|
{
|
|
|
|
struct RecursiveDirIterator : IReadDir
|
|
|
|
{
|
|
|
|
AuSPtr<IReadDir> pDir;
|
2024-05-19 16:03:59 +00:00
|
|
|
AuList<AuPair<AuUInt, AuString>> nextLevel;
|
2022-11-06 12:30:06 +00:00
|
|
|
AuString curPath;
|
|
|
|
AuString curSubDir;
|
2024-05-19 16:03:59 +00:00
|
|
|
AuUInt uCurrentDepth {};
|
2024-03-19 18:08:54 +00:00
|
|
|
bool bSymlinkTraversal { true };
|
|
|
|
AuUInt32 uErrorCount {};
|
|
|
|
AuList<AuString> failedPaths;
|
2024-05-19 16:03:59 +00:00
|
|
|
AuOptional<AuUInt> optMaxRecursion {};
|
2024-03-19 18:08:54 +00:00
|
|
|
|
|
|
|
virtual AuUInt32 GetErrorCount() override
|
|
|
|
{
|
|
|
|
return this->uErrorCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual AuList<AuString> GetErrorPaths() override
|
|
|
|
{
|
|
|
|
return this->failedPaths;
|
|
|
|
}
|
2022-11-06 12:30:06 +00:00
|
|
|
|
2024-04-19 03:33:57 +00:00
|
|
|
bool OpenDir(const AuROString &str)
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
2024-05-19 16:03:59 +00:00
|
|
|
this->curPath = str;
|
|
|
|
this->pDir = ReadDir(str);
|
2022-11-06 12:30:06 +00:00
|
|
|
return bool(pDir);
|
|
|
|
}
|
|
|
|
|
2024-04-19 03:33:57 +00:00
|
|
|
bool OpenNext(const AuROString &str)
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
2024-05-19 16:03:59 +00:00
|
|
|
this->curPath = str;
|
|
|
|
this->pDir = ReadDir(str);
|
2022-11-06 12:30:06 +00:00
|
|
|
return bool(pDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoNext()
|
|
|
|
{
|
2024-03-19 18:08:54 +00:00
|
|
|
if (this->pDir)
|
|
|
|
{
|
|
|
|
if (auto uCount = this->pDir->GetErrorCount())
|
|
|
|
{
|
|
|
|
this->failedPaths.push_back(this->curPath + AuString(1, AuFS::kPathSplitter) + this->curSubDir);
|
|
|
|
this->uErrorCount += uCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 12:30:06 +00:00
|
|
|
this->pDir.reset();
|
|
|
|
|
2024-05-19 16:03:59 +00:00
|
|
|
if (!this->nextLevel.size())
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-19 16:03:59 +00:00
|
|
|
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);
|
2022-11-06 12:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual StatEx *Next() override
|
|
|
|
{
|
2024-03-19 18:08:54 +00:00
|
|
|
AU_DEBUG_MEMCRUNCH;
|
|
|
|
|
2023-12-22 00:17:19 +00:00
|
|
|
StatEx *pNext {};
|
2022-11-06 12:30:06 +00:00
|
|
|
if (!this->pDir)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-03-19 18:08:54 +00:00
|
|
|
do
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
2024-03-19 18:08:54 +00:00
|
|
|
try
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
2024-03-19 18:08:54 +00:00
|
|
|
pNext = this->pDir->Next();
|
|
|
|
|
|
|
|
while (!pNext)
|
|
|
|
{
|
|
|
|
DoNext();
|
|
|
|
if (!this->pDir)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
pNext = this->pDir->Next();
|
|
|
|
}
|
|
|
|
|
2024-05-19 16:03:59 +00:00
|
|
|
if (this->curSubDir.size())
|
2023-12-22 00:17:19 +00:00
|
|
|
{
|
2024-05-19 16:03:59 +00:00
|
|
|
pNext->fileName.insert(pNext->fileName.begin(), this->curSubDir.begin(), this->curSubDir.end());
|
2023-12-22 00:17:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 18:08:54 +00:00
|
|
|
if (!this->bSymlinkTraversal && pNext->bSymLink)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-06 12:30:06 +00:00
|
|
|
|
2024-03-19 18:08:54 +00:00
|
|
|
if (pNext->bExistsDirectory)
|
|
|
|
{
|
2024-05-19 16:03:59 +00:00
|
|
|
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 + "/"));
|
|
|
|
}
|
2024-03-19 18:08:54 +00:00
|
|
|
}
|
2023-12-22 00:17:19 +00:00
|
|
|
}
|
2024-03-19 18:08:54 +00:00
|
|
|
catch (...)
|
2023-12-22 00:17:19 +00:00
|
|
|
{
|
2024-03-19 18:08:54 +00:00
|
|
|
SysPushErrorCatch();
|
2023-12-22 00:17:19 +00:00
|
|
|
}
|
2022-11-06 12:30:06 +00:00
|
|
|
}
|
2024-03-19 18:08:54 +00:00
|
|
|
while (false);
|
|
|
|
|
2022-11-06 12:30:06 +00:00
|
|
|
return pNext;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-19 16:03:59 +00:00
|
|
|
AUKN_SYM AuSPtr<IReadDir> ReadDirRecursive(const AuROString &string, AuOptional<bool> bTraverseSymlinks, AuOptional<AuUInt> optMaxRecursion)
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
2024-05-19 16:03:59 +00:00
|
|
|
SysCheckArgNotNull(string.size(), {});
|
|
|
|
|
2022-11-06 12:30:06 +00:00
|
|
|
auto pObj = AuMakeShared<RecursiveDirIterator>();
|
2024-05-19 16:03:59 +00:00
|
|
|
SysCheckNotNullMemory(pObj, {});
|
2022-11-06 12:30:06 +00:00
|
|
|
|
2024-03-19 18:08:54 +00:00
|
|
|
pObj->bSymlinkTraversal = bTraverseSymlinks.ValueOr(true);
|
2024-05-19 16:03:59 +00:00
|
|
|
pObj->optMaxRecursion = optMaxRecursion;
|
2024-03-19 18:08:54 +00:00
|
|
|
|
2023-12-22 00:17:19 +00:00
|
|
|
try
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
2023-12-22 00:17:19 +00:00
|
|
|
if (!pObj->OpenDir(string))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
SysPushErrorCatch();
|
2022-11-06 12:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pObj;
|
|
|
|
}
|
|
|
|
}
|