fix(savedata): preserve existing saves on replace failure
This commit is contained in:
parent
bc51b337ad
commit
d7f89a6654
|
|
@ -143,6 +143,36 @@ if(BUILD_TESTING)
|
|||
endif()
|
||||
add_test(NAME host_thread_local COMMAND host_thread_local_tests)
|
||||
set_tests_properties(host_thread_local PROPERTIES TIMEOUT 15)
|
||||
|
||||
add_test_executable(savedata_core_write_replacement_tests tests/SaveDataWriteReplacement.cpp)
|
||||
target_include_directories(savedata_core_write_replacement_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(savedata_core_write_replacement_tests PRIVATE libSceSaveData)
|
||||
configure_windows_unwind(savedata_core_write_replacement_tests)
|
||||
add_test(NAME savedata_core_write_replacement COMMAND savedata_core_write_replacement_tests)
|
||||
|
||||
add_test_executable(savedata_native_write_replacement_tests tests/SaveDataWriteReplacement.cpp)
|
||||
target_include_directories(savedata_native_write_replacement_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_compile_definitions(savedata_native_write_replacement_tests PRIVATE SAVEDATA_NATIVE_BACKEND)
|
||||
target_link_libraries(savedata_native_write_replacement_tests PRIVATE libSceSaveData.native)
|
||||
configure_windows_unwind(savedata_native_write_replacement_tests)
|
||||
add_test(NAME savedata_native_write_replacement COMMAND savedata_native_write_replacement_tests)
|
||||
|
||||
add_test_executable(savedata_replace_file_failure_tests tests/SaveDataFileWriteFailure.cpp)
|
||||
target_include_directories(savedata_replace_file_failure_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
configure_windows_unwind(savedata_replace_file_failure_tests)
|
||||
add_test(NAME savedata_replace_file_failure COMMAND savedata_replace_file_failure_tests)
|
||||
|
||||
if(MINGW)
|
||||
target_compile_options(savedata_core_write_replacement_tests PRIVATE -fasynchronous-unwind-tables)
|
||||
target_compile_options(savedata_native_write_replacement_tests PRIVATE -fasynchronous-unwind-tables)
|
||||
target_compile_options(savedata_replace_file_failure_tests PRIVATE -fasynchronous-unwind-tables)
|
||||
endif()
|
||||
if(WIN32)
|
||||
set_tests_properties(savedata_core_write_replacement PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceSaveData>")
|
||||
set_tests_properties(savedata_native_write_replacement PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceSaveData.native>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(MINGW)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "SaveData.hpp"
|
||||
#include "prx/libSceSaveData/SaveDataFile.hpp"
|
||||
|
||||
static constexpr char SAVE_DIR[] = "_sd";
|
||||
|
||||
|
|
@ -95,28 +96,7 @@ bool file_size_of(const std::string& path, std::size_t* out) {
|
|||
// Atomic-ish write: write to a temp file then rename over the target, so a kill mid-write never
|
||||
// leaves a torn save behind.
|
||||
bool write_file_replace(const std::string& path, const std::vector<char>& data) {
|
||||
const std::string tmp = path + ".tmp";
|
||||
{
|
||||
std::ofstream f(tmp, std::ios::binary | std::ios::trunc);
|
||||
if (!f) {
|
||||
return false;
|
||||
}
|
||||
if (!data.empty()) {
|
||||
f.write(data.data(), static_cast<std::streamsize>(data.size()));
|
||||
}
|
||||
f.flush();
|
||||
if (!f) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::error_code ec;
|
||||
std::filesystem::rename(tmp, path, ec);
|
||||
if (ec) {
|
||||
std::filesystem::remove(path, ec);
|
||||
ec.clear();
|
||||
std::filesystem::rename(tmp, path, ec);
|
||||
}
|
||||
return !ec;
|
||||
return savedata::replace_file(path, data.data(), data.size());
|
||||
}
|
||||
|
||||
bool read_file_all(const std::string& path, std::vector<char>& out) {
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "prx/libSceSaveData/SaveDataFile.hpp"
|
||||
|
||||
namespace savedata {
|
||||
|
||||
|
|
@ -288,34 +289,7 @@ std::filesystem::path save_blocks_path(const std::filesystem::path& directory) {
|
|||
}
|
||||
|
||||
bool write_blob(const std::filesystem::path& path, const void* data, std::size_t size) {
|
||||
std::error_code error;
|
||||
std::filesystem::create_directories(path.parent_path(), error);
|
||||
auto temporary = path;
|
||||
temporary += ".tmp";
|
||||
{
|
||||
std::ofstream file(temporary, std::ios::binary | std::ios::trunc);
|
||||
if (!file) {
|
||||
return false;
|
||||
}
|
||||
if (size != 0) {
|
||||
file.write(static_cast<const char*>(data), static_cast<std::streamsize>(size));
|
||||
}
|
||||
file.flush();
|
||||
if (!file) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
error.clear();
|
||||
std::filesystem::rename(temporary, path, error);
|
||||
if (error) {
|
||||
std::error_code ignored;
|
||||
std::filesystem::remove(path, ignored);
|
||||
error.clear();
|
||||
std::filesystem::rename(temporary, path, error);
|
||||
}
|
||||
std::error_code leftovers;
|
||||
std::filesystem::remove(temporary, leftovers);
|
||||
return !error;
|
||||
return replace_file(path, data, size);
|
||||
}
|
||||
|
||||
bool read_blob(const std::filesystem::path& path, void* data, std::size_t size) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <system_error>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace savedata {
|
||||
|
||||
template <typename ReplaceOperation>
|
||||
inline bool replace_file_with(const std::filesystem::path& path, const void* data, std::size_t size,
|
||||
ReplaceOperation&& replace_operation) {
|
||||
const auto parent = path.parent_path();
|
||||
std::error_code error;
|
||||
if (!parent.empty()) {
|
||||
std::filesystem::create_directories(parent, error);
|
||||
if (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
auto temporary = path;
|
||||
temporary += ".tmp";
|
||||
const auto remove_temporary = [&temporary]() {
|
||||
std::error_code ignored;
|
||||
std::filesystem::remove(temporary, ignored);
|
||||
};
|
||||
|
||||
{
|
||||
std::ofstream file(temporary, std::ios::binary | std::ios::trunc);
|
||||
if (!file) {
|
||||
remove_temporary();
|
||||
return false;
|
||||
}
|
||||
if (size != 0) {
|
||||
if (data == nullptr) {
|
||||
file.close();
|
||||
remove_temporary();
|
||||
return false;
|
||||
}
|
||||
file.write(static_cast<const char*>(data), static_cast<std::streamsize>(size));
|
||||
}
|
||||
file.flush();
|
||||
if (!file) {
|
||||
file.close();
|
||||
remove_temporary();
|
||||
return false;
|
||||
}
|
||||
file.close();
|
||||
if (!file) {
|
||||
remove_temporary();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const bool replaced = replace_operation(temporary, path);
|
||||
if (!replaced) {
|
||||
remove_temporary();
|
||||
}
|
||||
return replaced;
|
||||
}
|
||||
|
||||
inline bool replace_file(const std::filesystem::path& path, const void* data, std::size_t size) {
|
||||
#ifdef _WIN32
|
||||
return replace_file_with(path, data, size, [](const auto& temporary, const auto& destination) {
|
||||
return ::MoveFileExW(temporary.c_str(), destination.c_str(),
|
||||
MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) != 0;
|
||||
});
|
||||
#else
|
||||
return replace_file_with(path, data, size, [](const auto& temporary, const auto& destination) {
|
||||
std::error_code error;
|
||||
std::filesystem::rename(temporary, destination, error);
|
||||
return !error;
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace savedata
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
#include "prx/libSceSaveData/SaveDataFile.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static void Check(bool value, int line) {
|
||||
if (!value) {
|
||||
std::fprintf(stderr, "Save-data write failure check failed at line %d\n", line);
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
#define Require(value) Check((value), __LINE__)
|
||||
|
||||
int main() {
|
||||
const auto root = std::filesystem::temp_directory_path() /
|
||||
("anyps5-savedata-write-failure-" +
|
||||
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
Require(std::filesystem::create_directory(root));
|
||||
|
||||
const auto savePath = root / "memory.dat";
|
||||
const std::vector<char> oldData{'o', 'l', 'd'};
|
||||
const std::vector<char> replacement{'n', 'e', 'w'};
|
||||
{
|
||||
std::ofstream file(savePath, std::ios::binary);
|
||||
Require(file.is_open());
|
||||
file.write(oldData.data(), static_cast<std::streamsize>(oldData.size()));
|
||||
Require(static_cast<bool>(file));
|
||||
}
|
||||
|
||||
int replaceAttempts = 0;
|
||||
const bool replaced = savedata::replace_file_with(
|
||||
savePath, replacement.data(), replacement.size(),
|
||||
[&replaceAttempts, &savePath](const std::filesystem::path& temporary,
|
||||
const std::filesystem::path& destination) {
|
||||
++replaceAttempts;
|
||||
Require(destination == savePath);
|
||||
auto expectedTemporary = savePath;
|
||||
expectedTemporary += ".tmp";
|
||||
Require(temporary == expectedTemporary);
|
||||
return false;
|
||||
});
|
||||
|
||||
Require(!replaced);
|
||||
Require(replaceAttempts == 1);
|
||||
std::vector<char> savedData;
|
||||
{
|
||||
std::ifstream savedFile(savePath, std::ios::binary);
|
||||
savedData.assign(std::istreambuf_iterator<char>(savedFile), std::istreambuf_iterator<char>());
|
||||
}
|
||||
Require(savedData == oldData);
|
||||
auto temporary = savePath;
|
||||
temporary += ".tmp";
|
||||
Require(!std::filesystem::exists(temporary));
|
||||
|
||||
std::filesystem::remove_all(root);
|
||||
}
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
extern "C" {
|
||||
int APS5_VABI sceSaveDataInitialize3(const void*);
|
||||
int APS5_VABI sceSaveDataTerminate(void);
|
||||
int APS5_VABI sceSaveDataSetupSaveDataMemory2(const SaveDataMemorySetup2*, SaveDataMemorySetupResult*);
|
||||
#ifndef SAVEDATA_NATIVE_BACKEND
|
||||
int APS5_VABI sceSaveDataSyncSaveDataMemory(const void*);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void Check(bool value, int line) {
|
||||
if (!value) {
|
||||
std::fprintf(stderr, "Save-data replacement check failed at line %d\n", line);
|
||||
std::abort();
|
||||
}
|
||||
}
|
||||
#define Require(value) Check((value), __LINE__)
|
||||
|
||||
int main() {
|
||||
const auto originalDirectory = std::filesystem::current_path();
|
||||
const auto root = std::filesystem::temp_directory_path() /
|
||||
("anyps5-savedata-replacement-" +
|
||||
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
Require(std::filesystem::create_directory(root));
|
||||
std::filesystem::current_path(root);
|
||||
|
||||
constexpr std::int32_t userId = 7531;
|
||||
#ifdef SAVEDATA_NATIVE_BACKEND
|
||||
const auto savePath = std::filesystem::path("_sd_mem") / ("u" + std::to_string(userId)) / "slot0.bin";
|
||||
#else
|
||||
const auto savePath = std::filesystem::path("_sd") / "sce_sdmemory" /
|
||||
std::to_string(userId) / "memory.dat";
|
||||
#endif
|
||||
std::filesystem::create_directories(savePath.parent_path());
|
||||
const std::vector<char> oldData{'o', 'l', 'd'};
|
||||
{
|
||||
std::ofstream file(savePath, std::ios::binary);
|
||||
Require(file.is_open());
|
||||
file.write(oldData.data(), static_cast<std::streamsize>(oldData.size()));
|
||||
Require(static_cast<bool>(file));
|
||||
}
|
||||
|
||||
Require(sceSaveDataInitialize3(nullptr) == 0);
|
||||
SaveDataMemorySetup2 setup{};
|
||||
setup.user_id = userId;
|
||||
setup.memory_size = oldData.size() + 3;
|
||||
SaveDataMemorySetupResult result{};
|
||||
int status = sceSaveDataSetupSaveDataMemory2(&setup, &result);
|
||||
|
||||
#ifndef SAVEDATA_NATIVE_BACKEND
|
||||
struct MemorySyncParam {
|
||||
std::int32_t user_id;
|
||||
std::uint32_t slot_id;
|
||||
std::uint32_t option;
|
||||
} sync{userId, 0, 0};
|
||||
if (status == 0) {
|
||||
status = sceSaveDataSyncSaveDataMemory(&sync);
|
||||
}
|
||||
#endif
|
||||
|
||||
Require(result.existed_memory_size == oldData.size());
|
||||
Require(status == 0);
|
||||
std::vector<char> savedData;
|
||||
{
|
||||
std::ifstream savedFile(savePath, std::ios::binary);
|
||||
savedData.assign(std::istreambuf_iterator<char>(savedFile), std::istreambuf_iterator<char>());
|
||||
}
|
||||
Require(savedData.size() == setup.memory_size);
|
||||
Require(std::equal(oldData.begin(), oldData.end(), savedData.begin()));
|
||||
Require(std::all_of(savedData.begin() + static_cast<std::ptrdiff_t>(oldData.size()),
|
||||
savedData.end(), [](char byte) { return byte == 0; }));
|
||||
|
||||
#ifdef SAVEDATA_NATIVE_BACKEND
|
||||
Require(sceSaveDataTerminate() == 0);
|
||||
#endif
|
||||
std::filesystem::current_path(originalDirectory);
|
||||
std::filesystem::remove_all(root);
|
||||
}
|
||||
Loading…
Reference in New Issue