mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-04 05:51:27 +12:00
Rewrite fs::get_parent_dir
Don't analyse full path if possible.
This commit is contained in:
parent
629d608d4f
commit
0a23a5ef50
1 changed files with 45 additions and 4 deletions
|
@ -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)
|
std::string fs::get_parent_dir(const std::string& path)
|
||||||
{
|
{
|
||||||
// Get (basically) processed path
|
std::string_view result = path;
|
||||||
const auto real_path = fs::escape_path(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)
|
bool fs::stat(const std::string& path, stat_t& info)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue