fix(libc): fail with ENOTDIR for a file named with a trailing slash on Windows #9109

Merged
Retr0G28 merged 1 commit from upstream-pr/9109 into main 2026-10-23 13:08:04 +00:00
Retr0G28 commented 2026-10-21 12:33:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-21T12:33:03Z.


What

On Windows a path that names a regular file with a trailing slash (file/) did not fail with ENOTDIR. Under a mount alias such as /savedata0, ResolveAlias dropped the slash on Windows only, so unlink, remove, rename, open and sceKernelUnlink acted on the file. Outside an alias the slash reached the host, but Windows does not report ENOTDIR for file\ either: _wunlink fails with ERROR_DIRECTORY (267), which came back to the guest as EINVAL. FreeBSD 11.4's lookup() (sys/kern/vfs_lookup.c) fails with ENOTDIR when a path with a trailing slash ends at something that is not a directory (TRAILINGSLASH check).

#4681 fixed this for Linux and macOS and left Windows out, because Windows handles trailing separators differently (#4513) and it could not be tested there. This PR:

  • makes Resolve in core/libs/prx/libc/src/General.cpp throw ENOTDIR when the guest path ends in a slash and the host target exists and is not a directory (following symbolic links, as lookup() does with a trailing slash). Every libc and libkernel path call that resolves a guest path goes through it, so this covers open, sceKernelOpen, unlink, remove, sceKernelUnlink, rename (source and target), stat, access, chmod, mkdir and rmdir, under an alias or not. A directory with a trailing slash and a missing name with one are unchanged.
  • removes the #ifndef _WIN32 in ResolveAlias, so an alias keeps the trailing slash on every host, as paths outside an alias already do.

The Trailing slash on a mount alias entry is cut from docs/dev/debt/libc.md. One related gap remains and is recorded in docs/dev/debt/libkernel.md: open with O_CREAT on a missing new/ fails with EINVAL on Windows (and the host's EISDIR on Linux), where FreeBSD fails with ENOENT. That needs the operation in open and is a separate change.

Test changes in guest_filesystem (TestParentTraversal):

  • The trailing-slash block under the parent-test-mount alias now runs on every host, not only on Linux and macOS.
  • It also checks rename onto an existing file/, stat, access, chmod, mkdir, rmdir and sceKernelOpen(O_CREAT) on file/ (all ENOTDIR), unlink of file/ outside the alias, and sceKernelOpen("Existing/", O_DIRECTORY).

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, on main d099bc6cd; after rebasing onto main 2f161d4f3 (no changes to libc or libkernel), guest_filesystem, guest_posix_filesystem and guest_kernel_sys were run again and pass. Rebased again onto main 9d6adeb550 after #8797 merged: its dot-component checks are kept and the #ifndef _WIN32 below them is removed; guest_filesystem and file_streams pass.

  • Before the fix (General.cpp from main, new test kept), guest_filesystem fails at the first trailing-slash check: Filesystem check failed at line 252, unlink("/parent-test-mount/slash-unlink/") does not return -1 with errno 20. With only the #ifndef _WIN32 removed it fails at the same line with guest errno 22 (Win32 error 267).
  • With this PR, guest_filesystem passes.
  • 35 path-related tests pass when run serially: guest_filesystem, guest_posix_filesystem, guest_directory, guest_directory_rewind, guest_directory_entries, guest_directory_close, guest_directory_buffer, file_streams, file_position, file_identity, guest_kernel_sys, guest_kernel_rename, guest_rename_dot, guest_realpath, guest_working_directory, guest_path_case, guest_path_drive_component, guest_path_name_length, guest_statvfs, guest_fstatvfs, guest_fstatfs, guest_fdopendir, guest_fiopen, guest_fopen_long_host_path, guest_shm_open, guest_savedata_mount_status, savedata_replace_file_failure, guest_system_service_path_boundary, guest_app_archive_present, guest_app_archive_absent, guest_app_archive_damaged, guest_app_archive_invalid, guest_kernel_app_archive_archive, guest_kernel_app_archive_plain, guest_kernel_app_archive_mmap.
  • python tools/check_conventions.py --base origin/main reports no errors.
  • Second Windows machine: @xXPBSXx ran the PR head on Windows 10 (same toolchain): guest_filesystem passes 3 of 3, and fails 3 of 3 with main's General.cpp (comment below).
  • Not tested on Linux. There the host already fails with ENOTDIR for file/, so the new check returns the same error earlier; Linux CI runs the same guest_filesystem block it ran before, plus the new lines.
  • No title or homebrew run.

Overlap: tools/pr_overlap.py reports three PRs sharing lines. #8797 (now merged) added dot-component checks to TestParentTraversal in core/libs/tests/GuestFilesystem.cpp right above the #ifndef _WIN32 this PR removes, and this PR is rebased onto it; it makes unlink and rmdir of dir/. and dir/.. fail with EINVAL and trims the slash before resolving the parent, so it does not implement the same thing and does not change what this check sees. #8544 touches neighbouring lines of docs/dev/debt/libkernel.md only. #8546 removes the tzname entry directly above the docs/dev/debt/libc.md entry this PR removes, so whichever of the two merges second needs a one-line rebase. #8550 also edits core/libs/tests/GuestFilesystem.cpp, in the fopen directory checks in main, away from these lines. The script could not compare some PRs that conflict with main; of those touching General.cpp (#4627, #5021, #6544, #6879, #7201), none changes trailing-slash handling.

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 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951597913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9109). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-21T12:33:03Z. --- ### What On Windows a path that names a regular file with a trailing slash (`file/`) did not fail with `ENOTDIR`. Under a mount alias such as `/savedata0`, `ResolveAlias` dropped the slash on Windows only, so `unlink`, `remove`, `rename`, `open` and `sceKernelUnlink` acted on the file. Outside an alias the slash reached the host, but Windows does not report `ENOTDIR` for `file\` either: `_wunlink` fails with `ERROR_DIRECTORY` (267), which came back to the guest as `EINVAL`. FreeBSD 11.4's `lookup()` (`sys/kern/vfs_lookup.c`) fails with `ENOTDIR` when a path with a trailing slash ends at something that is not a directory (`TRAILINGSLASH` check). #4681 fixed this for Linux and macOS and left Windows out, because Windows handles trailing separators differently (#4513) and it could not be tested there. This PR: - makes `Resolve` in `core/libs/prx/libc/src/General.cpp` throw `ENOTDIR` when the guest path ends in a slash and the host target exists and is not a directory (following symbolic links, as `lookup()` does with a trailing slash). Every libc and libkernel path call that resolves a guest path goes through it, so this covers `open`, `sceKernelOpen`, `unlink`, `remove`, `sceKernelUnlink`, `rename` (source and target), `stat`, `access`, `chmod`, `mkdir` and `rmdir`, under an alias or not. A directory with a trailing slash and a missing name with one are unchanged. - removes the `#ifndef _WIN32` in `ResolveAlias`, so an alias keeps the trailing slash on every host, as paths outside an alias already do. The `Trailing slash on a mount alias` entry is cut from `docs/dev/debt/libc.md`. One related gap remains and is recorded in `docs/dev/debt/libkernel.md`: `open` with `O_CREAT` on a missing `new/` fails with `EINVAL` on Windows (and the host's `EISDIR` on Linux), where FreeBSD fails with `ENOENT`. That needs the operation in `open` and is a separate change. Test changes in `guest_filesystem` (`TestParentTraversal`): - The trailing-slash block under the `parent-test-mount` alias now runs on every host, not only on Linux and macOS. - It also checks `rename` onto an existing `file/`, `stat`, `access`, `chmod`, `mkdir`, `rmdir` and `sceKernelOpen(O_CREAT)` on `file/` (all `ENOTDIR`), `unlink` of `file/` outside the alias, and `sceKernelOpen("Existing/", O_DIRECTORY)`. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, on `main` d099bc6cd; after rebasing onto `main` 2f161d4f3 (no changes to libc or libkernel), `guest_filesystem`, `guest_posix_filesystem` and `guest_kernel_sys` were run again and pass. Rebased again onto `main` 9d6adeb550 after #8797 merged: its dot-component checks are kept and the `#ifndef _WIN32` below them is removed; `guest_filesystem` and `file_streams` pass. - Before the fix (`General.cpp` from `main`, new test kept), `guest_filesystem` fails at the first trailing-slash check: `Filesystem check failed at line 252`, `unlink("/parent-test-mount/slash-unlink/")` does not return -1 with errno 20. With only the `#ifndef _WIN32` removed it fails at the same line with guest errno 22 (Win32 error 267). - With this PR, `guest_filesystem` passes. - 35 path-related tests pass when run serially: `guest_filesystem`, `guest_posix_filesystem`, `guest_directory`, `guest_directory_rewind`, `guest_directory_entries`, `guest_directory_close`, `guest_directory_buffer`, `file_streams`, `file_position`, `file_identity`, `guest_kernel_sys`, `guest_kernel_rename`, `guest_rename_dot`, `guest_realpath`, `guest_working_directory`, `guest_path_case`, `guest_path_drive_component`, `guest_path_name_length`, `guest_statvfs`, `guest_fstatvfs`, `guest_fstatfs`, `guest_fdopendir`, `guest_fiopen`, `guest_fopen_long_host_path`, `guest_shm_open`, `guest_savedata_mount_status`, `savedata_replace_file_failure`, `guest_system_service_path_boundary`, `guest_app_archive_present`, `guest_app_archive_absent`, `guest_app_archive_damaged`, `guest_app_archive_invalid`, `guest_kernel_app_archive_archive`, `guest_kernel_app_archive_plain`, `guest_kernel_app_archive_mmap`. - `python tools/check_conventions.py --base origin/main` reports no errors. - Second Windows machine: @xXPBSXx ran the PR head on Windows 10 (same toolchain): `guest_filesystem` passes 3 of 3, and fails 3 of 3 with `main`'s `General.cpp` (comment below). - Not tested on Linux. There the host already fails with `ENOTDIR` for `file/`, so the new check returns the same error earlier; Linux CI runs the same `guest_filesystem` block it ran before, plus the new lines. - No title or homebrew run. Overlap: `tools/pr_overlap.py` reports three PRs sharing lines. #8797 (now merged) added dot-component checks to `TestParentTraversal` in `core/libs/tests/GuestFilesystem.cpp` right above the `#ifndef _WIN32` this PR removes, and this PR is rebased onto it; it makes `unlink` and `rmdir` of `dir/.` and `dir/..` fail with `EINVAL` and trims the slash before resolving the parent, so it does not implement the same thing and does not change what this check sees. #8544 touches neighbouring lines of `docs/dev/debt/libkernel.md` only. #8546 removes the `tzname` entry directly above the `docs/dev/debt/libc.md` entry this PR removes, so whichever of the two merges second needs a one-line rebase. #8550 also edits `core/libs/tests/GuestFilesystem.cpp`, in the `fopen` directory checks in `main`, away from these lines. The script could not compare some PRs that conflict with `main`; of those touching `General.cpp` (#4627, #5021, #6544, #6879, #7201), none changes trailing-slash handling. ### 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) - [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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
xXPBSXx commented 2026-10-21 16:44:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T16:44:05Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main is clean. I built only guest_filesystem_tests; ctest -N -R guest_filesystem lists exactly one test.

