fix(libkernel): fail unlink of a file named with a trailing slash on Windows #5165

Closed
DavidSamir wants to merge 2 commits from upstream-pr/5165 into main
DavidSamir commented 2026-10-13 13:22:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T13:22:39Z.


What

On Windows, unlink("file/") and sceKernelUnlink("file/") delete the file. On FreeBSD a name with a trailing slash must be a directory: lookup fails with ENOTDIR when the path ends in a slash and the result is not a directory, and the Linux build returns ENOTDIR and keeps the file. The same happens for file//, file/. and file/./.

A program that removes "the directory entry name/" expecting it to fail on anything that is not a directory loses the file instead.

Cause. sceKernelUnlink passes the resolved path, which keeps the trailing separator, to _wunlink. The Win32 delete accepts a separator after a file's name: on this machine _wunlink(L"a/") on a regular file a returns 0 and the file is gone. file/. is the same path after the resolver normalizes it.

Observed on main 5d50886ee, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. file is a regular file:

main on Windows Linux build
sceKernelUnlink("file/") 0, file deleted 0x80020014 (ENOTDIR), file kept
unlink("file/") 0, file deleted -1, errno 20, file kept
the same with file//, file/., file/./, dir/file/, dir/sub/../file/ 0, file deleted ENOTDIR, file kept
sceKernelUnlink("file/"), file has mode 0444 0x8002000D (EACCES) 0x80020014
sceKernelUnlink("file/"), file open through a descriptor 0x8002000D 0x80020014
remove("file/") -1, errno 20 -1, errno 20

I have not found a title that stops on this. It came out of running one probe of the path functions on the Windows build and on the Linux build and comparing the two outputs.

Closes #5164. Two commits:

  1. fix(libkernel): fail unlink of a file named with a trailing slash on Windows: Open.cpp.
  2. test(libkernel): unlink a file named with a trailing slash: guest_filesystem.

Windows only, in sceKernelUnlink, which unlink calls. When the resolved path ends in a separator, the name without it is looked up, and if it exists and is not a directory the call returns ENOTDIR before anything is deleted. The check runs only for such paths, so an ordinary unlink makes the same host calls as before. A directory named with a trailing slash still returns EPERM and a missing name ENOENT, as on main.

Not changed, and still different from FreeBSD: a path that runs through a regular file (file/x) gives ENOENT on Windows where FreeBSD gives ENOTDIR, in unlink and in most other path functions; and open, rename, truncate, utimes and lstat also accept file/ on Windows. I kept this change to the call that destroys data.

guest_filesystem gains checks next to the existing unlink ones: for a file named with /, //, /. and /./, sceKernelUnlink must return 0x80020014 and unlink must fail with errno 20, and the file must keep its four bytes; a second file with a trailing slash must give ENOTDIR and still exist; a directory with a trailing slash must give EPERM and a missing name ENOENT; and the plain name must then unlink.

Tested

