summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support
diff options
context:
space:
mode:
authorPavel Labath <labath@google.com>2017-01-24 10:32:03 +0000
committerPavel Labath <labath@google.com>2017-01-24 10:32:03 +0000
commit2f0960970f55637c0ff02764ad899d3c269f1053 (patch)
tree9e4d92f7e3d8ef91c30c4ae12487a3be8def3ad4 /llvm/lib/Support
parent5aeb880d909e50d85b44d6c019c3dfd3bcdb0ca2 (diff)
downloadbcm5719-llvm-2f0960970f55637c0ff02764ad899d3c269f1053.tar.gz
bcm5719-llvm-2f0960970f55637c0ff02764ad899d3c269f1053.zip
[Support] Add sys::fs::set_current_path() (aka chdir)
Summary: 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. Reviewers: rafael, silvas, zturner Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D29035 llvm-svn: 292907
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r--llvm/lib/Support/Unix/Path.inc10
-rw-r--r--llvm/lib/Support/Windows/Path.inc12
2 files changed, 22 insertions, 0 deletions
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 0bf9dbae220..f7334245a00 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -257,6 +257,16 @@ std::error_code current_path(SmallVectorImpl<char> &result) {
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;
diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 27b250b428a..3597b551098 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -200,6 +200,18 @@ std::error_code current_path(SmallVectorImpl<char> &result) {
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;
OpenPOWER on IntegriCloud