fix(libkernel): report POSIX rename errors from sceKernelRename #2260

Closed
reyani-dev wants to merge 2 commits from upstream-pr/2260 into main
reyani-dev commented 2026-10-09 13:35:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T13:35:03Z.


What

sceKernelRename returned ENOENT for a missing source and EIO for every other failure. On Windows the rename itself also went wrong: MinGW's std::filesystem::rename reports some missing paths as EIO and refuses to replace an existing empty directory, which FreeBSD's rename(2) does. Measured on main (d70b8998) with libstdc++'s std::filesystem::rename (generic category):

Case Windows std::filesystem::rename sceKernelRename on main FreeBSD rename(2)
source missing, target missing EIO ENOENT ENOENT
target's directory missing EIO EIO ENOENT
target's directory is a file EIO ENOTDIR
directory onto a file EIO ENOTDIR
file onto a directory EIO EISDIR
directory onto a non-empty directory EACCES EIO ENOTEMPTY
directory into its own subtree EIO EIO EINVAL
directory onto an empty directory EACCES fails with EIO replaces it

RenameError_nid_no_patch checks these rename(2) conditions before renaming and returns the guest error, the way sceKernelRmdir already checks its own. On Windows it removes the empty target directory first, so the replace succeeds as on the console. A rename onto the same file, including a case-only rename on Windows, still goes straight to std::filesystem::rename. Any other host failure is still EIO.

libc's rename has the same Windows behaviour (it calls std::filesystem::rename too). I left it alone because #1192 is moving that function into a new file; I can send the same fix for it afterwards.

Tested

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

  • New sceKernelRename checks in guest_filesystem_tests, next to the libc rename checks:

    • every row of the table, checked against the exact 0x8002xxxx codes;
    • a failed rename leaves the source tree in place;
    • file replace keeps the moved contents;
    • renaming a file onto itself still succeeds;
    • directory onto an empty directory replaces it with its contents.

    On main the test fails on the first new check (guest errno 5).

  • Mutation check: dropping the Windows empty-directory removal, or the subtree EINVAL check, each make guest_filesystem fail.

  • ctest -E "^agc_|video_out": 249/249 passed, including guest_kernel_sync, which renames through sceKernelRename.

  • tools/check_conventions.py passes, and tools/pr_overlap.py --branch reports no overlap. It also merges cleanly with #1192, #2170, #2115, #1620, #2129 and #2086, which touch the same files (git merge-tree).

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:4801152626 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2260). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T13:35:03Z. --- ### What `sceKernelRename` returned `ENOENT` for a missing source and `EIO` for every other failure. On Windows the rename itself also went wrong: MinGW's `std::filesystem::rename` reports some missing paths as `EIO` and refuses to replace an existing empty directory, which FreeBSD's `rename(2)` does. Measured on `main` (`d70b8998`) with libstdc++'s `std::filesystem::rename` (generic category): | Case | Windows `std::filesystem::rename` | `sceKernelRename` on `main` | FreeBSD `rename(2)` | |---|---|---|---| | source missing, target missing | `EIO` | `ENOENT` | `ENOENT` | | target's directory missing | `EIO` | `EIO` | `ENOENT` | | target's directory is a file | | `EIO` | `ENOTDIR` | | directory onto a file | | `EIO` | `ENOTDIR` | | file onto a directory | | `EIO` | `EISDIR` | | directory onto a non-empty directory | `EACCES` | `EIO` | `ENOTEMPTY` | | directory into its own subtree | `EIO` | `EIO` | `EINVAL` | | directory onto an empty directory | `EACCES` | fails with `EIO` | replaces it | `RenameError_nid_no_patch` checks these `rename(2)` conditions before renaming and returns the guest error, the way `sceKernelRmdir` already checks its own. On Windows it removes the empty target directory first, so the replace succeeds as on the console. A rename onto the same file, including a case-only rename on Windows, still goes straight to `std::filesystem::rename`. Any other host failure is still `EIO`. libc's `rename` has the same Windows behaviour (it calls `std::filesystem::rename` too). I left it alone because #1192 is moving that function into a new file; I can send the same fix for it afterwards. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, `-DBUILD_TESTING=ON`: - New `sceKernelRename` checks in `guest_filesystem_tests`, next to the libc `rename` checks: - every row of the table, checked against the exact `0x8002xxxx` codes; - a failed rename leaves the source tree in place; - file replace keeps the moved contents; - renaming a file onto itself still succeeds; - directory onto an empty directory replaces it with its contents. On `main` the test fails on the first new check (`guest errno 5`). - Mutation check: dropping the Windows empty-directory removal, or the subtree `EINVAL` check, each make `guest_filesystem` fail. - `ctest -E "^agc_|video_out"`: 249/249 passed, including `guest_kernel_sync`, which renames through `sceKernelRename`. - `tools/check_conventions.py` passes, and `tools/pr_overlap.py --branch` reports no overlap. It also merges cleanly with #1192, #2170, #2115, #1620, #2129 and #2086, which touch the same files (`git merge-tree`). ### 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)
MarcZX10 commented 2026-10-09 13:51:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T13:51:59Z.


