Rewrite real_path, try to find an existing directory in the given path

Never throw, return the given filename in the worst case.
This commit is contained in:
Maxime Coste 2014-07-30 00:03:01 +01:00
parent 6e4b0f5781
commit 06fc329739

View File

@ -50,21 +50,28 @@ String parse_filename(StringView filename)
String real_path(StringView filename)
{
StringView dirname = ".";
StringView basename = filename;
char buffer[PATH_MAX+1];
auto it = find(filename.rbegin(), filename.rend(), '/');
if (it != filename.rend())
StringView existing = filename;
StringView non_existing;
while (true)
{
dirname = StringView{filename.begin(), it.base()};
basename = StringView{it.base(), filename.end()};
char* res = realpath(existing.zstr(), buffer);
if (res)
{
if (non_existing.empty())
return res;
return res + "/"_str + non_existing;
}
char buffer[PATH_MAX+1];
char* res = realpath(dirname.zstr(), buffer);
if (not res)
throw file_not_found{dirname};
return res + "/"_str + basename;
auto it = find(existing.rbegin(), existing.rend(), '/');
if (it == existing.rend())
return filename;
existing = StringView{existing.begin(), it.base()-1};
non_existing = StringView{it.base(), filename.end()};
}
}
String compact_path(StringView filename)