1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-22 02:32:36 +01:00

Rewrite fs::get_parent_dir

Don't analyse full path if possible.
This commit is contained in:
Nekotekina 2021-02-12 15:59:57 +03:00
parent 629d608d4f
commit 0a23a5ef50

View File

@ -322,12 +322,53 @@ std::shared_ptr<fs::device_base> fs::set_virtual_device(const std::string& name,
std::string fs::get_parent_dir(const std::string& path)
{
// Get (basically) processed path
const auto real_path = fs::escape_path(path);
std::string_view result = path;
const auto pos = real_path.find_last_of(delim);
// Number of path components to remove
usz to_remove = 1;
return real_path.substr(0, pos == umax ? 0 : pos);
while (to_remove--)
{
// Trim contiguous delimiters at the end
if (usz sz = result.find_last_not_of(delim) + 1)
{
result = result.substr(0, sz);
}
else
{
return "/";
}
const auto elem = result.substr(result.find_last_of(delim) + 1);
if (elem.empty() || elem.size() == result.size())
{
break;
}
if (elem == ".")
{
to_remove += 1;
}
if (elem == "..")
{
to_remove += 2;
}
result.remove_suffix(elem.size());
}
if (usz sz = result.find_last_not_of(delim) + 1)
{
result = result.substr(0, sz);
}
else
{
return "/";
}
return std::string{result};
}
bool fs::stat(const std::string& path, stat_t& info)