Skip to content

gta6rt: a delete-on-close open of a GAME: file no longer deletes the package file - #3489

Open
shdann wants to merge 8 commits into
ItsNotRexed:mainfrom
shdann:fs-delete-on-close
Open

shdann wants to merge 8 commits into
ItsNotRexed:mainfrom
shdann:fs-delete-on-close

Conversation

@shdann

@shdann shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claim: #3381 (comment) (a new RT item, no ID yet)

What and why

FILE_FLAG_DELETE_ON_CLOSE deletes the file whatever access the open asks for: the host adds DELETE to such an open. runtime/shims/file_system.cpp chose between the package and the overlay from the access mask and the disposition only (writes), so

CreateFileW(L"GAME:\data\x.bin", GENERIC_READ, share, nullptr, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, nullptr);

was sent to the package path, and CloseHandle removed the file from the game folder. docs/runtime_imports.md says "The package is never written". CreateFile2 had the same hole through dwFileFlags.

The change

  • An open with the flag counts as a write, so it uses the overlay copy (copied up first when the disposition keeps the contents, as any write-open).
  • When the open succeeds on a name the package has, the name gets the deletion marker DeleteFile leaves. Until the close, reads still find the overlay copy; after it, the title sees the file gone and the package still holds it.
  • CreateFile2 reads the flag from its extended parameters.
  • One sentence in docs/runtime_imports.md and one line in the file's header comment.

Not changed: a host path keeps its flag and is deleted by the host as before. If the marker cannot be written, the open still succeeds (the delete it carries cannot be taken back) and the package file is visible again after the close; the comment on mark_deleted_on_close says so.

Reproduction (on main f847748, no game files)

The new block in runtime/test/file_system_test.cpp makes a file in the test's own stand-in package folder under %TEMP%, opens it through the shim with GENERIC_READ and FILE_FLAG_DELETE_ON_CLOSE, and closes the handle. Before the fix:

FAIL closing a delete-on-close handle leaves the package file in place
FAIL CreateFile2 with delete-on-close leaves the package file in place
149 check(s), 2 failure(s)

After it: 149 check(s), 0 failure(s). The block also checks that the name is gone for the title and from listings after the close, that no overlay copy is left, that an overlay-only file needs no marker, that a host path is still deleted by the host, and that a failed open leaves no marker.

That the host deletes on a read-only open with the flag was checked separately on a scratch file with the real CreateFileW (handle valid, file gone after CloseHandle).

Tests run

Windows 11, Visual Studio 18 (MSVC 14.51), Windows SDK 10.0.26100.8249, CMake 4.4.3 + Ninja, RelWithDebInfo, Vulkan headers from -DLEONIDA_VULKAN_HEADERS_FETCH=ON; no glslangValidator, llvm-mc or SDL3, so the tests that need them were not built.

  • cmake --build <build>: 347 steps, no warnings in the changed files.
  • file_system_test: 149 checks, 0 failures.
  • ctest -C RelWithDebInfo --output-on-failure: 122 tests, 121 passed, 1 failed (xbox_present), 8 skipped, 1 disabled. On unchanged main the same build gave 120 passed with d3d12_present and xbox_present failing. Both fail on the swapchain-extent check (d3d12_present_test.cpp:386, xbox_present_test.cpp:402) and vary from run to run; this machine runs a tiling window manager that resizes new windows, which is the likely cause. Neither test touches the file shim.
  • python -I tests/test_docs_links.py: OK. python -m compileall -q make_game.py tools tests: exit 0.

GPU: the change and its test use none. The full ctest run above used an NVIDIA GeForce RTX 4090, driver 32.0.16.2101 (an AMD Radeon integrated GPU, driver 32.0.21043.5001, is also present).

#3063 (copy-up through a staging name) and the RT-218 claim (G:\ / T:\) change other parts of file_system.cpp; no dependency.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

…package file

FILE_FLAG_DELETE_ON_CLOSE deletes the file whatever access the open asks
for: the host adds DELETE to it. The shim chose between the package and
the overlay from the access mask and the disposition only, so a read-only
open with the flag went to the package path, and closing the handle
removed the file from the game folder.