I found one error-mapping case to resolve before approval. symlink_status(source, error) and status(parent, error) set error when the path cannot be inspected (for example, permission_denied), but the helper only checks exists(status). A failed status then looks like file_type::not_found and is returned as guest ENOENT, masking the actual access failure. Please inspect error before mapping a not-found status, preserve the applicable POSIX error (or the documented EIO fallback), and add a regression where practical. The current PR checks skip full Linux/Windows Build, so they do not exercise this path.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082295401 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2260#issuecomment-6082295401). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T13:51:59Z. --- I found one error-mapping case to resolve before approval. `symlink_status(source, error)` and `status(parent, error)` set `error` when the path cannot be inspected (for example, `permission_denied`), but the helper only checks `exists(status)`. A failed status then looks like `file_type::not_found` and is returned as guest `ENOENT`, masking the actual access failure. Please inspect `error` before mapping a not-found status, preserve the applicable POSIX error (or the documented `EIO` fallback), and add a regression where practical. The current PR checks skip full Linux/Windows Build, so they do not exercise this path.
reyani-dev commented 2026-10-09 13:58:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T13:58:47Z.


Full Linux and Windows builds and tests for this head (ef5a4f3e), 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/37937885788

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082411219 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2260#issuecomment-6082411219). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T13:58:47Z. --- Full Linux and Windows builds and tests for this head (`ef5a4f3e`), 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/37937885788/job/113844509386) | success; full build, `ctest`: 516/516 passed | | [windows](https://github.com/AchrafReyani/AnyPS5/actions/runs/37937885788/job/113844509432) | success; full build, `ctest`: 507/507 passed | | relinker (Linux, Windows, macOS) | success | Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37937885788
MarcZX10 commented 2026-10-09 14:00:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T14:00:56Z.


Thanks for the exact-head fork Build report. The Linux 516/516 and Windows 507/507 suites plus the relinker matrix are useful evidence and close the general build/test gap. The specific status-inspection case I raised remains untested by the listed sceKernelRename cases: when symlink_status(source, error) or status(parent, error) reports an inspection error, the helper still needs to distinguish that from a genuine not-found status before returning guest ENOENT. I am keeping that focused finding open; the full suites do not currently exercise permission-denied metadata inspection.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082449161 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2260#issuecomment-6082449161). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T14:00:56Z. --- Thanks for the exact-head fork Build report. The Linux 516/516 and Windows 507/507 suites plus the relinker matrix are useful evidence and close the general build/test gap. The specific status-inspection case I raised remains untested by the listed `sceKernelRename` cases: when `symlink_status(source, error)` or `status(parent, error)` reports an inspection error, the helper still needs to distinguish that from a genuine not-found status before returning guest `ENOENT`. I am keeping that focused finding open; the full suites do not currently exercise permission-denied metadata inspection.
MarcZX10 commented 2026-10-09 14:02:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T14:02:25Z.

Code review location: core/libs/prx/libkernel/File/src/Stdio.cpp; line 638

