aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2016-12-15 12:33:26 +0000
committerGravatar John Cater <jcater@google.com>2016-12-15 20:38:03 +0000
commit77b880f8b38e1a0571c4804b52801f654efd30ac (patch)
tree25da88f9a6504afd26a6d7c2804105fd4344b1f5 /src
parent41ee5911f41d9654027f57b15b4a691ee520854b (diff)
Bazel client, Windows: use file_windows under MSYS
Use file_windows.cc and file_posix.cc when compiling for MSYS/GCC (--cpu=x64_windows). This way we can implement functions in file_windows.cc one by one, start using them with MSYS, and put #ifdef __CYGWIN__ ... #endif around these functions in file_posix.cc, until all functions are implemented in file_windows.cc. The change contains a lot of code deletion, but nothing should change because of that. The deleted code was not being used at all: neither with MSVC (it was behind #else // not COMPILER_MSVC) nor with MSYS (the file was not in the `select` of the rule's `srcs`). See https://github.com/bazelbuild/bazel/issues/2107 -- PiperOrigin-RevId: 142128611 MOS_MIGRATED_REVID=142128611
Diffstat (limited to 'src')
-rw-r--r--src/main/cpp/util/BUILD7
-rw-r--r--src/main/cpp/util/file_windows.cc144
2 files changed, 56 insertions, 95 deletions
diff --git a/src/main/cpp/util/BUILD b/src/main/cpp/util/BUILD
index bf62eff841..8462ff3fc3 100644
--- a/src/main/cpp/util/BUILD
+++ b/src/main/cpp/util/BUILD
@@ -26,6 +26,13 @@ cc_library(
cc_library(
name = "file",
srcs = ["file.cc"] + select({
+ "//src:windows": [
+ # TODO(bazel-team): implement functions in file_windows.cc and use
+ # more and more of them under MSYS until we can completely stop
+ # using file_posix.cc at which point remove it from this list.
+ "file_posix.cc",
+ "file_windows.cc",
+ ],
"//src:windows_msvc": [
"file_windows.cc",
],
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index 9813719cf2..dd18814070 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -22,6 +22,7 @@ namespace blaze_util {
using std::string;
+#ifdef COMPILER_MSVC
class WindowsPipe : public IPipe {
public:
bool Send(void* buffer, int size) override {
@@ -36,75 +37,113 @@ class WindowsPipe : public IPipe {
return 0;
}
};
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
IPipe* CreatePipe() {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::CreatePipe is not implemented on Windows");
return nullptr;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
bool ReadFile(const string& filename, string* content, int max_size) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::ReadFile is not implemented on Windows");
return false;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
bool WriteFile(const void* data, size_t size, const string& filename) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::WriteFile is not implemented on Windows");
return false;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
bool UnlinkPath(const string& file_path) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::UnlinkPath is not implemented on Windows");
return false;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
string Which(const string &executable) {
pdie(255, "blaze_util::Which is not implemented on Windows");
return "";
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
bool PathExists(const string& path) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::PathExists is not implemented on Windows");
return false;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
string MakeCanonical(const char *path) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::MakeCanonical is not implemented on Windows");
return "";
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
bool CanAccess(const string& path, bool read, bool write, bool exec) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::CanAccess is not implemented on Windows");
return false;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
bool IsDirectory(const string& path) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::IsDirectory is not implemented on Windows");
return false;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
void SyncFile(const string& path) {
// No-op on Windows native; unsupported by Cygwin.
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
time_t GetMtimeMillisec(const string& path) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::GetMtimeMillisec is not implemented on Windows");
return -1;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
bool SetMtimeMillisec(const string& path, time_t mtime) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::SetMtimeMillisec is not implemented on Windows");
return false;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
#ifdef COMPILER_MSVC
bool MakeDirectories(const string& path, unsigned int mode) {
@@ -112,119 +151,34 @@ bool MakeDirectories(const string& path, unsigned int mode) {
pdie(255, "blaze::MakeDirectories is not implemented on Windows");
return false;
}
-#else // not COMPILER_MSVC
-// Runs "stat" on `path`. Returns -1 and sets errno if stat fails or
-// `path` isn't a directory. If check_perms is true, this will also
-// make sure that `path` is owned by the current user and has `mode`
-// permissions (observing the umask). It attempts to run chmod to
-// correct the mode if necessary. If `path` is a symlink, this will
-// check ownership of the link, not the underlying directory.
-static bool GetDirectoryStat(const string& path, mode_t mode,
- bool check_perms) {
- struct stat filestat = {};
- if (stat(path.c_str(), &filestat) == -1) {
- return false;
- }
-
- if (!S_ISDIR(filestat.st_mode)) {
- errno = ENOTDIR;
- return false;
- }
-
- if (check_perms) {
- // If this is a symlink, run checks on the link. (If we did lstat above
- // then it would return false for ISDIR).
- struct stat linkstat = {};
- if (lstat(path.c_str(), &linkstat) != 0) {
- return false;
- }
- if (linkstat.st_uid != geteuid()) {
- // The directory isn't owned by me.
- errno = EACCES;
- return false;
- }
-
- mode_t mask = umask(022);
- umask(mask);
- mode = (mode & ~mask);
- if ((filestat.st_mode & 0777) != mode && chmod(path.c_str(), mode) == -1) {
- // errno set by chmod.
- return false;
- }
- }
- return true;
-}
-
-static bool MakeDirectories(const string& path, mode_t mode, bool childmost) {
- if (path.empty() || path == "/") {
- errno = EACCES;
- return false;
- }
-
- bool stat_succeeded = GetDirectoryStat(path, mode, childmost);
- if (stat_succeeded) {
- return true;
- }
-
- if (errno == ENOENT) {
- // Path does not exist, attempt to create its parents, then it.
- string parent = Dirname(path);
- if (!MakeDirectories(parent, mode, false)) {
- // errno set by stat.
- return false;
- }
-
- if (mkdir(path.c_str(), mode) == -1) {
- if (errno == EEXIST) {
- if (childmost) {
- // If there are multiple bazel calls at the same time then the
- // directory could be created between the MakeDirectories and mkdir
- // calls. This is okay, but we still have to check the permissions.
- return GetDirectoryStat(path, mode, childmost);
- } else {
- // If this isn't the childmost directory, we don't care what the
- // permissions were. If it's not even a directory then that error will
- // get caught when we attempt to create the next directory down the
- // chain.
- return true;
- }
- }
- // errno set by mkdir.
- return false;
- }
- return true;
- }
-
- return stat_succeeded;
-}
-
-// mkdir -p path. Returns true if the path was created or already exists and
-// could
-// be chmod-ed to exactly the given permissions. If final part of the path is a
-// symlink, this ensures that the destination of the symlink has the desired
-// permissions. It also checks that the directory or symlink is owned by us.
-// On failure, this returns false and sets errno.
-bool MakeDirectories(const string& path, mode_t mode) {
- return MakeDirectories(path, mode, true);
-}
+#else // not COMPILER_MSVC
#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
string GetCwd() {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::GetCwd is not implemented on Windows");
return "";
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
bool ChangeDirectory(const string& path) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::ChangeDirectory is not implemented on Windows");
return false;
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
+#ifdef COMPILER_MSVC
void ForEachDirectoryEntry(const string &path,
DirectoryEntryConsumer *consume) {
// TODO(bazel-team): implement this.
pdie(255, "blaze_util::ForEachDirectoryEntry is not implemented on Windows");
}
+#else // not COMPILER_MSVC
+#endif // COMPILER_MSVC
} // namespace blaze_util