Various fixes to resolving relative paths

Rewritten the canonicalization path logic to better handle complex
relative paths
This commit is contained in:
x1nixmzeng 2015-02-12 01:39:22 +00:00
parent bde6cf0d85
commit 9300551e31
1 changed files with 63 additions and 46 deletions

View File

@ -147,64 +147,81 @@ int FileSystem::DeleteSymbolicLink(const std::string& path) {
} }
std::string FileSystem::CanonicalizePath(const std::string& original_path) const { std::string FileSystem::CanonicalizePath(const std::string& original_path) const {
char path_seperator('\\'); char path_separator('\\');
std::string path(poly::fix_path_separators(original_path, path_seperator)); std::string path(poly::fix_path_separators(original_path, path_separator));
std::vector<std::string::size_type> hints; std::vector<std::string::size_type> path_breaks;
std::string::size_type pos(std::string::npos); std::string::size_type pos(path.find_first_of(path_separator));
while ((pos = path.find_first_of(path_seperator, pos + 1)) != -1) { std::string::size_type pos_n(std::string::npos);
hints.push_back(pos);
}
if (!hints.empty()) { while (pos != std::string::npos) {
if ((pos_n = path.find_first_of(path_separator, pos + 1)) == std::string::npos) {
pos_n = path.size();
}
auto diff(pos_n - pos);
switch (diff)
{ {
const std::string::size_type len(path.size()); case 0:
// Treat last char as a hint for our range indexing pos_n = std::string::npos;
if (hints[hints.size() - 1] != len - 1) { break;
hints.push_back(len); case 1:
// Duplicate separators
path.erase(pos, 1);
pos_n -= 1;
break;
case 2:
// Potential marker for current directory
if (path[pos + 1] == '.') {
path.erase(pos, 2);
pos_n -= 2;
} else {
path_breaks.push_back(pos);
} }
} break;
case 3:
auto it1 = hints.rbegin(); // Potential marker for parent directory
auto it2 = it1; if (path[pos + 1] == '.' && path[pos + 2] == '.'){
++it1; if (path_breaks.empty()) {
while (it1 != hints.rend()) { // Ensure we don't override the device name
auto diff(*it2 - *it1); std::string::size_type loc(path.find_first_of(':'));
switch (diff) { auto req(pos + 3);
case 2: if (loc == std::string::npos || loc > req) {
// Reference to current dir path.erase(0, req);
if (path[*it1 + 1] == '.') { pos_n -= req;
path.erase(*it1, 2);
}
++it1; ++it2;
break;
case 3:
// Reference to parent dir
if (path[*it1 + 1] == '.' && path[*it1 + 2] == '.') {
auto it3 = it1 + 1;
if (it3 != hints.rend()) {
diff = (*it1 - *it3);
path.erase(*it3, diff + 3);
++it2;
} else { } else {
path.erase(*it1); path.erase(loc + 1, req - (loc + 1));
pos_n -= req - (loc + 1);
} }
it1 = it3;
} else { } else {
++it1; ++it2; auto last(path_breaks.back());
auto last_diff((pos + 3) - last);
path.erase(last, last_diff);
pos_n = last;
// Also remove path reference
path_breaks.erase(path_breaks.end() - 1);
} }
break; } else {
default: path_breaks.push_back(pos);
++it1; ++it2;
} }
break;
default:
path_breaks.push_back(pos);
break;
} }
pos = pos_n;
} }
// Sanity checks // Remove trailing seperator
if ((path.size() == 1 && path[0] == '.') if (!path.empty() && path.back() == path_separator) {
path.erase(path.size() - 1);
}
// Final sanity check for dead paths
if ((path.size() == 1 && (path[0] == '.' || path[0] == path_separator))
|| (path.size() == 2 && path[0] == '.' && path[1] == '.')) { || (path.size() == 2 && path[0] == '.' && path[1] == '.')) {
return ""; return "";
} }
@ -218,7 +235,7 @@ std::unique_ptr<Entry> FileSystem::ResolvePath(const std::string& path) {
// If no path (starts with a slash) do it module-relative. // If no path (starts with a slash) do it module-relative.
// Which for now, we just make game:. // Which for now, we just make game:.
if (path[0] == '\\') { if (normalized_path[0] == '\\') {
normalized_path = "game:" + normalized_path; normalized_path = "game:" + normalized_path;
} }