xgameruntime_gamesave: copy blobs with CopyFileW, not CopyFile2 (RT-GAMESAVE-COPY-NO-COPYFILE2) #2532

Merged
Tyrannys merged 2 commits from upstream-pr/2532 into main 2026-11-28 23:25:17 +00:00
Tyrannys commented 2026-11-28 01:08:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T01:08:48Z.


Claimed in the coordination issue as: RT-GAMESAVE-COPY-NO-COPYFILE2 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231450902)

Depends on: none, but touches copy_blob() as #2003 does (#2003 already conflicts with main)

What and why

This fixes item 1 of kevincommits' batch 18 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231277215).

copy_blob() in runtime/shims/xgameruntime_gamesave.cpp has two jobs:

  • It backs up a blob before a commit overwrites it.
  • It copies the blob back when an update is rolled back.

It used std::filesystem::copy_file, which in MSVC calls CopyFile2. Wine 9 and the Protons built on it do not implement CopyFile2, and the call aborts the process ("unimplemented function KERNEL32.dll.CopyFile2, aborting"). So the first save that overwrote a blob would kill the title there. file_system.cpp, pe_loader.cpp and three tests already avoid CopyFile2 for the same reason.

copy_blob() now calls CopyFileW(from, to, FALSE). On Windows it copies the same way as before: contents, attributes and the last-write time, replacing an existing file. That last part also covers what #2003 adds to copy_blob (keeping the source's last-write time).

Tested

xgameruntime_gamesave_test gains a case that patches this process's CopyFile2 to fail and count its calls, the way Wine's stub would. It then runs:

  • an update that overwrites a blob, which needs the backup copy;
  • an update that fails and is rolled back, which needs the copy back from the backup.

