fix(libkernel): unlink read-only files on Windows #2910

Merged
reyani-dev merged 1 commit from upstream-pr/2910 into main 2026-10-10 19:58:25 +00:00
reyani-dev commented 2026-10-10 18:23:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T18:23:46Z.


What

Fixes #2601. This is the libkernel counterpart of #2342.

FreeBSD's unlink ignores the file's own permission bits, so chmod(path, 0400) followed by unlink(path) succeeds on the console. On Windows, the guest chmod sets FILE_ATTRIBUTE_READONLY, and _wunlink then failed with EACCES. So unlink returned -1 with errno 13, sceKernelUnlink returned 0x8002000D, and the file stayed.

NativeUnlink (Windows only) now decides up front, the shape requested on #2342, with no retry. When _wstat64 reports a regular file without _S_IWRITE, it makes the file writable with _wchmod before the single _wunlink. If the unlink fails, for example because the file is open without delete sharing, it restores the read-only attribute and reports the unlink's error. The change uses only CRT calls already in Open.cpp. Linux is unchanged.

Tested

guest_filesystem gains two files made read-only with sceKernelChmod(0400): one removed with unlink, one with sceKernelUnlink. Both must no longer exist.

  • Windows, me (Windows 11, WinLibs GCC 15.2.0 r7, Release, on current main bf6e25a1): guest_filesystem passes 3/3. Earlier, on 89a1fb4e:
    • the new checks failed without the Open.cpp change (guest errno 13);
    • a read-only file held open by another handle still returned 0x8002000D, stayed in place and kept its read-only attribute; after the handle was closed, it was removed;
    • ctest -E "^agc_|video_out" passed 341/341.
  • Windows, @harmsluis on b5f5362d (#2601): fails without the Open.cpp change, passes with it.
  • Linux, @MarcZX100 on b5f5362d and cf8b129e (#2601): guest_filesystem passes.
  • check_conventions.py passes, and pr_overlap.py --branch reports no overlap.

#2651 (open) targets the same behaviour; see the coordination comments there and on #2601.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4813460767 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2910). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T18:23:46Z. --- ### What Fixes #2601. This is the libkernel counterpart of #2342. FreeBSD's `unlink` ignores the file's own permission bits, so `chmod(path, 0400)` followed by `unlink(path)` succeeds on the console. On Windows, the guest `chmod` sets `FILE_ATTRIBUTE_READONLY`, and `_wunlink` then failed with `EACCES`. So `unlink` returned -1 with errno 13, `sceKernelUnlink` returned `0x8002000D`, and the file stayed. `NativeUnlink` (Windows only) now decides up front, the shape requested on #2342, with no retry. When `_wstat64` reports a regular file without `_S_IWRITE`, it makes the file writable with `_wchmod` before the single `_wunlink`. If the unlink fails, for example because the file is open without delete sharing, it restores the read-only attribute and reports the unlink's error. The change uses only CRT calls already in `Open.cpp`. Linux is unchanged. ### Tested `guest_filesystem` gains two files made read-only with `sceKernelChmod(0400)`: one removed with `unlink`, one with `sceKernelUnlink`. Both must no longer exist. - **Windows, me** (Windows 11, WinLibs GCC 15.2.0 r7, Release, on current `main` bf6e25a1): `guest_filesystem` passes 3/3. Earlier, on 89a1fb4e: - the new checks failed without the `Open.cpp` change (guest errno 13); - a read-only file held open by another handle still returned `0x8002000D`, stayed in place and kept its read-only attribute; after the handle was closed, it was removed; - `ctest -E "^agc_|video_out"` passed 341/341. - **Windows, @harmsluis** on `b5f5362d` (#2601): fails without the `Open.cpp` change, passes with it. - **Linux, @MarcZX100** on `b5f5362d` and `cf8b129e` (#2601): `guest_filesystem` passes. - `check_conventions.py` passes, and `pr_overlap.py --branch` reports no overlap. #2651 (open) targets the same behaviour; see the coordination comments there and on #2601. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
reyani-dev commented 2026-10-10 19:06:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T19:06:47Z.


Full Linux and Windows builds and tests for this head (f45d8933), using the project's Build workflow. I can't start the upstream manual run, so I ran the same build.yml in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current main (bf6e25a1):

Job Result
linux success; full build, ctest: 701/701 passed
windows success; full build, ctest: 709/709 passed
relinker (Linux, Windows, macOS) success

Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/38075637171

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6101136129 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2910#issuecomment-6101136129). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T19:06:47Z. --- Full Linux and Windows builds and tests for this head (`f45d8933`), using the project's Build workflow. I can't start the upstream manual run, so I ran the same `build.yml` in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current `main` (`bf6e25a1`): | Job | Result | |---|---| | [linux](https://github.com/AchrafReyani/AnyPS5/actions/runs/38075637171/job/114286444081) | success; full build, `ctest`: 701/701 passed | | [windows](https://github.com/AchrafReyani/AnyPS5/actions/runs/38075637171/job/114286444991) | success; full build, `ctest`: 709/709 passed | | relinker (Linux, Windows, macOS) | success | Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/38075637171
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
boykopovar/AnyPS5!2910
No description provided.