Skip to content

Commit

Permalink
Bazel client: platform-specific JoinPath
Browse files Browse the repository at this point in the history
This allows joining paths on "\" instead of "/"
when building for Windows.

See #2107

--
Change-Id: I417ba40af6530650be2394db4bd445f0883d40e5
Reviewed-on: https://cr.bazel.build/8495
PiperOrigin-RevId: 145961689
MOS_MIGRATED_REVID=145961689
  • Loading branch information
laszlocsomor authored and meteorcloudy committed Jan 30, 2017
1 parent 98cafe8 commit 855fbe9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
25 changes: 0 additions & 25 deletions src/main/cpp/util/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,6 @@ string Basename(const string &path) {
return SplitPath(path).second;
}

string JoinPath(const string &path1, const string &path2) {
if (path1.empty()) {
// "" + "/bar"
return path2;
}

if (path1[path1.size() - 1] == '/') {
if (path2.find('/') == 0) {
// foo/ + /bar
return path1 + path2.substr(1);
} else {
// foo/ + bar
return path1 + path2;
}
} else {
if (path2.find('/') == 0) {
// foo + /bar
return path1 + path2;
} else {
// foo + bar
return path1 + "/" + path2;
}
}
}

class DirectoryTreeWalker : public DirectoryEntryConsumer {
public:
DirectoryTreeWalker(vector<string> *files,
Expand Down
3 changes: 1 addition & 2 deletions src/main/cpp/util/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <functional>
#include <string>
#include <vector>
#include "src/main/cpp/util/file_platform.h"

namespace blaze_util {

Expand Down Expand Up @@ -59,8 +60,6 @@ std::string Dirname(const std::string &path);
// "/" in the path, the result is the same as the input.
std::string Basename(const std::string &path);

std::string JoinPath(const std::string &path1, const std::string &path2);

// Lists all files in `path` and all of its subdirectories.
//
// Does not follow symlinks / junctions.
Expand Down
2 changes: 2 additions & 0 deletions src/main/cpp/util/file_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class IFileMtime {
// Creates a platform-specific implementation of `IFileMtime`.
IFileMtime *CreateFileMtime();

std::string JoinPath(const std::string &path1, const std::string &path2);

// Split a path to dirname and basename parts.
std::pair<std::string, std::string> SplitPath(const std::string &path);

Expand Down
29 changes: 29 additions & 0 deletions src/main/cpp/util/file_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,35 @@ IPipe* CreatePipe() {
return new PosixPipe(fd[0], fd[1]);
}

string JoinPath(const string &path1, const string &path2) {
if (path1.empty()) {
// "" + "/bar"
return path2;
}
if (path2.empty()) {
// "foo/" + ""
return path1;
}

if (path1.back() == '/') {
if (path2.front() == '/') {
// foo/ + /bar
return path1 + path2.substr(1);
} else {
// foo/ + bar
return path1 + path2;
}
} else {
if (path2.front() == '/') {
// foo + /bar
return path1 + path2;
} else {
// foo + bar
return path1 + "/" + path2;
}
}
}

pair<string, string> SplitPath(const string &path) {
size_t pos = path.rfind('/');

Expand Down
38 changes: 38 additions & 0 deletions src/main/cpp/util/file_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,44 @@ static bool IsRootOrAbsolute(const basic_string<char_type>& path,
HasDriveSpecifierPrefix(path.c_str() + 4) && IsPathSeparator(path[6]));
}

string JoinPath(const string& path1, const string& path2) {
if (path1.empty()) {
// "" + "/bar"
return path2;
}
if (path2.empty()) {
// "foo/" + ""
return path1;
}

string p1 = path1;
if (path1.back() == '/') {
p1.pop_back();
}
string p2 = path2;
if (path2.front() == '/') {
p2[0] = '\\';
}

if (IsPathSeparator(p1.back())) {
if (!p2.empty() && IsPathSeparator(p2.front())) {
// foo/ + /bar
return p1 + p2.substr(1);
} else {
// foo/ + bar
return p1 + p2;
}
} else {
if (!p2.empty() && IsPathSeparator(p2.front())) {
// foo + /bar
return p1 + p2;
} else {
// foo + bar
return p1 + "\\" + p2;
}
}
}

pair<string, string> SplitPath(const string& path) {
if (path.empty()) {
return std::make_pair("", "");
Expand Down

0 comments on commit 855fbe9

Please sign in to comment.