added fopen and fclose functions

This commit is contained in:
georgemoralis 2023-11-13 13:50:37 +02:00
parent a1e5fcf0c5
commit 30e9012914
3 changed files with 32 additions and 3 deletions

View File

@ -36,6 +36,20 @@ std::string MntPoints::getHostDirectory(const std::string& guest_directory) {
} }
return ""; return "";
} }
std::string MntPoints::getHostFile(const std::string& guest_file) {
std::scoped_lock lock{m_mutex};
for (auto& pair : m_mnt_pairs) {
//horrible code but it works :D
int find = guest_file.find(pair.guest_path);
if (find == 0) {
std::string npath = guest_file.substr(pair.guest_path.size(), guest_file.size()-1);
std::replace(pair.host_path.begin(), pair.host_path.end(), '\\', '/');
return pair.host_path + npath;
}
}
return "";
}
int HandleTable::createHandle() { int HandleTable::createHandle() {
std::scoped_lock lock{m_mutex}; std::scoped_lock lock{m_mutex};
auto* file = new File{}; auto* file = new File{};

View File

@ -20,6 +20,7 @@ class MntPoints {
void unmount(const std::string& path); void unmount(const std::string& path);
void unmountAll(); void unmountAll();
std::string getHostDirectory(const std::string& guest_directory); std::string getHostDirectory(const std::string& guest_directory);
std::string MntPoints::getHostFile(const std::string& guest_file);
private: private:
std::vector<MntPair> m_mnt_pairs; std::vector<MntPair> m_mnt_pairs;

View File

@ -2,6 +2,8 @@
#include "common/debug.h" #include "common/debug.h"
#include "common/log.h" #include "common/log.h"
#include "common/singleton.h"
#include "core/file_sys/fs.h"
namespace Core::Libraries::LibC { namespace Core::Libraries::LibC {
@ -38,11 +40,23 @@ int PS4_SYSV_ABI vsnprintf(char* s, size_t n, const char* format, VaList* arg) {
int PS4_SYSV_ABI puts(const char* s) { return std::puts(s); } int PS4_SYSV_ABI puts(const char* s) { return std::puts(s); }
FILE* PS4_SYSV_ABI fopen(const char* filename, const char* mode) { FILE* PS4_SYSV_ABI fopen(const char* filename, const char* mode) {
LOG_ERROR_IF(log_file_libc, "Unimplemented fopen filename={} , mode ={}\n", filename, mode); LOG_ERROR_IF(log_file_libc, "fopen filename={} , mode ={}\n", filename, mode);
return nullptr; auto* h = Common::Singleton<Core::FileSys::HandleTable>::Instance();
auto* mnt = Common::Singleton<Core::FileSys::MntPoints>::Instance();
u32 handle = h->createHandle();
auto* file = h->getFile(handle);
file->m_guest_name = filename;
file->m_host_name = mnt->getHostFile(file->m_guest_name);
FILE* f = std::fopen(file->m_host_name.c_str(), mode);
return f;
} }
int PS4_SYSV_ABI fclose(FILE* stream) { int PS4_SYSV_ABI fclose(FILE* stream) {
LOG_ERROR_IF(log_file_libc, "Unimplemented fclose\n"); LOG_ERROR_IF(log_file_libc, "fclose\n");
if (stream != nullptr)
{
std::fclose(stream);
}
return 0; return 0;
} }
} // namespace Core::Libraries::LibC } // namespace Core::Libraries::LibC