From 0c4b2ff06bcbb996911dfb864c31ad22d521f6b9 Mon Sep 17 00:00:00 2001 From: Eladash Date: Sat, 20 Nov 2021 10:44:33 +0200 Subject: [PATCH] fs: Reimplement path resolving using std::filesystem::weakly_canonical --- Utilities/File.cpp | 15 +++++++++++++++ Utilities/File.h | 3 +++ rpcs3/Emu/System.cpp | 12 ++++++------ rpcs3/Emu/System.h | 1 - rpcs3/Emu/VFS.cpp | 28 +++++++++++++++++++++------- rpcs3/main_application.cpp | 7 ------- 6 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Utilities/File.cpp b/Utilities/File.cpp index deaa13dbdc..9c236a2d64 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -4,6 +4,7 @@ #include "Crypto/sha1.h" #include +#include #include #include #include @@ -1822,6 +1823,20 @@ bool fs::remove_all(const std::string& path, bool remove_root) return true; } +std::string fs::resolve_path(std::string_view path) +{ + std::error_code ec{}; + const auto result = std::filesystem::weakly_canonical(std::filesystem::path(path), ec); + + if (ec) + { + g_tls_error = error::inval; + return {}; + } + + return result.string(); +} + u64 fs::get_dir_size(const std::string& path, u64 rounding_alignment) { u64 result = 0; diff --git a/Utilities/File.h b/Utilities/File.h index 8e019d8cc5..790d7bdcc8 100644 --- a/Utilities/File.h +++ b/Utilities/File.h @@ -645,6 +645,9 @@ namespace fs std::string m_dest{}; // Destination file path }; + // Get real path for comparisons + std::string resolve_path(std::string_view path); + // Delete directory and all its contents recursively bool remove_all(const std::string& path, bool remove_root = true); diff --git a/rpcs3/Emu/System.cpp b/rpcs3/Emu/System.cpp index e67abdb023..2315a95427 100644 --- a/rpcs3/Emu/System.cpp +++ b/rpcs3/Emu/System.cpp @@ -554,7 +554,7 @@ void Emulator::SetForceBoot(bool force_boot) game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool is_disc_patch) { - const std::string resolved_path = GetCallbacks().resolve_path(m_path); + const std::string resolved_path = fs::resolve_path(m_path); if (!IsStopped()) { @@ -1213,7 +1213,7 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool // Check game updates const std::string hdd0_boot = hdd0_game + m_title_id + "/USRDIR/EBOOT.BIN"; - if (disc.empty() && !bdvd_dir.empty() && GetCallbacks().resolve_path(m_path) != GetCallbacks().resolve_path(hdd0_boot) && fs::is_file(hdd0_boot)) + if (disc.empty() && !bdvd_dir.empty() && fs::resolve_path(m_path) != fs::resolve_path(hdd0_boot) && fs::is_file(hdd0_boot)) { // Booting game update sys_log.success("Updates found at /dev_hdd0/game/%s/", m_title_id); @@ -1333,7 +1333,7 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool return fmt::merge(result, "/"); }; - const std::string resolved_hdd0 = GetCallbacks().resolve_path(hdd0_game) + '/'; + const std::string resolved_hdd0 = fs::resolve_path(hdd0_game) + '/'; if (from_hdd0_game && m_cat == "DG") { @@ -1357,7 +1357,7 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool else if (from_dev_flash) { // Firmware executables - argv[0] = "/dev_flash" + resolved_path.substr(GetCallbacks().resolve_path(g_cfg_vfs.get_dev_flash()).size()); + argv[0] = "/dev_flash" + resolved_path.substr(fs::resolve_path(g_cfg_vfs.get_dev_flash()).size()); m_dir = fs::get_parent_dir(argv[0]) + '/'; } else if (g_cfg.vfs.host_root) @@ -1975,9 +1975,9 @@ std::set Emulator::GetGameDirs() const bool Emulator::IsPathInsideDir(std::string_view path, std::string_view dir) const { - const std::string dir_path = GetCallbacks().resolve_path(dir); + const std::string dir_path = fs::resolve_path(dir); - return !dir_path.empty() && (GetCallbacks().resolve_path(path) + '/').starts_with(dir_path + '/'); + return !dir_path.empty() && (fs::resolve_path(path) + '/').starts_with(dir_path + '/'); }; const std::string& Emulator::GetFakeCat() const diff --git a/rpcs3/Emu/System.h b/rpcs3/Emu/System.h index f0159f9786..2534a34480 100644 --- a/rpcs3/Emu/System.h +++ b/rpcs3/Emu/System.h @@ -81,7 +81,6 @@ struct EmuCallbacks std::function get_localized_string; std::function get_localized_u32string; std::function play_sound; - std::string(*resolve_path)(std::string_view) = nullptr; // Resolve path using Qt }; class Emulator final diff --git a/rpcs3/Emu/VFS.cpp b/rpcs3/Emu/VFS.cpp index 966214a31e..bc770e6074 100644 --- a/rpcs3/Emu/VFS.cpp +++ b/rpcs3/Emu/VFS.cpp @@ -42,8 +42,6 @@ bool vfs::mount(std::string_view vpath, std::string_view path) // TODO: scan roots of mounted devices for undeleted vfs::host::unlink remnants, and try to delete them (_WIN32 only) - std::lock_guard lock(table.mutex); - if (vpath.empty()) { // Empty relative path, should set relative path base; unsupported @@ -51,8 +49,24 @@ bool vfs::mount(std::string_view vpath, std::string_view path) return false; } + const bool delim_suffixed = path.ends_with(fs::delim); + std::string final_path = fs::resolve_path(path); + + if (final_path.empty()) + { + vfs_log.error("Cannot mount path \"%s\" due to invalid host path \"%s\" (%s)", vpath, path, fs::g_tls_error); + return false; + } + + if (!final_path.ends_with(fs::delim) && delim_suffixed) + { + final_path += '/'; + } + const std::string_view vpath_backup = vpath; + std::lock_guard lock(table.mutex); + for (std::vector list{&table.root};;) { // Skip one or more '/' @@ -68,8 +82,8 @@ bool vfs::mount(std::string_view vpath, std::string_view path) if (pos == umax) { // Mounting completed - list.back()->path = path; - vfs_log.notice("Mounted path \"%s\" to \"%s\"", vpath_backup, path); + list.back()->path = std::move(final_path); + vfs_log.notice("Mounted path \"%s\" to \"%s\"", vpath_backup, final_path); return true; } @@ -743,7 +757,7 @@ bool vfs::host::rename(const std::string& from, const std::string& to, const lv2 { // Lock mount point, close file descriptors, retry const auto from0 = std::string_view(from).substr(0, from.find_last_not_of(fs::delim) + 1); - const auto escaped_from = Emu.GetCallbacks().resolve_path(from); + const auto escaped_from = fs::resolve_path(from); std::lock_guard lock(mp->mutex); @@ -754,7 +768,7 @@ bool vfs::host::rename(const std::string& from, const std::string& to, const lv2 idm::select([&](u32 /*id*/, lv2_file& file) { - if (check_path(Emu.GetCallbacks().resolve_path(file.real_path))) + if (check_path(fs::resolve_path(file.real_path))) { ensure(file.mp == mp); @@ -796,7 +810,7 @@ bool vfs::host::rename(const std::string& from, const std::string& to, const lv2 idm::select([&](u32 /*id*/, lv2_file& file) { - const auto escaped_real = Emu.GetCallbacks().resolve_path(file.real_path); + const auto escaped_real = fs::resolve_path(file.real_path); if (check_path(escaped_real)) { diff --git a/rpcs3/main_application.cpp b/rpcs3/main_application.cpp index ff61055cb2..8d4d110c96 100644 --- a/rpcs3/main_application.cpp +++ b/rpcs3/main_application.cpp @@ -26,8 +26,6 @@ #include "Emu/Audio/FAudio/FAudioBackend.h" #endif -#include // This shouldn't be outside rpcs3qt... - LOG_CHANNEL(sys_log, "SYS"); /** Emu.Init() wrapper for user management */ @@ -123,10 +121,5 @@ EmuCallbacks main_application::CreateCallbacks() return result; }; - callbacks.resolve_path = [](std::string_view sv) - { - return QFileInfo(QString::fromUtf8(sv.data(), static_cast(sv.size()))).canonicalFilePath().toStdString(); - }; - return callbacks; }