Bazel client: platform-specific {Read,Write}File

Move blaze::ReadFile and blaze::WriteFile to
file.h and file_platform.h (thus into the
blaze_util namespace), and update references.

This allows us to implement these methods in a
platform-specific way.

Also move UnlinkPath.

See https://github.com/bazelbuild/bazel/issues/2107

--
MOS_MIGRATED_REVID=140328273
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 96d6b4e..ea38384 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -576,7 +576,7 @@
   string version_spec_file = blaze_util::JoinPath(
       GetEmbeddedBinariesRoot(globals->options->install_base), "java.version");
   string version_spec = "";
-  if (ReadFile(version_spec_file, &version_spec)) {
+  if (blaze_util::ReadFile(version_spec_file, &version_spec)) {
     blaze_util::StripWhitespace(&version_spec);
     // A version specification is given, get version of java.
     string jvm_version = GetJvmVersion(exe);
@@ -608,7 +608,7 @@
   // for the existing server. If might be that the server dies and the cmdline
   // file stays there, but that is not a problem, since we always check the
   // server, too.
-  WriteFile(argument_string, server_dir + "/cmdline");
+  blaze_util::WriteFile(argument_string, server_dir + "/cmdline");
 
   // unless we restarted for a new-version, mark this as initial start
   if (globals->restart_reason == NO_RESTART) {
@@ -621,8 +621,8 @@
   // we can still print errors to the terminal.
   GoToWorkspace();
 
-  ExecuteDaemon(exe, jvm_args_vector, globals->jvm_log_file.c_str(),
-                server_dir, server_startup);
+  ExecuteDaemon(exe, jvm_args_vector, globals->jvm_log_file, server_dir,
+                server_startup);
 }
 
 // Replace this process with blaze in standalone/batch mode.
@@ -701,7 +701,7 @@
   string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile);
   string bufstr;
   int result;
-  if (!blaze::ReadFile(pid_file, &bufstr, 32) ||
+  if (!blaze_util::ReadFile(pid_file, &bufstr, 32) ||
       !blaze_util::safe_strto32(bufstr, &result)) {
     return -1;
   }
@@ -805,7 +805,7 @@
            "couldn't create '%s'", path.c_str());
     }
 
