AuroraRuntime/Source/IO/FS/DirDeleter.cpp
Reece Wilson f43251c8fc [+] AuNet::ISocketChannelEventListener
[+] AuFS::UpdateTimes
[+] AuFS::UpdateFileTimes
[+] AuFS::CompressEx
[*] AuFS::Compress now rejects files that look to be already compressed
[+] AuFS::DecompressEx
[+] AuFS::Create
[+] AuFS::WriteNewFile
[+] AuFS::WriteNewString
[+] AuFs::FileAttrsList
[+] AuFs::FileAttrsGet
[+] AuFs::FileAttrsSet
[+] DirectoryLogger::uMaxLogsOrZeroBeforeCompress
[+] ISocketChannel.AddEventListener
[+] ISocketChannel.AddEventListener
[+] DirectoryLogger.uMaxLogsOrZeroBeforeCompress
[*] Fix UNIX regression
[*] Fix up stream socket channel realloc IPC
[*] Fix shutdown regression in pretty much everything thanks to 8ff81df1's dumbass fix
    (fixes fence regression on shutdown)
[*] Fix DirDeleterEx formatting of reported failed paths
[*] Fix up file not truncated if already exists bugs. Extended and alternative apis added.
[*] Fix ICompressionStream::ReadEx returning the wrong read value
[+] Legacy compression API can now self-correct once newer stream processor objects are added
2023-02-04 19:43:01 +00:00

181 lines
4.2 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;
bool OpenDir(const AuString &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()
{
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
{
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
{
if (!AuFS::Remove(pNext->path))
{
this->failedPaths.push_back(pNext->fileName);
}
}
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());
}
}
}
};
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);
}
AUKN_SYM bool DirDeleterEx(const AuString &string, AuList<AuString> &failingPaths)
{
auto pObj = AuMakeShared<RecursiveDirDeleter>();
if (!pObj)
{
SysPushErrorMem();
return {};
}
if (!pObj->OpenDir(string))
{
return {};
}
while (pObj->Next())
{
}
pObj->RemoveDirs();
AuFS::Remove(string);
if (AuFS::DirExists(string))
{
for (const auto &str : pObj->failedPaths)
{
auto normalizedUserDir = 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;
}
return true;
}
}