Such an open now counts as a write and uses the overlay copy. When it
succeeds on a name the package has, the name gets the marker DeleteFile
leaves, so after the close the title sees the file gone and the package
still holds it. CreateFile2 reads the flag from its extended parameters.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At 15171b4 all 149 focused checks passed on Windows x64. Two synthetic registered-shim calls exposed gaps. OPEN_ALWAYS on an existing package file with delete-on-close succeeds but returns last error 3 instead of 183. Preserve last error immediately after the host open, before the line 360 path queries, then restore after marker handling. CREATE_NEW with the same flag succeeds on a visible package-only name and creates a deletion marker. Please coordinate with the existing CREATE_NEW claim (tracker comment 7085547167) and cover the interaction, including a previously deleted name. AI-assisted, synthetic CPU validation only.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

4 open issues
What changed in this PR

Fixes incorrect deletion behavior for GAME: package files opened with FILE_FLAG_DELETE_ON_CLOSE by routing such opens through the overlay and creating a deletion marker for package-backed names after a successful open.

Changes:

  • Treat FILE_FLAG_DELETE_ON_CLOSE as a “write-like” open when selecting package vs overlay targets.
  • After a successful delete-on-close open of a package-backed name, write a deletion marker so the package file doesn’t reappear after close.
  • Add regression tests (including CreateFile2) and update runtime import documentation.
File Description
runtime/​test/​file_system_test.cpp Adds regression coverage for delete-on-close behavior for package, overlay-only, and host paths (incl. CreateFile2).
runtime/​shims/​file_system.cpp Routes delete-on-close opens to overlay and marks package-backed names deleted-on-close via overlay marker.
docs/​runtime_imports.md Documents delete-on-close semantics and the marker behavior for package-backed names.

Review effort: Lite

