aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2017-01-11 18:00:54 +0000
committerGravatar Marcel Hlopko <hlopko@google.com>2017-01-12 09:10:00 +0000
commit56882b1d61f10d51f41279c5d60f2fffeca37e00 (patch)
tree00fb30178536b7ad165809fe4d2231b5f71cb281 /src/main
parentbb7963efe6966799d5910ecb721a606942107a02 (diff)
Bazel client, Windows: implement WriteFile
See https://github.com/bazelbuild/bazel/issues/2107 -- PiperOrigin-RevId: 144218095 MOS_MIGRATED_REVID=144218095
Diffstat (limited to 'src/main')
-rw-r--r--src/main/cpp/util/file_posix.cc6
-rw-r--r--src/main/cpp/util/file_windows.cc36
2 files changed, 30 insertions, 12 deletions
diff --git a/src/main/cpp/util/file_posix.cc b/src/main/cpp/util/file_posix.cc
index cc8cadbcf0..02c0fa8055 100644
--- a/src/main/cpp/util/file_posix.cc
+++ b/src/main/cpp/util/file_posix.cc
@@ -189,7 +189,6 @@ bool ReadFile(const string &filename, string *content, int max_size) {
close(fd);
return result;
}
-#endif // not __CYGWIN__
bool WriteFile(const void *data, size_t size, const string &filename) {
UnlinkPath(filename); // We don't care about the success of this.
@@ -209,11 +208,6 @@ bool WriteFile(const void *data, size_t size, const string &filename) {
return result;
}
-// TODO(bazel-team): implement all functions in file_windows.cc, use them from
-// MSYS, remove file_posix.cc from the `srcs` of
-// //src/main/cpp/util:file when building for MSYS, and remove all
-// #ifndef __CYGWIN__ directives.
-#ifndef __CYGWIN__
bool UnlinkPath(const string &file_path) {
return unlink(file_path.c_str()) == 0;
}
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index 63d6d66efb..d213b64179 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -406,14 +406,38 @@ bool ReadFile(const string& filename, string* content, int max_size) {
return result;
}
-#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;
+ if (IsDevNull(filename)) {
+ return true; // mimic write(2) behavior with /dev/null
+ }
+ wstring wpath;
+ if (!AsWindowsPathWithUncPrefix(filename, &wpath)) {
+ return false;
+ }
+
+ UnlinkPathW(wpath); // We don't care about the success of this.
+ HANDLE handle = CreateFileW(
+ /* lpFileName */ wpath.c_str(),
+ /* dwDesiredAccess */ GENERIC_WRITE,
+ /* dwShareMode */ FILE_SHARE_READ,
+ /* lpSecurityAttributes */ NULL,
+ /* dwCreationDisposition */ CREATE_ALWAYS,
+ /* dwFlagsAndAttributes */ FILE_ATTRIBUTE_NORMAL,
+ /* hTemplateFile */ NULL);
+ if (handle == INVALID_HANDLE_VALUE) {
+ return false;
+ }
+
+ bool result = WriteTo(
+ [handle](const void* buf, size_t bufsize) {
+ DWORD actually_written = 0;
+ ::WriteFile(handle, buf, bufsize, &actually_written, NULL);
+ return actually_written;
+ },
+ data, size);
+ CloseHandle(handle);
+ return result;
}
-#else // not COMPILER_MSVC
-#endif // COMPILER_MSVC
static bool UnlinkPathW(const wstring& path) {
DWORD attrs = ::GetFileAttributesW(path.c_str());