libkernel: mkdir and rmdir return ENOENT instead of ENOTDIR on Windows when a path component is a regular file #3097

Closed
opened 2026-10-10 22:07:24 +00:00 by husseinkad · 0 comments
husseinkad commented 2026-10-10 22:07:24 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T22:07:24Z.


What

On Windows, mkdir_nid_postfix, sceKernelMkdir, rmdir_nid_postfix and sceKernelRmdir (core/libs/prx/libkernel/File/src/Stdio.cpp) with a regular file as a path component return ENOENT (or a raw Windows code that maps to EIO): mkdir("file/child"), mkdir("file/child/leaf"), rmdir("file/child"). FreeBSD returns ENOTDIR: mkdir(2) "[ENOTDIR] A component of the path prefix is not a directory", rmdir(2) "[ENOTDIR] A component of the path is not a directory". Linux hosts already return ENOTDIR there. The same defect likely affects the other path-taking calls (#2300 fixed it for stat with a trailing slash); this issue is about mkdir and rmdir only.

Reproduce

Windows 11, WinLibs GCC 15.2.0 r7, main at the time of writing: create a regular file f, call mkdir_nid_postfix("f/child", 0700): -1 with errno 2 (FreeBSD: 20). sceKernelMkdir and sceKernelRmdir return 0x80020002 (or an EIO code) where the console gives 0x80020014.

Fix

A tested fix is on my fork: husseinkad:fix/kernel-mkdir-enotdir (two commits on main; PR #2707, closed by the intake check). A helper HasNonDirectoryPrefix walks up to the nearest existing ancestor and reports whether it is not a directory (a trailing separator is ignored); it runs only on the failure path, and the four functions return ENOTDIR when it is true (errno is captured before the helper runs; _wmkdir's EINVAL for names like file/a:b/x is translated too). Along the way sceKernelMkdir("new/") stops failing with ENOENT while mkdir("new/") succeeds (the parent was taken from the path with its trailing separator, which is the related #2743), so both strip it. Not covered: mkdir("file/") still reports EEXIST, and a path component longer than the limit is not ENAMETOOLONG.

guest_filesystem gains the file-prefix and a:b cases for all four functions, dir/missing/leaf (stays ENOENT), rmdir("missing/") and trailing-separator mkdir; it fails without the change. A guest-API probe of 124 paths x 4 functions (496 comparisons) against main's Stdio.cpp built into a second libkernel.prx: 371 identical, 104 changed ENOENT/EINVAL to ENOTDIR and all of them have a regular file as a prefix (junctions, case differences, non-ASCII names, NUL/x, 400 nested components, a file under a read-only directory), 20 are the trailing-separator sceKernelMkdir forms, none changed from success to failure; a 3-thread create/delete race against a flipping ancestor showed no hang or stale errno. Linux not run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795705010 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3097). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T22:07:24Z. --- ### What On Windows, `mkdir_nid_postfix`, `sceKernelMkdir`, `rmdir_nid_postfix` and `sceKernelRmdir` (`core/libs/prx/libkernel/File/src/Stdio.cpp`) with a regular file as a path component return `ENOENT` (or a raw Windows code that maps to `EIO`): `mkdir("file/child")`, `mkdir("file/child/leaf")`, `rmdir("file/child")`. FreeBSD returns `ENOTDIR`: mkdir(2) "[ENOTDIR] A component of the path prefix is not a directory", rmdir(2) "[ENOTDIR] A component of the path is not a directory". Linux hosts already return `ENOTDIR` there. The same defect likely affects the other path-taking calls (#2300 fixed it for `stat` with a trailing slash); this issue is about mkdir and rmdir only. ### Reproduce Windows 11, WinLibs GCC 15.2.0 r7, `main` at the time of writing: create a regular file `f`, call `mkdir_nid_postfix("f/child", 0700)`: `-1` with `errno` 2 (FreeBSD: 20). `sceKernelMkdir` and `sceKernelRmdir` return `0x80020002` (or an `EIO` code) where the console gives `0x80020014`. ### Fix A tested fix is on my fork: [`husseinkad:fix/kernel-mkdir-enotdir`](https://github.com/husseinkad/AnyPS5/tree/fix/kernel-mkdir-enotdir) (two commits on `main`; PR #2707, closed by the intake check). A helper `HasNonDirectoryPrefix` walks up to the nearest existing ancestor and reports whether it is not a directory (a trailing separator is ignored); it runs only on the failure path, and the four functions return `ENOTDIR` when it is true (`errno` is captured before the helper runs; `_wmkdir`'s `EINVAL` for names like `file/a:b/x` is translated too). Along the way `sceKernelMkdir("new/")` stops failing with `ENOENT` while `mkdir("new/")` succeeds (the parent was taken from the path with its trailing separator, which is the related #2743), so both strip it. Not covered: `mkdir("file/")` still reports `EEXIST`, and a path component longer than the limit is not `ENAMETOOLONG`. `guest_filesystem` gains the file-prefix and `a:b` cases for all four functions, `dir/missing/leaf` (stays `ENOENT`), `rmdir("missing/")` and trailing-separator mkdir; it fails without the change. A guest-API probe of 124 paths x 4 functions (496 comparisons) against `main`'s `Stdio.cpp` built into a second `libkernel.prx`: 371 identical, 104 changed `ENOENT`/`EINVAL` to `ENOTDIR` and all of them have a regular file as a prefix (junctions, case differences, non-ASCII names, `NUL/x`, 400 nested components, a file under a read-only directory), 20 are the trailing-separator `sceKernelMkdir` forms, none changed from success to failure; a 3-thread create/delete race against a flipping ancestor showed no hang or stale `errno`. Linux not run. AI-assisted: yes.
Sign in to join this conversation.
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#3097
No description provided.