libSaveData: persist save metadata so saved games can be loaded #434

Closed
github-bot wants to merge 1 commit from gh-pr/434/head into gh-pr/434/base
Owner

@⁠CheesyPoofs346 on GitHub · 2026-08-31 22:20 UTC · original
CheesyPoofs346:fix/savedata-param-persistence → main · +75 −5 · 2 files · 1 commits · closed without merging

Summary

  • Persist save metadata (title, subtitle, detail, mtime) so a title can find and load its own saves.
  • SaveDataSetParam discarded what it was given, SaveDataGetParam always returned zeros, and SaveDataDirNameSearch blanked the params of every save it listed.

Problem

SaveDataSetParam logged the parameters and returned OK without storing them:

if (param_type == 0) {
    const auto* p = static_cast<const SaveDataParam*>(param_buf);
    LOGF("\t title      = %s\n" ... , p->title, p->sub_title, p->detail, p->user_param);
}
return OK;

SaveDataGetParam zeroed its output regardless of what was on disk:

std::memset(param_buf, 0, sizeof(SaveDataParam));
return OK;

And SaveDataDirNameSearch found the save directories correctly but cleared their params:

if (result->params != nullptr) {
    result->params[i] = {};
}

So the metadata round-trip was entirely missing. A game enumerating its saves received a valid directory name with an empty title and mtime = 0, which is indistinguishable from an empty slot.

Observed in Grand Theft Auto V (PPSA04264): the game wrote a valid 565 KB save to SAVEDATASGTA50000 and mounted it successfully, but on the next launch the load menu did not offer it and starting the game began a new playthrough. The save data itself was intact the whole time — only its description was lost.

This change stores the SaveDataParam next to the save data as sce_param.bin, reads it back in SaveDataGetParam, and fills it in for each entry returned by SaveDataDirNameSearch. mtime is set from the wall clock when a title does not supply one. Saves written before this change have no metadata file; the search still lists them, and they gain metadata on the next save.

SaveDataMountSlots gains a Directory() accessor so a mount point can be resolved back to the directory its metadata belongs in.

Validation

  • Built on Windows with clang-cl/Ninja; the existing test executable passes.
  • Verified in GTA5: with this change the save is offered in the load menu and loads to the correct position, where previously the same save data started a new game.

AI assistance

AI assistance was used for this change.

**@⁠CheesyPoofs346** on GitHub · 2026-08-31 22:20 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434) `CheesyPoofs346:fix/savedata-param-persistence` → `main` · +75 −5 · 2 files · 1 commits · closed without merging ## Summary - Persist save metadata (title, subtitle, detail, mtime) so a title can find and load its own saves. - `SaveDataSetParam` discarded what it was given, `SaveDataGetParam` always returned zeros, and `SaveDataDirNameSearch` blanked the params of every save it listed. ## Problem `SaveDataSetParam` logged the parameters and returned `OK` without storing them: ```cpp if (param_type == 0) { const auto* p = static_cast<const SaveDataParam*>(param_buf); LOGF("\t title = %s\n" ... , p->title, p->sub_title, p->detail, p->user_param); } return OK; ``` `SaveDataGetParam` zeroed its output regardless of what was on disk: ```cpp std::memset(param_buf, 0, sizeof(SaveDataParam)); return OK; ``` And `SaveDataDirNameSearch` found the save directories correctly but cleared their params: ```cpp if (result->params != nullptr) { result->params[i] = {}; } ``` So the metadata round-trip was entirely missing. A game enumerating its saves received a valid directory name with an empty title and `mtime = 0`, which is indistinguishable from an empty slot. Observed in Grand Theft Auto V (PPSA04264): the game wrote a valid 565 KB save to `SAVEDATASGTA50000` and mounted it successfully, but on the next launch the load menu did not offer it and starting the game began a new playthrough. The save data itself was intact the whole time — only its description was lost. This change stores the `SaveDataParam` next to the save data as `sce_param.bin`, reads it back in `SaveDataGetParam`, and fills it in for each entry returned by `SaveDataDirNameSearch`. `mtime` is set from the wall clock when a title does not supply one. Saves written before this change have no metadata file; the search still lists them, and they gain metadata on the next save. `SaveDataMountSlots` gains a `Directory()` accessor so a mount point can be resolved back to the directory its metadata belongs in. ## Validation - Built on Windows with clang-cl/Ninja; the existing test executable passes. - Verified in GTA5: with this change the save is offered in the load menu and loads to the correct position, where previously the same save data started a new game. ## AI assistance AI assistance was used for this change.
SaveDataSetParam logged the title, subtitle, detail and mtime and discarded them. SaveDataGetParam zeroed its output buffer unconditionally. SaveDataDirNameSearch listed each save directory but blanked its params. A title asking what saves exist therefore received a directory name with an empty title and a zero mtime, which reads as an empty slot. Store the params beside the save data as sce_param.bin, read them back in GetParam, and populate them during the directory search.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:20:42 +00:00
github-bot closed this pull request 2026-11-26 09:20:42 +00:00
@ -345,0 +351,4 @@
return Common::FixDirectorySlash(std::string(SAVE_DATA_DIR) + "/" + get_title_id() + "/" +
dir_name) +
"/sce_param.bin";
}
Author
Owner

