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.
This commit is contained in:
Peter Nelson 2024-04-14 23:43:50 +01:00 committed by GitHub
parent 29e932e087
commit 4eaeccdaeb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 25 additions and 14 deletions

View File

@ -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 {

View File

@ -19,12 +19,6 @@
#include "fileio_func.h"
#include <sstream>
#ifdef _WIN32
# include <windows.h>
#else
# include <unistd.h>
#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);
}
/**

View File

@ -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.

View File

@ -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();

View File

@ -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<FiosType, std::string> FiosGetTypeAndNameProc(SaveLoadOperation fop, const std::string &filename, const std::string_view ext);

View File

@ -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__

View File

@ -169,7 +169,6 @@
# include <tchar.h>
# 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);