diff --git a/include/llvm/Support/FileSystem.h b/include/llvm/Support/FileSystem.h index ce4da7ea75d..5b8e2eab8ef 100644 --- a/include/llvm/Support/FileSystem.h +++ b/include/llvm/Support/FileSystem.h @@ -148,6 +148,13 @@ error_code create_hard_link(const Twine &to, const Twine &from); /// otherwise a platform specific error_code. error_code create_symlink(const Twine &to, const Twine &from); +/// @brief Get the current path. +/// +/// @param result Holds the current path on return. +/// @results errc::success if the current path has been stored in result, +/// otherwise a platform specific error_code. +error_code current_path(SmallVectorImpl &result); + /// @brief Remove path. Equivalent to POSIX remove(). /// /// @param path Input path. diff --git a/include/llvm/Support/PathV2.h b/include/llvm/Support/PathV2.h index 0f88d0bd47e..e2b87510452 100644 --- a/include/llvm/Support/PathV2.h +++ b/include/llvm/Support/PathV2.h @@ -202,15 +202,6 @@ error_code native(const Twine &path, SmallVectorImpl &result); /// @name Lexical Observers /// @{ -/// @brief Get the current path. -/// -/// @param result Holds the current path on return. -/// @results errc::success if the current path has been stored in result, -/// otherwise a platform specific error_code. -error_code current_path(SmallVectorImpl &result); - -// The following are purely lexical. - /// @brief Get root name. /// /// //net/hello => //net diff --git a/lib/Support/PathV2.cpp b/lib/Support/PathV2.cpp index 639b323a822..492903e4b53 100644 --- a/lib/Support/PathV2.cpp +++ b/lib/Support/PathV2.cpp @@ -437,7 +437,7 @@ error_code make_absolute(SmallVectorImpl &path) { // All of the following conditions will need the current directory. SmallString<128> current_dir; - if (error_code ec = current_path(current_dir)) return ec; + if (error_code ec = fs::current_path(current_dir)) return ec; // Relative path. Prepend the current directory. if (!rootName && !rootDirectory) { diff --git a/lib/Support/Unix/PathV2.inc b/lib/Support/Unix/PathV2.inc index b5cf724c5ff..f7ca64daf7e 100644 --- a/lib/Support/Unix/PathV2.inc +++ b/lib/Support/Unix/PathV2.inc @@ -69,24 +69,26 @@ namespace { namespace llvm { namespace sys { -namespace path { +namespace fs { error_code current_path(SmallVectorImpl &result) { - long size = ::pathconf(".", _PC_PATH_MAX); - result.reserve(size + 1); - result.set_size(size + 1); + result.reserve(MAXPATHLEN); - if (::getcwd(result.data(), result.size()) == 0) - return error_code(errno, system_category()); + while (true) { + if (::getcwd(result.data(), result.capacity()) == 0) { + // See if there was a real error. + if (errno != errc::not_enough_memory) + return error_code(errno, system_category()); + // Otherwise there just wasn't enough space. + result.reserve(result.capacity() * 2); + } else + break; + } result.set_size(strlen(result.data())); return success; } -} // end namespace path - -namespace fs{ - error_code copy_file(const Twine &from, const Twine &to, copy_option copt) { // Get arguments. SmallString<128> from_storage; diff --git a/lib/Support/Windows/PathV2.inc b/lib/Support/Windows/PathV2.inc index c724607453d..ff905153455 100644 --- a/lib/Support/Windows/PathV2.inc +++ b/lib/Support/Windows/PathV2.inc @@ -134,7 +134,7 @@ namespace { namespace llvm { namespace sys { -namespace path { +namespace fs { error_code current_path(SmallVectorImpl &result) { SmallVector cur_path; @@ -180,10 +180,6 @@ retry_cur_dir: return success; } -} // end namespace path - -namespace fs { - error_code copy_file(const Twine &from, const Twine &to, copy_option copt) { // Get arguments. SmallString<128> from_storage;