Both must work, with zero CopyFile2 calls. The patch is removed afterwards.

  • With main's copy_blob, 3 checks fail. With the fix, 96/96 pass.
  • ctest -R xgameruntime: 8/8 pass.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU-only test; not run under Wine)

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5535228317 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532). Originally posted by [Tyrannys](https://github.com/Tyrannys) on 2026-11-28T01:08:48Z. --- Claimed in the coordination issue as: RT-GAMESAVE-COPY-NO-COPYFILE2 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231450902) Depends on: none, but touches `copy_blob()` as #2003 does (#2003 already conflicts with `main`) ### What and why This fixes item 1 of kevincommits' batch 18 in #1608 (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7231277215). `copy_blob()` in `runtime/shims/xgameruntime_gamesave.cpp` has two jobs: - It backs up a blob before a commit overwrites it. - It copies the blob back when an update is rolled back. It used `std::filesystem::copy_file`, which in MSVC calls `CopyFile2`. Wine 9 and the Protons built on it do not implement `CopyFile2`, and the call aborts the process ("unimplemented function KERNEL32.dll.CopyFile2, aborting"). So the first save that overwrote a blob would kill the title there. `file_system.cpp`, `pe_loader.cpp` and three tests already avoid `CopyFile2` for the same reason. `copy_blob()` now calls `CopyFileW(from, to, FALSE)`. On Windows it copies the same way as before: contents, attributes and the last-write time, replacing an existing file. That last part also covers what #2003 adds to `copy_blob` (keeping the source's last-write time). ### Tested `xgameruntime_gamesave_test` gains a case that patches this process's `CopyFile2` to fail and count its calls, the way Wine's stub would. It then runs: - an update that overwrites a blob, which needs the backup copy; - an update that fails and is rolled back, which needs the copy back from the backup. Both must work, with zero `CopyFile2` calls. The patch is removed afterwards. - With main's `copy_blob`, 3 checks fail. With the fix, 96/96 pass. - `ctest -R xgameruntime`: 8/8 pass. GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 (CPU-only test; not run under Wine) AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
timbot103 commented 2026-11-28 02:01:38 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T02:01:38Z.


Reviewed at head 03d17a395 on Windows 11 (MSVC 2022 19.44).

Verification

  • Built xgameruntime_gamesave_test.
  • xgameruntime_gamesave_test: 96/96 checks passed on Windows 11.

Finding (Request changes)

  • Case E unconditionally fails on Wine 9 where CopyFile2 is absent (xgameruntime_gamesave_test.cpp:87-95, 112, 537-538):
    The production switch to CopyFileW for backup and restore (xgameruntime_gamesave.cpp:488-506, 568-586) is clean and preserves rollback/backup-retention behavior. However, in xgameruntime_gamesave_test.cpp:537-538, CopyFile2Trap leaves itself unarmed when GetProcAddress does not find CopyFile2, and Case E unconditionally fails check(trap.armed(), "CopyFile2 is trapped"). Because Wine 9 lacks the CopyFile2 export — the exact reason for this PR — the regression test will fail on Wine 9 even when both blob operations succeed. Treating a missing CopyFile2 export as expected on Wine (distinguishing export-absent from patch-failed) and still running the overwrite + rollback functional checks fixes this cleanly.

AI-assisted review + local MSVC verification.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232078074 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532#issuecomment-7232078074). Originally posted by [timbot103](https://github.com/timbot103) on 2026-11-28T02:01:38Z. --- Reviewed at head `03d17a395` on Windows 11 (MSVC 2022 `19.44`). ### Verification - Built `xgameruntime_gamesave_test`. - `xgameruntime_gamesave_test`: **96/96 checks passed** on Windows 11. ### Finding (Request changes) - **Case E unconditionally fails on Wine 9 where `CopyFile2` is absent (`xgameruntime_gamesave_test.cpp:87-95`, `112`, `537-538`):** The production switch to `CopyFileW` for backup and restore (`xgameruntime_gamesave.cpp:488-506`, `568-586`) is clean and preserves rollback/backup-retention behavior. However, in `xgameruntime_gamesave_test.cpp:537-538`, `CopyFile2Trap` leaves itself unarmed when `GetProcAddress` does not find `CopyFile2`, and Case E unconditionally fails `check(trap.armed(), "CopyFile2 is trapped")`. Because Wine 9 lacks the `CopyFile2` export — the exact reason for this PR — the regression test will fail on Wine 9 even when both blob operations succeed. Treating a missing `CopyFile2` export as expected on Wine (distinguishing export-absent from patch-failed) and still running the overwrite + rollback functional checks fixes this cleanly. AI-assisted review + local MSVC verification.
Tyrannys commented 2026-11-28 02:06:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T02:06:04Z.


Thanks @timbot103, fixed in 842e0f641. CopyFile2Trap now tells a missing export apart from a patch that failed:

  • No CopyFile2 export (Wine 9): the trap records that and stays unarmed. Case E prints a note and still runs the overwrite and rollback checks against the real host. With no export, nothing can call CopyFile2, so the zero-calls check holds trivially.
  • Export present but the patch failed: this still fails the check, now named CopyFile2 is trapped, or the host has none.

On Windows 11, all 96 checks pass, with the trap armed. I have no Wine 9 setup here, so the absent-export path was not run. AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232130595 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532#issuecomment-7232130595). Originally posted by [Tyrannys](https://github.com/Tyrannys) on 2026-11-28T02:06:04Z. --- Thanks @timbot103, fixed in 842e0f641. `CopyFile2Trap` now tells a missing export apart from a patch that failed: - **No `CopyFile2` export (Wine 9):** the trap records that and stays unarmed. Case E prints a note and still runs the overwrite and rollback checks against the real host. With no export, nothing can call `CopyFile2`, so the zero-calls check holds trivially. - **Export present but the patch failed:** this still fails the check, now named `CopyFile2 is trapped, or the host has none`. On Windows 11, all 96 checks pass, with the trap armed. I have no Wine 9 setup here, so the absent-export path was not run. AI-assisted (Claude Code).
zmasi commented 2026-11-28 07:05:25 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T07:05:25Z.


#2532 | head 842e0f641c (merged cleanly with main 09c1539483) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^xgameruntime_gamesave$" --output-on-failure | red on main: xgameruntime_gamesave.cpp calls std::filesystem::copy_file, importing MSVCP140.dll!__std_fs_copy_file (note: on Windows 11 with VC++ runtime 14.44, __std_fs_copy_file uses CreateFileW + read/write rather than CopyFile2, whereas pre-14.38 msvcp140.dll / Wine 9 redistributables route __std_fs_copy_file to CopyFile2 via api-ms-win-core-file-l1-2-0 -> KernelBase.dll!CopyFile2) | green on PR+main: 1/1 CTest passes (xgameruntime_gamesave 96/96 checks in 0.34s, 0 CopyFile2 calls under both kernel32.dll!CopyFile2 and KernelBase.dll!CopyFile2 traps, and dumpbin /imports confirms __std_fs_copy_file is gone in favor of KERNEL32.dll!CopyFileW); check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7235708582 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532#issuecomment-7235708582). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T07:05:25Z. --- #2532 | head 842e0f641c2a0a170c21983bf7689ec15cbc48bf (merged cleanly with main 09c153948386fd658fe2f999f6176efbaa8c8345) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^xgameruntime_gamesave$" --output-on-failure` | red on main: `xgameruntime_gamesave.cpp` calls `std::filesystem::copy_file`, importing `MSVCP140.dll!__std_fs_copy_file` (note: on Windows 11 with VC++ runtime `14.44`, `__std_fs_copy_file` uses `CreateFileW` + read/write rather than `CopyFile2`, whereas pre-`14.38` `msvcp140.dll` / Wine 9 redistributables route `__std_fs_copy_file` to `CopyFile2` via `api-ms-win-core-file-l1-2-0` -> `KernelBase.dll!CopyFile2`) | green on PR+main: `1/1` CTest passes (`xgameruntime_gamesave` `96/96` checks in `0.34s`, `0` `CopyFile2` calls under both `kernel32.dll!CopyFile2` and `KernelBase.dll!CopyFile2` traps, and `dumpbin /imports` confirms `__std_fs_copy_file` is gone in favor of `KERNEL32.dll!CopyFileW`); `check_no_game_data.py` `0` violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes
timbot103 commented 2026-11-28 14:36:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T14:36:34Z.

Review: COMMENTED


Static code audit against 842e0f641 + ec740bcc3.

Major — runtime/shims/xgameruntime_gamesave.cpp:492. Wine 9's CopyFileW returns success after a source read error once the destination has been opened. During rollback, that can leave the live blob truncated and cause cleanup to delete the intact backup (runtime/shims/xgameruntime_gamesave.cpp:502, runtime/shims/xgameruntime_gamesave.cpp:520). A write error can also leave a partial destination. Wine 9 copy implementation

Please use a checked copy to a sibling staging file, then replace the destination only after the copy completes. Case E covers overwrite and rollback after a blocked update. A copy fault test should also assert that an incomplete restore leaves the backup intact.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6098418726 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532#pullrequestreview-6098418726). Originally posted by [timbot103](https://github.com/timbot103) on 2026-11-28T14:36:34Z. **Review:** COMMENTED --- Static code audit against `842e0f641` + `ec740bcc3`. **Major** — `runtime/shims/xgameruntime_gamesave.cpp:492`. Wine 9's `CopyFileW` returns success after a source read error once the destination has been opened. During rollback, that can leave the live blob truncated and cause cleanup to delete the intact backup (`runtime/shims/xgameruntime_gamesave.cpp:502`, `runtime/shims/xgameruntime_gamesave.cpp:520`). A write error can also leave a partial destination. [Wine 9 copy implementation](https://github.com/wine-mirror/wine/blob/wine-9.0/dlls/kernelbase/file.c#L559-L599) Please use a checked copy to a sibling staging file, then replace the destination only after the copy completes. Case E covers overwrite and rollback after a blocked update. A copy fault test should also assert that an incomplete restore leaves the backup intact. AI-assisted.
lixonik commented 2026-11-28 20:10:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T20:10:30Z.

Review: COMMENTED


Reviewed and locally tested 842e0f641 (runtime/shims/xgameruntime_gamesave.cpp, runtime/test/xgameruntime_gamesave_test.cpp).

Local verification:

  • Built xgameruntime_gamesave_test (MSVC Release / Ninja) and ran ctest -R ^xgameruntime_gamesave$: 1/1 passed.
  • python -I scripts/check_no_game_data.py: passed.

Switching away from std::filesystem::copy_file avoids the Wine 9 CopyFile2 abort, and updating CopyFile2Trap so Case E runs without a CopyFile2 export is clean. However, I second @timbot103's concern on copy_blob (runtime/shims/xgameruntime_gamesave.cpp:492, called during rollback at :502 before deleting the backup at :520): on Wine 9 specifically, CopyFileW overwrites the destination in place and can return TRUE after a source read failure once the destination has been opened, which could truncate the live blob during rollback and then delete the intact backup. Copying via a checked read/write loop into a sibling staging file and replacing the destination with MoveFileExW(..., MOVEFILE_REPLACE_EXISTING) would avoid both CopyFile2 and Wine 9's CopyFileW quirk.

AI-assisted (Antigravity, Gemini).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6099901039 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532#pullrequestreview-6099901039). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-28T20:10:30Z. **Review:** COMMENTED --- Reviewed and locally tested `842e0f641` (`runtime/shims/xgameruntime_gamesave.cpp`, `runtime/test/xgameruntime_gamesave_test.cpp`). Local verification: - Built `xgameruntime_gamesave_test` (MSVC Release / Ninja) and ran `ctest -R ^xgameruntime_gamesave$`: 1/1 passed. - `python -I scripts/check_no_game_data.py`: passed. Switching away from `std::filesystem::copy_file` avoids the Wine 9 `CopyFile2` abort, and updating `CopyFile2Trap` so Case E runs without a `CopyFile2` export is clean. However, I second @timbot103's concern on `copy_blob` (`runtime/shims/xgameruntime_gamesave.cpp:492`, called during rollback at `:502` before deleting the backup at `:520`): on Wine 9 specifically, `CopyFileW` overwrites the destination in place and can return `TRUE` after a source read failure once the destination has been opened, which could truncate the live blob during rollback and then delete the intact backup. Copying via a checked read/write loop into a sibling staging file and replacing the destination with `MoveFileExW(..., MOVEFILE_REPLACE_EXISTING)` would avoid both `CopyFile2` and Wine 9's `CopyFileW` quirk. AI-assisted (Antigravity, Gemini).
OreoFR commented 2026-11-28 22:04:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T22:04:11Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 842e0f641; green run in a merge package of 11 PRs, no new ctest failure vs main
  • Test: ctest -R "^xgameruntime_gamesave$"
  • Red on main: OK (96 checks); the CopyFile2 trap is armed but main never calls it: std::filesystem::copy_file of this MSVC toolchain (BuildTools 2022) does not go through kernel32!CopyFile2
  • Green with the PR: OK
  • Verdict: NO RED (the author sees 3 failures on main; depends on the STL version)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246473970 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532#issuecomment-7246473970). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T22:04:11Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `842e0f641`; green run in a merge package of 11 PRs, no new ctest failure vs main - Test: `ctest -R "^xgameruntime_gamesave$"` - Red on main: OK (96 checks); the CopyFile2 trap is armed but main never calls it: std::filesystem::copy_file of this MSVC toolchain (BuildTools 2022) does not go through kernel32!CopyFile2 - Green with the PR: OK - Verdict: NO RED (the author sees 3 failures on main; depends on the STL version) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
TheGeogeo commented 2026-11-28 23:23:14 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-28T23:23:14Z.


Tested 842e0f641 merged (no commit) with main aa918e5d9. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), RelWithDebInfo, Ninja.

  • Green: xgameruntime_gamesave passes.
  • Red: with runtime/shims/xgameruntime_gamesave.cpp back to main, xgameruntime_gamesave fails 3 checks:
    • an update that overwrites a blob succeeds without CopyFile2
    • a failed update copies a back from its backup without CopyFile2
    • no commit or rollback calls CopyFile2 (Wine 9 has none)
  • No regression: the merge relinks 7 executables, and their 18 other tests pass.

