vfs: fix TRUNCATE_EXISTING on un-overlaid game files #88

Manually merged
github merged 1 commit from gh-pr/88/head into gh-pr/88/base 2026-11-26 04:09:12 +00:00
Owner

@⁠certified-retart on GitHub · 2026-11-23 02:45 UTC · original
certified-retart:fix/vfs-truncate-existing → main · +150 −1 · 2 files · 1 commits · merged 2026-11-23 10:47 UTC

Summary

Fixes TRUNCATE_EXISTING opens of un-overlaid read-only game files in vfs and vfs_hooks:

  • Problem: When a title opens an un-overlaid game file (in_orig == true, in_ov == false) with TRUNCATE_EXISTING (5), Vfs::open_decision_for called prepare_write(&rel, false) (which creates only the parent directory in the overlay, not the file) and returned Decision::Use(ov). The CreateFileW / CreateFileA / CreateFile2 hooks then forwarded ov to Windows with TRUNCATE_EXISTING, which failed with ERROR_FILE_NOT_FOUND because ov does not yet exist on disk.
  • Fix:
    • Added Decision::UseWithDisposition(String, u32) and wired it through hk_create_file_w, hk_create_file_a, and hk_create_file_2.
    • For an un-overlaid game file opened with TRUNCATE_EXISTING (and no existing overlay entry / dangling symlink), open_decision_for rewrites the disposition to CREATE_ALWAYS in the overlay instead of publishing a 0-byte placeholder file ahead of the native call. That way Windows atomically creates the truncated overlay copy only if the native CreateFile* succeeds, leaving the original file untouched and avoiding empty placeholder files on failed opens.
    • Added native Win32 pre-validation for TRUNCATE_EXISTING:
      • Requires GENERIC_WRITE (0x4000_0000) before checking existence, returning ERROR_INVALID_PARAMETER otherwise (matching Win32 CreateFileW, which also rejects GENERIC_READ, FILE_WRITE_DATA, and GENERIC_ALL without GENERIC_WRITE).
      • Rejects visible directories (honoring FILE_FLAG_OPEN_REPARSE_POINT) without creating overlay entries, returning ERROR_INVALID_PARAMETER when FILE_FLAG_BACKUP_SEMANTICS is set and ERROR_ACCESS_DENIED otherwise (matching native Win32 CreateFileW).
    • Existing overlaid files continue to use Decision::Use(ov) with the original TRUNCATE_EXISTING disposition.

Verification

  • cargo test -p xcl --lib vfs -- --test-threads=2: 7 passed; 0 failed
    • Added truncate_existing_rewrites_unoverlaid_disposition_and_opens_with_win32 (verifies zero placeholder file prior to native open, real Win32 CreateFileW creation/truncation for both un-overlaid and already-overlaid files, and untouched original game file).
    • Added truncate_existing_validation_errors (verifies GENERIC_WRITE enforcement, missing/whited-out files, and native Win32 parity for directory errors with and without FILE_FLAG_BACKUP_SEMANTICS).
**@⁠certified-retart** on GitHub · 2026-11-23 02:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/88) `certified-retart:fix/vfs-truncate-existing` → `main` · +150 −1 · 2 files · 1 commits · merged 2026-11-23 10:47 UTC ## Summary Fixes `TRUNCATE_EXISTING` opens of un-overlaid read-only game files in `vfs` and `vfs_hooks`: - **Problem:** When a title opens an un-overlaid game file (`in_orig == true`, `in_ov == false`) with `TRUNCATE_EXISTING` (`5`), `Vfs::open_decision_for` called `prepare_write(&rel, false)` (which creates only the parent directory in the overlay, not the file) and returned `Decision::Use(ov)`. The `CreateFileW` / `CreateFileA` / `CreateFile2` hooks then forwarded `ov` to Windows with `TRUNCATE_EXISTING`, which failed with `ERROR_FILE_NOT_FOUND` because `ov` does not yet exist on disk. - **Fix:** - Added `Decision::UseWithDisposition(String, u32)` and wired it through `hk_create_file_w`, `hk_create_file_a`, and `hk_create_file_2`. - For an un-overlaid game file opened with `TRUNCATE_EXISTING` (and no existing overlay entry / dangling symlink), `open_decision_for` rewrites the disposition to `CREATE_ALWAYS` in the overlay instead of publishing a 0-byte placeholder file ahead of the native call. That way Windows atomically creates the truncated overlay copy only if the native `CreateFile*` succeeds, leaving the original file untouched and avoiding empty placeholder files on failed opens. - Added native Win32 pre-validation for `TRUNCATE_EXISTING`: - Requires `GENERIC_WRITE` (`0x4000_0000`) before checking existence, returning `ERROR_INVALID_PARAMETER` otherwise (matching Win32 `CreateFileW`, which also rejects `GENERIC_READ`, `FILE_WRITE_DATA`, and `GENERIC_ALL` without `GENERIC_WRITE`). - Rejects visible directories (honoring `FILE_FLAG_OPEN_REPARSE_POINT`) without creating overlay entries, returning `ERROR_INVALID_PARAMETER` when `FILE_FLAG_BACKUP_SEMANTICS` is set and `ERROR_ACCESS_DENIED` otherwise (matching native Win32 `CreateFileW`). - Existing overlaid files continue to use `Decision::Use(ov)` with the original `TRUNCATE_EXISTING` disposition. ## Verification - `cargo test -p xcl --lib vfs -- --test-threads=2`: **7 passed; 0 failed** - Added `truncate_existing_rewrites_unoverlaid_disposition_and_opens_with_win32` (verifies zero placeholder file prior to native open, real Win32 `CreateFileW` creation/truncation for both un-overlaid and already-overlaid files, and untouched original game file). - Added `truncate_existing_validation_errors` (verifies `GENERIC_WRITE` enforcement, missing/whited-out files, and native Win32 parity for directory errors with and without `FILE_FLAG_BACKUP_SEMANTICS`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:09:10 +00:00
github manually merged commit 15243ad6e9 into gh-pr/88/base 2026-11-26 04:09:12 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-23 10:48 UTC · original

Merged. Tested on Windows 11 with an RX 9070 (RDNA4): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord and XDefiant run with this round merged. Thanks!

**@⁠ivall** on GitHub · 2026-11-23 10:48 UTC · [original](https://github.com/xcl-dev/xcl/pull/88#issuecomment-7098508848) Merged. Tested on Windows 11 with an RX 9070 (RDNA4): `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; Unicorn Overlord and XDefiant run with this round merged. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.