Comment on lines +354 to +363
std::vector<std::wstring> parts;
if (parse_console(path, parts) != Parsed::Console || parts.empty()) return;
const Roots r = roots();
if (r.game.empty() || r.overlay.empty() || deleted(r.overlay, parts)) return;
std::error_code ec;
if (!std::filesystem::is_regular_file(join(r.game, parts), ec)) return;
const DWORD last_error = GetLastError(); // CreateFile's ERROR_ALREADY_EXISTS or 0, which the title may read
DWORD error = 0;
write_marker(marker(r.overlay, parts), error);
SetLastError(last_error);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8b97064: the last error is taken right after the host open (opened_delete_on_close) and restored after the marker; three new checks cover it. (AI-assisted reply.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: e9d8608 moves the read of the last error into create_file_w and create_file2, to the statement after the host call. See the replies on lines 575 and 602. AI-assisted reply.

Comment on lines +489 to +491
const HANDLE h = ::CreateFileW(t.path.c_str(), access, share, attributes, disposition, flags, template_file);
if (h != INVALID_HANDLE_VALUE && (flags & FILE_FLAG_DELETE_ON_CLOSE) != 0) mark_deleted_on_close(name);
return h;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e9d8608. The first fix (8b97064) read the last error as the first statement of opened_delete_on_close. That was after parse_console, roots and is_regular_file, but not directly after the host call: the call built a std::wstring for the parameter first, and an allocation can change the last error. Now create_file_w reads GetLastError() in the statement after ::CreateFileW (file_system.cpp line 574) and gives the value to the helper. The helper restores it after the marker.

Check that failed first: file_system_test replaces operator new, and two new checks make each allocation set the last error. At 23bbbd0: 181 check(s), 2 failure(s). At e9d8608: 181 check(s), 0 failure(s). With the allocator of the host, I did not see a wrong value before this change.

AI-assisted reply.

Comment on lines +516 to +518
const HANDLE h = ::CreateFile2(t.path.c_str(), access, share, disposition, params);
if (h != INVALID_HANDLE_VALUE && (flags & FILE_FLAG_DELETE_ON_CLOSE) != 0) mark_deleted_on_close(name);
return h;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e9d8608, the same change as for create_file_w. create_file2 reads GetLastError() in the statement after ::CreateFile2 (file_system.cpp line 602) and gives the value to opened_delete_on_close. The new check and the same through CreateFile2 failed at 23bbbd0 and passes now: 181 check(s), 0 failure(s).

AI-assisted reply.

Comment thread runtime/test/file_system_test.cpp Outdated
Comment on lines +630 to +641
write_path(package / "data" / "once2.txt", "package");
CREATEFILE2_EXTENDED_PARAMETERS ext{};
ext.dwSize = sizeof(ext);
ext.dwFileFlags = FILE_FLAG_DELETE_ON_CLOSE;
h = create2 ? create2(L"GAME:\\data\\once2.txt", GENERIC_READ, kShareAll, OPEN_EXISTING, &ext)
: INVALID_HANDLE_VALUE;
check(h != INVALID_HANDLE_VALUE, "CreateFile2 opens a package file with delete-on-close");
if (h != INVALID_HANDLE_VALUE) ::CloseHandle(h);
check(read_path(package / "data" / "once2.txt") == "package",
"CreateFile2 with delete-on-close leaves the package file in place");
check(attributes_w(L"GAME:\\data\\once2.txt") == INVALID_FILE_ATTRIBUTES,
"the name is gone for the title after CreateFile2's delete-on-close handle is closed");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in e9d8608. One note on the cause: resolve reads the table of the shim (rt::shims::find), not the exports of kernel32.dll. The shim always registers CreateFile2, and it also imports the host function, so the test cannot start on a system without it. A missing registration is thus a defect, and the test must report it, not skip it.

The readability point is correct. Before, a missing registration gave four failures with misleading text. Now one check, CreateFile2 is registered under kernel32.dll (file_system_test.cpp line 647), reports it, and each CreateFile2 check of this block is in if (create2). Result: 181 check(s), 0 failure(s).

AI-assisted reply.

mark_deleted_on_close read the last error only after its own file
queries, which had already replaced it: OPEN_ALWAYS with
FILE_FLAG_DELETE_ON_CLOSE on a package file returned a handle with
ERROR_PATH_NOT_FOUND as the last error instead of ERROR_ALREADY_EXISTS,
and a new name could keep a stale value instead of 0. The value is now
taken right after the host's open and put back after the marker.

Found in review by @kacper-serewis.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
shdann added a commit to shdann/Leonida that referenced this pull request Nov 22, 2026
…th the creating dispositions

Brings in "a delete-on-close open keeps the last error of the host's
open" from the branch this one depends on, and adds the checks its
review asked for: CREATE_NEW and CREATE_ALWAYS with
FILE_FLAG_DELETE_ON_CLOSE on a name only the package has, and CREATE_NEW
with the flag on a name the title deleted.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @kacper-serewis, both points reproduced.

Last error. Fixed in 8b97064. mark_deleted_on_close read the last error only after its own file queries, so OPEN_ALWAYS + FILE_FLAG_DELETE_ON_CLOSE on a package file returned a handle with ERROR_PATH_NOT_FOUND instead of ERROR_ALREADY_EXISTS. The value is now taken right after the host's open (opened_delete_on_close) and put back after the marker. Three new checks failed before and pass now (CreateFileW and CreateFile2 on a package file, and 0 for a new name): file_system_test 153 checks, 0 failures.

CREATE_NEW with the flag on a package-only name. That open succeeding at all is the defect #3532 fixes (it says Depends on: this one), so the fix is there, not here: with #3532 it fails with ERROR_FILE_EXISTS and writes no marker. I merged 8b97064 into #3532 and added the interaction checks you asked for, including the previously deleted name: CREATE_NEW + flag on a package-only name (refused, no marker, name still there), CREATE_ALWAYS + flag (ERROR_ALREADY_EXISTS, gone after the close, package file kept), and CREATE_NEW + flag on a name the title deleted (succeeds with last error 0, gone again after the close). There: 174 checks, 0 failures.

Full ctest on the merged branch: 122 tests, 120 passed; d3d12_present and xbox_present fail on this machine on unchanged main too (swapchain-extent check, varies from run to run, most likely the tiling window manager here).

On the Copilot note about CreateFile2: the test resolves it through the shim registry (rt::shims::find), which always has it, not through the host's export table, as the existing CreateFile2 block of this test does.

AI-assisted (Claude Code).

shdann and others added 2 commits November 23, 2026 00:21
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
shdann added a commit to shdann/Leonida that referenced this pull request Nov 22, 2026
…he test is INVALID_HANDLE_VALUE

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At 3f4e912 the OPEN_ALWAYS error fix passes on Windows. CREATE_NEW plus DELETE_ON_CLOSE still returns a handle on a visible package-only file and hides the name on close. Check merged-view existence at file_system.cpp:343; allow creation when a deletion marker really freed the name. Recreation and original bytes passed. All 153 suite checks passed; additional synthetic CreateFileW controls reproduced the collision. AI-assisted.

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Windows + GPU check: AMD Radeon RX 9070 (RDNA4), Windows 11, MSVC Release, Vulkan SDK 1.4.363. Tested head 3f4e912 merged onto main f847748 (clean merge), full build, full ctest -C Release -j 4: 100% tests passed out of 132 | new failures vs main: none. The main baseline on the same machine passes 132/132.

AI-assisted (Claude Code); runs on my machine.

… the title sees

CREATE_NEW goes to the overlay, where a file only the package has is not
there to refuse it. With FILE_FLAG_DELETE_ON_CLOSE the open made a new
overlay file, and the marker written for the flag hid the package file
after the close: a file the open should never have got was deleted from
the title's view. The host fails such an open with ERROR_FILE_EXISTS, and
so does open_target now. A name a deletion marker has freed is not in the
view and can still be created.

Found in review by @kacper-serewis.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
shdann added a commit to shdann/Leonida that referenced this pull request Nov 22, 2026
… covered by the rule for every creating open

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @kacper-serewis, reproduced and fixed in 48ed73e, so this pull request is right on its own for that case.

Host first. Real CreateFileW with CREATE_NEW | FILE_FLAG_DELETE_ON_CLOSE on a file that exists: invalid handle, ERROR_FILE_EXISTS (80), the file stays, for read, write and zero access alike. On a new name: a handle, last error 0, and the file is gone after the close.

Reproduction at 3f4e912. Six new checks failed: the open returned a handle through CreateFileW (read and write access) and CreateFile2, left a marker and an overlay file, and the title could no longer read the package file.

Fix. open_target now asks the title's view before the overlay is used: CREATE_NEW with the flag on a file that is visible and only in the package fails with ERROR_FILE_EXISTS. Nothing is created and no marker is written. A name a deletion marker has freed is not in the view, so it can still be created, and the close deletes the new file again; that is checked too.

Results at 48ed73e. file_system_test: 161 checks, 0 failures. Full ctest (MSVC 14.51, RelWithDebInfo): 122 tests, 120 passed; d3d12_present and xbox_present fail on this machine on unchanged main as well (they pass on other machines, see @Dom-in's run above and the one on #3532).

How it fits #3532. This is the delete-on-close part of the rule #3532 adds for every creating open. I merged this head into #3532 (11606ae); there the general rule covers the case and this flag-specific test of the view is dropped in the merge, so the two do not end up with two rules. file_system_test there passes with these checks included.

AI-assisted (Claude Code).

main added the copy-up race and the G:/T: drive blocks at the end of the
test, where this branch added its delete-on-close block; both are kept.
file_system.cpp merged without conflict.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
shdann added a commit to shdann/Leonida that referenced this pull request Nov 22, 2026
…ystem_test blocks

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@shdann

shdann commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Updated to 23bbbd0: main 4c52ce2 is merged in (a merge, not a rebase, so the commits quoted in the reviews above, 15171b4, 8b97064, 3f4e912 and 48ed73e, are unchanged and still on the branch).

What conflicted. Only runtime/test/file_system_test.cpp: main added two blocks at the end of the test (the copy-up race of #3063 and the G:\ / T:\ drives of RT-218) at the place where this branch added its delete-on-close block. Both sides are kept, this branch's block first. runtime/shims/file_system.cpp and docs/runtime_imports.md merged without conflict.

Overlap. None of what landed changes how an open with FILE_FLAG_DELETE_ON_CLOSE is routed, so nothing of this pull request was dropped. A T:\ path maps into the overlay and has no package file behind it, so it gets no deletion marker, as for any overlay-only file.

Results at 23bbbd0 (MSVC 14.51, Windows SDK 10.0.26100.8249, RelWithDebInfo): file_system_test 178 checks, 0 failures (this branch's and main's new blocks together). Full ctest: 133 tests, 131 passed, 2 failed, 9 skipped; the two are d3d12_present and xbox_present, which fail on this machine on unchanged main too and pass on other machines.

AI-assisted (Claude Code).

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 23bbbd0 merged onto main aa2988c on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 154/154.
  • file_system: passes 20 of 20 runs, and passes under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

…allocates

create_file_w and create_file2 read GetLastError() as the first statement after the host's open and pass it to
opened_delete_on_close. Before, the helper read it, after the std::wstring for its parameter was built; an allocator
that changes the last error would replace ERROR_ALREADY_EXISTS. No such change was seen with the host's allocator.

file_system_test replaces operator new so two checks can make every allocation set the last error; they fail without
the change. The CreateFile2 checks of the delete-on-close block run only when the function is registered, and one
check reports a missing registration.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
shdann added a commit to shdann/Leonida that referenced this pull request Nov 23, 2026
…he host open, then the replaced-package-file rule applies

Both sides changed the two lines after the host open in create_file_w and create_file2. Kept both: the last error is
read first and given to opened_delete_on_close, then ERROR_ALREADY_EXISTS is set for a replaced package file.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Summary of the review fixes. The new head is e9d8608.

  • Last error (Copilot, file_system.cpp lines 575 and 602): create_file_w and create_file2 read GetLastError() in the statement after the host call. Two new checks failed at 23bbbd0 (181 check(s), 2 failure(s)) and pass now.
  • CreateFile2 gate (Copilot, file_system_test.cpp line 642): one check reports a missing registration, and the CreateFile2 checks of the block run only when the function is present.
  • No merge of main was necessary. GitHub reports no conflict.

Tests on Windows 11, MSVC 14.51, Windows SDK 10.0.26100.8249, CMake 4.4.3 with Ninja, RelWithDebInfo:

  • file_system_test: 181 checks, 0 failures.
  • ctest -C RelWithDebInfo: 133 tests, 131 passed, 2 failed (d3d12_present, xbox_present). These two also fail on main on this machine. They do not use the file shim.
  • python -I tests/test_docs_links.py: OK. python -m compileall -q make_game.py tools tests: exit 0.
  • GPU for the full run: NVIDIA GeForce RTX 4090, driver 621.01 (32.0.16.2101). The change uses no GPU.

@kacper-serewis: the change request of 2026-11-22 21:30 (CREATE_NEW with delete-on-close) has its fix in 48ed73e. Please do a new review at e9d8608. GitHub does not let me add you as a reviewer.

AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested e9d8608 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran ctest -R "^file_system$" (the test this PR changes): pass on both GPUs, 1/1 each. No game files.

AI-assisted.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

I merged upstream main 98fa503f. The new head is fee21c4e. Both branches changed the file access check. The merged shim routes an open to the overlay when the caller requests MAXIMUM_ALLOWED or sets FILE_FLAG_DELETE_ON_CLOSE.

On Windows 11, the RelWithDebInfo build passes. All 189 file-system checks and all six documentation link tests pass. Python compilation passes. Full CTest records 139 passes, 10 skips, one disabled test, and two failures. The same two tests fail on the baseline: d3d12_present and xbox_present.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants