mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libc): translate guest directory enumeration
This commit is contained in:
@@ -104,6 +104,15 @@ endforeach()
|
||||
add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS})
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_executable(guest_directory_tests tests/GuestDirectory.cpp)
|
||||
target_include_directories(guest_directory_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_directory_tests PRIVATE libc)
|
||||
configure_windows_unwind(guest_directory_tests)
|
||||
add_test(NAME guest_directory COMMAND guest_directory_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_directory PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_executable(guest_environment_tests tests/GuestEnvironment.cpp)
|
||||
target_include_directories(guest_environment_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_environment_tests PRIVATE libc)
|
||||
|
||||
@@ -20,6 +20,7 @@ target_sources(libc PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/LocaleSupport.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/Process.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/Environment.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/Directory.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/Errors.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/CxxAbiSupport.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/exception/Exports.cpp
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
// FreeBSD 11 ABI used by the PS5 SDK: names start at byte 8.
|
||||
struct GuestDirectoryEntry {
|
||||
std::uint32_t fileNumber;
|
||||
std::uint16_t recordLength;
|
||||
std::uint8_t type;
|
||||
std::uint8_t nameLength;
|
||||
char name[256];
|
||||
};
|
||||
static_assert(offsetof(GuestDirectoryEntry, name) == 8);
|
||||
static_assert(sizeof(GuestDirectoryEntry) == 264);
|
||||
@@ -0,0 +1,102 @@
|
||||
#include "prx/libc/include/GuestDirectory.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include <dirent.h>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
namespace {
|
||||
int DirectoryError(int error) {
|
||||
switch (error) {
|
||||
case ENOENT: return 2;
|
||||
case EACCES: return 13;
|
||||
case ENOTDIR: return 20;
|
||||
case EINVAL: return 22;
|
||||
case EMFILE: return 24;
|
||||
case ENFILE: return 23;
|
||||
case ENOMEM: return 12;
|
||||
case ENAMETOOLONG: return 63;
|
||||
default: return 5;
|
||||
}
|
||||
}
|
||||
struct Directory {
|
||||
DIR* native = nullptr;
|
||||
std::filesystem::path path;
|
||||
GuestDirectoryEntry entry{};
|
||||
std::mutex mutex;
|
||||
~Directory() { if (native) ::closedir(native); }
|
||||
};
|
||||
std::uint8_t DirectoryType(const std::filesystem::path& path) {
|
||||
std::error_code error;
|
||||
switch (std::filesystem::symlink_status(path, error).type()) {
|
||||
case std::filesystem::file_type::regular: return 8;
|
||||
case std::filesystem::file_type::directory: return 4;
|
||||
case std::filesystem::file_type::symlink: return 10;
|
||||
case std::filesystem::file_type::block: return 6;
|
||||
case std::filesystem::file_type::character: return 2;
|
||||
case std::filesystem::file_type::fifo: return 1;
|
||||
case std::filesystem::file_type::socket: return 12;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void* APS5_VABI opendir_nid_postfix(const char* path) {
|
||||
if (!path) { errno = 14; return nullptr; }
|
||||
if (!*path) { errno = 2; return nullptr; }
|
||||
try {
|
||||
auto directory = std::make_unique<Directory>();
|
||||
directory->path = ResolvePath_nid_no_patch(path);
|
||||
directory->native = ::opendir(directory->path.string().c_str());
|
||||
if (!directory->native) { errno = DirectoryError(errno); return nullptr; }
|
||||
return directory.release();
|
||||
} catch (const std::bad_alloc&) { errno = 12; return nullptr; }
|
||||
catch (const std::filesystem::filesystem_error&) { errno = 5; return nullptr; }
|
||||
}
|
||||
|
||||
GuestDirectoryEntry* APS5_VABI readdir_nid_postfix(void* handle) {
|
||||
if (!handle) { errno = 9; return nullptr; }
|
||||
auto& directory = *static_cast<Directory*>(handle);
|
||||
std::lock_guard lock(directory.mutex);
|
||||
const int savedError = errno;
|
||||
errno = 0;
|
||||
auto* entry = ::readdir(directory.native);
|
||||
if (!entry) {
|
||||
errno = errno ? DirectoryError(errno) : savedError;
|
||||
return nullptr;
|
||||
}
|
||||
const auto length = std::strlen(entry->d_name);
|
||||
if (length > 255) { errno = 63; return nullptr; }
|
||||
try {
|
||||
directory.entry = {};
|
||||
// MinGW reports no inode identity (zero); do not fabricate one.
|
||||
const auto inode = static_cast<std::uint64_t>(entry->d_ino);
|
||||
directory.entry.fileNumber = inode <= UINT32_MAX ? static_cast<std::uint32_t>(inode) : 0;
|
||||
directory.entry.recordLength = static_cast<std::uint16_t>(8 + ((length + 1 + 3) & ~3));
|
||||
directory.entry.nameLength = static_cast<std::uint8_t>(length);
|
||||
directory.entry.type = DirectoryType(directory.path / entry->d_name);
|
||||
std::memcpy(directory.entry.name, entry->d_name, length + 1);
|
||||
errno = savedError;
|
||||
return &directory.entry;
|
||||
} catch (const std::bad_alloc&) { errno = 12; return nullptr; }
|
||||
catch (const std::filesystem::filesystem_error&) { errno = 5; return nullptr; }
|
||||
}
|
||||
|
||||
int APS5_VABI closedir_nid_postfix(void* handle) {
|
||||
if (!handle) { errno = 9; return -1; }
|
||||
std::unique_ptr<Directory> directory(static_cast<Directory*>(handle));
|
||||
const int result = ::closedir(directory->native);
|
||||
directory->native = nullptr;
|
||||
if (result) errno = DirectoryError(errno);
|
||||
return result;
|
||||
}
|
||||
|
||||
void APS5_VABI rewinddir_nid_postfix(void* handle) {
|
||||
if (!handle) { errno = 9; return; }
|
||||
auto& directory = *static_cast<Directory*>(handle);
|
||||
std::lock_guard lock(directory.mutex);
|
||||
::rewinddir(directory.native);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "prx/libc/include/GuestDirectory.hpp"
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
extern "C" {
|
||||
void* APS5_VABI opendir_nid_postfix(const char*);
|
||||
GuestDirectoryEntry* APS5_VABI readdir_nid_postfix(void*);
|
||||
int APS5_VABI closedir_nid_postfix(void*);
|
||||
void APS5_VABI rewinddir_nid_postfix(void*);
|
||||
int* APS5_VABI __error_nid_postfix();
|
||||
}
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
int main() {
|
||||
const auto root = std::filesystem::path("anyps5-directory-test-" +
|
||||
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
Require(std::filesystem::create_directory(root));
|
||||
Require(std::filesystem::create_directory(root / "subdirectory"));
|
||||
{ std::ofstream file(root / "sample.txt"); file << "test"; }
|
||||
void* directory = opendir_nid_postfix(root.string().c_str());
|
||||
Require(directory != nullptr);
|
||||
std::map<std::string, int> entries;
|
||||
*__error_nid_postfix() = 13;
|
||||
while (auto* entry = readdir_nid_postfix(directory)) {
|
||||
Require(entry->nameLength == std::strlen(entry->name));
|
||||
Require(entry->recordLength == 8 + ((entry->nameLength + 4) & ~3));
|
||||
Require(entry->recordLength <= sizeof(*entry));
|
||||
entries[entry->name] = entry->type;
|
||||
}
|
||||
Require(*__error_nid_postfix() == 13); // EOF preserves errno.
|
||||
Require(entries.at("sample.txt") == 8);
|
||||
Require(entries.at("subdirectory") == 4);
|
||||
Require(entries.at(".") == 4 && entries.at("..") == 4);
|
||||
rewinddir_nid_postfix(directory);
|
||||
std::size_t count = 0;
|
||||
while (readdir_nid_postfix(directory)) ++count;
|
||||
Require(count == entries.size());
|
||||
Require(closedir_nid_postfix(directory) == 0);
|
||||
Require(opendir_nid_postfix((root / "missing").string().c_str()) == nullptr);
|
||||
Require(*__error_nid_postfix() == 2);
|
||||
Require(opendir_nid_postfix((root / "sample.txt").string().c_str()) == nullptr);
|
||||
Require(*__error_nid_postfix() == 20);
|
||||
Require(opendir_nid_postfix("") == nullptr && *__error_nid_postfix() == 2);
|
||||
Require(closedir_nid_postfix(nullptr) == -1 && *__error_nid_postfix() == 9);
|
||||
std::filesystem::remove(root / "sample.txt");
|
||||
std::filesystem::remove(root / "subdirectory");
|
||||
std::filesystem::remove(root);
|
||||
}
|
||||
@@ -253,3 +253,21 @@ host child processes or host locale functions are not implemented by this store.
|
||||
|
||||
Latest unchanged-game startup resolves getenv and stops at `ay3uROQAc5A`
|
||||
(`opendir`). Guest entry-point execution and rendered output remain unverified.
|
||||
|
||||
## Follow-up: directory enumeration
|
||||
|
||||
Added opendir, readdir, closedir, and rewinddir using the existing guest path
|
||||
resolver. Directory entries are translated into the SDK's FreeBSD 11 layout:
|
||||
32-bit file number, 16-bit record length, byte type/name length, and the name
|
||||
starting at offset 8. Record lengths include four-byte padding. Each directory
|
||||
owns its returned entry buffer. End-of-directory preserves the caller's errno;
|
||||
open/read failures translate common host errors into guest error values.
|
||||
|
||||
Tests enumerate actual files and subdirectories, verify names/types and record
|
||||
lengths, rewind, and check missing paths, non-directory paths, and EOF behavior.
|
||||
All sixteen suites pass. The Windows MinGW backend provides no inode identity
|
||||
(file number is zero) and uses host narrow-character filenames; full Unicode
|
||||
filename coverage and direct access to opaque DIR internals are not established.
|
||||
|
||||
The unchanged converted game now resolves opendir and stops at `MZO7FXyAPU8`
|
||||
(`remove`). No guest entry-point execution or rendered frame has been observed.
|
||||
|
||||
Reference in New Issue
Block a user