@@ -630,14 +630,44 @@ int64_t APS5_VABI sceKernelPwritev(int d, const KernelIovec* iov, int iovcnt, in
 
 #endif
 
+static int RenameError_nid_no_patch(const std::filesystem::path& source, const std::filesystem::path& destination) {
+    constexpr int GuestEisdir = 21;
+    namespace fs = std::filesystem;
+    std::error_code error;
+    const auto sourceStatus = fs::symlink_status(source, error);
+    if (!fs::exists(sourceStatus)) return GUEST_ENOENT;

Could check error before treating !exists(sourceStatus) as ENOENT? symlink_status can set permission_denied while the returned status is not_found, so this branch conflates an inspection failure with a genuinely missing source. The destination-parent and destination status checks below have the same pattern. Please preserve the applicable guest error (or the documented EIO fallback) when inspection fails.

<!-- greyfinch-sync:boykopovar/AnyPS5:review-comment:4230947692 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2260#discussion_r4230947692). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T14:02:25Z. **Code review location:** `core/libs/prx/libkernel/File/src/Stdio.cpp`; line 638 ~~~~diff @@ -630,14 +630,44 @@ int64_t APS5_VABI sceKernelPwritev(int d, const KernelIovec* iov, int iovcnt, in #endif +static int RenameError_nid_no_patch(const std::filesystem::path& source, const std::filesystem::path& destination) { + constexpr int GuestEisdir = 21; + namespace fs = std::filesystem; + std::error_code error; + const auto sourceStatus = fs::symlink_status(source, error); + if (!fs::exists(sourceStatus)) return GUEST_ENOENT; ~~~~ --- Could check `error` before treating `!exists(sourceStatus)` as `ENOENT`? `symlink_status` can set `permission_denied` while the returned status is `not_found`, so this branch conflates an inspection failure with a genuinely missing source. The destination-parent and destination status checks below have the same pattern. Please preserve the applicable guest error (or the documented `EIO` fallback) when inspection fails.
reyani-dev commented 2026-10-09 14:43:34 +00:00 (Migrated from github.com)

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


Good catch, thanks. Fixed in 2545929d:

  • sceKernelRename now reads the error that symlink_status/status set. libstdc++ also sets it for a plain miss (ENOENT/ENOTDIR with type not_found), so the mapping is:
    • not_a_directory → ENOTDIR;
    • any other not_found → ENOENT;
    • permission_denied → EACCES;
    • anything else → the EIO fallback.
  • The destination's status is checked the same way: absent is still fine, but any other inspection failure is returned instead of being read as "absent".
  • Regression (Linux, skipped as root): a directory with perms::none. Renaming its entry and renaming into it must both return 0x8002000D and leave the files in place. Windows has no comparable way to deny traversal, so the block is #ifndef _WIN32.

To show the test fails without the fix, I'm running it two ways on the fork's Build workflow: this head, and a control branch with only the new test on the previous head (ef5a4f3e). I'll post both results here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6083197512 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2260#issuecomment-6083197512). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T14:43:34Z. --- Good catch, thanks. Fixed in `2545929d`: - `sceKernelRename` now reads the `error` that `symlink_status`/`status` set. libstdc++ also sets it for a plain miss (`ENOENT`/`ENOTDIR` with type `not_found`), so the mapping is: - `not_a_directory` → `ENOTDIR`; - any other `not_found` → `ENOENT`; - `permission_denied` → `EACCES`; - anything else → the `EIO` fallback. - The destination's status is checked the same way: absent is still fine, but any other inspection failure is returned instead of being read as "absent". - Regression (Linux, skipped as root): a directory with `perms::none`. Renaming its entry and renaming into it must both return `0x8002000D` and leave the files in place. Windows has no comparable way to deny traversal, so the block is `#ifndef _WIN32`. To show the test fails without the fix, I'm running it two ways on the fork's Build workflow: this head, and a control branch with only the new test on the previous head (`ef5a4f3e`). I'll post both results here.
reyani-dev commented 2026-10-09 15:14:37 +00:00 (Migrated from github.com)

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


Results for 2545929d and the control. Both ran the project's build.yml (full builds plus ctest) in my fork, merged into main d70b8998:

Run Linux Windows
This head 2545929d 516/516 passed 507/507 passed
Control: the new test on the previous head ef5a4f3e failed, 1/516: guest_filesystem — Filesystem check failed at line 148 (guest errno 13) 507/507 passed (the check is #ifndef _WIN32)

Line 148 is the new perms::none check. Without the inspection fix, a source the kernel couldn't stat came back as ENOENT instead of EACCES. With it, both renames return 0x8002000D and the files stay where they were.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6083736919 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2260#issuecomment-6083736919). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T15:14:37Z. --- Results for `2545929d` and the control. Both ran the project's `build.yml` (full builds plus `ctest`) in my fork, merged into `main` `d70b8998`: | Run | Linux | Windows | |---|---|---| | This head `2545929d` | [516/516 passed](https://github.com/AchrafReyani/AnyPS5/actions/runs/37946297042/job/113873351298) | [507/507 passed](https://github.com/AchrafReyani/AnyPS5/actions/runs/37946297042/job/113873351377) | | Control: the new test on the previous head `ef5a4f3e` | [**failed**](https://github.com/AchrafReyani/AnyPS5/actions/runs/37946292445/job/113873335339), 1/516: `guest_filesystem` — `Filesystem check failed at line 148 (guest errno 13)` | [507/507 passed](https://github.com/AchrafReyani/AnyPS5/actions/runs/37946292445/job/113873335386) (the check is `#ifndef _WIN32`) | Line 148 is the new `perms::none` check. Without the inspection fix, a source the kernel couldn't stat came back as `ENOENT` instead of `EACCES`. With it, both renames return `0x8002000D` and the files stay where they were.
oneandonlydean commented 2026-10-09 20:10:48 +00:00 (Migrated from github.com)

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


Closing: no title is shown stopping on sceKernelRename's error codes or on the Windows empty-directory replace on main. Reopen with the title ID and the log line if one does.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088454336 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2260#issuecomment-6088454336). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T20:10:48Z. --- Closing: no title is shown stopping on `sceKernelRename`'s error codes or on the Windows empty-directory replace on main. Reopen with the title ID and the log line if one does.

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