fix(libc): remove read-only files on Windows #2342

Merged
reyani-dev merged 2 commits from upstream-pr/2342 into main 2026-10-10 07:07:47 +00:00
reyani-dev commented 2026-10-09 15:57:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T15:57:51Z.


What

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

On Windows, the guest chmod (_wchmod) sets FILE_ATTRIBUTE_READONLY when the write bit is cleared. DeleteFileW then fails with ERROR_ACCESS_DENIED, so remove returned -1 with EACCES, and the file stayed.

When the entry carries FILE_ATTRIBUTE_READONLY (the attributes are already read), remove now clears it before the single delete. If the delete fails (e.g. the file is open without delete sharing), it restores the original attributes and reports the delete's error, so a failed remove leaves the file as it was. Linux is unchanged.

Tested

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

  • guest_filesystem: a file made read-only with sceKernelChmod(0400) is removed by remove, and no longer exists.
    • On main it fails at that 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: remove fails (EBUSY), the file stays, and FILE_ATTRIBUTE_READONLY is restored;
    • guest chmod never sets the attribute on directories, so directory removal is unaffected.
  • ctest -E "^agc_|video_out": 249/249.
  • tools/check_conventions.py passes, and tools/pr_overlap.py --branch reports no overlap. It also merges cleanly with the open PRs that touch GuestFilesystem.cpp and Filesystem.cpp: #1192, #1620, #1624, #1628, #1755, #1764, #1777, #2086, #2115, #2129, #2170, #2260, #2289 and #2300.

sceKernelUnlink/unlink in libkernel likely behave the same way on Windows. They are left for a separate PR, because #2289 changes that function.

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:4802806353 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2342). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T15:57:51Z. --- ### What On FreeBSD, `unlink` (and so `remove`) ignores the file's own permission bits: what matters is write access to the directory. A guest that does `chmod(path, 0400)` and later `remove(path)` therefore succeeds on the console. On Windows, the guest `chmod` (`_wchmod`) sets `FILE_ATTRIBUTE_READONLY` when the write bit is cleared. `DeleteFileW` then fails with `ERROR_ACCESS_DENIED`, so `remove` returned -1 with `EACCES`, and the file stayed. When the entry carries `FILE_ATTRIBUTE_READONLY` (the attributes are already read), `remove` now clears it before the single delete. If the delete fails (e.g. the file is open without delete sharing), it restores the original attributes and reports the delete's error, so a failed `remove` leaves the file as it was. Linux is unchanged. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, `-DBUILD_TESTING=ON`: - `guest_filesystem`: a file made read-only with `sceKernelChmod(0400)` is removed by `remove`, and no longer exists. - On `main` it fails at that 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: `remove` fails (`EBUSY`), the file stays, and `FILE_ATTRIBUTE_READONLY` is restored; - guest `chmod` never sets the attribute on directories, so directory removal is unaffected. - `ctest -E "^agc_|video_out"`: 249/249. - `tools/check_conventions.py` passes, and `tools/pr_overlap.py --branch` reports no overlap. It also merges cleanly with the open PRs that touch `GuestFilesystem.cpp` and `Filesystem.cpp`: #1192, #1620, #1624, #1628, #1755, #1764, #1777, #2086, #2115, #2129, #2170, #2260, #2289 and #2300. `sceKernelUnlink`/`unlink` in libkernel likely behave the same way on Windows. They are left for a separate PR, because #2289 changes that function. 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)
reyani-dev commented 2026-10-09 16:30:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T16:30:01Z.


Full Linux and Windows builds and tests for this head (58e18f08), 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 (d70b8998):

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

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084994479 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2342#issuecomment-6084994479). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T16:30:01Z. --- Full Linux and Windows builds and tests for this head (`58e18f08`), 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` (`d70b8998`): | Job | Result | |---|---| | [linux](https://github.com/AchrafReyani/AnyPS5/actions/runs/37955618179/job/113905307610) | success; full build, `ctest`: 516/516 passed | | [windows](https://github.com/AchrafReyani/AnyPS5/actions/runs/37955618179/job/113905307401) | success; full build, `ctest`: 507/507 passed | | relinker (Linux, Windows, macOS) | success | Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37955618179
oneandonlydean commented 2026-10-09 20:15:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T20:15:23Z.


remove_nid_postfix deletes, and on ERROR_ACCESS_DENIED clears the read-only attribute and deletes again (Filesystem.cpp:121-136). Retrying after an error isn't allowed here; attributes is already read, so please clear FILE_ATTRIBUTE_READONLY before the first delete when it's set (restoring it if that delete fails), and drop the second attempt.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088519993 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2342#issuecomment-6088519993). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T20:15:23Z. --- `remove_nid_postfix` deletes, and on `ERROR_ACCESS_DENIED` clears the read-only attribute and deletes again (`Filesystem.cpp:121-136`). Retrying after an error isn't allowed here; `attributes` is already read, so please clear `FILE_ATTRIBUTE_READONLY` before the first delete when it's set (restoring it if that delete fails), and drop the second attempt.
reyani-dev commented 2026-10-09 20:24:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T20:24:54Z.


Done in e8d4c022: remove now clears FILE_ATTRIBUTE_READONLY before the single delete when it is set, and restores the original attributes if that delete fails. The second attempt is gone.

I rechecked on Windows:

  • guest_filesystem passes;
  • a read-only file held open by another handle still fails with EBUSY, stays in place and keeps its read-only attribute;
  • ctest -E "^agc_|video_out" is 249/249, and check_conventions.py passes.

The Linux and Windows full build for the new head is running and will be posted here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088661588 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2342#issuecomment-6088661588). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T20:24:54Z. --- Done in e8d4c022: `remove` now clears `FILE_ATTRIBUTE_READONLY` before the single delete when it is set, and restores the original attributes if that delete fails. The second attempt is gone. I rechecked on Windows: - `guest_filesystem` passes; - a read-only file held open by another handle still fails with `EBUSY`, stays in place and keeps its read-only attribute; - `ctest -E "^agc_|video_out"` is 249/249, and `check_conventions.py` passes. The Linux and Windows full build for the new head is running and will be posted here.
reyani-dev commented 2026-10-09 20:43:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T20:43:14Z.


Full Linux and Windows builds and tests for this head (e8d4c022), 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 (d70b8998):

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

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088927936 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2342#issuecomment-6088927936). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T20:43:14Z. --- Full Linux and Windows builds and tests for this head (`e8d4c022`), 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` (`d70b8998`): | Job | Result | |---|---| | [linux](https://github.com/AchrafReyani/AnyPS5/actions/runs/37986819849/job/114010835547) | success; full build, `ctest`: 516/516 passed | | [windows](https://github.com/AchrafReyani/AnyPS5/actions/runs/37986819849/job/114010835298) | success; full build, `ctest`: 507/507 passed | | relinker (Linux, Windows, macOS) | success | Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37986819849
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!2342
No description provided.