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

View file

@ -493,7 +493,7 @@ void GuiOrphanedDataCleanup::cleanupGamelists()
if (Utils::FileSystem::exists(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 << "\"";
{
std::unique_lock<std::mutex> lock {mMutex};
@ -639,12 +639,12 @@ void GuiOrphanedDataCleanup::cleanupGamelists()
}
LOG(LogInfo) << "Removed " << removeCount << (removeCount == 1 ? " entry " : " entries ")
<< "from system \"" << currentSystem << "\"";
<< "for system \"" << currentSystem << "\"";
if (!mFailed)
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 << "\"";
{
std::unique_lock<std::mutex> lock {mMutex};
@ -767,7 +767,7 @@ void GuiOrphanedDataCleanup::cleanupCollections()
if (Utils::FileSystem::exists(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";
{
std::unique_lock<std::mutex> lock {mMutex};
@ -877,7 +877,7 @@ void GuiOrphanedDataCleanup::cleanupCollections()
if (!mFailed)
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 << "\"";
{
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)};
while (parentPath != systemMediaDir + "/" + mediaType) {
if (Utils::FileSystem::getDirContent(parentPath).size() == 0) {
Utils::FileSystem::removeDirectory(parentPath);
Utils::FileSystem::removeDirectory(parentPath, false);
parentPath = Utils::FileSystem::getParent(parentPath);
}
else {
@ -664,7 +664,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getVideoPath())) {
mediaType = "videos";
path = game->getVideoPath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -672,7 +672,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getManualPath())) {
mediaType = "manuals";
path = game->getManualPath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -680,7 +680,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getMiximagePath())) {
mediaType = "miximages";
path = game->getMiximagePath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -688,7 +688,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getScreenshotPath())) {
mediaType = "screenshots";
path = game->getScreenshotPath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -696,7 +696,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getTitleScreenPath())) {
mediaType = "titlescreens";
path = game->getTitleScreenPath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -704,14 +704,15 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getCoverPath())) {
mediaType = "covers";
path = game->getCoverPath();
Utils::FileSystem::removeFile(path);
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
while (Utils::FileSystem::exists(game->getBackCoverPath())) {
mediaType = "backcovers";
path = game->getBackCoverPath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -719,7 +720,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getFanArtPath())) {
mediaType = "fanart";
path = game->getFanArtPath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -727,7 +728,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getMarqueePath())) {
mediaType = "marquees";
path = game->getMarqueePath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -735,7 +736,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->get3DBoxPath())) {
mediaType = "3dboxes";
path = game->get3DBoxPath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}
@ -743,7 +744,7 @@ void GamelistBase::removeMedia(FileData* game)
while (Utils::FileSystem::exists(game->getPhysicalMediaPath())) {
mediaType = "physicalmedia";
path = game->getPhysicalMediaPath();
if (Utils::FileSystem::removeFile(path))
if (!Utils::FileSystem::removeFile(path))
break;
removeEmptyDirFunc(systemMediaDir, mediaType, path);
}

View file

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

View file

@ -63,7 +63,7 @@ namespace Utils
bool overwrite);
bool createEmptyFile(const std::filesystem::path& 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 exists(const std::string& path);
bool driveExists(const std::string& path);