From d415c71bfe9bc1e8ed2a34bdd5d8ce48eeaaf485 Mon Sep 17 00:00:00 2001 From: Henrique Reis Neves <39978017+NevesHenry@users.noreply.github.com> Date: Tue, 20 Oct 2026 08:50:46 -0300 Subject: [PATCH] kernel: return ENOENT from mkdir when the parent does not exist (#5399) posix_mkdir used fs::create_directories, so mkdir("/data/a/b") with no /data/a returned 0 and created both directories. On the console mkdir(2) only creates the last component and fails with ENOENT when the parent is missing, so sceKernelMkdir should return 0x80020002. Use fs::create_directory and report ENOENT in that case. Also check for a null path before logging it and calling strlen on it, and report EFAULT like posix_rmdir does, instead of crashing. --- src/core/libraries/kernel/file_system.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index 3cff19872..2195a7199 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -591,15 +591,15 @@ s64 PS4_SYSV_ABI sceKernelRead(s32 fd, void* buf, u64 nbytes) { } s32 PS4_SYSV_ABI posix_mkdir(const char* path, u16 mode) { + if (path == nullptr) { + *__Error() = POSIX_EFAULT; + return -1; + } LOG_INFO(Kernel_Fs, "path = {} mode = {:#o}", path, mode); if (strlen(path) > 255) { *__Error() = POSIX_ENAMETOOLONG; return -1; } - if (path == nullptr) { - *__Error() = POSIX_ENOTDIR; - return -1; - } std::string sanitized_path(path); while (sanitized_path.length() > 1 && sanitized_path.back() == '/') { sanitized_path.pop_back(); @@ -621,7 +621,11 @@ s32 PS4_SYSV_ABI posix_mkdir(const char* path, u16 mode) { } std::error_code ec; - if (dir_name.empty() || !fs::create_directories(dir_name, ec)) { + if (dir_name.empty() || !fs::create_directory(dir_name, ec)) { + if (ec == std::errc::no_such_file_or_directory) { + *__Error() = POSIX_ENOENT; + return -1; + } *__Error() = (ec.value() == EACCES || ec.value() == EPERM) ? POSIX_EACCES : POSIX_EIO; return -1; }