Skip to content

Commit

Permalink
Bazel client, Windows: implement GetCwd
Browse files Browse the repository at this point in the history
See #2107
See #2181

--
PiperOrigin-RevId: 143947877
MOS_MIGRATED_REVID=143947877
  • Loading branch information
laszlocsomor authored and hlopko committed Jan 9, 2017
1 parent 91d1ea0 commit 2e08b9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/main/cpp/util/file_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,15 @@ 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) {
pdie(blaze_exit_code::INTERNAL_ERROR, "getcwd() failed");
}
return string(cwdbuf);
}
#endif // not __CYGWIN__

bool ChangeDirectory(const string& path) {
return chdir(path.c_str()) == 0;
Expand Down
31 changes: 22 additions & 9 deletions src/main/cpp/util/file_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2e08b9a

Please sign in to comment.