diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-07-30 00:03:01 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-07-30 00:03:01 +0100 |
| commit | 06fc3297399fdfad154d1af71cb2d2055d823996 (patch) | |
| tree | 54a69ef395a4e4e4ca50c69ebc9fa5840134d85d /src | |
| parent | 6e4b0f57811f71a7ae89304d3528bc9ac47d57e9 (diff) | |
Rewrite real_path, try to find an existing directory in the given path
Never throw, return the given filename in the worst case.
Diffstat (limited to 'src')
| -rw-r--r-- | src/file.cc | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/file.cc b/src/file.cc index 0c13c471..c0ffd643 100644 --- a/src/file.cc +++ b/src/file.cc @@ -50,21 +50,28 @@ String parse_filename(StringView filename) String real_path(StringView filename) { - StringView dirname = "."; - StringView basename = filename; + char buffer[PATH_MAX+1]; + + StringView existing = filename; + StringView non_existing; - auto it = find(filename.rbegin(), filename.rend(), '/'); - if (it != filename.rend()) + 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) |