-    if (!blaze::WriteFile(data, size, path)) {
+    if (!blaze_util::WriteFile(data, size, path)) {
       die(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
           "\nFailed to write zipped file \"%s\": %s", path.c_str(),
           strerror(errno));
@@ -914,8 +914,9 @@
     // Now rename the completed installation to its final name. If this
     // fails due to an ENOTEMPTY then we assume another good
     // installation snuck in before us.
-    if (rename(tmp_install.c_str(), globals->options->install_base.c_str()) == -1
-        && errno != ENOTEMPTY) {
+    if (rename(tmp_install.c_str(), globals->options->install_base.c_str()) ==
+            -1 &&
+        errno != ENOTEMPTY) {
       pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
            "install base directory '%s' could not be renamed into place",
            tmp_install.c_str());
@@ -1017,7 +1018,7 @@
   // worse, its behavior differs slightly between kernels (in some, when longer
   // command lines are truncated, the last 4 bytes are replaced with
   // "..." + NUL.
-  ReadFile(cmdline_path, &joined_arguments);
+  blaze_util::ReadFile(cmdline_path, &joined_arguments);
   vector<string> arguments = blaze_util::Split(joined_arguments, '\0');
 
   // These strings contain null-separated command line arguments. If they are
@@ -1045,7 +1046,7 @@
   // installation is running.
   string installation_path = globals->options->output_base + "/install";
   string prev_installation;
-  bool ok = ReadDirectorySymlink(installation_path.c_str(), &prev_installation);
+  bool ok = ReadDirectorySymlink(installation_path, &prev_installation);
   if (!ok || !CompareAbsolutePaths(
           prev_installation, globals->options->install_base)) {
     if (server->Connected()) {
@@ -1053,9 +1054,9 @@
     }
 
     globals->restart_reason = NEW_VERSION;
-    UnlinkPath(installation_path.c_str());
-    if (!SymlinkDirectories(globals->options->install_base.c_str(),
-                            installation_path.c_str())) {
+    blaze_util::UnlinkPath(installation_path);
+    if (!SymlinkDirectories(globals->options->install_base,
+                            installation_path)) {
       pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
            "failed to create installation symlink '%s'",
            installation_path.c_str());
@@ -1364,7 +1365,7 @@
   std::string ipv6_prefix_1 = "[0:0:0:0:0:0:0:1]:";
   std::string ipv6_prefix_2 = "[::1]:";
 
-  if (!ReadFile(server_dir + "/command_port", &port)) {
+  if (!blaze_util::ReadFile(server_dir + "/command_port", &port)) {
     return false;
   }
 
@@ -1375,11 +1376,12 @@
     return false;
   }
 
-  if (!ReadFile(server_dir + "/request_cookie", &request_cookie_)) {
+  if (!blaze_util::ReadFile(server_dir + "/request_cookie", &request_cookie_)) {
     return false;
   }
 
-  if (!ReadFile(server_dir + "/response_cookie", &response_cookie_)) {
+  if (!blaze_util::ReadFile(server_dir + "/response_cookie",
+                            &response_cookie_)) {
     return false;
   }
 
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc
index 4f568f5..d316a3c 100644
--- a/src/main/cpp/blaze_util.cc
+++ b/src/main/cpp/blaze_util.cc
@@ -77,75 +77,6 @@
   return cwd + separator + path;
 }
 
-bool ReadFrom(const std::function<int(void *, int)> &read_func, string *content,
-              int max_size) {
-  content->clear();
-  char buf[4096];
-  // OPT:  This loop generates one spurious read on regular files.
-  while (int r = read_func(
-             buf, max_size > 0
-                      ? std::min(max_size, static_cast<int>(sizeof buf))
-                      : sizeof buf)) {
-    if (r == -1) {
-      if (errno == EINTR || errno == EAGAIN) continue;
-      return false;
-    }
-    content->append(buf, r);
-    if (max_size > 0) {
-      if (max_size > r) {
-        max_size -= r;
-      } else {
-        break;
-      }
-    }
-  }
-  return true;
-}
-
-bool ReadFile(const string &filename, string *content, int max_size) {
-  int fd = open(filename.c_str(), O_RDONLY);
-  if (fd == -1) return false;
-  bool result =
-      ReadFrom([fd](void *buf, int len) { return read(fd, buf, len); }, content,
-               max_size);
-  close(fd);
-  return result;
-}
-
-bool WriteFile(const void* data, size_t size, const string &filename) {
-  UnlinkPath(filename);  // We don't care about the success of this.
-  int fd = open(filename.c_str(), O_CREAT|O_WRONLY|O_TRUNC, 0755);  // chmod +x
-  if (fd == -1) {
-    return false;
-  }
-  bool result = WriteTo(
-      [fd](const void *buf, size_t bufsize) { return write(fd, buf, bufsize); },
-      data, size);
-  int saved_errno = errno;
-  if (close(fd)) {
-    return false;  // Can fail on NFS.
-  }
-  errno = saved_errno;  // Caller should see errno from write().
-  return result;
-}
-
-bool WriteTo(const std::function<int(const void *, size_t)> &write_func,
-             const void *data, size_t size) {
-  int r = write_func(data, size);
-  if (r == -1) {
-    return false;
-  }
-  return static_cast<uint>(r) == size;
-}
-
-bool WriteFile(const std::string &content, const std::string &filename) {
-  return WriteFile(content.c_str(), content.size(), filename);
-}
-
-bool UnlinkPath(const string &file_path) {
-  return unlink(file_path.c_str()) == 0;
-}
-
 bool IsEmacsTerminal() {
   string emacs = GetEnv("EMACS");
   string inside_emacs = GetEnv("INSIDE_EMACS");
diff --git a/src/main/cpp/blaze_util.h b/src/main/cpp/blaze_util.h
index cdef849..85bf866 100644
--- a/src/main/cpp/blaze_util.h
+++ b/src/main/cpp/blaze_util.h
@@ -46,37 +46,6 @@
 //   MakeAbsolute("C:/foo") ---> "C:/foo"
 std::string MakeAbsolute(const std::string &path);
 
-// Replaces 'content' with contents of file 'filename'.
-// If `max_size` is positive, the method reads at most that many bytes;
-// otherwise the method reads the whole file.
-// Returns false on error. Can be called from a signal handler.
-bool ReadFile(const std::string &filename, std::string *content,
-              int max_size = 0);
-
-// Replaces 'content' with data read from a source using `read_func`.
-// If `max_size` is positive, the method reads at most that many bytes;
-// otherwise the method reads everything.
-// Returns false on error. Can be called from a signal handler.
-bool ReadFrom(const std::function<int(void *, int)> &read_func,
-              std::string *content, int max_size = 0);
-
-// Writes 'content' into file 'filename', and makes it executable.
-// Returns false on failure, sets errno.
-bool WriteFile(const std::string &content, const std::string &filename);
-
-// Writes `size` bytes from `data` into file 'filename' and makes it executable.
-// Returns false on failure, sets errno.
-bool WriteFile(const void* data, size_t size, const std::string &filename);
-
-// Writes `size` bytes from `data` into a destination using `write_func`.
-// Returns false on failure, sets errno.
-bool WriteTo(const std::function<int(const void *, size_t)> &write_func,
-             const void *data, size_t size);
-
-// Unlinks the file given by 'file_path'.
-// Returns true on success. In case of failure sets errno.
-bool UnlinkPath(const std::string &file_path);
-
 // Returns true iff the current terminal is running inside an Emacs.
 bool IsEmacsTerminal();
 
diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc
index 0057e3d..4d2ebfe 100644
--- a/src/main/cpp/blaze_util_linux.cc
+++ b/src/main/cpp/blaze_util_linux.cc
@@ -173,7 +173,7 @@
   string statfile = "/proc/" + pid + "/stat";
   string statline;
 
-  if (!ReadFile(statfile, &statline)) {
+  if (!blaze_util::ReadFile(statfile, &statline)) {
     return false;
   }
 
@@ -199,7 +199,7 @@
   }
 
   string start_time_file = blaze_util::JoinPath(server_dir, "server.starttime");
-  if (!WriteFile(start_time, start_time_file)) {
+  if (!blaze_util::WriteFile(start_time, start_time_file)) {
     pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
          "Cannot write start time in server dir %s", server_dir.c_str());
   }
@@ -218,7 +218,7 @@
   }
 
   string recorded_start_time;
-  bool file_present = ReadFile(
+  bool file_present = blaze_util::ReadFile(
       blaze_util::JoinPath(output_base, "server/server.starttime"),
       &recorded_start_time);
 
diff --git a/src/main/cpp/blaze_util_posix.cc b/src/main/cpp/blaze_util_posix.cc
index fabeabd..1564086 100644
--- a/src/main/cpp/blaze_util_posix.cc
+++ b/src/main/cpp/blaze_util_posix.cc
@@ -244,7 +244,7 @@
   string pid_string = GetProcessIdAsString();
   string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile);
 
-  if (!WriteFile(pid_string, pid_file)) {
+  if (!blaze_util::WriteFile(pid_string, pid_file)) {
     // The exit code does not matter because we are already in the daemonized
     // server. The output of this operation will end up in jvm.out .
     pdie(0, "Cannot write PID file");
@@ -270,7 +270,7 @@
   } else if (child > 0) {  // we're the parent
     close(send_socket);    // parent keeps only the reading side
     string result;
-    bool success = ReadFrom(
+    bool success = blaze_util::ReadFrom(
         [recv_socket](void* buf, int size) {
           return read(recv_socket, buf, size);
         },
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc
index 05e93a9..a8d7351 100644
--- a/src/main/cpp/blaze_util_windows.cc
+++ b/src/main/cpp/blaze_util_windows.cc
@@ -37,6 +37,7 @@
 #include "src/main/cpp/util/errors.h"
 #include "src/main/cpp/util/exit_code.h"
 #include "src/main/cpp/util/file.h"
+#include "src/main/cpp/util/file_platform.h"
 #include "src/main/cpp/util/md5.h"
 #include "src/main/cpp/util/strings.h"
 
@@ -584,7 +585,7 @@
 
   string pid_string = ToString(processInfo.dwProcessId);
   string pid_file = blaze_util::JoinPath(server_dir, kServerPidFile);
-  if (!WriteFile(pid_string, pid_file)) {
+  if (!blaze_util::WriteFile(pid_string, pid_file)) {
     // Not a lot we can do if this fails
     fprintf(stderr, "Cannot write PID file %s\n", pid_file.c_str());
   }
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index e3224a2..680e2e3 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -74,7 +74,7 @@
   string filename(filename_ref);  // file
   BAZEL_LOG(INFO) << "Parsing the RcFile " << filename;
   string contents;
-  if (!ReadFile(filename, &contents)) {
+  if (!blaze_util::ReadFile(filename, &contents)) {
     // We checked for file readability before, so this is unexpected.
     blaze_util::StringPrintf(error,
         "Unexpected error reading .blazerc file '%s'", filename.c_str());
diff --git a/src/main/cpp/startup_options.cc b/src/main/cpp/startup_options.cc
index 06f5126..75fa396 100644
--- a/src/main/cpp/startup_options.cc
+++ b/src/main/cpp/startup_options.cc
@@ -383,17 +383,17 @@
     const vector<string> &user_options, string *error) const {
   // Configure logging
   const string propFile = output_base + "/javalog.properties";
-  if (!WriteFile(
-      "handlers=java.util.logging.FileHandler\n"
-      ".level=INFO\n"
-      "java.util.logging.FileHandler.level=INFO\n"
-      "java.util.logging.FileHandler.pattern="
-      + output_base + "/java.log\n"
-      "java.util.logging.FileHandler.limit=50000\n"
-      "java.util.logging.FileHandler.count=1\n"
-      "java.util.logging.FileHandler.formatter="
-      "java.util.logging.SimpleFormatter\n",
-      propFile)) {
+  if (!blaze_util::WriteFile("handlers=java.util.logging.FileHandler\n"
+                             ".level=INFO\n"
+                             "java.util.logging.FileHandler.level=INFO\n"
+                             "java.util.logging.FileHandler.pattern=" +
+                                 output_base +
+                                 "/java.log\n"
+                                 "java.util.logging.FileHandler.limit=50000\n"
+                                 "java.util.logging.FileHandler.count=1\n"
+                                 "java.util.logging.FileHandler.formatter="
+                                 "java.util.logging.SimpleFormatter\n",
+                             propFile)) {
     perror(("Couldn't write logging file " + propFile).c_str());
   } else {
     result->push_back("-Djava.util.logging.config.file=" + propFile);
diff --git a/src/main/cpp/util/file.cc b/src/main/cpp/util/file.cc
index 2aa6642..41242e7 100644
--- a/src/main/cpp/util/file.cc
+++ b/src/main/cpp/util/file.cc
@@ -13,8 +13,8 @@
 // limitations under the License.
 #include "src/main/cpp/util/file.h"
 
+#include <errno.h>
 #include <limits.h>  // PATH_MAX
-#include <sys/stat.h>
 #include <cstdlib>
 #include <vector>
 
@@ -29,6 +29,44 @@
 using std::string;
 using std::vector;
 
+bool ReadFrom(const std::function<int(void *, int)> &read_func, string *content,
+              int max_size) {
+  content->clear();
+  char buf[4096];
+  // OPT:  This loop generates one spurious read on regular files.
+  while (int r = read_func(
+             buf, max_size > 0
+                      ? std::min(max_size, static_cast<int>(sizeof buf))
+                      : sizeof buf)) {
+    if (r == -1) {
+      if (errno == EINTR || errno == EAGAIN) continue;
+      return false;
+    }
+    content->append(buf, r);
+    if (max_size > 0) {
+      if (max_size > r) {
+        max_size -= r;
+      } else {
+        break;
+      }
+    }
+  }
+  return true;
+}
+
+bool WriteTo(const std::function<int(const void *, size_t)> &write_func,
+             const void *data, size_t size) {
+  int r = write_func(data, size);
+  if (r == -1) {
+    return false;
+  }
+  return r == static_cast<int>(size);
+}
+
+bool WriteFile(const std::string &content, const std::string &filename) {
+  return WriteFile(content.c_str(), content.size(), filename);
+}
+
 pair<string, string> SplitPath(const string &path) {
   size_t pos = path.rfind('/');
 
diff --git a/src/main/cpp/util/file.h b/src/main/cpp/util/file.h
index 13a2b48..7b4d3af 100644
--- a/src/main/cpp/util/file.h
+++ b/src/main/cpp/util/file.h
@@ -14,6 +14,7 @@
 #ifndef BAZEL_SRC_MAIN_CPP_UTIL_FILE_H_
 #define BAZEL_SRC_MAIN_CPP_UTIL_FILE_H_
 
+#include <functional>
 #include <string>
 #include <vector>
 
@@ -33,6 +34,22 @@
   virtual int Receive(void *buffer, int size) = 0;
 };
 
+// Replaces 'content' with data read from a source using `read_func`.
+// If `max_size` is positive, the method reads at most that many bytes;
+// otherwise the method reads everything.
+// Returns false on error. Can be called from a signal handler.
+bool ReadFrom(const std::function<int(void *, int)> &read_func,
+              std::string *content, int max_size = 0);
+
+// Writes `size` bytes from `data` into file 'filename' and makes it executable.
+// Returns false on failure, sets errno.
+bool WriteFile(const void *data, size_t size, const std::string &filename);
+
+// Writes `size` bytes from `data` into a destination using `write_func`.
+// Returns false on failure, sets errno.
+bool WriteTo(const std::function<int(const void *, size_t)> &write_func,
+             const void *data, size_t size);
+
 // Returns the part of the path before the final "/".  If there is a single
 // leading "/" in the path, the result will be the leading "/".  If there is
 // no "/" in the path, the result is the empty prefix of the input (i.e., "").
diff --git a/src/main/cpp/util/file_platform.h b/src/main/cpp/util/file_platform.h
index 07d5b7e..58755f27 100644
--- a/src/main/cpp/util/file_platform.h
+++ b/src/main/cpp/util/file_platform.h
@@ -25,6 +25,21 @@
 
 IPipe* CreatePipe();
 
+// Replaces 'content' with contents of file 'filename'.
+// If `max_size` is positive, the method reads at most that many bytes;
+// otherwise the method reads the whole file.
+// Returns false on error. Can be called from a signal handler.
+bool ReadFile(const std::string &filename, std::string *content,
+              int max_size = 0);
+
+// Writes 'content' into file 'filename', and makes it executable.
+// Returns false on failure, sets errno.
+bool WriteFile(const std::string &content, const std::string &filename);
+
+// Unlinks the file given by 'file_path'.
+// Returns true on success. In case of failure sets errno.
+bool UnlinkPath(const std::string &file_path);
+
 // Checks each element of the PATH variable for executable. If none is found, ""
 // is returned.  Otherwise, the full path to executable is returned. Can die if
 // looking up PATH fails.
diff --git a/src/main/cpp/util/file_posix.cc b/src/main/cpp/util/file_posix.cc
index c25b928..803dec78 100644
--- a/src/main/cpp/util/file_posix.cc
+++ b/src/main/cpp/util/file_posix.cc
@@ -13,6 +13,7 @@
 // limitations under the License.
 #include "src/main/cpp/util/file_platform.h"
 
+#include <errno.h>
 #include <dirent.h>  // DIR, dirent, opendir, closedir
 #include <fcntl.h>   // O_RDONLY
 #include <limits.h>  // PATH_MAX
@@ -77,6 +78,38 @@
   return new PosixPipe(fd[0], fd[1]);
 }
 
+bool ReadFile(const string &filename, string *content, int max_size) {
+  int fd = open(filename.c_str(), O_RDONLY);
+  if (fd == -1) return false;
+  bool result =
+      ReadFrom([fd](void *buf, int len) { return read(fd, buf, len); }, content,
+               max_size);
+  close(fd);
+  return result;
+}
+
+bool WriteFile(const void *data, size_t size, const string &filename) {
+  UnlinkPath(filename);  // We don't care about the success of this.
+  int fd =
+      open(filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0755);  // chmod +x
+  if (fd == -1) {
+    return false;
+  }
+  bool result = WriteTo(
+      [fd](const void *buf, size_t bufsize) { return write(fd, buf, bufsize); },
+      data, size);
+  int saved_errno = errno;
+  if (close(fd)) {
+    return false;  // Can fail on NFS.
+  }
+  errno = saved_errno;  // Caller should see errno from write().
+  return result;
+}
+
+bool UnlinkPath(const string &file_path) {
+  return unlink(file_path.c_str()) == 0;
+}
+
 string Which(const string &executable) {
   char *path_cstr = getenv("PATH");
   if (path_cstr == NULL || path_cstr[0] == '\0') {
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index 72b9f47..6cab2b4 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -21,6 +21,24 @@
 
 using std::string;
 
+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;
+}
+
+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;
+}
+
+bool UnlinkPath(const string& file_path) {
+  // TODO(bazel-team): implement this.
+  pdie(255, "blaze_util::UnlinkPath is not implemented on Windows");
+  return false;
+}
+
 string Which(const string &executable) {
   pdie(255, "blaze_util::Which is not implemented on Windows");
   return "";
diff --git a/src/main/native/windows_processes.cc b/src/main/native/windows_processes.cc
index 2e8d055..17de103 100644
--- a/src/main/native/windows_processes.cc
+++ b/src/main/native/windows_processes.cc
@@ -323,8 +323,8 @@
   jbyte* bytes = env->GetByteArrayElements(java_bytes, NULL);
   DWORD bytes_written;
 
-  if (!WriteFile(process->stdin_, bytes + offset, length, &bytes_written,
-      NULL)) {
+  if (!::WriteFile(process->stdin_, bytes + offset, length, &bytes_written,
+                   NULL)) {
     process->error_ = GetLastErrorString("WriteFile()");
     bytes_written = -1;
   }
@@ -368,7 +368,7 @@
 
   jbyte* bytes = env->GetByteArrayElements(java_bytes, NULL);
   DWORD bytes_read;
-  if (!ReadFile(stream->handle_, bytes + offset, length, &bytes_read, NULL)) {
+  if (!::ReadFile(stream->handle_, bytes + offset, length, &bytes_read, NULL)) {
     // Check if either the other end closed the pipe or we did it with
     // NativeOutputStream.close() . In the latter case, we'll get a "system
     // call interrupted" error.
diff --git a/src/test/cpp/blaze_util_test.cc b/src/test/cpp/blaze_util_test.cc
index 9199ec3..0849d74 100644
--- a/src/test/cpp/blaze_util_test.cc
+++ b/src/test/cpp/blaze_util_test.cc
@@ -23,6 +23,7 @@
 #include "src/main/cpp/blaze_util.h"
 #include "src/main/cpp/blaze_util_platform.h"
 #include "src/main/cpp/util/file.h"
+#include "src/main/cpp/util/file_platform.h"
 #include "gtest/gtest.h"
 
 namespace blaze {
@@ -97,7 +98,7 @@
       FAIL() << "Unable to create a pipe!";
     } else {
       string result;
-      bool success = ReadFrom(
+      bool success = blaze_util::ReadFrom(
           [fd](void* buf, int size) { return read(fd, buf, size); }, &result);
       close(fd);
       if (!success) {
diff --git a/src/tools/singlejar/output_jar_simple_test.cc b/src/tools/singlejar/output_jar_simple_test.cc
index 5bd0eca..af88f4b 100644
--- a/src/tools/singlejar/output_jar_simple_test.cc
+++ b/src/tools/singlejar/output_jar_simple_test.cc
@@ -16,6 +16,7 @@
 
 #include "src/main/cpp/blaze_util.h"
 #include "src/main/cpp/util/file.h"
+#include "src/main/cpp/util/file_platform.h"
 #include "src/main/cpp/util/port.h"
 #include "src/main/cpp/util/strings.h"
 #include "src/tools/singlejar/input_jar.h"
@@ -324,7 +325,7 @@
 // --classpath_resources
 TEST_F(OutputJarSimpleTest, ClasspathResources) {
   string res1_path = OutputFilePath("cp_res");
-  ASSERT_TRUE(blaze::WriteFile("line1\nline2\n", res1_path));
+  ASSERT_TRUE(blaze_util::WriteFile("line1\nline2\n", res1_path));
   string out_path = OutputFilePath("out.jar");
   CreateOutput(out_path, {"--classpath_resources", res1_path.c_str()});
   string res = GetEntryContents(out_path, "cp_res");
diff --git a/src/tools/singlejar/test_util.cc b/src/tools/singlejar/test_util.cc
index d026388..b2de761 100644
--- a/src/tools/singlejar/test_util.cc
+++ b/src/tools/singlejar/test_util.cc
@@ -23,6 +23,7 @@
 #include "src/main/cpp/blaze_util.h"
 #include "src/main/cpp/blaze_util_platform.h"
 #include "src/main/cpp/util/file.h"
+#include "src/main/cpp/util/file_platform.h"
 #include "src/main/cpp/util/strings.h"
 
 #include "gtest/gtest.h"
@@ -107,7 +108,7 @@
 string CreateTextFile(const string& relpath, const char *contents) {
   string out_path = OutputFilePath(relpath);
   blaze::MakeDirectories(blaze_util::Dirname(out_path), 0777);
-  if (blaze::WriteFile(contents, out_path)) {
+  if (blaze_util::WriteFile(contents, out_path)) {
     return out_path;
   }
   ADD_FAILURE() << "Cannot write " << out_path;