[PATCH] D29035: [Support] Add sys::fs::set_current_path() (aka chdir)

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 11:09:42 PST 2017


Unix version is fine with me.

Why do you need to change the current directory btw?

Pavel Labath via Phabricator <reviews at reviews.llvm.org> writes:

> labath created this revision.
>
> This adds a cross-platform way of setting the current working directory
> analogous to the existing current_path() function used for retrieving
> it. The function will be used in lldb.
>
>
> https://reviews.llvm.org/D29035
>
> Files:
>   include/llvm/Support/FileSystem.h
>   lib/Support/Unix/Path.inc
>   lib/Support/Windows/Path.inc
>   unittests/Support/Path.cpp
>
>
> Index: unittests/Support/Path.cpp
> ===================================================================
> --- unittests/Support/Path.cpp
> +++ unittests/Support/Path.cpp
> @@ -1135,4 +1135,23 @@
>  
>    ::close(FileDescriptor);
>  }
> +
> +TEST_F(FileSystemTest, set_current_path) {
> +  SmallString<128> path;
> +
> +  ASSERT_NO_ERROR(fs::current_path(path));
> +  ASSERT_NE(TestDirectory, path);
> +
> +  struct RestorePath {
> +    SmallString<128> path;
> +    RestorePath(const SmallString<128> &path) : path(path) {}
> +    ~RestorePath() { fs::set_current_path(path); }
> +  } restore_path(path);
> +
> +  ASSERT_NO_ERROR(fs::set_current_path(TestDirectory));
> +
> +  ASSERT_NO_ERROR(fs::current_path(path));
> +  ASSERT_EQ(TestDirectory, path);
> +}
> +
>  } // anonymous namespace
> Index: lib/Support/Windows/Path.inc
> ===================================================================
> --- lib/Support/Windows/Path.inc
> +++ lib/Support/Windows/Path.inc
> @@ -200,6 +200,18 @@
>    return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
>  }
>  
> +std::error_code set_current_path(const Twine &path) {
> +  // Convert to utf-16.
> +  SmallVector<wchar_t, 128> wide_path;
> +  if (std::error_code ec = widenPath(path, wide_path))
> +    return ec;
> +
> +  if (!::SetCurrentDirectoryW(wide_path.begin()))
> +    return mapWindowsError(::GetLastError());
> +
> +  return std::error_code();
> +}
> +
>  std::error_code create_directory(const Twine &path, bool IgnoreExisting,
>                                   perms Perms) {
>    SmallVector<wchar_t, 128> path_utf16;
> Index: lib/Support/Unix/Path.inc
> ===================================================================
> --- lib/Support/Unix/Path.inc
> +++ lib/Support/Unix/Path.inc
> @@ -257,6 +257,16 @@
>    return std::error_code();
>  }
>  
> +std::error_code set_current_path(const Twine &path) {
> +  SmallString<128> path_storage;
> +  StringRef p = path.toNullTerminatedStringRef(path_storage);
> +
> +  if (::chdir(p.begin()) == -1)
> +    return std::error_code(errno, std::generic_category());
> +
> +  return std::error_code();
> +}
> +
>  std::error_code create_directory(const Twine &path, bool IgnoreExisting,
>                                   perms Perms) {
>    SmallString<128> path_storage;
> Index: include/llvm/Support/FileSystem.h
> ===================================================================
> --- include/llvm/Support/FileSystem.h
> +++ include/llvm/Support/FileSystem.h
> @@ -357,6 +357,13 @@
>  ///          otherwise a platform-specific error_code.
>  std::error_code current_path(SmallVectorImpl<char> &result);
>  
> +/// @brief Set the current path.
> +///
> +/// @param path The path to set.
> +/// @returns errc::success if the current path was successfully set,
> +///          otherwise a platform-specific error_code.
> +std::error_code set_current_path(const Twine &path);
> +
>  /// @brief Remove path. Equivalent to POSIX remove().
>  ///
>  /// @param path Input path.
>
>
> Index: unittests/Support/Path.cpp
> ===================================================================
> --- unittests/Support/Path.cpp
> +++ unittests/Support/Path.cpp
> @@ -1135,4 +1135,23 @@
>  
>    ::close(FileDescriptor);
>  }
> +
> +TEST_F(FileSystemTest, set_current_path) {
> +  SmallString<128> path;
> +
> +  ASSERT_NO_ERROR(fs::current_path(path));
> +  ASSERT_NE(TestDirectory, path);
> +
> +  struct RestorePath {
> +    SmallString<128> path;
> +    RestorePath(const SmallString<128> &path) : path(path) {}
> +    ~RestorePath() { fs::set_current_path(path); }
> +  } restore_path(path);
> +
> +  ASSERT_NO_ERROR(fs::set_current_path(TestDirectory));
> +
> +  ASSERT_NO_ERROR(fs::current_path(path));
> +  ASSERT_EQ(TestDirectory, path);
> +}
> +
>  } // anonymous namespace
> Index: lib/Support/Windows/Path.inc
> ===================================================================
> --- lib/Support/Windows/Path.inc
> +++ lib/Support/Windows/Path.inc
> @@ -200,6 +200,18 @@
>    return UTF16ToUTF8(cur_path.begin(), cur_path.size(), result);
>  }
>  
> +std::error_code set_current_path(const Twine &path) {
> +  // Convert to utf-16.
> +  SmallVector<wchar_t, 128> wide_path;
> +  if (std::error_code ec = widenPath(path, wide_path))
> +    return ec;
> +
> +  if (!::SetCurrentDirectoryW(wide_path.begin()))
> +    return mapWindowsError(::GetLastError());
> +
> +  return std::error_code();
> +}
> +
>  std::error_code create_directory(const Twine &path, bool IgnoreExisting,
>                                   perms Perms) {
>    SmallVector<wchar_t, 128> path_utf16;
> Index: lib/Support/Unix/Path.inc
> ===================================================================
> --- lib/Support/Unix/Path.inc
> +++ lib/Support/Unix/Path.inc
> @@ -257,6 +257,16 @@
>    return std::error_code();
>  }
>  
> +std::error_code set_current_path(const Twine &path) {
> +  SmallString<128> path_storage;
> +  StringRef p = path.toNullTerminatedStringRef(path_storage);
> +
> +  if (::chdir(p.begin()) == -1)
> +    return std::error_code(errno, std::generic_category());
> +
> +  return std::error_code();
> +}
> +
>  std::error_code create_directory(const Twine &path, bool IgnoreExisting,
>                                   perms Perms) {
>    SmallString<128> path_storage;
> Index: include/llvm/Support/FileSystem.h
> ===================================================================
> --- include/llvm/Support/FileSystem.h
> +++ include/llvm/Support/FileSystem.h
> @@ -357,6 +357,13 @@
>  ///          otherwise a platform-specific error_code.
>  std::error_code current_path(SmallVectorImpl<char> &result);
>  
> +/// @brief Set the current path.
> +///
> +/// @param path The path to set.
> +/// @returns errc::success if the current path was successfully set,
> +///          otherwise a platform-specific error_code.
> +std::error_code set_current_path(const Twine &path);
> +
>  /// @brief Remove path. Equivalent to POSIX remove().
>  ///
>  /// @param path Input path.


More information about the llvm-commits mailing list