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

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