Support/PathV2: Move current_path from path to fs and fix the Unix implementation.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Tue, 7 Dec 2010 01:22:31 +0000 (01:22 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Tue, 7 Dec 2010 01:22:31 +0000 (01:22 +0000)
Unix bug spotted by Dan Gohman.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121090 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/FileSystem.h
include/llvm/Support/PathV2.h
lib/Support/PathV2.cpp
lib/Support/Unix/PathV2.inc
lib/Support/Windows/PathV2.inc

index ce4da7ea75df856bfbb90bf1cfbdf2b3405dded2..5b8e2eab8ef73eea9fd89300a020b6c7459fbacc 100644 (file)
@@ -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<char> &result);
+
 /// @brief Remove path. Equivalent to POSIX remove().
 ///
 /// @param path Input path.
index 0f88d0bd47e4971e2450d118db59d9fafe59fcee..e2b87510452a878a74cd4716e240f6411dfe6cd1 100644 (file)
@@ -202,15 +202,6 @@ error_code native(const Twine &path, SmallVectorImpl<char> &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<char> &result);
-
-// The following are purely lexical.
-
 /// @brief Get root name.
 ///
 /// //net/hello => //net
index 639b323a8229a5ca3b8e64004955d28b2f114d12..492903e4b53a706ea1a85dd767f92bda8a193a24 100644 (file)
@@ -437,7 +437,7 @@ error_code make_absolute(SmallVectorImpl<char> &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) {
index b5cf724c5ff862eb256034fdda11eed6f88e537f..f7ca64daf7e9bf578a60d785d702f8408773f5b6 100644 (file)
@@ -69,24 +69,26 @@ namespace {
 
 namespace llvm {
 namespace sys  {
-namespace path {
+namespace fs {
 
 error_code current_path(SmallVectorImpl<char> &result) {
-  long size = ::pathconf(".", _PC_PATH_MAX);
-  result.reserve(size + 1);
-  result.set_size(size + 1);
-
-  if (::getcwd(result.data(), result.size()) == 0)
-    return error_code(errno, system_category());
+  result.reserve(MAXPATHLEN);
+
+  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;
index c724607453d3c5f62e001fcb5a1c4c1d35b68c7f..ff905153455108c5923f2cf666b91299f433e87d 100644 (file)
@@ -134,7 +134,7 @@ namespace {
 
 namespace llvm {
 namespace sys  {
-namespace path {
+namespace fs {
 
 error_code current_path(SmallVectorImpl<char> &result) {
   SmallVector<wchar_t, 128> 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;