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

Closed
reyani-dev wants to merge 1 commit from upstream-pr/2405 into main
reyani-dev commented 2026-10-09 18:28:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T18:28:31Z.


What

This is the libkernel counterpart of #2342.

FreeBSD's unlink ignores the file's own permission bits: what matters is write access to the directory. A guest that does chmod(path, 0400) and then unlink(path) or sceKernelUnlink(path) therefore succeeds on the console.

On Windows, the guest chmod sets the read-only attribute. _wunlink then fails with EACCES, so both calls failed (0x8002000D / errno 13) and the file stayed.

NativeUnlink (Windows only) now handles that case. When _wunlink fails with EACCES on a regular file without write permission, it makes the file writable with _wchmod and retries. If the retry fails too (e.g. the file is open without delete sharing), it puts the read-only attribute back and reports the retry's error, so a failed unlink leaves the file as it was. Only CRT calls already used in this file are needed (_wstat64, _wchmod); there is no windows.h. Linux is unchanged.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Release, -DBUILD_TESTING=ON:

  • guest_filesystem: two files made read-only with sceKernelChmod(0400) are removed, one by unlink and one by sceKernelUnlink, and no longer exist.
    • On main it fails at the unlink line with guest errno 13.
    • With the fix it passes. On Linux the case already held, and it still does.
  • Probed separately on the fixed build:
    • a read-only file held open by another handle: sceKernelUnlink returns 0x8002000D, the file stays, and the read-only attribute is restored;
    • after the handle is closed, sceKernelUnlink removes it.
  • ctest -E "^agc_|video_out": 249/249.
  • tools/check_conventions.py passes, and tools/pr_overlap.py --branch reports no overlap.
  • It merges cleanly with the open PRs that touch Open.cpp or GuestFilesystem.cpp: #1620, #1624, #1628, #1755, #1764, #1777, #2086, #2115, #2129, #2144, #2170, #2193, #2260, #2289, #2300 and #2342. #1727 already conflicts with main in TechnicalDebt.md, which this PR doesn't touch.

A Linux and Windows full build will be posted here once it has run.

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:4804344716 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2405). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T18:28:31Z. --- ### What This is the libkernel counterpart of #2342. FreeBSD's `unlink` ignores the file's own permission bits: what matters is write access to the directory. A guest that does `chmod(path, 0400)` and then `unlink(path)` or `sceKernelUnlink(path)` therefore succeeds on the console. On Windows, the guest `chmod` sets the read-only attribute. `_wunlink` then fails with `EACCES`, so both calls failed (`0x8002000D` / errno 13) and the file stayed. `NativeUnlink` (Windows only) now handles that case. When `_wunlink` fails with `EACCES` on a regular file without write permission, it makes the file writable with `_wchmod` and retries. If the retry fails too (e.g. the file is open without delete sharing), it puts the read-only attribute back and reports the retry's error, so a failed unlink leaves the file as it was. Only CRT calls already used in this file are needed (`_wstat64`, `_wchmod`); there is no `windows.h`. Linux is unchanged. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, `-DBUILD_TESTING=ON`: - `guest_filesystem`: two files made read-only with `sceKernelChmod(0400)` are removed, one by `unlink` and one by `sceKernelUnlink`, and no longer exist. - On `main` it fails at the `unlink` line with guest errno 13. - With the fix it passes. On Linux the case already held, and it still does. - Probed separately on the fixed build: - a read-only file held open by another handle: `sceKernelUnlink` returns `0x8002000D`, the file stays, and the read-only attribute is restored; - after the handle is closed, `sceKernelUnlink` removes it. - `ctest -E "^agc_|video_out"`: 249/249. - `tools/check_conventions.py` passes, and `tools/pr_overlap.py --branch` reports no overlap. - It merges cleanly with the open PRs that touch `Open.cpp` or `GuestFilesystem.cpp`: #1620, #1624, #1628, #1755, #1764, #1777, #2086, #2115, #2129, #2144, #2170, #2193, #2260, #2289, #2300 and #2342. #1727 already conflicts with `main` in `TechnicalDebt.md`, which this PR doesn't touch. A Linux and Windows full build will be posted here once it has run. ### 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)
github-actions[bot] commented 2026-10-09 18:28:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T18:28:42Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086873788 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2405#issuecomment-6086873788). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T18:28:42Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

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!2405
No description provided.