FileSystem: Add DeleteDirectory() and recursive variant

This commit is contained in:
Connor McLaughlin 2022-03-25 19:22:57 +10:00 committed by refractionpcsx2
parent 29c0d93864
commit 47fff9304b
2 changed files with 51 additions and 4 deletions

View File

@ -575,6 +575,29 @@ bool FileSystem::EnsureDirectoryExists(const char* path, bool recursive)
return FileSystem::CreateDirectoryPath(path, recursive);
}
bool FileSystem::RecursiveDeleteDirectory(const char* path)
{
FindResultsArray results;
if (FindFiles(path, "*", FILESYSTEM_FIND_FILES | FILESYSTEM_FIND_FOLDERS | FILESYSTEM_FIND_HIDDEN_FILES, &results))
{
for (const FILESYSTEM_FIND_DATA& fd : results)
{
if (fd.Attributes & FILESYSTEM_FILE_ATTRIBUTE_DIRECTORY)
{
if (!RecursiveDeleteDirectory(fd.FileName.c_str()))
return false;
}
else
{
if (!DeleteFilePath(fd.FileName.c_str()))
return false;
}
}
}
return DeleteDirectory(path);
}
#ifdef _WIN32
static u32 TranslateWin32Attributes(u32 Win32Attributes)
@ -1040,15 +1063,15 @@ bool FileSystem::DeleteFilePath(const char* path)
#endif
}
bool FileSystem::RenamePath(const char* old_path, const char* new_patah)
bool FileSystem::RenamePath(const char* old_path, const char* new_path)
{
const std::wstring old_wpath(StringUtil::UTF8StringToWideString(old_path));
const std::wstring new_wpath(StringUtil::UTF8StringToWideString(new_patah));
const std::wstring new_wpath(StringUtil::UTF8StringToWideString(new_path));
#ifndef _UWP
if (!MoveFileExW(old_wpath.c_str(), new_wpath.c_str(), MOVEFILE_REPLACE_EXISTING))
{
Console.Error("MoveFileEx('%s', '%s') failed: %08X", old_path, new_patah, GetLastError());
Console.Error("MoveFileEx('%s', '%s') failed: %08X", old_path, new_path, GetLastError());
return false;
}
#else
@ -1064,7 +1087,7 @@ bool FileSystem::RenamePath(const char* old_path, const char* new_patah)
if (!MoveFileFromAppW(old_wpath.c_str(), new_wpath.c_str()))
{
Log_ErrorPrintf("MoveFileFromAppW('%s', '%s') failed: %08X", old_path, new_patah, GetLastError());
Log_ErrorPrintf("MoveFileFromAppW('%s', '%s') failed: %08X", old_path, new_path, GetLastError());
return false;
}
#endif
@ -1072,6 +1095,12 @@ bool FileSystem::RenamePath(const char* old_path, const char* new_patah)
return true;
}
bool FileSystem::DeleteDirectory(const char* path)
{
const std::wstring wpath(StringUtil::UTF8StringToWideString(path));
return RemoveDirectoryW(wpath.c_str());
}
std::string FileSystem::GetProgramPath()
{
std::wstring buffer;
@ -1485,6 +1514,18 @@ bool FileSystem::RenamePath(const char* old_path, const char* new_path)
return true;
}
bool FileSystem::DeleteDirectory(const char* path)
{
if (path[0] == '\0')
return false;
struct stat sysStatData;
if (stat(path, &sysStatData) != 0 || !S_ISDIR(sysStatData.st_mode))
return false;
return (unlink(path) == 0);
}
std::string FileSystem::GetProgramPath()
{
#if defined(__linux__)

View File

@ -154,6 +154,12 @@ namespace FileSystem
/// Returns false if it does not exist and creation failed.
bool EnsureDirectoryExists(const char* path, bool recursive);
/// Removes a directory.
bool DeleteDirectory(const char* path);
/// Recursively removes a directory and all subdirectories/files.
bool RecursiveDeleteDirectory(const char* path);
/// Returns the path to the current executable.
std::string GetProgramPath();