libkernel: unlink and sceKernelUnlink fail on read-only files on Windows (counterpart of #2342) #2601

Closed
opened 2026-10-10 08:19:22 +00:00 by reyani-dev · 7 comments
reyani-dev commented 2026-10-10 08:19:22 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T08:19:22Z.


unlink and sceKernelUnlink fail on Windows for a file the guest made read-only. This is the libkernel counterpart of the libc remove case fixed in #2342.

FreeBSD's unlink ignores the file's own permission bits; what matters is write access to the directory. So chmod(path, 0400) followed by unlink(path) succeeds on the console. On Windows, the guest chmod (_wchmod) sets FILE_ATTRIBUTE_READONLY when the write bit is cleared. NativeUnlink's _wunlink then fails with EACCES, so unlink returns -1 with errno 13 and sceKernelUnlink returns 0x8002000D, and the file stays. Linux is not affected.

Fix, ready to cherry-pick. AchrafReyani:fix/unlink-readonly (fbf0ec0a, one commit on main 89a1fb4e). I can't open it as a PR because of the intake check (it was #2405, closed by the bot).

The fix follows the shape requested on #2342: decide up front, with no retry. When _wstat64 reports a regular file without _S_IWRITE, NativeUnlink (Windows only) makes it 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, with no windows.h.

guest_filesystem gains a case: two files made read-only with sceKernelChmod(0400) are removed, one by unlink and one by sceKernelUnlink, and no longer exist.

Checked on Windows (WinLibs GCC 15.2.0, Release, on main 89a1fb4e):

  • The new check fails without the change (guest errno 13) and passes with it.
  • A probe with the 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" passes 341/341, check_conventions.py passes, and pr_overlap.py reports no overlap.
  • The fork CI run is 38037442905. I'll post the result here.

No title is known to stop on this, unlike #2559. It is the same Windows behaviour gap that #2342 closed for remove.

Found with Claude Code.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5789521255 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2601). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T08:19:22Z. --- `unlink` and `sceKernelUnlink` fail on Windows for a file the guest made read-only. This is the libkernel counterpart of the libc `remove` case fixed in #2342. FreeBSD's `unlink` ignores the file's own permission bits; what matters is write access to the directory. So `chmod(path, 0400)` followed by `unlink(path)` succeeds on the console. On Windows, the guest `chmod` (`_wchmod`) sets `FILE_ATTRIBUTE_READONLY` when the write bit is cleared. `NativeUnlink`'s `_wunlink` then fails with `EACCES`, so `unlink` returns -1 with errno 13 and `sceKernelUnlink` returns `0x8002000D`, and the file stays. Linux is not affected. **Fix, ready to cherry-pick.** [`AchrafReyani:fix/unlink-readonly`](https://github.com/AchrafReyani/AnyPS5/tree/fix/unlink-readonly) (fbf0ec0a, one commit on `main` 89a1fb4e). I can't open it as a PR because of the intake check (it was #2405, closed by the bot). The fix follows the shape requested on #2342: decide up front, with no retry. When `_wstat64` reports a regular file without `_S_IWRITE`, `NativeUnlink` (Windows only) makes it 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`, with no `windows.h`. `guest_filesystem` gains a case: two files made read-only with `sceKernelChmod(0400)` are removed, one by `unlink` and one by `sceKernelUnlink`, and no longer exist. **Checked on Windows** (WinLibs GCC 15.2.0, Release, on `main` 89a1fb4e): - The new check fails without the change (guest errno 13) and passes with it. - A probe with the 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"` passes 341/341, `check_conventions.py` passes, and `pr_overlap.py` reports no overlap. - The fork CI run is [38037442905](https://github.com/AchrafReyani/AnyPS5/actions/runs/38037442905). I'll post the result here. No title is known to stop on this, unlike #2559. It is the same Windows behaviour gap that #2342 closed for `remove`. Found with Claude Code.
MarcZX10 commented 2026-10-10 08:46:22 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T08:46:22Z.


Updated validation on current main

I reapplied the one-commit patch from AchrafReyani:fix/unlink-readonly to upstream main at b5f5362d in a detached local worktree. The two-file patch applies cleanly.

Check Result
Build guest_filesystem_tests (Linux x86_64, GCC 12.2, Release) Passed
CTest guest_filesystem Passed (1/1)
Conventions against current main Passed
git diff --check Passed

The Windows evidence remains Achraf's Windows 11 / WinLibs GCC 15.2 run: read-only unlink succeeds, while a held-open file without delete sharing remains present and read-only after failure. That run and their fork CI were based on older commits (3d1a7d90 for CI); I have not repeated Windows validation on b5f5362d.

Coordination with PR #2651

PR #2651 is still open, but its current head f42420e7 has 70 commits and changes 234 files (+6,050/−448) relative to its merge base. The unrelated diff needs to be removed before review. Its current unlink code also does not restore the read-only attribute after a failed retry, and GuestFilesystem.cpp redeclares readOnly in the same scope, so the Linux test target still cannot compile. I updated my review comment on #2651 with these current-head findings. Please coordinate with its author and the maintainers on one scoped implementation while preserving the Windows held-open-file evidence.

