AuroraRuntime/Source/IO/FS/DirDeleter.cpp
Jamie Reece Wilson 83f34b0c47 [*] I was right. String views are [mostly] pointless (*)
03:28:55:638  17>2 of 53388 functions (<0.1%) were compiled, the rest were copied from previous compilation.
03:28:55:638  17>  0 functions were new in current compilation
03:28:55:638  17>  65 functions had inline decision re-evaluated but remain unchanged
03:28:56:749  17>Finished generating code

the header of const AuString & is the same as std::string_view therefore nothing changes. in fact, we still need to alloc strings a bunch of times for a zero terminated string. worse, <c++20 always allocs each time we want to access a hashmap with o(1) lookup, making small hashmaps kinda pointless when we always have to alloc+copy (thx std)

perhaps this will help some language binders
2024-04-19 05:58:08 +01:00

227 lines
5.7 KiB
C++

/***
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;
AuList<AuString> failedPaths;
AuUInt32 uErrorCount {};
virtual AuUInt32 GetErrorCount() override
{
return this->uErrorCount;
}
virtual AuList<AuString> GetErrorPaths() override
{
if (this->uErrorCount || this->failedPaths.size())
{
return this->failedPaths;
}
else
{
return {};
}
}
bool DoThing(const AuROString &string)
{
AU_DEBUG_MEMCRUNCH;
if (!this->OpenDir(string))
{
return {};
}
while (this->Next())
{
}
this->RemoveDirs();
AuFS::Remove(string);
return true;
}
bool OpenDir(const AuROString &str)
{
this->curPath = str;
this->pDir = ReadDir(str);
return bool(pDir);
}
bool OpenNext(const AuString &str)
{
this->curPath = str;
this->pDir = ReadDir(str);
return bool(pDir);
}
void DoNext()
{
if (this->pDir)
{
if (auto uCount = this->pDir->GetErrorCount())
{
this->failedPaths.push_back(this->curPath + AuString(1, AuFS::kPathSplitter) + this->curSubDir);
this->uErrorCount += uCount;
}
}
this->pDir.reset();
if (!this->nextLevel.size())
{
return;
}
auto a = this->nextLevel[0];
this->nextLevel.erase(this->nextLevel.begin());
this->pDir = ReadDir(this->curPath + "/" + a);
this->curSubDir = a;
}
virtual StatEx *Next() override
{
StatEx *pNext {};
if (!this->pDir)
{
return {};
}
try
{
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 && !pNext->bSymLink)
{
nextLevel.push_back(pNext->fileName + "/");
nextLevel2.push_back(pNext->fileName + "/");
}
else
{
if (!AuFS::Remove(pNext->path))
{
this->failedPaths.push_back(pNext->fileName);
this->uErrorCount++;
}
}
}
catch (...)
{
SysPushErrorCatch();
}
return pNext;
}
void RemoveDirs()
{
for (auto itr = this->nextLevel2.rbegin(); itr != this->nextLevel2.rend(); itr++)
{
auto dir = this->curPath + "/" + itr->c_str();
if (!AuFS::Remove(dir))
{
this->failedPaths.push_back(itr->c_str());
this->uErrorCount++;
}
}
}
};
AUKN_SYM bool DirDeleter(const AuROString &string)
{
auto pObj = AuMakeShared<RecursiveDirDeleter>();
SysCheckNotNullMemory(pObj, false);
try
{
if (!pObj->DoThing(string))
{
return {};
}
}
catch (...)
{
SysPushErrorCatch();
}
return !AuFS::DirExists(string);
}
AUKN_SYM bool DirDeleterEx(const AuROString &string, AuList<AuString> &failingPaths)
{
auto pObj = AuMakeShared<RecursiveDirDeleter>();
SysCheckNotNullMemory(pObj, false);
try
{
if (!pObj->DoThing(string))
{
return {};
}
if (AuFS::DirExists(string))
{
for (const auto &str : pObj->failedPaths)
{
auto normalizedUserDir = AuString(string) + "/";
auto normalizedUserDir2 = normalizedUserDir + str;
if (AuFS::FileExists(normalizedUserDir2) ||
AuFS::DirExists(normalizedUserDir2))
{
if (AuEndsWith(normalizedUserDir, "//") ||
AuEndsWith(normalizedUserDir, "\\/"))
{
normalizedUserDir.pop_back();
normalizedUserDir[normalizedUserDir.size() - 1] = '/';
}
normalizedUserDir += str;
failingPaths.push_back(normalizedUserDir);
}
}
return false;
}
}
catch (...)
{
SysPushErrorCatch();
}
return true;
}
}