mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libc): track guest working directory and resolve relative paths
This commit is contained in:
@@ -104,6 +104,15 @@ endforeach()
|
||||
add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS})
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_executable(guest_working_directory_tests tests/GuestWorkingDirectory.cpp)
|
||||
target_include_directories(guest_working_directory_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_working_directory_tests PRIVATE libc)
|
||||
configure_windows_unwind(guest_working_directory_tests)
|
||||
add_test(NAME guest_working_directory COMMAND guest_working_directory_tests)
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_working_directory PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
add_library(guest_module_fixture SHARED tests/GuestModuleFixture.cpp)
|
||||
target_include_directories(guest_module_fixture PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_custom_command(TARGET guest_module_fixture POST_BUILD
|
||||
|
||||
@@ -1,24 +1,86 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include <mutex>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "prx/libc/include/GuestHeap.hpp"
|
||||
|
||||
namespace {
|
||||
struct WorkingDirectory {
|
||||
std::mutex mutex;
|
||||
const std::filesystem::path root = std::filesystem::canonical(std::filesystem::current_path());
|
||||
std::filesystem::path current = root;
|
||||
};
|
||||
WorkingDirectory& Directories() { static WorkingDirectory state; return state; }
|
||||
std::filesystem::path Resolve(WorkingDirectory& state, const char* path) {
|
||||
std::string text(path);
|
||||
for (auto& character : text) if (character == '\\') character = '/';
|
||||
std::filesystem::path input(text);
|
||||
#ifdef _WIN32
|
||||
// Preserve the existing ability to pass explicit native drive paths.
|
||||
if (input.has_root_name()) return input;
|
||||
#endif
|
||||
auto guest = (std::filesystem::path("/") / state.current.lexically_relative(state.root));
|
||||
guest = (input.is_absolute() ? input : guest / input).lexically_normal();
|
||||
return (state.root / guest.relative_path()).make_preferred();
|
||||
}
|
||||
int DirectoryFailure(const std::error_code& error) {
|
||||
if (error == std::errc::permission_denied) return 13;
|
||||
if (error == std::errc::not_a_directory) return 20;
|
||||
if (error == std::errc::no_such_file_or_directory) return 2;
|
||||
if (error == std::errc::filename_too_long) return 63;
|
||||
if (error == std::errc::too_many_symbolic_link_levels) return 62;
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" std::filesystem::path ResolvePath_nid_no_patch(const char* path) {
|
||||
if (path == nullptr) {
|
||||
APS5_INVALID_ARG_EX;
|
||||
}
|
||||
std::string s(path);
|
||||
std::size_t start = 0;
|
||||
while (start < s.size() && (s[start] == '/' || s[start] == '\\')) {
|
||||
++start;
|
||||
}
|
||||
std::size_t end = s.size();
|
||||
while (end > start && (s[end - 1] == '/' || s[end - 1] == '\\')) {
|
||||
--end;
|
||||
}
|
||||
std::filesystem::path result = std::filesystem::current_path() / std::filesystem::path(s.substr(start, end - start));
|
||||
return result.make_preferred();
|
||||
if (!path) { APS5_INVALID_ARG_EX; }
|
||||
auto& state = Directories();
|
||||
std::lock_guard lock(state.mutex);
|
||||
return Resolve(state, path);
|
||||
}
|
||||
|
||||
extern "C" int APS5_VABI chdir_nid_postfix(const char* path) {
|
||||
if (!path) { errno = 14; return -1; }
|
||||
if (!*path) { errno = 2; return -1; }
|
||||
try {
|
||||
auto& state = Directories();
|
||||
std::lock_guard lock(state.mutex);
|
||||
std::error_code error;
|
||||
const auto resolved = std::filesystem::canonical(Resolve(state, path), error);
|
||||
if (error) { errno = DirectoryFailure(error); return -1; }
|
||||
if (!std::filesystem::is_directory(resolved, error)) {
|
||||
errno = error ? DirectoryFailure(error) : 20; return -1;
|
||||
}
|
||||
const auto relative = resolved.lexically_relative(state.root);
|
||||
if (relative.empty() || *relative.begin() == "..") { errno = 45; return -1; }
|
||||
state.current = resolved;
|
||||
return 0;
|
||||
} catch (const std::bad_alloc&) { errno = 12; return -1; }
|
||||
catch (const std::filesystem::filesystem_error& error) { errno = DirectoryFailure(error.code()); return -1; }
|
||||
}
|
||||
|
||||
extern "C" char* APS5_VABI getcwd_nid_postfix(char* buffer, std::size_t size) {
|
||||
if (buffer && size == 0) { errno = 22; return nullptr; }
|
||||
try {
|
||||
auto& state = Directories();
|
||||
std::lock_guard lock(state.mutex);
|
||||
std::error_code error;
|
||||
if (!std::filesystem::is_directory(state.current, error)) {
|
||||
errno = error ? DirectoryFailure(error) : 2; return nullptr;
|
||||
}
|
||||
const auto relative = state.current.lexically_relative(state.root);
|
||||
const auto path = relative == "." ? std::string("/") : "/" + relative.generic_string();
|
||||
const auto required = path.size() + 1;
|
||||
if ((buffer || size) && size < required) { errno = 34; return nullptr; }
|
||||
if (!buffer) buffer = static_cast<char*>(GuestHeap::GuestHeapAllocate_nid_postfix(size ? size : required));
|
||||
std::memcpy(buffer, path.c_str(), required);
|
||||
return buffer;
|
||||
} catch (const std::bad_alloc&) { errno = 12; return nullptr; }
|
||||
catch (const std::filesystem::filesystem_error& error) { errno = DirectoryFailure(error.code()); return nullptr; }
|
||||
}
|
||||
|
||||
extern "C" void NotImplemented_nid_no_patch(const char* funcName) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "prx/libc/include/GuestHeap.hpp"
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
extern "C" {
|
||||
int APS5_VABI chdir_nid_postfix(const char*);
|
||||
char* APS5_VABI getcwd_nid_postfix(char*, std::size_t);
|
||||
int* APS5_VABI __error_nid_postfix();
|
||||
}
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
int main() {
|
||||
const auto host = std::filesystem::canonical(std::filesystem::current_path());
|
||||
char path[1024];
|
||||
Require(getcwd_nid_postfix(path, sizeof(path)) == path && std::strcmp(path, "/") == 0);
|
||||
const auto name = "anyps5-cwd-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
|
||||
const auto directory = host / name;
|
||||
Require(std::filesystem::create_directory(directory));
|
||||
{ std::ofstream file(directory / "sample.txt"); file << "sample"; }
|
||||
Require(chdir_nid_postfix(name.c_str()) == 0);
|
||||
Require(std::filesystem::current_path() == host);
|
||||
Require(getcwd_nid_postfix(path, sizeof(path)) == path && path == "/" + name);
|
||||
Require(ResolvePath_nid_no_patch("sample.txt") == directory / "sample.txt");
|
||||
Require(ResolvePath_nid_no_patch(("/" + name + "/sample.txt").c_str()) == directory / "sample.txt");
|
||||
char tiny[] = "xyz";
|
||||
Require(getcwd_nid_postfix(tiny, 2) == nullptr && *__error_nid_postfix() == 34);
|
||||
Require(std::strcmp(tiny, "xyz") == 0);
|
||||
char* allocated = getcwd_nid_postfix(nullptr, 0);
|
||||
Require(allocated && std::strcmp(allocated, path) == 0);
|
||||
GuestHeap::GuestHeapFree_nid_postfix(allocated);
|
||||
Require(chdir_nid_postfix("sample.txt") == -1 && *__error_nid_postfix() == 20);
|
||||
Require(chdir_nid_postfix("missing") == -1 && *__error_nid_postfix() == 2);
|
||||
Require(chdir_nid_postfix("..") == 0);
|
||||
Require(getcwd_nid_postfix(path, sizeof(path)) == path && std::strcmp(path, "/") == 0);
|
||||
Require(chdir_nid_postfix("../..") == 0);
|
||||
Require(getcwd_nid_postfix(path, sizeof(path)) == path && std::strcmp(path, "/") == 0);
|
||||
std::filesystem::remove(directory / "sample.txt");
|
||||
std::filesystem::remove(directory);
|
||||
}
|
||||
@@ -312,3 +312,20 @@ full console library-search and mount semantics are not established.
|
||||
|
||||
Latest unchanged-game startup resolves dlsym and stops at `DYivN1nO-JQ`
|
||||
(`getcwd`). No guest entry-point execution or rendered frame has been observed.
|
||||
|
||||
## Follow-up: working-directory state
|
||||
|
||||
Added getcwd and chdir with shared guest path state. The initial host working
|
||||
directory is captured as the guest root; guest cwd begins at `/`. Relative paths
|
||||
follow guest cwd while absolute guest paths remain rooted at that captured
|
||||
directory. Guest chdir does not change the host process cwd. Canonical directory
|
||||
targets within that root are supported; targets outside it return EOPNOTSUPP.
|
||||
Explicit Windows drive paths remain accepted by the general resolver for
|
||||
compatibility with existing host-path callers. This is not a filesystem sandbox
|
||||
or a complete PS5 mount implementation.
|
||||
|
||||
Tests verify root/parent traversal, relative and absolute resolution after chdir,
|
||||
missing/non-directory failures, unchanged host cwd, short-buffer handling, and
|
||||
getcwd allocation freed through the guest heap. All nineteen suites pass.
|
||||
Latest unchanged-game startup resolves getcwd and stops at `oVkZ8W8-Q8A`
|
||||
(`strtok`). Guest execution and rendered output remain unverified.
|
||||
|
||||
Reference in New Issue
Block a user