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
No known key found for this signature in database
8 changed files with 50 additions and 37 deletions

View file

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

View file

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

View file

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

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
* a copy of this software and associated documentation files (the
@ -49,6 +49,7 @@
#include <boost/algorithm/string/split.hpp>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fmt/printf.h>
#include <initializer_list>
#include <js/Array.h>
@ -56,6 +57,7 @@
#include <js/RootingAPI.h>
#include <js/TypeDecls.h>
#include <js/Value.h>
#include <system_error>
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();
}

View file

@ -51,10 +51,12 @@
#include <cstdint>
#include <ctime>
#include <filesystem>
#include <js/RootingAPI.h>
#include <js/TypeDecls.h>
#include <memory>
#include <sstream>
#include <system_error>
#include <utility>
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

View file

@ -42,6 +42,7 @@
#include <SDL_events.h>
#include <SDL_quit.h>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <js/Array.h>
@ -50,6 +51,7 @@
#include <js/Value.h>
#include <map>
#include <string>
#include <system_error>
#include <tuple>
#include <utility>
#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");
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");
}

View file

@ -43,6 +43,7 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <fmt/format.h>
#include <js/Array.h>
#include <js/PropertyAndElement.h>
@ -53,6 +54,7 @@
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
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,

View file

@ -60,6 +60,7 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iomanip>
@ -70,6 +71,7 @@
#include <optional>
#include <set>
#include <sstream>
#include <system_error>
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<std::string_view>({
"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");