use realpath(3) to canonicalize file paths on posix

This commit is contained in:
Evan Klitzke 2019-06-20 15:18:39 -07:00
parent 9f25474e73
commit 0b2a9d7b9e
No known key found for this signature in database
GPG Key ID: 3279048B58C0BCC2
2 changed files with 8 additions and 6 deletions

View File

@ -39,9 +39,6 @@ limitations under the License.
#include <malloc.h>
#endif
#include <llvm/ADT/SmallString.h>
#include <llvm/Support/Path.h>
#include <atomic>
#include <condition_variable>
#include <mutex>
@ -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() {

View File

@ -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