Skip to content
Open
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
52 changes: 47 additions & 5 deletions core/libs/prx/libc/src/General.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <limits>
#include <optional>
#include <cerrno>
#include <climits>
#include <cstring>
#include <map>
#include <memory>
Expand Down Expand Up @@ -211,19 +212,19 @@ std::error_code MatchNames(const std::filesystem::path& directory, std::span<Nam
if (!stem && CaseInsensitiveDirectoryWindows(directory)) return MatchNamesWindows(directory, names);
#endif
std::error_code error;
std::filesystem::directory_iterator entry(directory, error), end;
std::filesystem::directory_iterator entry(directory.empty() ? std::filesystem::path(".") : directory, error), end;
for (; !error && entry != end; entry.increment(error)) {
const auto filename = entry->path().filename().string();
for (auto& name : names) {
if (name.stem) {
std::error_code status;
if (!SameRelinkedStem(filename, name.name) || !entry->is_regular_file(status) || status) continue;
if (!name.path || filename.size() < name.path->filename().string().size()) name.path = entry->path();
if (!name.path || filename.size() < name.path->filename().string().size()) name.path = directory / entry->path().filename();
continue;
}
if (!SameName(filename, name.name)) continue;
name.ambiguous = name.ambiguous || name.path.has_value();
name.path = entry->path();
name.path = directory / entry->path().filename();
}
}
return error;
Expand Down Expand Up @@ -362,9 +363,22 @@ std::optional<std::filesystem::path> ProvidedPath(const WorkingDirectory& state,
if (provided.empty()) return std::nullopt;
return provided.make_preferred();
}
bool FitsHostPath(const std::filesystem::path& path) {
#ifdef _WIN32
return true;
#else
return path.native().size() < PATH_MAX;
#endif
}
std::filesystem::path ShortHostPath(const WorkingDirectory& state, const std::filesystem::path& path) {
if (FitsHostPath(path)) return path;
const auto relative = path.lexically_relative(state.root);
return relative.empty() || *relative.begin() == ".." ? path : relative;
}
std::filesystem::path HostPath(WorkingDirectory& state, const std::vector<std::string>& names, bool module = false) {
if (auto aliased = ResolveAlias(JoinNames(names), module)) return *aliased;
if (auto provided = ProvidedPath(state, names)) return *provided;
if (!FitsHostPath(Child(state.root, JoinNames(names)))) return ResolveHostPath({}, names, module);
return ResolveHostPath(state.root, names, module).make_preferred();
}
void RequireHostDirectory(WorkingDirectory& state, const std::vector<std::string>& names) {
Expand Down Expand Up @@ -595,15 +609,39 @@ int SetLongWorkingDirectory(WorkingDirectory& state, const std::filesystem::path
}
#endif

#ifndef _WIN32
std::filesystem::path CanonicalHostPath(const WorkingDirectory& state, std::filesystem::path resolved, std::error_code& error) {
if (resolved.is_absolute()) return std::filesystem::canonical(resolved, error);
while (!resolved.has_filename() && resolved.has_relative_path()) resolved = resolved.parent_path();
auto prefix = resolved;
std::vector<std::filesystem::path> tail;
while (!FitsHostPath(state.root / prefix) && prefix.has_parent_path()) {
tail.push_back(prefix.filename());
prefix = prefix.parent_path();
}
auto canonical = std::filesystem::canonical(state.root / prefix, error);
for (auto name = tail.rbegin(); !error && name != tail.rend(); ++name) {
prefix /= *name;
const auto status = std::filesystem::symlink_status(prefix, error);
if (!error && !std::filesystem::exists(status)) error = std::make_error_code(std::errc::no_such_file_or_directory);
if (!error && std::filesystem::is_symlink(status)) error = std::make_error_code(std::errc::filename_too_long);
canonical /= *name;
}
return canonical;
}
#endif

int SetWorkingDirectory(WorkingDirectory& state, const std::filesystem::path& resolved) {
std::error_code error;
const auto canonical = std::filesystem::canonical(resolved, error);
#ifdef _WIN32
const auto canonical = std::filesystem::canonical(resolved, error);
if (error) return SetLongWorkingDirectory(state, resolved, error);
if (!std::filesystem::is_directory(canonical, error)) return error ? DirectoryFailure(error) : 20;
#else
const auto canonical = CanonicalHostPath(state, resolved, error);
if (error) return DirectoryFailure(error);
if (!std::filesystem::is_directory(ShortHostPath(state, canonical), error)) return error ? DirectoryFailure(error) : 20;
#endif
if (!std::filesystem::is_directory(canonical, error)) return error ? DirectoryFailure(error) : 20;
auto relative = canonical.lexically_relative(state.root);
#ifdef _WIN32
if (relative.empty() || *relative.begin() == "..") {
Expand Down Expand Up @@ -1151,7 +1189,11 @@ extern "C" char* APS5_VABI getcwd_nid_postfix(char* buffer, std::size_t size) {
auto& state = Directories();
std::lock_guard lock(state.mutex);
std::error_code error;
#ifdef _WIN32
if (!IsDirectory(state.current, error)) {
#else
if (!std::filesystem::is_directory(ShortHostPath(state, state.current), error)) {
#endif
errno = error ? DirectoryFailure(error) : 2; return nullptr;
}
const auto relative = state.current.lexically_relative(state.root);
Expand Down
13 changes: 13 additions & 0 deletions core/libs/tests/GuestKernelSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/RemoveTree.hpp"
#include "prx/libc/include/WindowsVerbatimPath.hpp"
#include "prx/libkernel/File/include/FileFlags.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include "SceTypes.hpp"
Expand Down Expand Up @@ -1449,6 +1450,18 @@ void CheckRawGetcwdPathLimit() {
++depth;
}

Require(getcwd_nid_postfix(current.data(), current.size()) == current.data());
std::string climb;
for (std::size_t level = 0; level < depth + 4; ++level) climb += "../";
const std::string inside = std::string(current.data() + 1) + "/" + std::string(1023 - climb.size() - length, 'e');
std::error_code hostError;
const auto host = NativeVerbatimPath_nid_no_patch(inside, hostError);
Require(!hostError);
Require(mkdirat_nid_postfix(-100, (climb + inside).c_str(), 0755) == 0);
Require(std::filesystem::is_directory(host));
Require(unlinkat_nid_postfix(-100, ("/../../" + inside).c_str(), 0x800) == 0);
Require(!std::filesystem::exists(host));

std::array<char, 4096> buffer;
for (const std::size_t target : {std::size_t{1023}, std::size_t{1024}}) {
const std::string leaf(target - length - 1, target == 1023 ? 'a' : 'b');
Expand Down
5 changes: 5 additions & 0 deletions docs/dev/debt/changes/fix-libc-macos-long-host-paths.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libc

## change fail on macOS with `ENAMETOOLONG` once their host form

- [Guest path names](../../../core/libs/prx/libc/src/General.cpp) (libc) whose host form under the root directory reaches the host `PATH_MAX` are passed to the host relative to the root, which is the host working directory. On macOS a guest path that resolves to 1024 bytes or more relative to the root still fails with `ENAMETOOLONG`: a relative name used from a deep working directory, a path under an alias such as `/app0`, which stays absolute, and a path joined to a directory descriptor's host path by the `*at` calls. On macOS, `fchdir` on a descriptor for a directory at the long-path limit fails with `ENOTDIR` because the host descriptor path cannot be recovered. `chdir` into such a directory fails with `ENAMETOOLONG` when a component past the host limit is a symbolic link, because the root check then cannot resolve it.
Loading