No blocker from my side. AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7247396149 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532#issuecomment-7247396149). Originally posted by [TheGeogeo](https://github.com/TheGeogeo) on 2026-11-28T23:23:14Z. --- Tested `842e0f641` merged (no commit) with `main` `aa918e5d9`. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.40, Windows 11 10.0.26300; MSVC 19.52 (Visual Studio 18), RelWithDebInfo, Ninja. - Green: `xgameruntime_gamesave` passes. - Red: with `runtime/shims/xgameruntime_gamesave.cpp` back to `main`, `xgameruntime_gamesave` fails 3 checks: - `an update that overwrites a blob succeeds without CopyFile2` - `a failed update copies a back from its backup without CopyFile2` - `no commit or rollback calls CopyFile2 (Wine 9 has none)` - No regression: the merge relinks 7 executables, and their 18 other tests pass. No blocker from my side. AI-assisted (Claude Code).
MoonEyes commented 2026-11-28 23:25:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T23:25:19Z.


Merged after TheGeogeo's red/green check (check: green with this PR, red with main's code, no regression over the relinked tests) and a re-check here: no conflict with main 7f0b878d3, same head as checked, no-game-data green, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known on main or Vulkan: vk_xdxr_postbuild_readback vk_bindless_dimensions d3d12_compile_pool xbox_present ). The Python jobs fail on main too.

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7247419326 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2532#issuecomment-7247419326). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T23:25:19Z. --- Merged after **TheGeogeo**'s red/green check ([check](https://github.com/ProjectLeonida/mandy/pull/2532#issuecomment-7247396149): green with this PR, red with `main`'s code, no regression over the relinked tests) and a re-check here: no conflict with `main` 7f0b878d3, same head as checked, `no-game-data` green, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known on `main` or Vulkan: vk_xdxr_postbuild_readback vk_bindless_dimensions d3d12_compile_pool xbox_present ). The Python jobs fail on `main` too. _AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes._
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2532
No description provided.