PR head tested: c44e884cab97d7591827b62039d68b4bc50e38a9.

Results (3 runs each):

  • PR head: guest_filesystem passes 3/3.
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libc/src/General.cpp with the version from the PR's merge base (2f161d4f33), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards.

GPU: the run output has no Selected GPU line; no GPU claim is made.

Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6321368494 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9109#issuecomment-6321368494). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T16:44:05Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` is clean. I built only `guest_filesystem_tests`; `ctest -N -R guest_filesystem` lists exactly one test. **PR head tested:** `c44e884cab97d7591827b62039d68b4bc50e38a9`. **Results (3 runs each):** - PR head: `guest_filesystem` passes 3/3. - Baseline: I kept the PR's test and replaced only `core/libs/prx/libc/src/General.cpp` with the version from the PR's merge base (`2f161d4f33`), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards. **GPU:** the run output has no `Selected GPU` line; no GPU claim is made. **Not run:** other platforms, the other tests in the same family, the full repository CTest, and any title or console run. AI-assisted (Claude Code).
DavidSamir commented 2026-10-22 08:50:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T08:50:05Z.


Hi @Retr0G28, the same note as on #8544: my #9557 and #9561 edit the fcntl row of docs/dev/debt/libkernel.md that this PR also changes, and they conflict in that file only. Proposal: no dependency either way; whichever merges second keeps every sentence. OK with you?

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6336711799 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9109#issuecomment-6336711799). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T08:50:05Z. --- Hi @Retr0G28, the same note as on #8544: my #9557 and #9561 edit the `fcntl` row of `docs/dev/debt/libkernel.md` that this PR also changes, and they conflict in that file only. Proposal: no dependency either way; whichever merges second keeps every sentence. OK with you? AI-assisted: yes
Retr0G28 commented 2026-10-22 15:27:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-22T15:27:50Z.


@DavidSamir Agreed, no dependency either way. This PR doesn't change the fcntl row itself: it adds the "Creating through a trailing slash" entry directly above it in docs/dev/debt/libkernel.md, so the conflict with #9557 and #9561 is only the two adjacent edits. Whoever merges second keeps both.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6344533827 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9109#issuecomment-6344533827). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-22T15:27:50Z. --- @DavidSamir Agreed, no dependency either way. This PR doesn't change the `fcntl` row itself: it adds the "Creating through a trailing slash" entry directly above it in `docs/dev/debt/libkernel.md`, so the conflict with #9557 and #9561 is only the two adjacent edits. Whoever merges second keeps both.
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!9109
No description provided.