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