fs: Reimplement path resolving using std::filesystem::weakly_canonical

This commit is contained in:
Eladash 2021-11-20 10:44:33 +02:00 committed by Ivan
parent 8c9090fd03
commit 0c4b2ff06b
6 changed files with 45 additions and 21 deletions

View File

@ -4,6 +4,7 @@
#include "Crypto/sha1.h"
#include <unordered_map>
#include <filesystem>
#include <algorithm>
#include <cstring>
#include <map>
@ -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;

View File

@ -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);

View File

@ -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<std::string> 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

View File

@ -81,7 +81,6 @@ struct EmuCallbacks
std::function<std::string(localized_string_id, const char*)> get_localized_string;
std::function<std::u32string(localized_string_id, const char*)> get_localized_u32string;
std::function<void(const std::string&)> play_sound;
std::string(*resolve_path)(std::string_view) = nullptr; // Resolve path using Qt
};
class Emulator final

View File

@ -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<vfs_directory*> 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<lv2_fs_object, lv2_file>([&](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<lv2_fs_object, lv2_file>([&](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))
{

View File

@ -26,8 +26,6 @@
#include "Emu/Audio/FAudio/FAudioBackend.h"
#endif
#include <QFileInfo> // 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<int>(sv.size()))).canonicalFilePath().toStdString();
};
return callbacks;
}