Changed two FileSystemUtil functions to use std::filesystem facilities

This commit is contained in:
Leon Styhre 2023-08-14 19:03:37 +02:00
parent 24801680c5
commit 88373f06ee
5 changed files with 51 additions and 65 deletions

View file

@ -1123,7 +1123,7 @@ bool SystemData::createSystemDirectories()
replaceInfoFile = false; replaceInfoFile = false;
if (replaceInfoFile) { if (replaceInfoFile) {
if (Utils::FileSystem::removeFile(rompath + systemDir + systemInfoFileName)) if (!Utils::FileSystem::removeFile(rompath + systemDir + systemInfoFileName))
return true; return true;
} }
@ -1203,7 +1203,7 @@ bool SystemData::createSystemDirectories()
bool systemsFileSuccess {true}; bool systemsFileSuccess {true};
if (Utils::FileSystem::exists(rompath + systemsFileName)) { if (Utils::FileSystem::exists(rompath + systemsFileName)) {
if (Utils::FileSystem::removeFile(rompath + systemsFileName)) if (!Utils::FileSystem::removeFile(rompath + systemsFileName))
systemsFileSuccess = false; systemsFileSuccess = false;
} }
@ -1308,8 +1308,8 @@ std::string SystemData::getGamelistPath(bool forWrite) const
std::string SystemData::getThemePath() const std::string SystemData::getThemePath() const
{ {
// Check for the precence of [CURRENT_THEME_PATH]/[SYSTEM]/theme.xml and if this does not // Check for the presence of [CURRENT_THEME_PATH]/[SYSTEM]/theme.xml and if this does not
// exist, then try the default for the theme set, i.e. [CURRENT_THEME_PATH]/theme.xml // exist, then try the default file for the theme, i.e. [CURRENT_THEME_PATH]/theme.xml
std::string themePath {ThemeData::getThemeFromCurrentSet(mThemeFolder)}; std::string themePath {ThemeData::getThemeFromCurrentSet(mThemeFolder)};
if (Utils::FileSystem::exists(themePath)) if (Utils::FileSystem::exists(themePath))
@ -1500,7 +1500,7 @@ void SystemData::loadTheme(ThemeTriggers::TriggerType trigger)
// No theme available for this platform. // No theme available for this platform.
if (!mIsCustomCollectionSystem) { if (!mIsCustomCollectionSystem) {
LOG(LogWarning) << "There is no \"" << mThemeFolder LOG(LogWarning) << "There is no \"" << mThemeFolder
<< "\" configuration available for the selected theme set \"" << "\" configuration available for the selected theme \""
<< Settings::getInstance()->getString("ThemeSet") << Settings::getInstance()->getString("ThemeSet")
<< "\", system will be unthemed"; << "\", system will be unthemed";
} }

View file

@ -493,7 +493,7 @@ void GuiOrphanedDataCleanup::cleanupGamelists()
if (Utils::FileSystem::exists(tempFile)) { if (Utils::FileSystem::exists(tempFile)) {
LOG(LogWarning) << "Found existing temporary file \"" << tempFile << "\""; LOG(LogWarning) << "Found existing temporary file \"" << tempFile << "\"";
if (Utils::FileSystem::removeFile(tempFile)) { if (!Utils::FileSystem::removeFile(tempFile)) {
LOG(LogError) << "Couldn't remove temporary file \"" << tempFile << "\""; LOG(LogError) << "Couldn't remove temporary file \"" << tempFile << "\"";
{ {
std::unique_lock<std::mutex> lock {mMutex}; std::unique_lock<std::mutex> lock {mMutex};
@ -639,12 +639,12 @@ void GuiOrphanedDataCleanup::cleanupGamelists()
} }
LOG(LogInfo) << "Removed " << removeCount << (removeCount == 1 ? " entry " : " entries ") LOG(LogInfo) << "Removed " << removeCount << (removeCount == 1 ? " entry " : " entries ")
<< "from system \"" << currentSystem << "\""; << "for system \"" << currentSystem << "\"";
if (!mFailed) if (!mFailed)
mProcessedCount += removeCount; mProcessedCount += removeCount;
if (Utils::FileSystem::exists(tempFile) && Utils::FileSystem::removeFile(tempFile)) { if (Utils::FileSystem::exists(tempFile) && !Utils::FileSystem::removeFile(tempFile)) {
LOG(LogError) << "Couldn't remove temporary file \"" << tempFile << "\""; LOG(LogError) << "Couldn't remove temporary file \"" << tempFile << "\"";
{ {
std::unique_lock<std::mutex> lock {mMutex}; std::unique_lock<std::mutex> lock {mMutex};
@ -767,7 +767,7 @@ void GuiOrphanedDataCleanup::cleanupCollections()
if (Utils::FileSystem::exists(tempFile)) { if (Utils::FileSystem::exists(tempFile)) {
LOG(LogWarning) << "Found existing temporary file \"" << tempFile << "\""; LOG(LogWarning) << "Found existing temporary file \"" << tempFile << "\"";
if (Utils::FileSystem::removeFile(tempFile)) { if (!Utils::FileSystem::removeFile(tempFile)) {
LOG(LogError) << "Couldn't remove temporary file"; LOG(LogError) << "Couldn't remove temporary file";
{ {
std::unique_lock<std::mutex> lock {mMutex}; std::unique_lock<std::mutex> lock {mMutex};
@ -877,7 +877,7 @@ void GuiOrphanedDataCleanup::cleanupCollections()
if (!mFailed) if (!mFailed)
mProcessedCount += removeCount; mProcessedCount += removeCount;
if (Utils::FileSystem::exists(tempFile) && Utils::FileSystem::removeFile(tempFile)) { if (Utils::FileSystem::exists(tempFile) && !Utils::FileSystem::removeFile(tempFile)) {
LOG(LogError) << "Couldn't remove temporary file \"" << tempFile << "\""; LOG(LogError) << "Couldn't remove temporary file \"" << tempFile << "\"";
{ {
std::unique_lock<std::mutex> lock {mMutex}; std::unique_lock<std::mutex> lock {mMutex};

View file

@ -651,7 +651,7 @@ void GamelistBase::removeMedia(FileData* game)
std::string parentPath {Utils::FileSystem::getParent(path)}; std::string parentPath {Utils::FileSystem::getParent(path)};
while (parentPath != systemMediaDir + "/" + mediaType) { while (parentPath != systemMediaDir + "/" + mediaType) {
if (Utils::FileSystem::getDirContent(parentPath).size() == 0) { if (Utils::FileSystem::getDirContent(parentPath).size() == 0) {
Utils::FileSystem::removeDirectory(parentPath); Utils::FileSystem::removeDirectory(parentPath, false);
parentPath = Utils::FileSystem::getParent(parentPath); parentPath = Utils::FileSystem::getParent(parentPath);
} }
else { else {
@ -664,7 +664,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getVideoPath())) { while (Utils::FileSystem::exists(game->getVideoPath())) {
mediaType = "videos"; mediaType = "videos";
path = game->getVideoPath(); path = game->getVideoPath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -672,7 +672,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getManualPath())) { while (Utils::FileSystem::exists(game->getManualPath())) {
mediaType = "manuals"; mediaType = "manuals";
path = game->getManualPath(); path = game->getManualPath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -680,7 +680,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getMiximagePath())) { while (Utils::FileSystem::exists(game->getMiximagePath())) {
mediaType = "miximages"; mediaType = "miximages";
path = game->getMiximagePath(); path = game->getMiximagePath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -688,7 +688,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getScreenshotPath())) { while (Utils::FileSystem::exists(game->getScreenshotPath())) {
mediaType = "screenshots"; mediaType = "screenshots";
path = game->getScreenshotPath(); path = game->getScreenshotPath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -696,7 +696,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getTitleScreenPath())) { while (Utils::FileSystem::exists(game->getTitleScreenPath())) {
mediaType = "titlescreens"; mediaType = "titlescreens";
path = game->getTitleScreenPath(); path = game->getTitleScreenPath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -704,14 +704,15 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getCoverPath())) { while (Utils::FileSystem::exists(game->getCoverPath())) {
mediaType = "covers"; mediaType = "covers";
path = game->getCoverPath(); path = game->getCoverPath();
Utils::FileSystem::removeFile(path); if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
while (Utils::FileSystem::exists(game->getBackCoverPath())) { while (Utils::FileSystem::exists(game->getBackCoverPath())) {
mediaType = "backcovers"; mediaType = "backcovers";
path = game->getBackCoverPath(); path = game->getBackCoverPath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -719,7 +720,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getFanArtPath())) { while (Utils::FileSystem::exists(game->getFanArtPath())) {
mediaType = "fanart"; mediaType = "fanart";
path = game->getFanArtPath(); path = game->getFanArtPath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -727,7 +728,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getMarqueePath())) { while (Utils::FileSystem::exists(game->getMarqueePath())) {
mediaType = "marquees"; mediaType = "marquees";
path = game->getMarqueePath(); path = game->getMarqueePath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -735,7 +736,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->get3DBoxPath())) { while (Utils::FileSystem::exists(game->get3DBoxPath())) {
mediaType = "3dboxes"; mediaType = "3dboxes";
path = game->get3DBoxPath(); path = game->get3DBoxPath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }
@ -743,7 +744,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getPhysicalMediaPath())) { while (Utils::FileSystem::exists(game->getPhysicalMediaPath())) {
mediaType = "physicalmedia"; mediaType = "physicalmedia";
path = game->getPhysicalMediaPath(); path = game->getPhysicalMediaPath();
if (Utils::FileSystem::removeFile(path)) if (!Utils::FileSystem::removeFile(path))
break; break;
removeEmptyDirFunc(systemMediaDir, mediaType, path); removeEmptyDirFunc(systemMediaDir, mediaType, path);
} }

View file

@ -801,56 +801,41 @@ namespace Utils
bool removeFile(const std::string& path) bool removeFile(const std::string& path)
{ {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};
try {
// Don't remove if it doesn't exists.
if (!exists(genericPath))
return true;
#if defined(_WIN64) #if defined(_WIN64)
if (_wunlink(Utils::String::stringToWideString(genericPath).c_str()) != 0) { return std::filesystem::remove(Utils::String::stringToWideString(genericPath));
LOG(LogError) << "Couldn't delete file, permission problems?";
LOG(LogError) << genericPath;
return true;
}
else {
return false;
}
#else #else
if (unlink(genericPath.c_str()) != 0) { return std::filesystem::remove(genericPath);
LOG(LogError) << "Couldn't delete file, permission problems?";
LOG(LogError) << genericPath;
return true;
}
else {
return false;
}
return (unlink(genericPath.c_str()) == 0);
#endif #endif
} }
catch (std::filesystem::filesystem_error& error) {
LOG(LogError) << "FileSystemUtil::removeFile(): " << error.what();
return false;
}
}
bool removeDirectory(const std::string& path) bool removeDirectory(const std::string& path, bool recursive)
{ {
if (getDirContent(path).size() != 0) { const std::string& genericPath {getGenericPath(path)};
LOG(LogError) << "Couldn't delete directory as it's not empty"; try {
LOG(LogError) << path;
return false;
}
if (isSymlink(path)) {
LOG(LogError) << "Couldn't delete directory as it's actually a symlink";
LOG(LogError) << path;
return false;
}
#if defined(_WIN64) #if defined(_WIN64)
if (_wrmdir(Utils::String::stringToWideString(path).c_str()) != 0) { if (recursive)
return std::filesystem::remove_all(
Utils::String::stringToWideString(genericPath));
else
return std::filesystem::remove(Utils::String::stringToWideString(genericPath));
#else #else
if (rmdir(path.c_str()) != 0) { if (recursive)
return std::filesystem::remove_all(genericPath);
else
return std::filesystem::remove(genericPath);
#endif #endif
LOG(LogError) << "Couldn't delete directory, permission problems?"; }
LOG(LogError) << path; catch (std::filesystem::filesystem_error& error) {
LOG(LogError) << "FileSystemUtil::removeDirectory(): " << error.what();
return false; return false;
} }
return true; }
} // namespace FileSystem
bool createDirectory(const std::string& path) bool createDirectory(const std::string& path)
{ {
@ -885,8 +870,8 @@ namespace Utils
bool exists(const std::string& path) bool exists(const std::string& path)
{ {
try {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};
try {
#if defined(_WIN64) #if defined(_WIN64)
return std::filesystem::exists(Utils::String::stringToWideString(genericPath)); return std::filesystem::exists(Utils::String::stringToWideString(genericPath));
#else #else
@ -918,8 +903,8 @@ namespace Utils
bool isAbsolute(const std::string& path) bool isAbsolute(const std::string& path)
{ {
try {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};
try {
#if defined(_WIN64) #if defined(_WIN64)
return ((genericPath.size() > 1) && (genericPath[1] == ':')); return ((genericPath.size() > 1) && (genericPath[1] == ':'));
#else #else
@ -933,8 +918,8 @@ namespace Utils
bool isRegularFile(const std::string& path) bool isRegularFile(const std::string& path)
{ {
try {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};
try {
#if defined(_WIN64) #if defined(_WIN64)
return std::filesystem::is_regular_file( return std::filesystem::is_regular_file(
Utils::String::stringToWideString(genericPath)); Utils::String::stringToWideString(genericPath));
@ -950,8 +935,8 @@ namespace Utils
bool isDirectory(const std::string& path) bool isDirectory(const std::string& path)
{ {
try {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};
try {
#if defined(_WIN64) #if defined(_WIN64)
return std::filesystem::is_directory( return std::filesystem::is_directory(
Utils::String::stringToWideString(genericPath)); Utils::String::stringToWideString(genericPath));
@ -967,8 +952,8 @@ namespace Utils
bool isSymlink(const std::string& path) bool isSymlink(const std::string& path)
{ {
try {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};
try {
#if defined(_WIN64) #if defined(_WIN64)
return std::filesystem::is_symlink(Utils::String::stringToWideString(genericPath)); return std::filesystem::is_symlink(Utils::String::stringToWideString(genericPath));
#else #else

View file

@ -63,7 +63,7 @@ namespace Utils
bool overwrite); bool overwrite);
bool createEmptyFile(const std::filesystem::path& path); bool createEmptyFile(const std::filesystem::path& path);
bool removeFile(const std::string& path); bool removeFile(const std::string& path);
bool removeDirectory(const std::string& path); bool removeDirectory(const std::string& path, bool recursive);
bool createDirectory(const std::string& path); bool createDirectory(const std::string& path);
bool exists(const std::string& path); bool exists(const std::string& path);
bool driveExists(const std::string& path); bool driveExists(const std::string& path);