2022-11-06 12:30:06 +00:00
|
|
|
/***
|
|
|
|
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;
|
2023-01-26 21:43:19 +00:00
|
|
|
AuList<AuString> failedPaths;
|
2022-11-06 12:30:06 +00:00
|
|
|
|
|
|
|
bool OpenDir(const AuString &str)
|
|
|
|
{
|
2023-01-26 21:43:19 +00:00
|
|
|
this->curPath = str;
|
|
|
|
this->pDir = ReadDir(str);
|
2022-11-06 12:30:06 +00:00
|
|
|
return bool(pDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OpenNext(const AuString &str)
|
|
|
|
{
|
2023-01-26 21:43:19 +00:00
|
|
|
this->curPath = str;
|
|
|
|
this->pDir = ReadDir(str);
|
2022-11-06 12:30:06 +00:00
|
|
|
return bool(pDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoNext()
|
|
|
|
{
|
|
|
|
this->pDir.reset();
|
|
|
|
|
2023-01-26 21:43:19 +00:00
|
|
|
if (!this->nextLevel.size())
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-26 21:43:19 +00:00
|
|
|
auto a = this->nextLevel[0];
|
|
|
|
this->nextLevel.erase(this->nextLevel.begin());
|
|
|
|
this->pDir = ReadDir(this->curPath + "/" + a);
|
|
|
|
this->curSubDir = a;
|
2022-11-06 12:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-01-26 21:43:19 +00:00
|
|
|
if (!AuFS::Remove(pNext->path))
|
|
|
|
{
|
|
|
|
this->failedPaths.push_back(pNext->fileName);
|
|
|
|
}
|
2022-11-06 12:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveDirs()
|
|
|
|
{
|
2023-01-26 21:43:19 +00:00
|
|
|
for (auto itr = this->nextLevel2.rbegin(); itr != this->nextLevel2.rend(); itr++)
|
2022-11-06 12:30:06 +00:00
|
|
|
{
|
2023-01-26 21:43:19 +00:00
|
|
|
auto dir = this->curPath + "/" + itr->c_str();
|
|
|
|
if (!AuFS::Remove(dir))
|
|
|
|
{
|
|
|
|
this->failedPaths.push_back(itr->c_str());
|
|
|
|
}
|
2022-11-06 12:30:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-01-26 21:43:19 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2023-02-04 19:43:01 +00:00
|
|
|
auto normalizedUserDir = string + "/" ;
|
|
|
|
auto normalizedUserDir2 = normalizedUserDir + str;
|
2023-01-29 23:03:54 +00:00
|
|
|
|
2023-02-04 19:43:01 +00:00
|
|
|
if (AuFS::FileExists(normalizedUserDir2) ||
|
|
|
|
AuFS::DirExists(normalizedUserDir2))
|
2023-01-26 21:43:19 +00:00
|
|
|
{
|
2023-02-04 19:43:01 +00:00
|
|
|
if (AuEndsWith(normalizedUserDir, "//") ||
|
|
|
|
AuEndsWith(normalizedUserDir, "\\/"))
|
|
|
|
{
|
|
|
|
normalizedUserDir.pop_back();
|
|
|
|
normalizedUserDir[normalizedUserDir.size() - 1] = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
normalizedUserDir += str;
|
|
|
|
|
2023-01-29 23:03:54 +00:00
|
|
|
failingPaths.push_back(normalizedUserDir);
|
2023-01-26 21:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-11-06 12:30:06 +00:00
|
|
|
}
|