From 0b2a9d7b9ef549b607ac3998cbaa39a662dcfc6d Mon Sep 17 00:00:00 2001 From: Evan Klitzke Date: Thu, 20 Jun 2019 15:18:39 -0700 Subject: [PATCH] use realpath(3) to canonicalize file paths on posix --- src/platform_posix.cc | 13 +++++++------ src/utils.cc | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/platform_posix.cc b/src/platform_posix.cc index c172446b..71dbb591 100644 --- a/src/platform_posix.cc +++ b/src/platform_posix.cc @@ -39,9 +39,6 @@ limitations under the License. #include #endif -#include -#include - #include #include #include @@ -52,10 +49,14 @@ namespace pipeline { void ThreadEnter(); } +// Normalize the path, including eliminating redundant . and duplicated / +// characters. std::string NormalizePath(const std::string &path) { - llvm::SmallString<256> P(path); - llvm::sys::path::remove_dots(P, true); - return {P.data(), P.size()}; + char canonical[PATH_MAX + 1]; + if (realpath(path.c_str(), canonical)) { + return canonical; + } + return path; } void FreeUnusedMemory() { diff --git a/src/utils.cc b/src/utils.cc index 642f4e24..67347002 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -125,6 +125,7 @@ void EnsureEndsInSlash(std::string &path) { std::string EscapeFileName(std::string path) { bool slash = path.size() && path.back() == '/'; + path = NormalizePath(path); #ifdef _WIN32 std::replace(path.begin(), path.end(), ':', '@'); #endif