Resolve relative file paths

Implemented path normalization so relative paths can be correctly
resolved
This commit is contained in:
x1nixmzeng 2015-02-11 01:11:52 +00:00
parent dbfd0b0f7b
commit 4351f48c7b
4 changed files with 94 additions and 8 deletions

View File

@ -104,4 +104,26 @@ std::wstring fix_path_separators(const std::wstring& source, wchar_t new_sep) {
return dest; return dest;
} }
std::string fix_path_separators(const std::string& source, char new_sep) {
// Swap all separators to new_sep.
char old_sep = new_sep == '\\' ? '/' : '\\';
std::string::size_type pos = 0;
std::string dest = source;
while ((pos = source.find_first_of(old_sep, pos)) != std::string::npos) {
dest[pos] = new_sep;
++pos;
}
// Replace redundant separators.
pos = 0;
while ((pos = dest.find_first_of(new_sep, pos)) != std::string::npos) {
if (pos < dest.size() - 1) {
if (dest[pos + 1] == new_sep) {
dest.erase(pos + 1, 1);
}
}
++pos;
}
return dest;
}
} // namespace poly } // namespace poly

View File

@ -45,6 +45,8 @@ std::wstring join_paths(const std::wstring& left, const std::wstring& right,
// separators. // separators.
std::wstring fix_path_separators(const std::wstring& source, std::wstring fix_path_separators(const std::wstring& source,
wchar_t new_sep = poly::path_separator); wchar_t new_sep = poly::path_separator);
std::string fix_path_separators(const std::string& source,
char new_sep = poly::path_separator);
} // namespace poly } // namespace poly

View File

@ -146,18 +146,78 @@ int FileSystem::DeleteSymbolicLink(const std::string& path) {
return 0; return 0;
} }
std::unique_ptr<Entry> FileSystem::ResolvePath(const std::string& path) { std::string FileSystem::CanonicalizePath(const std::string& original_path) const {
// Strip off prefix and pass to device. char path_seperator('\\');
// e.g., d:\some\PATH.foo -> some\PATH.foo std::string path(poly::fix_path_separators(original_path, path_seperator));
// Support both symlinks and device specifiers, like:
// \\Device\Foo\some\PATH.foo, d:\some\PATH.foo, etc.
// TODO(benvanik): normalize path/etc std::vector<std::string::size_type> hints;
// e.g., remove ..'s and such
std::string::size_type pos(std::string::npos);
while ((pos = path.find_first_of(path_seperator, pos + 1)) != -1) {
hints.push_back(pos);
}
if (!hints.empty()) {
{
const std::string::size_type len(path.size());
// Treat last char as a hint for our range indexing
if (hints[hints.size() - 1] != len - 1) {
hints.push_back(len);
}
}
auto it1 = hints.rbegin();
auto it2 = it1;
++it1;
while (it1 != hints.rend()) {
auto diff(*it2 - *it1);
switch (diff) {
case 2:
// Reference to current dir
if (path[*it1 + 1] == '.') {
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 {
path.erase(*it1);
}
it1 = it3;
} else {
++it1; ++it2;
}
break;
default:
++it1; ++it2;
}
}
}
// Sanity checks
if ((path.size() == 1 && path[0] == '.')
|| (path.size() == 2 && path[0] == '.' && path[1] == '.')) {
return "";
}
return path;
}
std::unique_ptr<Entry> FileSystem::ResolvePath(const std::string& path) {
// Resolve relative paths
std::string normalized_path(CanonicalizePath(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:.
std::string normalized_path = path;
if (path[0] == '\\') { if (path[0] == '\\') {
normalized_path = "game:" + normalized_path; normalized_path = "game:" + normalized_path;
} }

View File

@ -56,6 +56,8 @@ class FileSystem {
private: private:
std::vector<Device*> devices_; std::vector<Device*> devices_;
std::unordered_map<std::string, std::string> symlinks_; std::unordered_map<std::string, std::string> symlinks_;
std::string CanonicalizePath(const std::string& original_path) const;
}; };
} // namespace fs } // namespace fs