@⁠Copilot on GitHub · 2026-08-31 22:24 UTC · original

param_file_path() currently produces a path with a double slash (FixDirectorySlash() already appends '/', then the code adds "/sce_param.bin"). While most filesystems tolerate it, it’s easy to avoid and keeps paths consistent.

**@⁠Copilot** on GitHub · 2026-08-31 22:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#discussion_r3898952497) param_file_path() currently produces a path with a double slash (FixDirectorySlash() already appends '/', then the code adds "/sce_param.bin"). While most filesystems tolerate it, it’s easy to avoid and keeps paths consistent.
@ -322,2 +324,3 @@
return SAVE_DATA_ERROR_BUSY;
::printf("SaveDataMount3: refused (%s)\n", "SAVE_DATA_ERROR_BUSY");
return SAVE_DATA_ERROR_BUSY;
}
Author
Owner

@⁠Copilot on GitHub · 2026-08-31 22:24 UTC · original

SaveDataTerminate logs "SaveDataMount3" on the busy path, which is misleading and makes debugging harder. Also the return line is misindented compared to the surrounding style.

**@⁠Copilot** on GitHub · 2026-08-31 22:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#discussion_r3898952528) SaveDataTerminate logs "SaveDataMount3" on the busy path, which is misleading and makes debugging harder. Also the return line is misindented compared to the surrounding style.
@ -454,2 +500,3 @@
return SAVE_DATA_ERROR_MOUNT_FULL;
::printf("SaveDataMount3: refused (%s)\n", "SAVE_DATA_ERROR_MOUNT_FULL");
return SAVE_DATA_ERROR_MOUNT_FULL;
}
Author
Owner

@⁠Copilot on GitHub · 2026-08-31 22:24 UTC · original

The return statement in this error path is misindented, which makes it look like it might not be part of the if-block. Please indent consistently to avoid confusion during future edits.

This issue also appears in the following locations of the same file:

  • line 508
  • line 513
**@⁠Copilot** on GitHub · 2026-08-31 22:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#discussion_r3898952553) The return statement in this error path is misindented, which makes it look like it might not be part of the if-block. Please indent consistently to avoid confusion during future edits. This issue also appears in the following locations of the same file: - line 508 - line 513
@ -718,0 +774,4 @@
(void)read_save_param(g_mount_slots.Directory(static_cast<size_t>(slot)),
static_cast<SaveDataParam*>(param_buf));
}
}
Author
Owner

@⁠Copilot on GitHub · 2026-08-31 22:24 UTC · original

SaveDataGetParam holds g_mount_mutex while doing disk I/O via read_save_param(). If the filesystem stalls, this can block unrelated mount/umount operations. Consider copying the directory string under the lock, then releasing the lock before reading the metadata file.

**@⁠Copilot** on GitHub · 2026-08-31 22:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#discussion_r3898952570) SaveDataGetParam holds g_mount_mutex while doing disk I/O via read_save_param(). If the filesystem stalls, this can block unrelated mount/umount operations. Consider copying the directory string under the lock, then releasing the lock before reading the metadata file.
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-08-31 22:24 UTC · original · reviewed

Pull request overview

This PR implements persistence for save metadata in libSaveData so that titles can enumerate saves and display/load them correctly across restarts, by storing and reloading SaveDataParam data from disk.