The Achraf patch remains staged locally for review; I have not committed or pushed it. No PR was opened while intake requires 20 merged PRs and my count is 15. AI-assisted change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095811638 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2601#issuecomment-6095811638). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T08:46:22Z. --- ### Updated validation on current `main` I reapplied the one-commit patch from `AchrafReyani:fix/unlink-readonly` to upstream `main` at `b5f5362d` in a detached local worktree. The two-file patch applies cleanly. | Check | Result | |---|---| | Build `guest_filesystem_tests` (Linux x86_64, GCC 12.2, Release) | Passed | | CTest `guest_filesystem` | Passed (1/1) | | Conventions against current `main` | Passed | | `git diff --check` | Passed | The Windows evidence remains Achraf's Windows 11 / WinLibs GCC 15.2 run: read-only unlink succeeds, while a held-open file without delete sharing remains present and read-only after failure. That run and their fork CI were based on older commits (`3d1a7d90` for CI); I have not repeated Windows validation on `b5f5362d`. ### Coordination with PR #2651 PR #2651 is still open, but its current head `f42420e7` has 70 commits and changes 234 files (+6,050/−448) relative to its merge base. The unrelated diff needs to be removed before review. Its current unlink code also does not restore the read-only attribute after a failed retry, and `GuestFilesystem.cpp` redeclares `readOnly` in the same scope, so the Linux test target still cannot compile. I updated my review comment on #2651 with these current-head findings. Please coordinate with its author and the maintainers on one scoped implementation while preserving the Windows held-open-file evidence. The Achraf patch remains staged locally for review; I have not committed or pushed it. No PR was opened while intake requires 20 merged PRs and my count is 15. AI-assisted change.
reyani-dev commented 2026-10-10 09:26:08 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T09:26:08Z.


Thanks @MarcZX100. Here is the Windows side for the same commit:

  • Fork CI on main 3d1a7d90 (run 38040339170):
    • Linux passes 690/690.
    • Windows runs 696/698 including guest_filesystem with the new read-only checks. The two failures are guest_file_vector and guest_memory, which fail identically on plain main since #2620 (details there), not this change.
  • Locally (Windows 11, WinLibs GCC 15.2.0, Release):
    • the new check fails without the change (guest errno 13) and passes with it;
    • a read-only file held open by another handle still fails with 0x8002000D, stays in place and keeps its read-only attribute;
    • after the handle is closed, it is removed.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096108195 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2601#issuecomment-6096108195). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T09:26:08Z. --- Thanks @MarcZX100. Here is the Windows side for the same commit: - Fork CI on `main` 3d1a7d90 ([run 38040339170](https://github.com/AchrafReyani/AnyPS5/actions/runs/38040339170)): - Linux passes 690/690. - Windows runs 696/698 including `guest_filesystem` with the new read-only checks. The two failures are `guest_file_vector` and `guest_memory`, which fail identically on plain `main` since #2620 (details there), not this change. - Locally (Windows 11, WinLibs GCC 15.2.0, Release): - the new check fails without the change (guest errno 13) and passes with it; - a read-only file held open by another handle still fails with `0x8002000D`, stays in place and keeps its read-only attribute; - after the handle is closed, it is removed.
MarcZX10 commented 2026-10-10 10:30:43 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T10:30:43Z.


Coordination with open PR #2651

There is now an open PR for the same Windows read-only unlink behavior: #2651. I left a review comment there because its current head has a Linux test-build failure (readOnly is redeclared in GuestFilesystem.cpp) and its retry path does not restore the read-only attribute if the second unlink attempt also fails.

Your branch already has Windows 11 evidence for both successful deletion and the held-open-file case, including restoration of the read-only attribute after failure. Please coordinate with @sridhar777911 and the maintainers so the two implementations converge on one canonical change and your test/evidence is preserved.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096583438 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2601#issuecomment-6096583438). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T10:30:43Z. --- ### Coordination with open PR #2651 There is now an open PR for the same Windows read-only `unlink` behavior: [#2651](https://github.com/boykopovar/AnyPS5/pull/2651). I left a review comment there because its current head has a Linux test-build failure (`readOnly` is redeclared in `GuestFilesystem.cpp`) and its retry path does not restore the read-only attribute if the second unlink attempt also fails. Your branch already has Windows 11 evidence for both successful deletion and the held-open-file case, including restoration of the read-only attribute after failure. Please coordinate with @sridhar777911 and the maintainers so the two implementations converge on one canonical change and your test/evidence is preserved.
harmsluis commented 2026-10-10 11:31:14 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by harmsluis on 2026-10-10T11:31:14Z.


Windows validation of AchrafReyani:fix/unlink-readonly (fbf0ec0a) on current main b5f5362d, filling the gap noted above. Windows 11 (10.0.26300), WinLibs GCC 15.2.0, CMake 4.3.1 / Ninja, Release, BUILD_TESTING=ON.

The commit cherry-picks cleanly onto b5f5362d (two files, +19/−1).

