From 518ed74496115207b711eead4a4d7a71da1cd209 Mon Sep 17 00:00:00 2001 From: Ralph Sennhauser Date: Sun, 14 Jun 2026 19:44:09 +0200 Subject: [PATCH] Replace all use of POSIX unlink Use `` instead of `unlink` and remove `unlink` portability wrapper. Signed-off-by: Ralph Sennhauser --- source/lib/sysdep/filesystem.h | 2 -- source/lib/sysdep/os/unix/ufilesystem.cpp | 6 ---- .../lib/sysdep/os/win/wposix/wfilesystem.cpp | 6 ---- source/ps/ModIo.cpp | 8 +++-- source/ps/SavedGame.cpp | 6 +++- source/ps/VisualReplay.cpp | 8 +++-- source/ps/scripting/JSInterface_VFS.cpp | 16 ++++++--- source/simulation2/Simulation2.cpp | 35 ++++++++++++------- 8 files changed, 50 insertions(+), 37 deletions(-) diff --git a/source/lib/sysdep/filesystem.h b/source/lib/sysdep/filesystem.h index 5d4e05bf7f..4b5fba9e2b 100644 --- a/source/lib/sysdep/filesystem.h +++ b/source/lib/sysdep/filesystem.h @@ -67,8 +67,6 @@ extern int wclose(int fd); // this function called. int wtruncate(const OsPath& pathname, off_t length); -int wunlink(const OsPath& pathname); - int wrmdir(const OsPath& path); diff --git a/source/lib/sysdep/os/unix/ufilesystem.cpp b/source/lib/sysdep/os/unix/ufilesystem.cpp index 6bee293273..71da63d66c 100644 --- a/source/lib/sysdep/os/unix/ufilesystem.cpp +++ b/source/lib/sysdep/os/unix/ufilesystem.cpp @@ -93,17 +93,11 @@ int wclose(int fd) return close(fd); } - int wtruncate(const OsPath& pathname, off_t length) { return truncate(OsString(pathname).c_str(), length); } -int wunlink(const OsPath& pathname) -{ - return unlink(OsString(pathname).c_str()); -} - int wrmdir(const OsPath& path) { return rmdir(OsString(path).c_str()); diff --git a/source/lib/sysdep/os/win/wposix/wfilesystem.cpp b/source/lib/sysdep/os/win/wposix/wfilesystem.cpp index 29c067a569..d9bbd6fcdd 100644 --- a/source/lib/sysdep/os/win/wposix/wfilesystem.cpp +++ b/source/lib/sysdep/os/win/wposix/wfilesystem.cpp @@ -146,12 +146,6 @@ int wtruncate(const OsPath& pathname, off_t length) } -int wunlink(const OsPath& pathname) -{ - return _wunlink(OsString(pathname).c_str()); -} - - int wrmdir(const OsPath& path) { return _wrmdir(OsString(path).c_str()); diff --git a/source/ps/ModIo.cpp b/source/ps/ModIo.cpp index 950727bc8e..e2ac664b46 100644 --- a/source/ps/ModIo.cpp +++ b/source/ps/ModIo.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -56,6 +57,7 @@ #include #include #include +#include class ScriptInterface; @@ -545,7 +547,9 @@ bool ModIo::ParseMods(const ScriptInterface& scriptInterface, std::string& err) void ModIo::DeleteDownloadedFile() { - if (wunlink(m_DownloadFilePath) != 0) + std::error_code ec{}; + std::filesystem::remove(m_DownloadFilePath.string(), ec); + if (ec) LOGERROR("Failed to delete temporary file."); m_DownloadFilePath = OsPath(); } diff --git a/source/ps/SavedGame.cpp b/source/ps/SavedGame.cpp index 2be85dbf35..b794c07b32 100644 --- a/source/ps/SavedGame.cpp +++ b/source/ps/SavedGame.cpp @@ -51,10 +51,12 @@ #include #include +#include #include #include #include #include +#include #include class ScriptInterface; @@ -343,7 +345,9 @@ bool SavedGames::DeleteSavedGame(const std::wstring& name) return false; // Error // Delete actual file - if (wunlink(realpath) != 0) + std::error_code ec{}; + std::filesystem::remove(realpath.string(), ec); + if (ec) return false; // Error // Successfully deleted file diff --git a/source/ps/VisualReplay.cpp b/source/ps/VisualReplay.cpp index 196176c26a..b5c5dc05cc 100644 --- a/source/ps/VisualReplay.cpp +++ b/source/ps/VisualReplay.cpp @@ -42,6 +42,7 @@ #include #include +#include #include #include #include @@ -50,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -110,7 +112,8 @@ bool VisualReplay::ReadCacheFile(const ScriptInterface& scriptInterface, JS::Mut } LOGWARNING("The replay cache file is corrupted, it will be deleted"); - wunlink(GetCacheFilePath()); + std::error_code ec{}; + std::filesystem::remove(GetCacheFilePath().string(), ec); return false; } @@ -123,7 +126,8 @@ void VisualReplay::StoreCacheFile(const ScriptInterface& scriptInterface, JS::Ha cacheStream << Script::StringifyJSON(rq, &replaysRooted); cacheStream.close(); - wunlink(GetCacheFilePath()); + std::error_code ec{}; + std::filesystem::remove(GetCacheFilePath().string(), ec); if (RenameFile(GetTempCacheFilePath(), GetCacheFilePath())) LOGERROR("Could not store the replay cache"); } diff --git a/source/ps/scripting/JSInterface_VFS.cpp b/source/ps/scripting/JSInterface_VFS.cpp index cbc38a32e6..b4124cf58c 100644 --- a/source/ps/scripting/JSInterface_VFS.cpp +++ b/source/ps/scripting/JSInterface_VFS.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,7 @@ #include #include #include +#include class ScriptInterface; @@ -288,11 +290,15 @@ bool DeleteCampaignSave(const CStrW& filePath) OsPath realPath; if (filePath.Left(16) != L"saves/campaigns/" || filePath.Right(12) != L".0adcampaign") return false; - - return VfsFileExists(filePath) && - g_VFS->GetRealPath(filePath, realPath) == INFO::OK && - g_VFS->RemoveFile(filePath) == INFO::OK && - wunlink(realPath) == 0; + if (!VfsFileExists(filePath)) + return false; + if (g_VFS->GetRealPath(filePath, realPath) != INFO::OK) + return false; + if (g_VFS->RemoveFile(filePath) != INFO::OK) + return false; + std::error_code ec; + std::filesystem::remove(realPath.string(), ec); + return !ec; } void RegisterScriptFunctions_ReadWriteAnywhere(const ScriptRequest& rq, diff --git a/source/simulation2/Simulation2.cpp b/source/simulation2/Simulation2.cpp index 38e1e1630c..013b71bb32 100644 --- a/source/simulation2/Simulation2.cpp +++ b/source/simulation2/Simulation2.cpp @@ -60,6 +60,7 @@ #include #include #include +#include #include #include #include @@ -70,6 +71,7 @@ #include #include #include +#include class CSimulation2Impl { @@ -321,19 +323,26 @@ void CSimulation2Impl::ReportSerializationFailure( const OsPath path = createDateIndexSubdirectory(psLogDir() / "serializationtest"); debug_printf("Writing serializationtest-data to %s\n", path.string8().c_str()); - // Clean up obsolete files from previous runs - wunlink(path / "hash.before.a"); - wunlink(path / "hash.before.b"); - wunlink(path / "debug.before.a"); - wunlink(path / "debug.before.b"); - wunlink(path / "state.before.a"); - wunlink(path / "state.before.b"); - wunlink(path / "hash.after.a"); - wunlink(path / "hash.after.b"); - wunlink(path / "debug.after.a"); - wunlink(path / "debug.after.b"); - wunlink(path / "state.after.a"); - wunlink(path / "state.after.b"); + // Try to clean up obsolete files from previous runs. + constexpr auto namesToRemove{std::to_array({ + "hash.before.a", + "hash.before.b", + "debug.before.a", + "debug.before.b", + "state.before.a", + "state.before.b", + "hash.after.a", + "hash.after.b", + "debug.after.a", + "debug.after.b", + "state.after.a", + "state.after.b", + })}; + + const std::filesystem::path fspath{path.string()}; + std::error_code ec{}; + for (const std::string_view nameToRemove : namesToRemove) + std::filesystem::remove(fspath / nameToRemove, ec); if (primaryStateBefore) DumpSerializationTestState(*primaryStateBefore, path, L"before.a");