fix(libkernel): treat Windows junctions in unlinkat like unlink and rmdir #9095

Merged
mazen320 merged 3 commits from upstream-pr/9095 into main 2026-10-26 21:29:03 +00:00
mazen320 commented 2026-10-21 12:08:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mazen320 on 2026-10-21T12:08:48Z.


What

Relative unlinkat on Windows did not apply the directory-junction rules that unlink and rmdir already use (#7530). With AT_REMOVEDIR, a junction returns ENOTDIR and the target is not inspected. Without that flag, unlinkat removes the junction with RemoveDirectoryW and leaves the target, including when the target is not empty. Absolute and AT_FDCWD paths already went through unlink/rmdir.

Tested

Windows 11, MinGW GCC 15.2, Release, build-win, head d91194a8b9 on current upstream/main (still Depends on #7530):

  • guest_kernel_sys passed (junction AT_REMOVEDIR → ENOTDIR, plain unlinkat removes junction and keeps non-empty target, file symlink unlinkat removes the link and keeps the target)
  • guest_filesystem passed
  • python tools/check_conventions.py --base upstream/main

Checklist

  • Depends on: #7530
  • Fixes: (none)
  • Unblocks: (none)
  • GPUs tested: N/A
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951293096 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9095). Originally posted by [mazen320](https://github.com/mazen320) on 2026-10-21T12:08:48Z. --- ### What Relative `unlinkat` on Windows did not apply the directory-junction rules that `unlink` and `rmdir` already use (#7530). With `AT_REMOVEDIR`, a junction returns `ENOTDIR` and the target is not inspected. Without that flag, `unlinkat` removes the junction with `RemoveDirectoryW` and leaves the target, including when the target is not empty. Absolute and `AT_FDCWD` paths already went through `unlink`/`rmdir`. ### Tested Windows 11, MinGW GCC 15.2, Release, `build-win`, head `d91194a8b9` on current `upstream/main` (still Depends on #7530): - `guest_kernel_sys` passed (junction AT_REMOVEDIR → ENOTDIR, plain unlinkat removes junction and keeps non-empty target, file symlink unlinkat removes the link and keeps the target) - `guest_filesystem` passed - `python tools/check_conventions.py --base upstream/main` ### Checklist - [x] Depends on: #7530 - [x] Fixes: (none) - [x] Unblocks: (none) - [x] GPUs tested: N/A - [x] AI-assisted: yes
cadenasynyster commented 2026-10-21 21:59:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-21T21:59:04Z.


Independent Windows run of this PR (head d20190a68). The branch contains #7530's commit (81fe7856f) and this PR's commit, so I checked them separately.

Environment: Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON.

Results:

  • PR head (both commits): guest_kernel_sys and guest_filesystem pass 3/3 (--repeat until-fail:3).
  • This PR's commit alone: I kept #7530's changes and the tests, and replaced only core/libs/prx/libkernel_sys/Export.cpp with the merge-base version (d099bc6cd). guest_kernel_sys fails with Kernel sys check failed at line 561 (guest errno 66): unlinkat(base, "junction-full", AT_REMOVEDIR) returns ENOTEMPTY instead of ENOTDIR. guest_filesystem still passes.
  • Both commits removed (Open.cpp, Stdio.cpp and Export.cpp from the merge base): guest_kernel_sys fails the same way, and guest_filesystem aborts in the new junction-full block, at the first call after mklink /J (rmdir on the junction), before any check prints. That block comes from #7530.
  • I restored the PR's files afterwards.

Not run: Linux/macOS, the full repository CTest, and any title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6327473821 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9095#issuecomment-6327473821). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-21T21:59:04Z. --- Independent Windows run of this PR (head d20190a68). The branch contains #7530's commit (81fe7856f) and this PR's commit, so I checked them separately. **Environment:** Windows 11 10.0.26300, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 (MinGW), Release, BUILD_TESTING=ON. **Results:** - PR head (both commits): `guest_kernel_sys` and `guest_filesystem` pass 3/3 (`--repeat until-fail:3`). - This PR's commit alone: I kept #7530's changes and the tests, and replaced only `core/libs/prx/libkernel_sys/Export.cpp` with the merge-base version (d099bc6cd). `guest_kernel_sys` fails with `Kernel sys check failed at line 561 (guest errno 66)`: `unlinkat(base, "junction-full", AT_REMOVEDIR)` returns `ENOTEMPTY` instead of `ENOTDIR`. `guest_filesystem` still passes. - Both commits removed (`Open.cpp`, `Stdio.cpp` and `Export.cpp` from the merge base): `guest_kernel_sys` fails the same way, and `guest_filesystem` aborts in the new `junction-full` block, at the first call after `mklink /J` (`rmdir` on the junction), before any check prints. That block comes from #7530. - I restored the PR's files afterwards. **Not run:** Linux/macOS, the full repository CTest, and any title. AI-assisted: yes
mazen320 commented 2026-10-21 22:02:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mazen320 on 2026-10-21T22:02:08Z.


Thanks for the independent Windows run and the isolation checks — useful confirmation that the head needs both commits together.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6327524216 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9095#issuecomment-6327524216). Originally posted by [mazen320](https://github.com/mazen320) on 2026-10-21T22:02:08Z. --- Thanks for the independent Windows run and the isolation checks — useful confirmation that the head needs both commits together.
harisibrar1 commented 2026-10-24 13:25:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:25:45Z.

Review: COMMENTED


Checked against main 6cd36e6517 (read-only git merge-tree / range-diff; I didn't build or run it):

  • It merges cleanly into current main, and main still has no junction handling in unlink, rmdir or unlinkat. The rebase since the d20190a68 run left the code hunks unchanged (range-diff shows only test and doc movement).
  • In unlinkat without AT_REMOVEDIR (libkernel_sys/Export.cpp:618), the check tests only FILE_ATTRIBUTE_REPARSE_POINT, while the other three new sites are restricted to directories (the AT_REMOVEDIR branch at :582 and sceKernelRmdir check FILE_ATTRIBUTE_DIRECTORY; sceKernelUnlink is inside is_directory(symlink_status)). As written, any reparse-point file, such as a guest symlink link, now goes through ReadJunction, which throws if CreateFileW or FSCTL_GET_REPARSE_POINT fails, where main went straight to _wunlink. I couldn't run this to see whether it ever trips; adding && (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0 would match the other sites and keep file unlinks on the old path.
  • Nit: GuestFilesystem.cpp:1134 (const auto fullTarget = ...) lost its indentation in the rebase.
  • Nit on the debt entry: ReadJunction only returns true when the target is a drive path, so a volume mount point or other mount-point reparse still takes the old path. "return ENOTDIR for that junction", as the earlier O_NOFOLLOW sentence says, would match the code. The new behaviour also lives in Open.cpp, Stdio.cpp and libkernel_sys/Export.cpp rather than NativeStat.cpp, the file the entry links.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638406558 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9095#pullrequestreview-5638406558). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:25:45Z. **Review:** COMMENTED --- Checked against main 6cd36e6517 (read-only `git merge-tree` / `range-diff`; I didn't build or run it): - It merges cleanly into current main, and main still has no junction handling in `unlink`, `rmdir` or `unlinkat`. The rebase since the d20190a68 run left the code hunks unchanged (range-diff shows only test and doc movement). - In `unlinkat` without `AT_REMOVEDIR` (`libkernel_sys/Export.cpp:618`), the check tests only `FILE_ATTRIBUTE_REPARSE_POINT`, while the other three new sites are restricted to directories (the `AT_REMOVEDIR` branch at :582 and `sceKernelRmdir` check `FILE_ATTRIBUTE_DIRECTORY`; `sceKernelUnlink` is inside `is_directory(symlink_status)`). As written, any reparse-point file, such as a guest `symlink` link, now goes through `ReadJunction`, which throws if CreateFileW or FSCTL_GET_REPARSE_POINT fails, where main went straight to `_wunlink`. I couldn't run this to see whether it ever trips; adding `&& (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0` would match the other sites and keep file unlinks on the old path. - Nit: `GuestFilesystem.cpp:1134` (`const auto fullTarget = ...`) lost its indentation in the rebase. - Nit on the debt entry: `ReadJunction` only returns true when the target is a drive path, so a volume mount point or other mount-point reparse still takes the old path. "return `ENOTDIR` for that junction", as the earlier `O_NOFOLLOW` sentence says, would match the code. The new behaviour also lives in `Open.cpp`, `Stdio.cpp` and `libkernel_sys/Export.cpp` rather than `NativeStat.cpp`, the file the entry links. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9095
No description provided.