Build cmake --build build --target guest_filesystem_tests CTest guest_filesystem
b5f5362d + full patch passed Passed (1/1)
b5f5362d + test change only (Open.cpp reverted) passed Failed, exit 0xc0000409

So the new read-only checks fail on main and pass with the Open.cpp change, natively on Windows. I did not repeat the held-open-file case; Achraf's earlier run covers it.

AI-assisted: yes. An AI agent applied the patch, ran the builds and tests, and drafted this comment; I reviewed the results.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097044332 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2601#issuecomment-6097044332). Originally posted by [harmsluis](https://github.com/harmsluis) on 2026-10-10T11:31:14Z. --- Windows validation of `AchrafReyani:fix/unlink-readonly` (`fbf0ec0a`) on current `main` `b5f5362d`, filling the gap noted above. Windows 11 (10.0.26300), WinLibs GCC 15.2.0, CMake 4.3.1 / Ninja, Release, `BUILD_TESTING=ON`. The commit cherry-picks cleanly onto `b5f5362d` (two files, +19/−1). | Build | `cmake --build build --target guest_filesystem_tests` | CTest `guest_filesystem` | |---|---|---| | `b5f5362d` + full patch | passed | **Passed** (1/1) | | `b5f5362d` + test change only (`Open.cpp` reverted) | passed | **Failed**, exit `0xc0000409` | So the new read-only checks fail on `main` and pass with the `Open.cpp` change, natively on Windows. I did not repeat the held-open-file case; Achraf's earlier run covers it. AI-assisted: yes. An AI agent applied the patch, ran the builds and tests, and drafted this comment; I reviewed the results.
MarcZX10 commented 2026-10-10 12:38:45 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-10T12:38:45Z.


Current-main refresh of the narrow #2601 patch

I carried AchrafReyani's one-commit fix forward onto the latest upstream main, b69270dd, preserving him as the commit author. The refreshed commit is 1ba3c397 on my fork branch codex/fix-unlink-readonly-2601. The two touched files have not changed since the earlier b5f5362d validation base.

Linux x86_64, GCC 12.2, Release Result
Build libkernel and guest_filesystem_tests Passed on b69270dd + 1ba3c397
CTest guest_filesystem Passed (1/1)
Conventions against current main Passed
git diff --check Passed

This refresh is Linux evidence only. The Windows results remain those reported by Achraf and harmsluis on b5f5362d / fbf0ec0a, including restoring the read-only attribute when a held-open file cannot be deleted; I did not repeat Windows on b69270dd.

PR #2651 remains open at f42420e7, marked DIRTY, with 234 changed files and only Conventions complete. Achraf offered the narrow tested change to that PR's author; there has been no author update since. I have not opened a replacement PR: the intake gate still requires 20 merged PRs, and my current count is 15.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097575993 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2601#issuecomment-6097575993). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-10T12:38:45Z. --- ### Current-main refresh of the narrow #2601 patch I carried AchrafReyani's one-commit fix forward onto the latest upstream `main`, `b69270dd`, preserving him as the commit author. The refreshed commit is [`1ba3c397`](https://github.com/MarcZX100/AnyPS5/commit/1ba3c3974ecdc9801b4a6b95ca1f0cbde4db8e8a) on my fork branch [`codex/fix-unlink-readonly-2601`](https://github.com/MarcZX100/AnyPS5/tree/codex/fix-unlink-readonly-2601). The two touched files have not changed since the earlier `b5f5362d` validation base. | Linux x86_64, GCC 12.2, Release | Result | | --- | --- | | Build `libkernel` and `guest_filesystem_tests` | Passed on `b69270dd` + `1ba3c397` | | CTest `guest_filesystem` | Passed (1/1) | | Conventions against current `main` | Passed | | `git diff --check` | Passed | This refresh is Linux evidence only. The Windows results remain those reported by Achraf and harmsluis on `b5f5362d` / `fbf0ec0a`, including restoring the read-only attribute when a held-open file cannot be deleted; I did not repeat Windows on `b69270dd`. PR #2651 remains open at `f42420e7`, marked `DIRTY`, with 234 changed files and only Conventions complete. Achraf offered the narrow tested change to that PR's author; there has been no author update since. I have not opened a replacement PR: the intake gate still requires 20 merged PRs, and my current count is 15.
oneandonlydean commented 2026-10-10 18:08:49 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T18:08:49Z.


@AchrafReyani the intake check needs 10 merged PRs now, so your branch (or MarcZX100's refresh, 1ba3c397) can go up as a PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6100608815 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2601#issuecomment-6100608815). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T18:08:49Z. --- @AchrafReyani the intake check needs 10 merged PRs now, so your branch (or MarcZX100's refresh, 1ba3c397) can go up as a PR.
reyani-dev commented 2026-10-10 18:24:15 +00:00 (Migrated from github.com)
Author
Contributor

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


Thanks @oneandonlydean, it's up as #2910 (rebased onto current main, same single commit).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6100756546 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2601#issuecomment-6100756546). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T18:24:15Z. --- Thanks @oneandonlydean, it's up as #2910 (rebased onto current `main`, same single commit).
Sign in to join this conversation.
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#2601
No description provided.