diff --git a/core/libs/prx/libSceSaveData.native/Export.cpp b/core/libs/prx/libSceSaveData.native/Export.cpp index 785ec69ca..f58ae05f0 100644 --- a/core/libs/prx/libSceSaveData.native/Export.cpp +++ b/core/libs/prx/libSceSaveData.native/Export.cpp @@ -228,6 +228,23 @@ static int deleteSave(const SaveDataDelete* del) { return SAVE_DATA_ERROR_PARAMETER; } const std::string path = save_root() + "/" + dirName; + std::lock_guard lock(g_slots_mutex); + for (const auto& slot : g_slots) { + if (!slot.used) { + continue; + } + if (slot.real_path == path) { + return SAVE_DATA_ERROR_BUSY; + } + std::error_code ec; + const bool same_directory = std::filesystem::equivalent(slot.real_path, path, ec); + if (ec && ec != std::errc::no_such_file_or_directory) { + throw std::filesystem::filesystem_error("sceSaveDataDelete: failed to compare save paths", slot.real_path, path, ec); + } + if (same_directory) { + return SAVE_DATA_ERROR_BUSY; + } + } if (std::filesystem::is_directory(path)) { std::filesystem::remove_all(path); } diff --git a/core/libs/tests/GuestSaveDataDelete.cpp b/core/libs/tests/GuestSaveDataDelete.cpp index 8dbee512d..71b19c03d 100644 --- a/core/libs/tests/GuestSaveDataDelete.cpp +++ b/core/libs/tests/GuestSaveDataDelete.cpp @@ -1,18 +1,33 @@ #include "prx/libc/include/general/VabiMacros.hpp" +#include "prx/libc/include/FileStream.hpp" #include "SceTypes.hpp" +#include #include #include #include #include #include +#include #include #include +#include +#include -extern "C" int APS5_VABI sceSaveDataDelete(const SaveDataDelete*); +extern "C" { +int APS5_VABI sceSaveDataDelete(const SaveDataDelete*); +int APS5_VABI sceSaveDataInitialize3(const void*); +int APS5_VABI sceSaveDataMount3(const SaveDataMount3*, SaveDataMountResult*); +int APS5_VABI sceSaveDataUmount2(std::uint32_t, const SaveDataMountPoint*); +FileStream* APS5_VABI fopen_nid_postfix(const char*, const char*); +std::size_t APS5_VABI fread_nid_postfix(void*, std::size_t, std::size_t, FileStream*); +std::size_t APS5_VABI fwrite_nid_postfix(const void*, std::size_t, std::size_t, FileStream*); +int APS5_VABI fclose_nid_postfix(FileStream*); +} namespace { constexpr int SaveDataErrorParameter = -2137063424; +constexpr int SaveDataErrorBusy = -2137063421; int failures = 0; @@ -35,6 +50,26 @@ void Write(const std::filesystem::path& path) { std::ofstream(path) << "data"; } +std::vector Read(const std::filesystem::path& path) { + std::ifstream stream(path, std::ios::binary); + if (!stream) return {}; + return {std::istreambuf_iterator(stream), std::istreambuf_iterator()}; +} + +bool WriteMountedFile(const char* payload, std::size_t size) { + auto* stream = fopen_nid_postfix("/savedata0/data.bin", "wb"); + if (stream == nullptr) return false; + const bool written = fwrite_nid_postfix(payload, 1, size, stream) == size; + return fclose_nid_postfix(stream) == 0 && written; +} + +bool ReadMountedFile(std::array& buffer, std::size_t expectedSize) { + auto* stream = fopen_nid_postfix("/savedata0/data.bin", "rb"); + if (stream == nullptr) return false; + const bool read = fread_nid_postfix(buffer.data(), 1, expectedSize, stream) == expectedSize; + return fclose_nid_postfix(stream) == 0 && read; +} + } int main() { @@ -55,6 +90,44 @@ int main() { std::memset(unterminated, 'a', sizeof(unterminated)); Check(Delete(unterminated, sizeof(unterminated)) == SaveDataErrorParameter, "an unterminated name is rejected"); + constexpr char mountedPayload[] = "live mounted save"; + constexpr char updatedPayload[] = "still mounted"; + Check(sceSaveDataInitialize3(nullptr) == 0, "SaveData initializes"); + SceSaveDataDirName dirName{}; + std::memcpy(dirName.data, "kept", sizeof("kept")); + SaveDataMount3 mount{}; + mount.dir_name = &dirName; + mount.mount_mode = 2; + SaveDataMountResult mountResult{}; + const int mountResultCode = sceSaveDataMount3(&mount, &mountResult); + Check(mountResultCode == 0, "the existing save mounts read/write"); + if (mountResultCode == 0) { + Check(WriteMountedFile(mountedPayload, sizeof(mountedPayload)), "the guest alias writes the mounted save"); + std::error_code caseAliasError; + const bool caseAlias = std::filesystem::equivalent(kept.parent_path(), work / "_sd" / "Kept", caseAliasError); + if (caseAliasError && caseAliasError != std::errc::no_such_file_or_directory) { + Check(false, "case-alias host identity probe has no unexpected filesystem error"); + } else if (caseAlias) { + std::printf("savedata delete case-alias control: host paths are equivalent\n"); + Check(Delete("Kept", sizeof("Kept")) == SaveDataErrorBusy, "case-variant mounted delete returns BUSY"); + Check(Read(kept) == std::vector(mountedPayload, mountedPayload + sizeof(mountedPayload)), "case-variant mounted delete preserves backing data"); + std::array caseAliasBuffer{}; + const bool readCaseAlias = ReadMountedFile(caseAliasBuffer, sizeof(mountedPayload)); + Check(readCaseAlias && std::memcmp(caseAliasBuffer.data(), mountedPayload, sizeof(mountedPayload)) == 0, "case-variant mounted delete preserves the live guest alias"); + } else { + std::printf("savedata delete case-alias control: host paths are distinct\n"); + } + const int deleteResult = Delete("kept", sizeof("kept")); + Check(deleteResult == SaveDataErrorBusy, "mounted delete returns BUSY"); + Check(Read(kept) == std::vector(mountedPayload, mountedPayload + sizeof(mountedPayload)), "mounted data remains on disk after delete"); + std::array buffer{}; + const bool readMountedData = ReadMountedFile(buffer, sizeof(mountedPayload)); + Check(readMountedData && std::memcmp(buffer.data(), mountedPayload, sizeof(mountedPayload)) == 0, "mounted data remains accessible through /savedata0"); + Check(WriteMountedFile(updatedPayload, sizeof(updatedPayload)), "the live /savedata0 alias remains writable after delete"); + Check(Read(kept) == std::vector(updatedPayload, updatedPayload + sizeof(updatedPayload)), "writes through /savedata0 remain attached to the save"); + Check(sceSaveDataUmount2(0, &mountResult.mount_point) == 0, "the mounted save unmounts"); + } + Check(Delete("kept", 5) == 0, "a valid name is deleted"); Check(!std::filesystem::exists(kept.parent_path()), "the valid save directory is gone"); Check(std::filesystem::exists(victim), "the directory beside the save root is untouched");