mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(libc): fix ftools exports
This commit is contained in:
@@ -164,7 +164,7 @@ int sceSaveDataGetSaveDataMemory2(SaveDataMemoryGet2* get_param) {
|
||||
|
||||
int sceSaveDataInitialize3(const void* init) {
|
||||
(void)init;
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
// NotImplemented_nid_no_patch(__func__);
|
||||
if (g_initialized) {
|
||||
return SAVE_DATA_ERROR_ALREADY_INITIALIZED;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_FILESTREAM_HPP
|
||||
#define CORE_LIBS_PRX_LIBC_INCLUDE_FILESTREAM_HPP
|
||||
|
||||
#include <cstdio>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
class FileStream {
|
||||
std::FILE* _handle;
|
||||
bool _dynamic;
|
||||
|
||||
public:
|
||||
explicit FileStream(std::FILE* handle, bool dynamic = false) : _handle(handle), _dynamic(dynamic) {
|
||||
if (!_handle) throw std::runtime_error("FileStream: null handle");
|
||||
}
|
||||
|
||||
FileStream(const FileStream&) = delete;
|
||||
FileStream& operator=(const FileStream&) = delete;
|
||||
|
||||
std::FILE* GetHandle() const {
|
||||
if (!_handle) throw std::runtime_error("FileStream: closed stream");
|
||||
return _handle;
|
||||
}
|
||||
|
||||
bool IsDynamic() const {
|
||||
return _dynamic;
|
||||
}
|
||||
|
||||
void Close() {
|
||||
GetHandle();
|
||||
if (std::fclose(std::exchange(_handle, nullptr)) != 0) throw std::runtime_error("FileStream: close failed");
|
||||
}
|
||||
};
|
||||
|
||||
inline std::FILE* GetNativeStream(FileStream* stream) {
|
||||
if (!stream) throw std::runtime_error("FileStream: null stream");
|
||||
return stream->GetHandle();
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
extern FileStream _Stdout_nid_postfix;
|
||||
extern FileStream _Stderr_nid_postfix;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -2,41 +2,70 @@
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include "prx/libc/include/FileStream.hpp"
|
||||
|
||||
extern "C" {
|
||||
|
||||
[[noreturn]] void _ZSt11_Xbad_allocv_nid_postfix();
|
||||
|
||||
FILE* fopen_nid_postfix(const char* filename, const char* mode) {
|
||||
return std::fopen(filename, mode);
|
||||
FileStream* fopen_nid_postfix(const char* filename, const char* mode) {
|
||||
if (!filename || !mode) throw std::runtime_error("fopen: null argument");
|
||||
std::unique_ptr<std::FILE, decltype(&std::fclose)> handle(std::fopen(filename, mode), std::fclose);
|
||||
if (!handle) throw std::runtime_error(std::string("fopen: open failed: ") + filename);
|
||||
|
||||
auto stream = std::make_unique<FileStream>(handle.get(), true);
|
||||
handle.release();
|
||||
return stream.release();
|
||||
}
|
||||
|
||||
int fclose_nid_postfix(FILE* stream) {
|
||||
return std::fclose(stream);
|
||||
int fclose_nid_postfix(FileStream* stream) {
|
||||
GetNativeStream(stream);
|
||||
std::unique_ptr<FileStream> owner(stream->IsDynamic() ? stream : nullptr);
|
||||
stream->Close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t fread_nid_postfix(void* buffer, size_t size, size_t count, FILE* stream) {
|
||||
return std::fread(buffer, size, count, stream);
|
||||
size_t fread_nid_postfix(void* buffer, size_t size, size_t count, FileStream* stream) {
|
||||
auto* handle = GetNativeStream(stream);
|
||||
if (size == 0 || count == 0) return 0;
|
||||
if (!buffer) throw std::runtime_error("fread: null buffer");
|
||||
const auto result = std::fread(buffer, size, count, handle);
|
||||
if (std::ferror(handle)) throw std::runtime_error("fread: read failed");
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t fwrite_nid_postfix(const void* buffer, size_t size, size_t count, FILE* stream) {
|
||||
return std::fwrite(buffer, size, count, stream);
|
||||
size_t fwrite_nid_postfix(const void* buffer, size_t size, size_t count, FileStream* stream) {
|
||||
auto* handle = GetNativeStream(stream);
|
||||
if (size == 0 || count == 0) return 0;
|
||||
if (!buffer) throw std::runtime_error("fwrite: null buffer");
|
||||
const auto result = std::fwrite(buffer, size, count, handle);
|
||||
if (result != count || std::ferror(handle)) throw std::runtime_error("fwrite: write failed");
|
||||
return result;
|
||||
}
|
||||
|
||||
int fseek_nid_postfix(FILE* stream, long offset, int origin) {
|
||||
return std::fseek(stream, offset, origin);
|
||||
int fseek_nid_postfix(FileStream* stream, long offset, int origin) {
|
||||
if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END) throw std::runtime_error("fseek: invalid origin");
|
||||
if (std::fseek(GetNativeStream(stream), offset, origin) != 0) throw std::runtime_error("fseek: seek failed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
long ftell_nid_postfix(FILE* stream) {
|
||||
return std::ftell(stream);
|
||||
long ftell_nid_postfix(FileStream* stream) {
|
||||
const auto result = std::ftell(GetNativeStream(stream));
|
||||
if (result == -1L) throw std::runtime_error("ftell: position query failed");
|
||||
return result;
|
||||
}
|
||||
|
||||
int fputs_nid_postfix(const char* str, FILE* stream) {
|
||||
return std::fputs(str, stream);
|
||||
int fputs_nid_postfix(const char* str, FileStream* stream) {
|
||||
if (!str) throw std::runtime_error("fputs: null string");
|
||||
const int result = std::fputs(str, GetNativeStream(stream));
|
||||
if (result == EOF) throw std::runtime_error("fputs: write failed");
|
||||
return result;
|
||||
}
|
||||
|
||||
int fflush_nid_postfix(FILE* stream) {
|
||||
return std::fflush(stream);
|
||||
int fflush_nid_postfix(FileStream* stream) {
|
||||
if (std::fflush(stream ? GetNativeStream(stream) : nullptr) != 0) throw std::runtime_error("fflush: flush failed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
constexpr std::uintptr_t AlignedBlockTag = 1;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include "prx/libc/include/specifics/gcc/AtomicOps.hpp"
|
||||
#include "prx/libc/include/FileStream.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -16,13 +17,8 @@ std::mutex g_sysLock;
|
||||
|
||||
extern "C" {
|
||||
|
||||
FILE* _Stderr_nid_postfix() {
|
||||
return stderr;
|
||||
}
|
||||
|
||||
FILE* _Stdout_nid_postfix() {
|
||||
return stdout;
|
||||
}
|
||||
FileStream _Stderr_nid_postfix{stderr};
|
||||
FileStream _Stdout_nid_postfix{stdout};
|
||||
|
||||
int __cxa_atexit_nid_postfix(void (*func)(void*), void* arg, void* dsoHandle) {
|
||||
(void)dsoHandle;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "prx/libc/include/FileStream.hpp"
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
extern "C" {
|
||||
FileStream* fopen_nid_postfix(const char* filename, const char* mode);
|
||||
int fclose_nid_postfix(FileStream* stream);
|
||||
std::size_t fread_nid_postfix(void* buffer, std::size_t size, std::size_t count, FileStream* stream);
|
||||
std::size_t fwrite_nid_postfix(const void* buffer, std::size_t size, std::size_t count, FileStream* stream);
|
||||
int fseek_nid_postfix(FileStream* stream, long offset, int origin);
|
||||
long ftell_nid_postfix(FileStream* stream);
|
||||
int fputs_nid_postfix(const char* str, FileStream* stream);
|
||||
int fflush_nid_postfix(FileStream* stream);
|
||||
}
|
||||
|
||||
static void Require(bool condition) {
|
||||
if (!condition) throw std::runtime_error("File stream check failed");
|
||||
}
|
||||
|
||||
template<typename TAction>
|
||||
static void ExpectException(TAction action) {
|
||||
try {
|
||||
action();
|
||||
} catch (const std::runtime_error&) {
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("Expected a stream exception");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Require(argc == 2);
|
||||
Require(_Stdout_nid_postfix.GetHandle() == stdout);
|
||||
Require(_Stderr_nid_postfix.GetHandle() == stderr);
|
||||
Require(fputs_nid_postfix("stdout object works\n", &_Stdout_nid_postfix) >= 0);
|
||||
Require(fputs_nid_postfix("stderr object works\n", &_Stderr_nid_postfix) >= 0);
|
||||
Require(fflush_nid_postfix(nullptr) == 0);
|
||||
auto* stream = fopen_nid_postfix(argv[1], "w+b");
|
||||
constexpr char payload[] = "stream round trip";
|
||||
Require(fputs_nid_postfix(payload, stream) >= 0);
|
||||
Require(fwrite_nid_postfix(payload, 1, sizeof(payload), stream) == sizeof(payload));
|
||||
const auto expectedSize = sizeof(payload) * 2 - 1;
|
||||
Require(ftell_nid_postfix(stream) == static_cast<long>(expectedSize));
|
||||
Require(fflush_nid_postfix(stream) == 0);
|
||||
Require(fseek_nid_postfix(stream, 0, SEEK_SET) == 0);
|
||||
std::array<char, sizeof(payload)> buffer{};
|
||||
Require(fread_nid_postfix(buffer.data(), 1, sizeof(payload) - 1, stream) == sizeof(payload) - 1);
|
||||
Require(std::strcmp(buffer.data(), payload) == 0);
|
||||
Require(fread_nid_postfix(buffer.data(), 1, buffer.size(), stream) == buffer.size());
|
||||
Require(std::strcmp(buffer.data(), payload) == 0);
|
||||
Require(fread_nid_postfix(buffer.data(), 1, buffer.size(), stream) == 0);
|
||||
ExpectException([&] { fputs_nid_postfix(nullptr, stream); });
|
||||
ExpectException([&] { fseek_nid_postfix(stream, 0, -1); });
|
||||
Require(fclose_nid_postfix(stream) == 0);
|
||||
stream = fopen_nid_postfix(argv[1], "rb");
|
||||
ExpectException([&] { fwrite_nid_postfix(payload, 1, sizeof(payload), stream); });
|
||||
Require(fclose_nid_postfix(stream) == 0);
|
||||
ExpectException([] { fputs_nid_postfix("invalid stream", nullptr); });
|
||||
ExpectException([] { fopen_nid_postfix(nullptr, "r"); });
|
||||
FileStream closed(std::tmpfile());
|
||||
closed.Close();
|
||||
ExpectException([&] { fflush_nid_postfix(&closed); });
|
||||
Require(std::remove(argv[1]) == 0);
|
||||
ExpectException([&] { fopen_nid_postfix(argv[1], "rb"); });
|
||||
std::cout << "PASS: stream objects, file operations, EOF and error handling\n";
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "prx/libkernel/Pthread/Pthread.hpp"
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
extern "C" int scePthreadCreate(Pthread* thread, const PthreadAttr* attr, void* (*entry)(void*), void* arg, const char* name);
|
||||
extern "C" int scePthreadJoin(Pthread thread, void** retval);
|
||||
extern "C" int scePthreadAttrInit(PthreadAttr* attr);
|
||||
extern "C" int scePthreadAttrDestroy(PthreadAttr* attr);
|
||||
extern "C" int scePthreadAttrGet(Pthread thread, PthreadAttr* attr);
|
||||
|
||||
struct ThreadContext {
|
||||
Pthread thread = nullptr;
|
||||
std::promise<void> checked;
|
||||
};
|
||||
|
||||
static void* CheckThread(void* arg) {
|
||||
auto& context = *static_cast<ThreadContext*>(arg);
|
||||
if (!context.thread) throw std::runtime_error("Thread handle was not published");
|
||||
if (context.thread->_thr.get_id() != std::this_thread::get_id()) throw std::runtime_error("Thread object was not initialized");
|
||||
if (!context.thread->_thr.joinable()) throw std::runtime_error("Thread object is not joinable");
|
||||
PthreadAttr attr = nullptr;
|
||||
if (scePthreadAttrInit(&attr) != 0) throw std::runtime_error("Attribute initialization failed");
|
||||
if (scePthreadAttrGet(context.thread, &attr) != 0) throw std::runtime_error("Attribute query failed");
|
||||
if (scePthreadAttrDestroy(&attr) != 0) throw std::runtime_error("Attribute destruction failed");
|
||||
context.checked.set_value();
|
||||
return arg;
|
||||
}
|
||||
|
||||
int main() {
|
||||
for (int iteration = 0; iteration < 10000; ++iteration) {
|
||||
ThreadContext context;
|
||||
if (scePthreadCreate(&context.thread, nullptr, CheckThread, &context, nullptr) != 0) throw std::runtime_error("Thread creation failed");
|
||||
context.checked.get_future().get();
|
||||
void* result = nullptr;
|
||||
if (scePthreadJoin(context.thread, &result) != 0) throw std::runtime_error("Thread join failed");
|
||||
if (result != &context) throw std::runtime_error("Thread return value was lost");
|
||||
}
|
||||
std::cout << "PASS: 10000 thread starts, initialized handles, attribute queries and joins\n";
|
||||
}
|
||||
Reference in New Issue
Block a user