Windows (91a1855b3 on main 5d50886ee, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):

  • With Open.cpp as on main and the test kept, guest_filesystem fails (exit 0xc0000409, Filesystem check failed at line 715). With the fix it passes 5/5 (--repeat until-fail:5), and guest_kernel_fcntl, guest_truncate, guest_dup, guest_file_vector, file_streams, guest_standard_streams, guest_posix_filesystem, guest_fdopendir, guest_kernel_rename and guest_path_case pass 3/3.
  • The probe behind the table, 164 lines, with main's libraries, the branch's and on the Linux build: sceKernelUnlink, unlink and remove on each of 26 names (a file, a directory with contents, an empty directory and a missing name, each plain, with /, with /. and under ..), printing the result and which of the files and directories are left; then a 0444 file and a file held open, each plain and with the slash. 34 lines change against main: the calls in the table and the files they no longer delete. Against the Linux build, every remaining difference is one of two kinds that are the same on main: sceKernelUnlink returns the sceKernel error without setting errno, and the three file/x lines above.
  • Probes of 122 lines over the descriptor functions, 866 over streams, 188 over the file, directory, vector and mapping functions, 431 over time, number, character and string functions, 145 over threads, 95 and 54 over flock, 44 over pipe streams, 15 over fcntl status flags, 127 over freopen, 130 over directory descriptors, 117 over streams used the other way, 144 over truncating opens, 313 over fdopen and 198 over paths that run through a regular file, run with both sets of libraries: the only lines that differ are the four in the last probe that unlink file/ and file/., and a file size in the fdopen probe that changes from run to run with either set.
  • Timing, three alternating runs of three passes: create and unlink 422 to 556 µs with main's libraries and 424 to 598 µs with the branch's; unlink of a missing name 270 to 332 µs and 272 to 320 µs (both include the call log the probe build prints). No difference I can separate from the spread.
  • Titles from the homebrew catalog, 35 s each with both sets of libraries, windows kept off-screen: ccleste (PPSA59322), RommPS (PPSA76677), ProsperoTV (PPSA99003), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000), OpenNOW (PPSA99082) and ProsperoRadio (PPSA99001) end at the same place in both, with the same number of stderr lines. SwanStationPS5 (PPSA98510) and PSXS5 (PPSA97510) stop on poll: POLLPRI is not supported by WSAPoll in both (#4478).
  • Full ctest --parallel 4: 1061 of 1061 pass, as with main 5d50886ee built without this change.
  • Commit 1 alone (ea57b4c1d): full build, and guest_filesystem, guest_kernel_fcntl, guest_truncate, guest_posix_filesystem and file_streams pass 3/3.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch reports no overlap with open pull requests.

Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): libs builds, and guest_filesystem and guest_kernel_fcntl pass 3/3, so the new checks match the existing Linux behaviour. The changed code is Windows only.