Changes:

  • Add binary persistence for SaveDataParam (read/write sce_param.bin) and populate it in SaveDataGetParam and SaveDataDirNameSearch.
  • Persist parameters provided to SaveDataSetParam and synthesize mtime when missing.
  • Add SaveDataMountSlots::Directory() to resolve a mount point back to its source save directory.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/libs/saveDataMountSlots.h Adds a directory accessor for mounted slots to support metadata persistence.
src/libs/libSaveData.cpp Implements read/write of SaveDataParam to disk and wires it into search/get/set flows (plus extra mount diagnostics).
Suppressed comments (2)

src/libs/libSaveData.cpp:511

  • The return statement in this error path is misindented, which makes the control flow harder to read and easier to break during future edits.
	if (open && !Common::File::IsDirectoryExisting(mount_dir)) {
		::printf("SaveDataMount3: refused (%s)\n", "SAVE_DATA_ERROR_NOT_FOUND");
	return SAVE_DATA_ERROR_NOT_FOUND;
	}

src/libs/libSaveData.cpp:516

  • The return statement in this error path is misindented; please keep indentation consistent so the if-block structure is unambiguous.
	if (create && Common::File::IsDirectoryExisting(mount_dir)) {
		::printf("SaveDataMount3: refused (%s)\n", "SAVE_DATA_ERROR_EXISTS");
	return SAVE_DATA_ERROR_EXISTS;
	}

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-08-31 22:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#pullrequestreview-5071950097) · **reviewed** ## Pull request overview This PR implements persistence for save metadata in `libSaveData` so that titles can enumerate saves and display/load them correctly across restarts, by storing and reloading `SaveDataParam` data from disk. **Changes:** - Add binary persistence for `SaveDataParam` (read/write `sce_param.bin`) and populate it in `SaveDataGetParam` and `SaveDataDirNameSearch`. - Persist parameters provided to `SaveDataSetParam` and synthesize `mtime` when missing. - Add `SaveDataMountSlots::Directory()` to resolve a mount point back to its source save directory. ### Reviewed changes Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments. | File | Description | | ---- | ----------- | | src/libs/saveDataMountSlots.h | Adds a directory accessor for mounted slots to support metadata persistence. | | src/libs/libSaveData.cpp | Implements read/write of `SaveDataParam` to disk and wires it into search/get/set flows (plus extra mount diagnostics). | <details> <summary>Suppressed comments (2)</summary> **src/libs/libSaveData.cpp:511** * The return statement in this error path is misindented, which makes the control flow harder to read and easier to break during future edits. ``` if (open && !Common::File::IsDirectoryExisting(mount_dir)) { ::printf("SaveDataMount3: refused (%s)\n", "SAVE_DATA_ERROR_NOT_FOUND"); return SAVE_DATA_ERROR_NOT_FOUND; } ``` **src/libs/libSaveData.cpp:516** * The return statement in this error path is misindented; please keep indentation consistent so the if-block structure is unambiguous. ``` if (create && Common::File::IsDirectoryExisting(mount_dir)) { ::printf("SaveDataMount3: refused (%s)\n", "SAVE_DATA_ERROR_EXISTS"); return SAVE_DATA_ERROR_EXISTS; } ``` </details> --- 💡 <a href="/KytyPS5/KytyPS5/new/main?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a `code-review` agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
@ -840,0 +900,4 @@
Common::LockGuard lock(g_mount_mutex);
const int slot = g_mount_slots.Find(mount_point->data);
if (slot >= 0) {
write_save_param(g_mount_slots.Directory(static_cast<size_t>(slot)), *p);
Author
Owner

@⁠Copilot on GitHub · 2026-08-31 22:24 UTC · original

SaveDataSetParam persists *p to disk but still doesn’t validate param_buf/param_buf_size before dereferencing it (both for logging and for write_save_param). Also, doing file I/O while holding g_mount_mutex can block other save/mount operations if the filesystem is slow. Validate the buffer and copy the directory under the lock, then write after releasing it.

**@⁠Copilot** on GitHub · 2026-08-31 22:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#discussion_r3898952598) SaveDataSetParam persists *p to disk but still doesn’t validate param_buf/param_buf_size before dereferencing it (both for logging and for write_save_param). Also, doing file I/O while holding g_mount_mutex can block other save/mount operations if the filesystem is slow. Validate the buffer and copy the directory under the lock, then write after releasing it.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-08-31 22:27 UTC · original · reviewed

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 279573b5d5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback".

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-08-31 22:27 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#pullrequestreview-5071974550) · **reviewed** ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `279573b5d5` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback". </details>
@ -345,0 +377,4 @@
}
std::ofstream file(param_file_path(dir_name), std::ios::binary | std::ios::trunc);
if (file.is_open()) {
file.write(reinterpret_cast<const char*>(&stored), sizeof(stored));
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-08-31 22:27 UTC · original

P1 Badge Replace metadata atomically and propagate write failures

When the host filesystem is full, read-only, or a write is interrupted, opening with trunc can destroy the previously valid metadata and file.write may leave a short file, yet write_save_param discards the stream state and SaveDataSetParam still returns OK. On the next launch, read_save_param rejects that file and callers receive a zeroed parameter, making an otherwise intact save inaccessible. Write a temporary file, verify the complete write, atomically replace the old file, and return an appropriate save-data error on failure.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-08-31 22:27 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#discussion_r3898973080) **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Replace metadata atomically and propagate write failures** When the host filesystem is full, read-only, or a write is interrupted, opening with `trunc` can destroy the previously valid metadata and `file.write` may leave a short file, yet `write_save_param` discards the stream state and `SaveDataSetParam` still returns `OK`. On the next launch, `read_save_param` rejects that file and callers receive a zeroed parameter, making an otherwise intact save inaccessible. Write a temporary file, verify the complete write, atomically replace the old file, and return an appropriate save-data error on failure. Useful? React with 👍 / 👎.
@ -345,0 +373,4 @@
static void write_save_param(const std::string& dir_name, const SaveDataParam& param) {
SaveDataParam stored = param;
if (stored.mtime == 0) {
stored.mtime = static_cast<int64_t>(::time(nullptr));
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-08-31 22:27 UTC · original

P2 Badge Use RTC ticks when synthesizing mtime

When a title supplies mtime == 0, this stores Unix time_t seconds directly in the save-data mtime field, whose guest ABI uses an RTC tick value with a different epoch and microsecond units. Games that convert the returned value to a calendar date will therefore see a timestamp near the RTC epoch, and comparisons with title-supplied RTC ticks will place these saves incorrectly. Generate the timestamp in the guest RTC representation instead of copying Unix seconds.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-08-31 22:27 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#discussion_r3898973089) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Use RTC ticks when synthesizing mtime** When a title supplies `mtime == 0`, this stores Unix `time_t` seconds directly in the save-data `mtime` field, whose guest ABI uses an RTC tick value with a different epoch and microsecond units. Games that convert the returned value to a calendar date will therefore see a timestamp near the RTC epoch, and comparisons with title-supplied RTC ticks will place these saves incorrectly. Generate the timestamp in the guest RTC representation instead of copying Unix seconds. Useful? React with 👍 / 👎.
@ -345,0 +350,4 @@
static std::string param_file_path(const std::string& dir_name) {
return Common::FixDirectorySlash(std::string(SAVE_DATA_DIR) + "/" + get_title_id() + "/" +
dir_name) +
"/sce_param.bin";
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-08-31 22:27 UTC · original

P2 Badge Keep emulator metadata outside the guest mount

This path places sce_param.bin inside the same directory that mount_save_data exposes wholesale as /savedataN. Consequently, titles that enumerate, checksum, copy, clear, or create all files in their save mount can observe or overwrite the emulator's private metadata; deleting or replacing it makes the next search return blank parameters again. Store the control file outside the mounted directory, or explicitly hide and protect it in the filesystem layer.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-08-31 22:27 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#discussion_r3898973095) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Keep emulator metadata outside the guest mount** This path places `sce_param.bin` inside the same directory that `mount_save_data` exposes wholesale as `/savedataN`. Consequently, titles that enumerate, checksum, copy, clear, or create all files in their save mount can observe or overwrite the emulator's private metadata; deleting or replacing it makes the next search return blank parameters again. Store the control file outside the mounted directory, or explicitly hide and protect it in the filesystem layer. Useful? React with 👍 / 👎.
Author
Owner

@⁠Nmzik on GitHub · 2026-09-23 18:51 UTC · original

Credited

**@⁠Nmzik** on GitHub · 2026-09-23 18:51 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/434#issuecomment-5800916392) Credited

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.