Replace all use of POSIX unlink

Use `<filesystem>` instead of `unlink` and remove `unlink` portability
wrapper.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
This commit is contained in:
Ralph Sennhauser
2026-06-14 19:44:09 +02:00
parent 4259c78150
commit 518ed74496
8 changed files with 50 additions and 37 deletions
-2
View File
@@ -67,8 +67,6 @@ extern int wclose(int fd);
// this function called. // this function called.
int wtruncate(const OsPath& pathname, off_t length); int wtruncate(const OsPath& pathname, off_t length);
int wunlink(const OsPath& pathname);
int wrmdir(const OsPath& path); int wrmdir(const OsPath& path);
@@ -93,17 +93,11 @@ int wclose(int fd)
return close(fd); return close(fd);
} }
int wtruncate(const OsPath& pathname, off_t length) int wtruncate(const OsPath& pathname, off_t length)
{ {
return truncate(OsString(pathname).c_str(), length); return truncate(OsString(pathname).c_str(), length);
} }
int wunlink(const OsPath& pathname)
{
return unlink(OsString(pathname).c_str());
}
int wrmdir(const OsPath& path) int wrmdir(const OsPath& path)
{ {
return rmdir(OsString(path).c_str()); return rmdir(OsString(path).c_str());
@@ -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) int wrmdir(const OsPath& path)
{ {
return _wrmdir(OsString(path).c_str()); return _wrmdir(OsString(path).c_str());
+6 -2
View File
@@ -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 * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@@ -49,6 +49,7 @@
#include <boost/algorithm/string/split.hpp> #include <boost/algorithm/string/split.hpp>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <filesystem>
#include <fmt/printf.h> #include <fmt/printf.h>
#include <initializer_list> #include <initializer_list>
#include <js/Array.h> #include <js/Array.h>
@@ -56,6 +57,7 @@
#include <js/RootingAPI.h> #include <js/RootingAPI.h>
#include <js/TypeDecls.h> #include <js/TypeDecls.h>
#include <js/Value.h> #include <js/Value.h>
#include <system_error>
class ScriptInterface; class ScriptInterface;
@@ -545,7 +547,9 @@ bool ModIo::ParseMods(const ScriptInterface& scriptInterface, std::string& err)
void ModIo::DeleteDownloadedFile() 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."); LOGERROR("Failed to delete temporary file.");
m_DownloadFilePath = OsPath(); m_DownloadFilePath = OsPath();
} }
+5 -1
View File
@@ -51,10 +51,12 @@
#include <cstdint> #include <cstdint>
#include <ctime> #include <ctime>
#include <filesystem>
#include <js/RootingAPI.h> #include <js/RootingAPI.h>
#include <js/TypeDecls.h> #include <js/TypeDecls.h>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <system_error>
#include <utility> #include <utility>
class ScriptInterface; class ScriptInterface;
@@ -343,7 +345,9 @@ bool SavedGames::DeleteSavedGame(const std::wstring& name)
return false; // Error return false; // Error
// Delete actual file // Delete actual file
if (wunlink(realpath) != 0) std::error_code ec{};
std::filesystem::remove(realpath.string(), ec);
if (ec)
return false; // Error return false; // Error
// Successfully deleted file // Successfully deleted file
+6 -2
View File
@@ -42,6 +42,7 @@
#include <SDL_events.h> #include <SDL_events.h>
#include <SDL_quit.h> #include <SDL_quit.h>
#include <filesystem>
#include <fstream> #include <fstream>
#include <iterator> #include <iterator>
#include <js/Array.h> #include <js/Array.h>
@@ -50,6 +51,7 @@
#include <js/Value.h> #include <js/Value.h>
#include <map> #include <map>
#include <string> #include <string>
#include <system_error>
#include <tuple> #include <tuple>
#include <utility> #include <utility>
#include <vector> #include <vector>
@@ -110,7 +112,8 @@ bool VisualReplay::ReadCacheFile(const ScriptInterface& scriptInterface, JS::Mut
} }
LOGWARNING("The replay cache file is corrupted, it will be deleted"); 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; return false;
} }
@@ -123,7 +126,8 @@ void VisualReplay::StoreCacheFile(const ScriptInterface& scriptInterface, JS::Ha
cacheStream << Script::StringifyJSON(rq, &replaysRooted); cacheStream << Script::StringifyJSON(rq, &replaysRooted);
cacheStream.close(); cacheStream.close();
wunlink(GetCacheFilePath()); std::error_code ec{};
std::filesystem::remove(GetCacheFilePath().string(), ec);
if (RenameFile(GetTempCacheFilePath(), GetCacheFilePath())) if (RenameFile(GetTempCacheFilePath(), GetCacheFilePath()))
LOGERROR("Could not store the replay cache"); LOGERROR("Could not store the replay cache");
} }
+11 -5
View File
@@ -43,6 +43,7 @@
#include <array> #include <array>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <filesystem>
#include <fmt/format.h> #include <fmt/format.h>
#include <js/Array.h> #include <js/Array.h>
#include <js/PropertyAndElement.h> #include <js/PropertyAndElement.h>
@@ -53,6 +54,7 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <system_error>
class ScriptInterface; class ScriptInterface;
@@ -288,11 +290,15 @@ bool DeleteCampaignSave(const CStrW& filePath)
OsPath realPath; OsPath realPath;
if (filePath.Left(16) != L"saves/campaigns/" || filePath.Right(12) != L".0adcampaign") if (filePath.Left(16) != L"saves/campaigns/" || filePath.Right(12) != L".0adcampaign")
return false; return false;
if (!VfsFileExists(filePath))
return VfsFileExists(filePath) && return false;
g_VFS->GetRealPath(filePath, realPath) == INFO::OK && if (g_VFS->GetRealPath(filePath, realPath) != INFO::OK)
g_VFS->RemoveFile(filePath) == INFO::OK && return false;
wunlink(realPath) == 0; 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, void RegisterScriptFunctions_ReadWriteAnywhere(const ScriptRequest& rq,
+22 -13
View File
@@ -60,6 +60,7 @@
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <filesystem>
#include <fstream> #include <fstream>
#include <functional> #include <functional>
#include <iomanip> #include <iomanip>
@@ -70,6 +71,7 @@
#include <optional> #include <optional>
#include <set> #include <set>
#include <sstream> #include <sstream>
#include <system_error>
class CSimulation2Impl class CSimulation2Impl
{ {
@@ -321,19 +323,26 @@ void CSimulation2Impl::ReportSerializationFailure(
const OsPath path = createDateIndexSubdirectory(psLogDir() / "serializationtest"); const OsPath path = createDateIndexSubdirectory(psLogDir() / "serializationtest");
debug_printf("Writing serializationtest-data to %s\n", path.string8().c_str()); debug_printf("Writing serializationtest-data to %s\n", path.string8().c_str());
// Clean up obsolete files from previous runs // Try to clean up obsolete files from previous runs.
wunlink(path / "hash.before.a"); constexpr auto namesToRemove{std::to_array<std::string_view>({
wunlink(path / "hash.before.b"); "hash.before.a",
wunlink(path / "debug.before.a"); "hash.before.b",
wunlink(path / "debug.before.b"); "debug.before.a",
wunlink(path / "state.before.a"); "debug.before.b",
wunlink(path / "state.before.b"); "state.before.a",
wunlink(path / "hash.after.a"); "state.before.b",
wunlink(path / "hash.after.b"); "hash.after.a",
wunlink(path / "debug.after.a"); "hash.after.b",
wunlink(path / "debug.after.b"); "debug.after.a",
wunlink(path / "state.after.a"); "debug.after.b",
wunlink(path / "state.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) if (primaryStateBefore)
DumpSerializationTestState(*primaryStateBefore, path, L"before.a"); DumpSerializationTestState(*primaryStateBefore, path, L"before.a");