fix(libc): change into a directory reached through a substituted drive on Windows #24149

Merged
XFlawless1 merged 3 commits from upstream-pr/24149 into main 2026-11-21 05:17:40 +00:00
XFlawless1 commented 2026-11-20 08:01:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XFlawless1 on 2026-11-20T08:01:34Z.


What

Depends on #24114: this branch contains its commit. Only the last commit (fix(libc): change into a directory reached through a substituted drive on Windows) belongs to this PR; I will rebase once #24114 is merged.

On Windows, fchdir into a directory fails with error 45 when the emulator was started from a substituted drive (subst, DefineDosDevice), even for a short path. fchdir gets the directory's final path from its descriptor, which names the drive's target (C:\...), while the root keeps the letter it was started from (W:\...). SetWorkingDirectory relates the two by walking up with std::filesystem::equivalent, which MinGW's libstdc++ decides by drive letter, so no ancestor ever matched.

  • When the canonical path is not lexically under the root, SetWorkingDirectory now compares the final paths of the directory and of the root first (FinalDirectoryPath, added in #24114) and uses that relation; the equivalent walk stays for anything they do not relate.
  • guest_kernel_sys_substituted_drive (Windows, new executable) maps a free drive letter to a temporary directory with DefineDosDeviceW, starts there, opens a subdirectory, and checks fchdir and getcwd. It removes the mapping and the directory on every exit, including a failed check, and returns 77 (skipped) when no letter can be mapped. It is a separate executable because the root is taken from the working directory at the first libc path call.

Tested

Windows 11, MinGW-w64 GCC (WinLibs UCRT), Ninja, Release, AMD Ryzen 7 9800X3D. Found with the build directory on a subst drive: with #24114 and #24130 applied, guest_kernel_sys stopped at its fchdir(directory) check with errno 45.

  • With General.cpp from #24114, the new test fails at fchdir with errno 45 and leaves no drive mapping or directory behind. With the change it passes, three runs in a row, also inside the parallel full run.
  • Full ctest -j16 (6471 tests) against the #24114 run: the only differences are agc_driver_store_over_untracked (#22670) and agc_driver, which flip between runs under the parallel load.
  • tools/check_conventions.py --base origin/main passes.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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: #24114
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5409912649 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24149). Originally posted by [XFlawless1](https://github.com/XFlawless1) on 2026-11-20T08:01:34Z. --- ### What Depends on #24114: this branch contains its commit. Only the last commit (`fix(libc): change into a directory reached through a substituted drive on Windows`) belongs to this PR; I will rebase once #24114 is merged. On Windows, `fchdir` into a directory fails with error 45 when the emulator was started from a substituted drive (`subst`, `DefineDosDevice`), even for a short path. `fchdir` gets the directory's final path from its descriptor, which names the drive's target (`C:\...`), while the root keeps the letter it was started from (`W:\...`). `SetWorkingDirectory` relates the two by walking up with `std::filesystem::equivalent`, which MinGW's libstdc++ decides by drive letter, so no ancestor ever matched. - When the canonical path is not lexically under the root, `SetWorkingDirectory` now compares the final paths of the directory and of the root first (`FinalDirectoryPath`, added in #24114) and uses that relation; the `equivalent` walk stays for anything they do not relate. - `guest_kernel_sys_substituted_drive` (Windows, new executable) maps a free drive letter to a temporary directory with `DefineDosDeviceW`, starts there, opens a subdirectory, and checks `fchdir` and `getcwd`. It removes the mapping and the directory on every exit, including a failed check, and returns 77 (skipped) when no letter can be mapped. It is a separate executable because the root is taken from the working directory at the first libc path call. ### Tested Windows 11, MinGW-w64 GCC (WinLibs UCRT), Ninja, Release, AMD Ryzen 7 9800X3D. Found with the build directory on a `subst` drive: with #24114 and #24130 applied, `guest_kernel_sys` stopped at its `fchdir(directory)` check with errno 45. - With `General.cpp` from #24114, the new test fails at `fchdir` with errno 45 and leaves no drive mapping or directory behind. With the change it passes, three runs in a row, also inside the parallel full run. - Full `ctest -j16` (6471 tests) against the #24114 run: the only differences are `agc_driver_store_over_untracked` (#22670) and `agc_driver`, which flip between runs under the parallel load. - `tools/check_conventions.py --base origin/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: #24114 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-20 08:04:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T08:04:46Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 7a254b3875:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_sys_tests, guest_working_directory_tests built cleanly.
  • CTest (guest_kernel_sys, guest_working_directory):
    1/2 Test #1298: guest_working_directory ..........   Passed    0.02 sec
    2/2 Test  #947: guest_kernel_sys .................   Passed    0.09 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libc/src/General.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #1298: guest_working_directory ..........   Passed    0.04 sec
    2/2 Test  #947: guest_kernel_sys .................   Passed    0.10 sec
    100% tests passed, 0 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified SetWorkingDirectory in core/libs/prx/libc/src/General.cpp:596-608 correctly computes lexically_relative between normalized FinalDirectoryPath of target and root when canonical paths are not lexically under state.root
    • Verified FinalDirectoryPath correctly handles MAX_PATH buffer sizing, dynamic resizing with GetFinalPathNameByHandleW, and normalizes UNC and verbatim device prefixes (\\?\UNC\ and \\?\)
    • Verified IsDirectory and SetLongWorkingDirectory handle paths exceeding MAX_PATH via NativeVerbatimPath_nid_no_patch without buffer overruns or API failures
    • Verified GuestKernelSysSubstitutedDrive test properly cleans up with DDD_REMOVE_DEFINITION and directory removal on all return and abort paths via RAII/Cleanup helper, returning code 77 when no free drive letter exists
    • Verified AGENTS.md compliance: no unauthorized comments, all Conventional Commits guidelines adhered to, and no invalid platform or compiler assumptions

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5989360098 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24149#pullrequestreview-5989360098). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T08:04:46Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `7a254b3875`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_sys_tests`, `guest_working_directory_tests` built cleanly. - **CTest** (`guest_kernel_sys`, `guest_working_directory`): ``` 1/2 Test #1298: guest_working_directory .......... Passed 0.02 sec 2/2 Test #947: guest_kernel_sys ................. Passed 0.09 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libc/src/General.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/2 Test #1298: guest_working_directory .......... Passed 0.04 sec 2/2 Test #947: guest_kernel_sys ................. Passed 0.10 sec 100% tests passed, 0 tests failed out of 2 ``` Restoring the change makes it pass again. - **Code reading**: - Verified SetWorkingDirectory in core/libs/prx/libc/src/General.cpp:596-608 correctly computes lexically_relative between normalized FinalDirectoryPath of target and root when canonical paths are not lexically under state.root - Verified FinalDirectoryPath correctly handles MAX_PATH buffer sizing, dynamic resizing with GetFinalPathNameByHandleW, and normalizes UNC and verbatim device prefixes (\\\\?\\UNC\\ and \\\\?\\) - Verified IsDirectory and SetLongWorkingDirectory handle paths exceeding MAX_PATH via NativeVerbatimPath_nid_no_patch without buffer overruns or API failures - Verified GuestKernelSysSubstitutedDrive test properly cleans up with DDD_REMOVE_DEFINITION and directory removal on all return and abort paths via RAII/Cleanup helper, returning code 77 when no free drive letter exists - Verified AGENTS.md compliance: no unauthorized comments, all Conventional Commits guidelines adhered to, and no invalid platform or compiler assumptions Looks Good To Me
MarcZX10 commented 2026-11-20 08:30:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T08:30:01Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected code supports the described Windows directory handling and adds targeted regression coverage.

What I ran

Reviewed head 7a254b387596c4eb1ae083ac100da7bc7c8b07c6 using repo_read.py to inspect the prepared description, commits, manifest, diff, and changed file contents. No build or tests were run in this review.

Results

Check Result Note
Diff and changed code review passed All changed files inspected
Build and tests not run No local execution available

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build/test results reported in the PR description were not independently run. Read all five changed files listed in the manifest; files.json was not available in the prepared review interface. No changed files were excluded from this review.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5989617212 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24149#pullrequestreview-5989617212). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T08:30:01Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected code supports the described Windows directory handling and adds targeted regression coverage. ## What I ran Reviewed head `7a254b387596c4eb1ae083ac100da7bc7c8b07c6` using `repo_read.py` to inspect the prepared description, commits, manifest, diff, and changed file contents. No build or tests were run in this review. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code review | passed | All changed files inspected | | Build and tests | not run | No local execution available | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Build/test results reported in the PR description were not independently run. Read all five changed files listed in the manifest; `files.json` was not available in the prepared review interface. No changed files were excluded from this review. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-21 04:10:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T04:10:22Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head 37af04807707a8c6456a7d4be5e468f8f3a3c24f using the pinned repo_read.py reader. No build or tests were run.

Results

Check Result Note
Diff and targeted source review passed Five changed files read
Build and tests not run Author reports Windows runs

Findings

Blocking

None.

Non-blocking

None.

Not verified

No programmatic checks were run. The author’s Windows test results were not independently verified. Read description.md, commits.md, manifest.json, and diff.patch; read all five changed files listed in the manifest. No files were excluded from review.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002738870 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24149#pullrequestreview-6002738870). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T04:10:22Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `37af04807707a8c6456a7d4be5e468f8f3a3c24f` using the pinned `repo_read.py` reader. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Five changed files read | | Build and tests | not run | Author reports Windows runs | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No programmatic checks were run. The author’s Windows test results were not independently verified. Read `description.md`, `commits.md`, `manifest.json`, and `diff.patch`; read all five changed files listed in the manifest. No files were excluded from review. ## To get this to a good state Nothing is required.
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!24149
No description provided.