Not checked on a console or with a commercial title.

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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4843897571 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5165). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T13:22:39Z. --- ### What On Windows, `unlink("file/")` and `sceKernelUnlink("file/")` delete the file. On FreeBSD a name with a trailing slash must be a directory: [`lookup`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/vfs_lookup.c#L1068-L1075) fails with `ENOTDIR` when the path ends in a slash and the result is not a directory, and the Linux build returns `ENOTDIR` and keeps the file. The same happens for `file//`, `file/.` and `file/./`. A program that removes "the directory entry `name/`" expecting it to fail on anything that is not a directory loses the file instead. **Cause.** [`sceKernelUnlink`](https://github.com/boykopovar/AnyPS5/blob/5d50886ee/core/libs/prx/libkernel/File/src/Open.cpp#L310-L325) passes the resolved path, which keeps the trailing separator, to `_wunlink`. The Win32 delete accepts a separator after a file's name: on this machine `_wunlink(L"a/")` on a regular file `a` returns 0 and the file is gone. `file/.` is the same path after the resolver normalizes it. Observed on `main` 5d50886ee, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. `file` is a regular file: | | `main` on Windows | Linux build | |---|---|---| | `sceKernelUnlink("file/")` | 0, file deleted | `0x80020014` (ENOTDIR), file kept | | `unlink("file/")` | 0, file deleted | -1, errno 20, file kept | | the same with `file//`, `file/.`, `file/./`, `dir/file/`, `dir/sub/../file/` | 0, file deleted | ENOTDIR, file kept | | `sceKernelUnlink("file/")`, file has mode 0444 | `0x8002000D` (EACCES) | `0x80020014` | | `sceKernelUnlink("file/")`, file open through a descriptor | `0x8002000D` | `0x80020014` | | `remove("file/")` | -1, errno 20 | -1, errno 20 | I have not found a title that stops on this. It came out of running one probe of the path functions on the Windows build and on the Linux build and comparing the two outputs. Closes #5164. Two commits: 1. `fix(libkernel): fail unlink of a file named with a trailing slash on Windows`: `Open.cpp`. 2. `test(libkernel): unlink a file named with a trailing slash`: `guest_filesystem`. Windows only, in `sceKernelUnlink`, which `unlink` calls. When the resolved path ends in a separator, the name without it is looked up, and if it exists and is not a directory the call returns `ENOTDIR` before anything is deleted. The check runs only for such paths, so an ordinary `unlink` makes the same host calls as before. A directory named with a trailing slash still returns `EPERM` and a missing name `ENOENT`, as on `main`. Not changed, and still different from FreeBSD: a path that runs through a regular file (`file/x`) gives `ENOENT` on Windows where FreeBSD gives `ENOTDIR`, in `unlink` and in most other path functions; and `open`, `rename`, `truncate`, `utimes` and `lstat` also accept `file/` on Windows. I kept this change to the call that destroys data. `guest_filesystem` gains checks next to the existing unlink ones: for a file named with `/`, `//`, `/.` and `/./`, `sceKernelUnlink` must return `0x80020014` and `unlink` must fail with errno 20, and the file must keep its four bytes; a second file with a trailing slash must give `ENOTDIR` and still exist; a directory with a trailing slash must give `EPERM` and a missing name `ENOENT`; and the plain name must then unlink. ### Tested Windows (91a1855b3 on `main` 5d50886ee, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `Open.cpp` as on `main` and the test kept, `guest_filesystem` fails (exit 0xc0000409, `Filesystem check failed at line 715`). With the fix it passes 5/5 (`--repeat until-fail:5`), and `guest_kernel_fcntl`, `guest_truncate`, `guest_dup`, `guest_file_vector`, `file_streams`, `guest_standard_streams`, `guest_posix_filesystem`, `guest_fdopendir`, `guest_kernel_rename` and `guest_path_case` pass 3/3. - The probe behind the table, 164 lines, with `main`'s libraries, the branch's and on the Linux build: `sceKernelUnlink`, `unlink` and `remove` on each of 26 names (a file, a directory with contents, an empty directory and a missing name, each plain, with `/`, with `/.` and under `..`), printing the result and which of the files and directories are left; then a 0444 file and a file held open, each plain and with the slash. 34 lines change against `main`: the calls in the table and the files they no longer delete. Against the Linux build, every remaining difference is one of two kinds that are the same on `main`: `sceKernelUnlink` returns the `sceKernel` error without setting errno, and the three `file/x` lines above. - Probes of 122 lines over the descriptor functions, 866 over streams, 188 over the file, directory, vector and mapping functions, 431 over time, number, character and string functions, 145 over threads, 95 and 54 over `flock`, 44 over pipe streams, 15 over `fcntl` status flags, 127 over `freopen`, 130 over directory descriptors, 117 over streams used the other way, 144 over truncating opens, 313 over `fdopen` and 198 over paths that run through a regular file, run with both sets of libraries: the only lines that differ are the four in the last probe that unlink `file/` and `file/.`, and a file size in the `fdopen` probe that changes from run to run with either set. - Timing, three alternating runs of three passes: create and unlink 422 to 556 µs with `main`'s libraries and 424 to 598 µs with the branch's; unlink of a missing name 270 to 332 µs and 272 to 320 µs (both include the call log the probe build prints). No difference I can separate from the spread. - Titles from the homebrew catalog, 35 s each with both sets of libraries, windows kept off-screen: ccleste (PPSA59322), RommPS (PPSA76677), ProsperoTV (PPSA99003), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000), OpenNOW (PPSA99082) and ProsperoRadio (PPSA99001) end at the same place in both, with the same number of stderr lines. SwanStationPS5 (PPSA98510) and PSXS5 (PPSA97510) stop on `poll: POLLPRI is not supported by WSAPoll` in both (#4478). - Full `ctest --parallel 4`: 1061 of 1061 pass, as with `main` 5d50886ee built without this change. - Commit 1 alone (ea57b4c1d): full build, and `guest_filesystem`, `guest_kernel_fcntl`, `guest_truncate`, `guest_posix_filesystem` and `file_streams` pass 3/3. - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` reports no overlap with open pull requests. Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): `libs` builds, and `guest_filesystem` and `guest_kernel_fcntl` pass 3/3, so the new checks match the existing Linux behaviour. The changed code is Windows only. Not checked on a console or with a commercial title. ### 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 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] Unblocks: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-14 04:41:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T04:41:56Z.


Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161055516 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5165#issuecomment-6161055516). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T04:41:56Z. --- Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

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