From 4eaeccdaeb81ad2c6067bf8cef3f04b8ed37e846 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 14 Apr 2024 23:43:50 +0100 Subject: [PATCH] Codechange: Introduce FioRemove() to remove files. (#12491) New function FioRemove() handles OTTD2FS conversion, and uses std::filesystem::remove instead of unlink, all in one location. --- src/console_cmds.cpp | 2 +- src/driver.cpp | 10 ++-------- src/fileio.cpp | 17 +++++++++++++++++ src/fileio_func.h | 1 + src/fios.cpp | 3 +-- src/network/network_content.cpp | 5 +++-- src/stdafx.h | 1 - 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 8ece62b2f8..1a7460ce43 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -523,7 +523,7 @@ DEF_CONSOLE_CMD(ConRemove) _console_file_list_savegame.ValidateFileList(); const FiosItem *item = _console_file_list_savegame.FindItem(file); if (item != nullptr) { - if (unlink(item->name.c_str()) != 0) { + if (!FioRemove(item->name)) { IConsolePrint(CC_ERROR, "Failed to delete '{}'.", item->name); } } else { diff --git a/src/driver.cpp b/src/driver.cpp index 630a974fd4..26815874a0 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -19,12 +19,6 @@ #include "fileio_func.h" #include -#ifdef _WIN32 -# include -#else -# include -#endif /* _WIN32 */ - #include "safeguards.h" std::string _ini_videodriver; ///< The video driver a stored in the configuration file. @@ -129,7 +123,7 @@ bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type t * hardware acceleration. */ auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE); if (!filename.empty()) { - unlink(filename.c_str()); + FioRemove(filename); Debug(driver, 1, "Probing {} driver '{}' skipped due to earlier crash", GetDriverTypeName(type), d->name); @@ -216,7 +210,7 @@ void DriverFactoryBase::MarkVideoDriverOperational() * and as we are operational now, remove the hardware acceleration * test-file. */ auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE); - if (!filename.empty()) unlink(filename.c_str()); + if (!filename.empty()) FioRemove(filename); } /** diff --git a/src/fileio.cpp b/src/fileio.cpp index e2c7281513..79f16392ab 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -368,6 +368,23 @@ void FioCreateDirectory(const std::string &name) #endif } +/** + * Remove a file. + * @param filename Filename to remove. + * @return true iff the file was removed. + */ +bool FioRemove(const std::string &filename) +{ + std::filesystem::path path = OTTD2FS(filename); + std::error_code error_code; + std::filesystem::remove(path, error_code); + if (error_code) { + Debug(misc, 0, "Removing {} failed: {}", filename, error_code.message()); + return false; + } + return true; +} + /** * Appends, if necessary, the path separator character to the end of the string. * It does not add the path separator to zero-sized strings. diff --git a/src/fileio_func.h b/src/fileio_func.h index 6f9bd65442..2a13a454d4 100644 --- a/src/fileio_func.h +++ b/src/fileio_func.h @@ -20,6 +20,7 @@ std::string FioFindFullPath(Subdirectory subdir, const std::string &filename); std::string FioGetDirectory(Searchpath sp, Subdirectory subdir); std::string FioFindDirectory(Subdirectory subdir); void FioCreateDirectory(const std::string &name); +bool FioRemove(const std::string &filename); const char *FiosGetScreenshotDir(); diff --git a/src/fios.cpp b/src/fios.cpp index ec1f3de3b2..c020560ecb 100644 --- a/src/fios.cpp +++ b/src/fios.cpp @@ -246,8 +246,7 @@ std::string FiosMakeHeightmapName(const char *name) */ bool FiosDelete(const char *name) { - std::string filename = FiosMakeSavegameName(name); - return unlink(filename.c_str()) == 0; + return FioRemove(FiosMakeSavegameName(name)); } typedef std::tuple FiosGetTypeAndNameProc(SaveLoadOperation fop, const std::string &filename, const std::string_view ext); diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp index 94f6362fde..27bd3ce730 100644 --- a/src/network/network_content.cpp +++ b/src/network/network_content.cpp @@ -13,6 +13,7 @@ #include "../game/game.hpp" #include "../window_func.h" #include "../error.h" +#include "../fileio_func.h" #include "../base_media_base.h" #include "../settings_type.h" #include "network_content.h" @@ -550,7 +551,7 @@ void ClientNetworkContentSocketHandler::AfterDownload() this->curFile = nullptr; if (GunzipFile(this->curInfo)) { - unlink(GetFullFilename(this->curInfo, true).c_str()); + FioRemove(GetFullFilename(this->curInfo, true)); Subdirectory sd = GetContentInfoSubDir(this->curInfo->type); if (sd == NO_DIRECTORY) NOT_REACHED(); @@ -562,7 +563,7 @@ void ClientNetworkContentSocketHandler::AfterDownload() if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) { /* Music can't be in a tar. So extract the tar! */ ExtractTar(fname, BASESET_DIR); - unlink(fname.c_str()); + FioRemove(fname); } #ifdef __EMSCRIPTEN__ diff --git a/src/stdafx.h b/src/stdafx.h index 7f598de39e..3297c3eec2 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -169,7 +169,6 @@ # include # define fopen(file, mode) _wfopen(OTTD2FS(file).c_str(), _T(mode)) -# define unlink(file) _wunlink(OTTD2FS(file).c_str()) std::string FS2OTTD(const std::wstring &name); std::wstring OTTD2FS(const std::string &name);