diff --git a/src/core/libraries/kernel/file_system.cpp b/src/core/libraries/kernel/file_system.cpp index 6fe57406..f0ab1713 100644 --- a/src/core/libraries/kernel/file_system.cpp +++ b/src/core/libraries/kernel/file_system.cpp @@ -9,6 +9,7 @@ #include "core/libraries/error_codes.h" #include "core/libraries/kernel/file_system.h" #include "core/libraries/libs.h" +#include "libkernel.h" namespace Libraries::Kernel { @@ -101,6 +102,7 @@ int PS4_SYSV_ABI posix_open(const char* path, int flags, /* SceKernelMode*/ u16 int result = sceKernelOpen(path, flags, mode); // Posix calls different only for their return values if (result < 0) { + err_sce_to_posix(result); return -1; } return result; @@ -125,7 +127,13 @@ int PS4_SYSV_ABI sceKernelClose(int d) { } int PS4_SYSV_ABI posix_close(int d) { - ASSERT(sceKernelClose(d) == 0); + int result = sceKernelClose(d); + if (result < 0) { + LOG_ERROR(Kernel_Pthread, "posix_close: error = {}", result); + err_sce_to_posix(result); + return -1; + } + return result; return ORBIS_OK; } @@ -178,7 +186,13 @@ s64 PS4_SYSV_ABI sceKernelLseek(int d, s64 offset, int whence) { } s64 PS4_SYSV_ABI posix_lseek(int d, s64 offset, int whence) { - return sceKernelLseek(d, offset, whence); + int result = sceKernelLseek(d, offset, whence); + if (result < 0) { + LOG_ERROR(Kernel_Pthread, "posix_lseek: error = {}", result); + err_sce_to_posix(result); + return -1; + } + return result; } s64 PS4_SYSV_ABI sceKernelRead(int d, void* buf, size_t nbytes) { @@ -193,7 +207,13 @@ s64 PS4_SYSV_ABI sceKernelRead(int d, void* buf, size_t nbytes) { } int PS4_SYSV_ABI posix_read(int d, void* buf, size_t nbytes) { - return sceKernelRead(d, buf, nbytes); + int result = sceKernelRead(d, buf, nbytes); + if (result < 0) { + LOG_ERROR(Kernel_Pthread, "posix_read: error = {}", result); + err_sce_to_posix(result); + return -1; + } + return result; } int PS4_SYSV_ABI sceKernelMkdir(const char* path, u16 mode) { @@ -218,7 +238,13 @@ int PS4_SYSV_ABI sceKernelMkdir(const char* path, u16 mode) { } int PS4_SYSV_ABI posix_mkdir(const char* path, u16 mode) { - return sceKernelMkdir(path, mode); + int result = sceKernelMkdir(path, mode); + if (result < 0) { + LOG_ERROR(Kernel_Pthread, "posix_mkdir: error = {}", result); + err_sce_to_posix(result); + return -1; + } + return result; } int PS4_SYSV_ABI sceKernelStat(const char* path, OrbisKernelStat* sb) { @@ -249,9 +275,10 @@ int PS4_SYSV_ABI sceKernelStat(const char* path, OrbisKernelStat* sb) { int PS4_SYSV_ABI posix_stat(const char* path, OrbisKernelStat* sb) { int result = sceKernelStat(path, sb); - if (result != 0) { + if (result < 0) { LOG_ERROR(Kernel_Pthread, "posix_stat: error = {}", result); - result += ORBIS_KERNEL_ERROR_UNKNOWN; + err_sce_to_posix(result); + return -1; } return result; } @@ -311,7 +338,13 @@ int PS4_SYSV_ABI sceKernelFStat(int fd, OrbisKernelStat* sb) { } int PS4_SYSV_ABI posix_fstat(int fd, OrbisKernelStat* sb) { - return sceKernelFStat(fd, sb); + int result = sceKernelFStat(fd, sb); + if (result < 0) { + LOG_ERROR(Kernel_Pthread, "posix_fstat: error = {}", result); + err_sce_to_posix(result); + return -1; + } + return result; } s32 PS4_SYSV_ABI sceKernelFsync(int fd) { diff --git a/src/core/libraries/kernel/libkernel.cpp b/src/core/libraries/kernel/libkernel.cpp index e7e7d11d..f2faf049 100644 --- a/src/core/libraries/kernel/libkernel.cpp +++ b/src/core/libraries/kernel/libkernel.cpp @@ -63,9 +63,16 @@ size_t PS4_SYSV_ABI _writev(int fd, const struct iovec* iov, int iovcn) { return total_written; } -static thread_local int libc_error{}; +static thread_local int g_posix_errno = 0; int* PS4_SYSV_ABI __Error() { - return &libc_error; + return &g_posix_errno; +} + +void err_sce_to_posix(int result) { + int rt = result > SCE_KERNEL_ERROR_UNKNOWN && result <= SCE_KERNEL_ERROR_ESTOP + ? result + -SCE_KERNEL_ERROR_UNKNOWN + : POSIX_EOTHER; + g_posix_errno = rt; } int PS4_SYSV_ABI sceKernelMmap(void* addr, u64 len, int prot, int flags, int fd, size_t offset, diff --git a/src/core/libraries/kernel/libkernel.h b/src/core/libraries/kernel/libkernel.h index 0cc6b0b2..07413888 100644 --- a/src/core/libraries/kernel/libkernel.h +++ b/src/core/libraries/kernel/libkernel.h @@ -12,6 +12,8 @@ class SymbolsResolver; namespace Libraries::Kernel { +void err_sce_to_posix(int result); + struct OrbisTimesec { time_t t; u32 west_sec;