Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions core/libs/prx/libkernel/File/src/Stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,22 +718,15 @@ int APS5_VABI _open_nid_postfix(const char* path, int flags, ...) {
}

int64_t APS5_VABI pread_nid_postfix(int d, void* buf, size_t nbytes, int64_t offset) {
if (buf == nullptr) {
APS5_INVALID_ARG_EX;
}
if (offset < 0) {
APS5_INVALID_ARG_EX;
}
if (buf == nullptr && nbytes != 0) return PosixFailure(GUEST_EFAULT);
if (offset < 0) return PosixFailure(GUEST_EINVAL);
if (FailPosixPositionedGuestSocket(d)) return -1;
const GuestArena::HostWrite destination(buf, nbytes);
if (!destination.Open()) errno = EFAULT;
else if (File::ReadRandomDevice(d, buf, nbytes)) return static_cast<int64_t>(nbytes);
auto n = destination.Open() ? NativePread(d, buf, nbytes, offset) : -1;
if (n < 0 && errno == EBADF) return PosixFailure(GUEST_EBADF);
if (n < 0) {
throw std::runtime_error(std::string(__func__) + ": pread failed, fd=" + std::to_string(d) + ", errno=" + std::to_string(errno));
}
return n;
if (!destination.Open()) return PosixFailure(GUEST_EFAULT);
if (File::ReadRandomDevice(d, buf, nbytes)) return static_cast<int64_t>(nbytes);
char empty = 0;
const auto result = NativePread(d, buf == nullptr ? &empty : buf, nbytes, offset);
return result < 0 ? PosixFailure(SceErrorFromErrno(errno) & 0xffff) : result;
}

int64_t APS5_VABI pwrite_nid_disambig1_nid_postfix(int d, const void* buf, size_t nbytes, int64_t offset) {
Expand Down
11 changes: 11 additions & 0 deletions core/libs/tests/GuestFileVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ std::int64_t APS5_VABI pwritev_nid_postfix(int, const GuestIovec*, int, std::int
int* APS5_VABI __error_nid_postfix();
std::int64_t APS5_VABI sceKernelPread(int, void*, std::size_t, std::int64_t);
std::int64_t APS5_VABI sceKernelPwrite(int, const void*, std::size_t, std::int64_t);
std::int64_t APS5_VABI pread_nid_postfix(int, void*, std::size_t, std::int64_t);
std::int64_t APS5_VABI pwrite_nid_disambig1_nid_postfix(int, const void*, std::size_t, std::int64_t);
int APS5_VABI sceKernelFtruncate(int, std::int64_t);
}

Expand Down Expand Up @@ -176,6 +178,14 @@ int main() {
Require(sceKernelPwrite(file, nullptr, 1, 0) == ErrorEfault);
Require(sceKernelPread(file, &byte, 1, -1) == ErrorEinval);
Require(sceKernelPwrite(file, &byte, 1, -1) == ErrorEinval);
Require(pread_nid_postfix(file, &byte, 1, 3) == 1 && byte == 'B');
Require(pread_nid_postfix(file, nullptr, 0, 0) == 0);
Require(pread_nid_postfix(file, nullptr, 1, 0) == -1 && *__error_nid_postfix() == 14);
Require(pwrite_nid_disambig1_nid_postfix(file, nullptr, 1, 0) == -1 && *__error_nid_postfix() == 14);
Require(pread_nid_postfix(file, &byte, 1, -1) == -1 && *__error_nid_postfix() == 22);
Require(pwrite_nid_disambig1_nid_postfix(file, &byte, 1, -1) == -1 && *__error_nid_postfix() == 22);
Require(pread_nid_postfix(9999, &byte, 1, 0) == -1 && *__error_nid_postfix() == 9);
Require(pwrite_nid_disambig1_nid_postfix(9999, &byte, 1, 0) == -1 && *__error_nid_postfix() == 9);
Require(sceKernelFtruncate(file, -1) == ErrorEinval);
const int readOnly = sceKernelOpen(path.string().c_str(), SCE_KERNEL_O_RDONLY, 0);
Require(readOnly >= 0);
Expand All @@ -185,6 +195,7 @@ int main() {
const int writeOnly = sceKernelOpen(path.string().c_str(), SCE_KERNEL_O_WRONLY, 0);
Require(writeOnly >= 0);
Require(sceKernelPread(writeOnly, &byte, 1, 0) == ErrorEbadf);
Require(pread_nid_postfix(writeOnly, &byte, 1, 0) == -1 && *__error_nid_postfix() == 9);
Require(sceKernelClose(writeOnly) == 0);
Require(Contents(path) == "01ABCDE7ABCDE");
Require(sceKernelFtruncate(file, 5) == 0);
Expand Down
Loading