diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2017-01-09 12:06:38 +0000 |
---|---|---|
committer | Marcel Hlopko <hlopko@google.com> | 2017-01-09 13:21:57 +0000 |
commit | 2e08b9a8001f87903a04183387d2a67605592138 (patch) | |
tree | 8a3a3b1d2967a5a877f70f6ce827e51a50882ca4 /src/main/cpp | |
parent | 91d1ea0d1a1806b2f0fc9dd1f959b61e5ae1704e (diff) |
Bazel client, Windows: implement GetCwd
See https://github.com/bazelbuild/bazel/issues/2107
See https://github.com/bazelbuild/bazel/issues/2181
--
PiperOrigin-RevId: 143947877
MOS_MIGRATED_REVID=143947877
Diffstat (limited to 'src/main/cpp')
-rw-r--r-- | src/main/cpp/util/file_posix.cc | 2 | ||||
-rw-r--r-- | src/main/cpp/util/file_windows.cc | 31 |
2 files changed, 24 insertions, 9 deletions
diff --git a/src/main/cpp/util/file_posix.cc b/src/main/cpp/util/file_posix.cc index 475de9ef18..cdeb0ecd5b 100644 --- a/src/main/cpp/util/file_posix.cc +++ b/src/main/cpp/util/file_posix.cc @@ -299,6 +299,7 @@ bool MakeDirectories(const string &path, unsigned int mode) { return MakeDirectories(path, mode, true); } +#ifndef __CYGWIN__ string GetCwd() { char cwdbuf[PATH_MAX]; if (getcwd(cwdbuf, sizeof cwdbuf) == NULL) { @@ -306,6 +307,7 @@ string GetCwd() { } return string(cwdbuf); } +#endif // not __CYGWIN__ bool ChangeDirectory(const string& path) { return chdir(path.c_str()) == 0; diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index f947df1508..10d3e0604c 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.cc @@ -32,6 +32,14 @@ using std::unique_ptr; using std::vector; using std::wstring; +template <typename char_type> +static bool HasUncPrefix(const char_type* path) { + return path[0] == '\\' && (path[1] == '\\' || path[1] == '?') && + (path[2] == '.' || path[2] == '?') && path[3] == '\\'; +} + +static unique_ptr<WCHAR[]> GetCwdW(); + class WindowsPipe : public IPipe { public: WindowsPipe(const HANDLE& read_handle, const HANDLE& write_handle) @@ -101,9 +109,8 @@ static bool IsRootOrAbsolute(const string& path, bool must_be_root) { (path[2] == '/' || path[2] == '\\')) || // path is (or starts with) "\\?\c:\" or "\??\c:\" or similar ((must_be_root ? path.size() == 7 : path.size() >= 7) && - path[0] == '\\' && (path[1] == '\\' || path[1] == '?') && - path[2] == '?' && path[3] == '\\' && isalpha(path[4]) && - path[5] == ':' && path[6] == '\\'); + HasUncPrefix(path.c_str()) && isalpha(path[4]) && path[5] == ':' && + path[6] == '\\'); } pair<string, string> SplitPath(const string& path) { @@ -525,14 +532,20 @@ bool MakeDirectories(const string& path, unsigned int mode) { #else // not COMPILER_MSVC #endif // COMPILER_MSVC -#ifdef COMPILER_MSVC +static unique_ptr<WCHAR[]> GetCwdW() { + DWORD len = ::GetCurrentDirectoryW(0, nullptr); + unique_ptr<WCHAR[]> cwd(new WCHAR[len]); + if (!::GetCurrentDirectoryW(len, cwd.get())) { + die(255, "GetCurrentDirectoryW failed, err=%d\n", GetLastError()); + } + return std::move(cwd); +} + string GetCwd() { - // TODO(bazel-team): implement this. - pdie(255, "blaze_util::GetCwd is not implemented on Windows"); - return ""; + unique_ptr<WCHAR[]> cwd(GetCwdW()); + return string( + WstringToCstring(cwd.get() + (HasUncPrefix(cwd.get()) ? 4 : 0)).get()); } -#else // not COMPILER_MSVC -#endif // COMPILER_MSVC #ifdef COMPILER_MSVC bool ChangeDirectory(const string& path) { |