fix(libkernel): return -1 and set errno from the POSIX open, stat, unlink and rmdir

open, _open, stat, unlink and rmdir forwarded the result of their sceKernel
twin, so a failure returned 0x8002xxxx instead of -1 and errno was never set
to the guest error. They now return -1 with errno set to the code carried by
the sce error, and report a null pointer as EFAULT and an empty path as
ENOENT.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DotDebian
2026-10-02 18:17:12 +02:00
parent 313de1c5a3
commit 1817d83fec
2 changed files with 79 additions and 8 deletions
+29 -8
View File
@@ -131,6 +131,22 @@ static int SceErrorFromErrno(int error) {
return static_cast<int>(0x80020000u | static_cast<unsigned>(error > 0 && error <= 34 ? error : error == GUEST_ENOTEMPTY ? error : GUEST_EIO));
}
extern "C" int* APS5_VABI __error_nid_postfix();
static int PosixFailure(int error) {
*__error_nid_postfix() = error;
return -1;
}
static int PosixResult(int result) {
return result < 0 ? PosixFailure(result & 0xffff) : result;
}
static int PathError(const char* path) {
if (path == nullptr) return GUEST_EFAULT;
return *path == '\0' ? GUEST_ENOENT : 0;
}
extern "C" {
int APS5_VABI chmod_nid_postfix(const char* path, int mode) {
@@ -213,25 +229,26 @@ int APS5_VABI mkdir_nid_postfix(const char* path, uint16_t mode) {
}
int APS5_VABI open_nid_postfix(const char* path, int flags, int mode) {
return sceKernelOpen(path, flags, static_cast<std::uint16_t>(mode));
if (const int error = PathError(path)) return PosixFailure(error);
return PosixResult(sceKernelOpen(path, flags, static_cast<std::uint16_t>(mode)));
}
int APS5_VABI _open_nid_postfix(const char* path, int flags, ...) {
std::uint16_t mode = 0;
int mode = 0;
if (flags & SCE_KERNEL_O_CREAT) {
#ifdef _WIN32
__builtin_sysv_va_list arguments;
__builtin_sysv_va_start(arguments, flags);
mode = static_cast<std::uint16_t>(__builtin_va_arg(arguments, int));
mode = __builtin_va_arg(arguments, int);
__builtin_sysv_va_end(arguments);
#else
std::va_list arguments;
va_start(arguments, flags);
mode = static_cast<std::uint16_t>(va_arg(arguments, int));
mode = va_arg(arguments, int);
va_end(arguments);
#endif
}
return sceKernelOpen(path, flags, mode);
return open_nid_postfix(path, flags, mode);
}
int64_t APS5_VABI pread_nid_postfix(int d, void* buf, size_t nbytes, int64_t offset) {
@@ -282,11 +299,14 @@ std::int64_t APS5_VABI _write_nid_postfix(int descriptor, const void* buffer, st
}
int APS5_VABI stat_nid_postfix(const char* path, FileStat* sb) {
return sceKernelStat(path, sb);
if (sb == nullptr) return PosixFailure(GUEST_EFAULT);
if (const int error = PathError(path)) return PosixFailure(error);
return PosixResult(sceKernelStat(path, sb));
}
int APS5_VABI unlink_nid_postfix(const char* path) {
return sceKernelUnlink(path);
if (const int error = PathError(path)) return PosixFailure(error);
return PosixResult(sceKernelUnlink(path));
}
int APS5_VABI sceKernelCheckReachability(const char* path) {
@@ -432,7 +452,8 @@ int APS5_VABI sceKernelRmdir(const char* path) {
}
int APS5_VABI rmdir_nid_postfix(const char* path) {
return sceKernelRmdir(path);
if (const int error = PathError(path)) return PosixFailure(error);
return PosixResult(sceKernelRmdir(path));
}
}
+50
View File
@@ -1,3 +1,4 @@
#include "SceTypes.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <chrono>
#include <cstdlib>
@@ -12,6 +13,16 @@ int APS5_VABI sceKernelFsync(int);
int APS5_VABI sceKernelFtruncate(int, long long);
int APS5_VABI sceKernelTruncate_nid_postfix(const char*, long long);
int APS5_VABI sceKernelUtimes_nid_postfix(const char*, const void*);
int APS5_VABI open_nid_postfix(const char*, int, int);
int APS5_VABI _open_nid_postfix(const char*, int, ...);
int APS5_VABI close_nid_postfix(int);
int APS5_VABI stat_nid_postfix(const char*, FileStat*);
int APS5_VABI unlink_nid_postfix(const char*);
int APS5_VABI rmdir_nid_postfix(const char*);
int APS5_VABI sceKernelOpen(const char*, int, unsigned short);
int APS5_VABI sceKernelStat(const char*, FileStat*);
int APS5_VABI sceKernelUnlink(const char*);
int APS5_VABI sceKernelRmdir(const char*);
int* APS5_VABI __error_nid_postfix();
}
static void Check(bool value, int line) {
@@ -67,6 +78,45 @@ int main() {
Require(std::fclose(native) == 0);
Require(std::filesystem::file_size(sized) == 3);
Require(remove_nid_postfix(sized.string().c_str()) == 0);
const auto present = root / "present.txt";
const auto presentName = present.string();
const auto missingName = (root / "missing.txt").string();
const auto rootName = root.string();
{ std::ofstream stream(present); stream << "posix"; }
FileStat status{};
Require(stat_nid_postfix(presentName.c_str(), &status) == 0 && status.st_size == 5);
Require(stat_nid_postfix(missingName.c_str(), &status) == -1 && *__error_nid_postfix() == 2);
Require(sceKernelStat(missingName.c_str(), &status) == static_cast<int>(0x80020002u));
Require(stat_nid_postfix("", &status) == -1 && *__error_nid_postfix() == 2);
Require(stat_nid_postfix(nullptr, &status) == -1 && *__error_nid_postfix() == 14);
Require(stat_nid_postfix(presentName.c_str(), nullptr) == -1 && *__error_nid_postfix() == 14);
const int opened = open_nid_postfix(presentName.c_str(), 0, 0);
Require(opened >= 0 && close_nid_postfix(opened) == 0);
const int reopened = _open_nid_postfix(presentName.c_str(), 0);
Require(reopened >= 0 && close_nid_postfix(reopened) == 0);
Require(open_nid_postfix(missingName.c_str(), 0, 0) == -1 && *__error_nid_postfix() == 2);
Require(_open_nid_postfix(missingName.c_str(), 0) == -1 && *__error_nid_postfix() == 2);
Require(sceKernelOpen(missingName.c_str(), 0, 0) == static_cast<int>(0x80020002u));
Require(open_nid_postfix(presentName.c_str(), 0x0a02, 0644) == -1 && *__error_nid_postfix() == 17);
Require(_open_nid_postfix(presentName.c_str(), 0x0a02, 0644) == -1 && *__error_nid_postfix() == 17);
Require(open_nid_postfix("", 0, 0) == -1 && *__error_nid_postfix() == 2);
Require(_open_nid_postfix("", 0) == -1 && *__error_nid_postfix() == 2);
Require(open_nid_postfix(nullptr, 0, 0) == -1 && *__error_nid_postfix() == 14);
Require(_open_nid_postfix(nullptr, 0) == -1 && *__error_nid_postfix() == 14);
Require(rmdir_nid_postfix(rootName.c_str()) == -1 && *__error_nid_postfix() == 66);
Require(sceKernelRmdir(rootName.c_str()) == static_cast<int>(0x80020042u));
Require(rmdir_nid_postfix(presentName.c_str()) == -1 && *__error_nid_postfix() == 20);
Require(rmdir_nid_postfix(missingName.c_str()) == -1 && *__error_nid_postfix() == 2);
Require(rmdir_nid_postfix("") == -1 && *__error_nid_postfix() == 2);
Require(rmdir_nid_postfix(nullptr) == -1 && *__error_nid_postfix() == 14);
Require(unlink_nid_postfix(missingName.c_str()) == -1 && *__error_nid_postfix() == 2);
Require(sceKernelUnlink(missingName.c_str()) == static_cast<int>(0x80020002u));
Require(unlink_nid_postfix("") == -1 && *__error_nid_postfix() == 2);
Require(unlink_nid_postfix(nullptr) == -1 && *__error_nid_postfix() == 14);
Require(unlink_nid_postfix(presentName.c_str()) == 0 && !std::filesystem::exists(present));
const auto empty = root / "empty";
Require(std::filesystem::create_directory(empty));
Require(rmdir_nid_postfix(empty.string().c_str()) == 0 && !std::filesystem::exists(empty));
Require(remove_nid_postfix(root.string().c_str()) == 0);
Require(!std::filesystem::exists(root));
}