Skip to content
Merged
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
5 changes: 4 additions & 1 deletion core/libs/prx/libSceAvPlayer/src/Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "prx/libSceAvPlayer/include/AvPlayer.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"

#include <algorithm>
#include <atomic>
Expand Down Expand Up @@ -243,7 +244,9 @@ class FfmpegSource final : public ISource {
format->pb = replacement->Context();
format->flags |= AVFMT_FLAG_CUSTOM_IO;
} else {
const auto resolved = ResolvePath_nid_no_patch(path.c_str()).u8string();
std::filesystem::path hostPath;
if (ResolvePathError_nid_no_patch(path.c_str(), hostPath) != 0) return false;
const auto resolved = hostPath.u8string();
url = "file:" + std::string(resolved.begin(), resolved.end());
}
if (const int error = avformat_open_input(&format, replacement ? nullptr : url.c_str(), nullptr, nullptr); error < 0) {
Expand Down
7 changes: 6 additions & 1 deletion core/libs/prx/libSceFont/src/Open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <vector>

#include "prx/libSceFont/include/FontInternal.hpp"
#include "prx/libc/include/FilesystemError.hpp"

namespace {

Expand Down Expand Up @@ -280,7 +281,11 @@ int APS5_VABI sceFontOpenFontFile(FontLibrary library, const char* path, std::ui
return SCE_FONT_ERROR_INVALID_LIBRARY;
}
if (!pFontHandle) return SCE_FONT_ERROR_INVALID_PARAMETER;
const std::filesystem::path hostPath = path ? ResolvePath_nid_no_patch(path) : std::filesystem::path{};
std::filesystem::path hostPath;
if (path && ResolvePathError_nid_no_patch(path, hostPath) != 0) {
*pFontHandle = nullptr;
return SCE_FONT_ERROR_FS_OPEN_FAILED;
}
const std::string hostPathString = hostPath.string();
std::uint32_t libraryLock = 0;
const int beginRc = BeginOpen(library, pFontHandle, libraryLock);
Expand Down
11 changes: 11 additions & 0 deletions core/libs/prx/libc/include/FilesystemError.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CORE_LIBS_PRX_LIBC_INCLUDE_FILESYSTEMERROR_HPP

#include <system_error>
#include "General.hpp"

inline int FilesystemError(const std::error_code& error) {
if (error == std::errc::no_such_file_or_directory) return 2;
Expand All @@ -22,4 +23,14 @@ inline int FilesystemError(const std::error_code& error) {
return 5;
}

inline int ResolvePathError_nid_no_patch(const char* path, std::filesystem::path& resolved) {
try {
resolved = ResolvePath_nid_no_patch(path);
return 0;
} catch (const std::filesystem::filesystem_error& error) {
if (error.code() == std::errc::illegal_byte_sequence) return 2;
return FilesystemError(error.code());
}
}

#endif
3 changes: 2 additions & 1 deletion core/libs/prx/libc/src/Directory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "prx/libc/include/GuestDirectory.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include <dirent.h>
#include <cerrno>
#include <cstring>
Expand Down Expand Up @@ -79,7 +80,7 @@ void* APS5_VABI opendir_nid_postfix(const char* path) {
if (!*path) { errno = 2; return nullptr; }
try {
auto directory = std::make_unique<Directory>();
directory->path = ResolvePath_nid_no_patch(path);
if (const int error = ResolvePathError_nid_no_patch(path, directory->path)) { errno = error; return nullptr; }
directory->native = OpenNative(directory->path);
if (!directory->native) { errno = DirectoryError(errno); return nullptr; }
return directory.release();
Expand Down
14 changes: 12 additions & 2 deletions core/libs/prx/libc/src/FileAndHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "prx/libc/include/FileStream.hpp"
#include "prx/libc/include/ApplicationHeap.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include "prx/libc/include/GuestArena.hpp"

struct GuestFileMode {
Expand Down Expand Up @@ -83,7 +84,15 @@ FileStream* APS5_VABI freopen_nid_postfix(const char* filename, const char* mode
const auto parsed = ParseFileMode(mode);
if (!parsed) { errno = 22; return nullptr; }
try {
const std::filesystem::path path = *filename ? ResolvePath_nid_no_patch(filename) : std::filesystem::path{};
std::filesystem::path path;
if (*filename) {
if (const int error = ResolvePathError_nid_no_patch(filename, path)) {
stream->Close();
if (stream->IsDynamic()) delete stream;
errno = error;
return nullptr;
}
}
if (stream->Reopen(path, parsed->native.c_str())) {
if (!path.empty() && parsed->writes) RecordWrittenPath_nid_no_patch(path);
return stream;
Expand All @@ -103,7 +112,8 @@ FileStream* APS5_VABI fopen_nid_postfix(const char* filename, const char* mode)
errno = EINVAL;
return nullptr;
}
const std::filesystem::path fpath = ResolvePath_nid_no_patch(filename);
std::filesystem::path fpath;
if (const int error = ResolvePathError_nid_no_patch(filename, fpath)) { errno = error; return nullptr; }
const auto abs_path = fpath.string();
std::unique_ptr<std::FILE, int (*)(std::FILE*)> handle(OpenNative(fpath, parsed->native), std::fclose);
if (!handle) {
Expand Down
38 changes: 34 additions & 4 deletions core/libs/prx/libc/src/General.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,29 @@ struct WorkingDirectory {
std::filesystem::path current = root;
};
WorkingDirectory& Directories() { static WorkingDirectory state; return state; }
std::filesystem::path HostPath(WorkingDirectory& state, const std::filesystem::path& guest) {
if (auto aliased = ResolveAlias(guest.relative_path().generic_string())) return *aliased;
return ResolveHostPath(state.root, guest.relative_path()).make_preferred();
}
void RequireHostDirectory(WorkingDirectory& state, const std::filesystem::path& guest) {
const auto host = HostPath(state, guest);
std::error_code error;
const auto status = std::filesystem::status(host, error);
if (std::filesystem::is_directory(status)) return;
if (!error) error = std::make_error_code(std::filesystem::exists(status) ? std::errc::not_a_directory : std::errc::no_such_file_or_directory);
#ifdef _WIN32
if (error == std::errc::no_such_file_or_directory) {
for (auto prefix = host.parent_path(); prefix.has_relative_path(); prefix = prefix.parent_path()) {
std::error_code prefixError;
const auto prefixStatus = std::filesystem::status(prefix, prefixError);
if (!std::filesystem::exists(prefixStatus)) continue;
if (!std::filesystem::is_directory(prefixStatus)) error = std::make_error_code(std::errc::not_a_directory);
break;
}
}
#endif
throw std::filesystem::filesystem_error("Guest path component is not a directory", host, error);
}
std::filesystem::path Resolve(WorkingDirectory& state, const char* path) {
std::string text(path);
for (auto& character : text) if (character == '\\') character = '/';
Expand All @@ -123,10 +146,17 @@ std::filesystem::path Resolve(WorkingDirectory& state, const char* path) {
// 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();
if (auto aliased = ResolveAlias(guest.relative_path().generic_string())) return *aliased;
return ResolveHostPath(state.root, guest.relative_path()).make_preferred();
const auto current = std::filesystem::path("/") / state.current.lexically_relative(state.root);
const auto relative = (input.is_absolute() ? input : current / input).relative_path();
std::filesystem::path guest("/");
for (auto part = relative.begin(); part != relative.end(); ++part) {
if (*part == "..") {
const auto parent = guest.lexically_normal();
if (parent.has_relative_path()) RequireHostDirectory(state, parent);
}
guest /= *part;
}
return HostPath(state, guest.lexically_normal());
}
#ifdef _WIN32
using WriteMark = FILETIME;
Expand Down
4 changes: 3 additions & 1 deletion core/libs/prx/libkernel/Apr/src/Apr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cstddef>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include "prx/libc/include/PreciseWait.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libkernel/Apr/include/AprCommandBuffer.hpp"
Expand Down Expand Up @@ -107,7 +108,8 @@ int _fail(int guestErrno) {

bool _resolve(const char* guestPath, std::uint32_t* id, std::uint64_t* size) {
if (!guestPath) return false;
auto hostPath = ResolvePath_nid_no_patch(guestPath);
std::filesystem::path hostPath;
if (ResolvePathError_nid_no_patch(guestPath, hostPath) != 0) return false;
std::uint64_t bytes = 0;
if (!_fileSize(hostPath, bytes)) return false;
std::lock_guard lock(g_filesLock);
Expand Down
13 changes: 6 additions & 7 deletions core/libs/prx/libkernel/File/src/Open.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "prx/libkernel/File/include/FileFlags.hpp"
#include "prx/libkernel/File/include/NativeStat.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libkernel/File/include/File.hpp"
#include "prx/libkernel/File/include/DirectoryDescriptor.hpp"
Expand Down Expand Up @@ -142,7 +143,8 @@ int APS5_VABI sceKernelOpen(const char* path, int flags, std::uint16_t mode) {
const int fd = File::OpenRandomDevice();
return fd < 0 ? SceErrorFromErrno(errno) : fd;
}
auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
#ifdef _WIN32
if ((flags & (SCE_KERNEL_O_DIRECTORY | SCE_KERNEL_O_CREAT)) == SCE_KERNEL_O_DIRECTORY) {
std::error_code error;
Expand Down Expand Up @@ -229,11 +231,7 @@ int APS5_VABI sceKernelStat(const char* path, FileStat* sb) {
}
if (*path == '\0') return SceErrorFromErrno(2);
std::filesystem::path native;
try {
native = ResolvePath_nid_no_patch(path);
} catch (const std::filesystem::filesystem_error&) {
return SceErrorFromErrno(2);
}
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
std::error_code error;
constexpr int GuestEnotdir = 20;
const auto status = std::filesystem::status(native, error);
Expand All @@ -252,7 +250,8 @@ int APS5_VABI sceKernelUnlink(const char* path) {
throw std::invalid_argument(std::string(__func__) + ": path is null");
}
if (*path == '\0') return SceErrorFromErrno(2);
auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
constexpr int GuestEperm = 1;
std::error_code error;
if (std::filesystem::is_directory(std::filesystem::symlink_status(native, error))) return SceErrorFromErrno(GuestEperm);
Expand Down
36 changes: 25 additions & 11 deletions core/libs/prx/libkernel/File/src/Stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <limits>
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libkernel/File/include/File.hpp"
#include "prx/libkernel/File/include/FileFlags.hpp"
Expand Down Expand Up @@ -436,7 +437,8 @@ extern "C" {

int APS5_VABI chmod_nid_postfix(const char* path, int mode) {
if (const int error = PathError(path)) return PosixFailure(error);
const auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return PosixFailure(error);
if (NativeChmod(native, mode & 07777) != 0) return PosixResult(SceErrorFromErrno(errno));
return 0;
}
Expand Down Expand Up @@ -575,7 +577,8 @@ int64_t APS5_VABI lseek_nid_postfix(int d, int64_t offset, int whence) {

int APS5_VABI mkdir_nid_postfix(const char* path, uint16_t mode) {
if (const int error = PathError(path)) return PosixFailure(error);
auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return PosixFailure(error);
if (NativeMkdir(native, mode) != 0) {
const int nativeError = errno;
const bool blockedByFile = (nativeError == ENOENT || nativeError == EINVAL) && HasNonDirectoryPrefix(native);
Expand Down Expand Up @@ -685,7 +688,9 @@ int APS5_VABI stat_nid_postfix(const char* path, FileStat* sb) {
int APS5_VABI lstat_nid_postfix(const char* path, FileStat* sb) {
if (sb == nullptr) return PosixFailure(GUEST_EFAULT);
if (const int error = PathError(path)) return PosixFailure(error);
if (!File::FillLinkStat(ResolvePath_nid_no_patch(path), sb)) return PosixResult(SceErrorFromErrno(errno));
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return PosixFailure(error);
if (!File::FillLinkStat(native, sb)) return PosixResult(SceErrorFromErrno(errno));
return 0;
}

Expand All @@ -698,8 +703,10 @@ int APS5_VABI sceKernelCheckReachability(const char* path) {
if (path == nullptr) return SCE_KERNEL_ERROR_EINVAL;
if (*path == '\0') return SCE_KERNEL_ERROR_ENOENT;
if (std::strlen(path) > 255) return SCE_KERNEL_ERROR_ENAMETOOLONG;
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
std::error_code error;
return std::filesystem::exists(ResolvePath_nid_no_patch(path), error) ? 0 : SCE_KERNEL_ERROR_ENOENT;
return std::filesystem::exists(native, error) ? 0 : SCE_KERNEL_ERROR_ENOENT;
}

int APS5_VABI sceKernelFstat(int d, FileStat* sb) {
Expand Down Expand Up @@ -808,7 +815,8 @@ int APS5_VABI sceKernelMkdir(const char* path, uint16_t mode) {
(void)mode;
if (path == nullptr) throw std::invalid_argument("sceKernelMkdir: path is null");
if (!*path) return SceErrorFromErrno(GUEST_ENOENT);
auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
while (!native.has_filename() && native.has_relative_path()) native = native.parent_path();
std::error_code error;
if (std::filesystem::exists(native, error)) return SceErrorFromErrno(GUEST_EEXIST);
Expand Down Expand Up @@ -1001,10 +1009,12 @@ int64_t APS5_VABI pwritev_nid_postfix(int d, const KernelIovec* iov, int iovcnt,
int APS5_VABI sceKernelRename(const char* from, const char* to) {
if (from == nullptr || to == nullptr) throw std::invalid_argument("sceKernelRename: path is null");
if (*from == '\0' || *to == '\0') return SceErrorFromErrno(GUEST_ENOENT);
const auto source = ResolvePath_nid_no_patch(from);
std::filesystem::path source;
if (const int error = ResolvePathError_nid_no_patch(from, source)) return SceKernelError(error);
std::error_code error;
if (!std::filesystem::exists(source, error)) return SceErrorFromErrno(GUEST_ENOENT);
const auto destination = ResolvePath_nid_no_patch(to);
std::filesystem::path destination;
if (const int result = ResolvePathError_nid_no_patch(to, destination)) return SceKernelError(result);
std::filesystem::rename(source, destination, error);
if (error) return SceErrorFromErrno(GUEST_EIO);
RecordWrittenPath_nid_no_patch(source);
Expand All @@ -1015,7 +1025,8 @@ int APS5_VABI sceKernelRename(const char* from, const char* to) {
int APS5_VABI sceKernelRmdir(const char* path) {
if (path == nullptr) throw std::invalid_argument("sceKernelRmdir: path is null");
if (*path == '\0') return SceErrorFromErrno(GUEST_ENOENT);
const auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
std::error_code error;
if (!std::filesystem::is_directory(native, error)) {
const bool blockedByFile = HasNonDirectoryPrefix(native) || std::filesystem::exists(WithoutTrailingSeparator(native), error);
Expand All @@ -1040,7 +1051,8 @@ extern "C" {
int APS5_VABI sceKernelChmod_nid_postfix(const char* path, std::uint16_t mode) {
if (!path) return SceErrorFromErrno(GUEST_EFAULT);
if (!*path) return SceErrorFromErrno(GUEST_ENOENT);
const auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
if (NativeChmod(native, mode & 07777) != 0) return SceErrorFromErrno(errno);
return 0;
}
Expand All @@ -1059,7 +1071,8 @@ int APS5_VABI sceKernelTruncate_nid_postfix(const char* path, std::int64_t lengt
if (path == nullptr) throw std::invalid_argument("sceKernelTruncate: path is null");
if (*path == '\0') return SceErrorFromErrno(GUEST_ENOENT);
if (length < 0) return SceErrorFromErrno(GUEST_EINVAL);
const auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
std::error_code error;
const auto status = std::filesystem::status(native, error);
if (!std::filesystem::exists(status)) return SceErrorFromErrno(GUEST_ENOENT);
Expand All @@ -1082,7 +1095,8 @@ int APS5_VABI sceKernelUtimes_nid_postfix(const char* path, const KernelTimeval*
if (times[i].tv_usec < 0 || times[i].tv_usec >= 1000000) return SCE_KERNEL_ERROR_EINVAL;
}
}
const auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
if (NativeUtimes(native, times) != 0) return SceErrorFromErrno(errno);
return 0;
}
Expand Down
12 changes: 8 additions & 4 deletions core/libs/prx/libkernel_sys/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#endif
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include "prx/libkernel/File/include/DirectoryDescriptor.hpp"
#ifdef _WIN32
#ifndef NOMINMAX
Expand Down Expand Up @@ -75,7 +76,8 @@ std::int64_t APS5_VABI readlink_nid_postfix(const char* path, char* buffer, std:
if (path == nullptr) return Fail(GuestEfault);
if (*path == '\0') return Fail(GuestEnoent);
if (size > static_cast<std::size_t>(std::numeric_limits<std::int64_t>::max())) return Fail(GuestEinval);
const auto native = ResolvePath_nid_no_patch(path);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return Fail(error);
#ifdef _WIN32
const DWORD attributes = GetFileAttributesW(native.wstring().c_str());
if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
Expand All @@ -99,8 +101,9 @@ std::int64_t APS5_VABI readlink_nid_postfix(const char* path, char* buffer, std:
int APS5_VABI link_nid_postfix(const char* existing, const char* created) {
if (existing == nullptr || created == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*existing == '\0' || *created == '\0') return static_cast<int>(Fail(GuestEnoent));
const auto source = ResolvePath_nid_no_patch(existing);
const auto destination = ResolvePath_nid_no_patch(created);
std::filesystem::path source, destination;
if (const int error = ResolvePathError_nid_no_patch(existing, source)) return static_cast<int>(Fail(error));
if (const int error = ResolvePathError_nid_no_patch(created, destination)) return static_cast<int>(Fail(error));
std::error_code error;
const auto status = std::filesystem::symlink_status(source, error);
if (!std::filesystem::exists(status)) return static_cast<int>(Fail(GuestEnoent));
Expand Down Expand Up @@ -134,7 +137,8 @@ int APS5_VABI symlink_nid_postfix(const char* target, const char* created) {
NotImplemented_nid_no_patch("symlink: absolute targets");
return -1;
}
const auto native = ResolvePath_nid_no_patch(created);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(created, native)) return static_cast<int>(Fail(error));
std::error_code error;
#ifdef _WIN32
if (GetFileAttributesW(native.wstring().c_str()) != INVALID_FILE_ATTRIBUTES) return static_cast<int>(Fail(GuestEexist));
Expand Down
Loading
Loading