Rollback of commit 69a127b8f4e353ecb163688ed3271fb47e0f385d.

*** Reason for rollback ***

[] has detected that 500 or more targets failed to build at commit 69a127b8f4e353ecb163688ed3271fb47e0f385d, each of which successfully built at the prior CL that affected them.
[] double-checked that //javatests/com/google/apphosting/tests/usercode:Security_StubTest
built at 148995024. The verification run was: 
[]
but the same target failed to build at 148995025. The [] link for this run is available here: []
To see all targets that ran, along with their final status, visit: []
Questions? Comments? See the URL.
go/autorollback

*** Original change description ***

ijar: use bazel's file utilities

This change not only implements ijar for Windows
(with MSVC), but also fixes a bug in
mapped_file_windows (path conversion didn't make
the input path absolute, so we could not build
java code with the MSYS-less bazel).

Fixes https://github.com/bazelbuild/bazel/issues/2157

--
PiperOrigin-RevId: 148998092
MOS_MIGRATED_REVID=148998092
diff --git a/third_party/ijar/BUILD b/third_party/ijar/BUILD
index 7836ea1..5e97bc8 100644
--- a/third_party/ijar/BUILD
+++ b/third_party/ijar/BUILD
@@ -30,18 +30,7 @@
     deps = [
         ":platform_utils",
         ":zlib_client",
-    ] + select({
-        "//src:windows": [
-            "//src/main/cpp/util:errors",
-            "//src/main/cpp/util:file",
-        ],
-        "//src:windows_msvc": [
-            "//src/main/cpp/util:errors",
-            "//src/main/cpp/util:file",
-        ],
-        "//conditions:default": [
-        ],
-    }),
+    ],
 )
 
 cc_library(
@@ -62,10 +51,16 @@
         "platform_utils.h",
     ],
     visibility = ["//visibility:private"],
-    deps = [
-        "//src/main/cpp/util:errors",
-        "//src/main/cpp/util:file",
-    ],
+    deps = select({
+        "//src:windows": [
+            "//src/main/cpp/util:file",
+        ],
+        "//src:windows_msvc": [
+            "//src/main/cpp/util:file",
+        ],
+        "//conditions:default": [
+        ],
+    }),
 )
 
 cc_binary(
diff --git a/third_party/ijar/mapped_file_windows.cc b/third_party/ijar/mapped_file_windows.cc
index 5ab6ce0..e96f4d4 100644
--- a/third_party/ijar/mapped_file_windows.cc
+++ b/third_party/ijar/mapped_file_windows.cc
@@ -12,23 +12,35 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#include <malloc.h>  // malloc, free
 #include <stdio.h>
 #include <windows.h>
 
-#include <string>
+#ifdef COMPILER_MSVC
+#include <stdlib.h>  // exit
+#else  // not COMPILER_MSVC
+#include <sys/cygwin.h>  // cygwin_create_path, CCP_POSIX_TO_WIN_A
+#endif  // COMPILER_MSVC
 
-#include "src/main/cpp/util/errors.h"
-#include "src/main/cpp/util/file_platform.h"
 #include "third_party/ijar/mapped_file.h"
 
 #define MAX_ERROR 2048
 
 namespace devtools_ijar {
 
-using std::wstring;
-
 static char errmsg[MAX_ERROR] = "";
 
+class WindowsPath {
+ public:
+  WindowsPath(const char* path);
+  ~WindowsPath();
+  const char* GetWindowsPath() const { return _win_path; }
+
+ private:
+  char* _win_path;
+};
+
+
 void PrintLastError(const char* op) {
   char *message;
   DWORD err = GetLastError();
@@ -67,13 +79,11 @@
   opened_ = false;
   errmsg_ = errmsg;
 
-  wstring wname;
-  if (!blaze_util::AsWindowsPathWithUncPrefix(name, &wname)) {
-    blaze_util::die(
-        255, "MappedInputFile(%s): AsWindowsPathWithUncPrefix failed", name);
-  }
-  HANDLE file = CreateFileW(wname.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
-                            OPEN_EXISTING, 0, NULL);
+  WindowsPath path(name);
+  char* unicode_path = ToUnicodePath(path.GetWindowsPath());
+  HANDLE file = CreateFile(unicode_path, GENERIC_READ, FILE_SHARE_READ, NULL,
+                           OPEN_EXISTING, 0, NULL);
+  free(unicode_path);
   if (file == INVALID_HANDLE_VALUE) {
     PrintLastError("CreateFile()");
     return;
@@ -88,7 +98,7 @@
 
   HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY,
       size.HighPart, size.LowPart, NULL);
-  if (mapping == NULL || mapping == INVALID_HANDLE_VALUE) {
+  if (mapping == NULL) {
     PrintLastError("CreateFileMapping()");
     CloseHandle(file);
     return;
@@ -152,13 +162,11 @@
   opened_ = false;
   errmsg_ = errmsg;
 
-  wstring wname;
-  if (!blaze_util::AsWindowsPathWithUncPrefix(name, &wname)) {
-    blaze_util::die(
-        255, "MappedOutputFile(%s): AsWindowsPathWithUncPrefix failed", name);
-  }
-  HANDLE file = CreateFileW(wname.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
-                            NULL, CREATE_ALWAYS, 0, NULL);
+  WindowsPath path(name);
+  char* unicode_path = ToUnicodePath(path.GetWindowsPath());
+  HANDLE file = CreateFile(unicode_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
+                           CREATE_ALWAYS, 0, NULL);
+  free(unicode_path);
   if (file == INVALID_HANDLE_VALUE) {
     PrintLastError("CreateFile()");
     return;
@@ -166,7 +174,7 @@
 
   HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READWRITE,
       estimated_size >> 32, estimated_size & 0xffffffffUL, NULL);
-  if (mapping == NULL || mapping == INVALID_HANDLE_VALUE) {
+  if (mapping == NULL) {
     PrintLastError("CreateFileMapping()");
     CloseHandle(file);
     return;
@@ -218,4 +226,37 @@
   return 0;
 }
 
+#ifdef COMPILER_MSVC
+
+  WindowsPath::WindowsPath(const char* path)
+      : _win_path(const_cast<char*>(path)) {
+    // Input path should already be Windows-style, but let's do a sanity check
+    // nevertheless. Not using assert(2) because we need this even in non-debug
+    // builds.
+    if (path[0] == '/') {
+      fprintf(
+          stderr,
+          "ERROR: Illegal state; '%s' is assumed to be a Windows path. This" \
+          " is a programming error, fix" \
+          " third_party/ijar/mapped_file_windows.cc\n",
+          path);
+      exit(1);
+    }
+  }
+
+  WindowsPath::~WindowsPath() {}
+
+#else  // not COMPILER_MSVC
+
+  WindowsPath::WindowsPath(const char* path) {
+    this->_win_path =
+        reinterpret_cast<char*>(cygwin_create_path(CCP_POSIX_TO_WIN_A, path));
+  }
+
+  WindowsPath::~WindowsPath() {
+    free(this->_win_path);
+  }
+
+#endif  // COMPILER_MSVC
+
 }  // namespace devtools_ijar
diff --git a/third_party/ijar/platform_utils.cc b/third_party/ijar/platform_utils.cc
index 29d468a..e1b46db 100644
--- a/third_party/ijar/platform_utils.cc
+++ b/third_party/ijar/platform_utils.cc
@@ -14,22 +14,26 @@
 
 #include "third_party/ijar/platform_utils.h"
 
+#include <errno.h>
 #include <limits.h>
 #include <stdio.h>
 
 #if defined(COMPILER_MSVC) || defined(__CYGWIN__)
 #include <windows.h>
-#else  // !(defined(COMPILER_MSVC) || defined(__CYGWIN__))
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
 #endif  // defined(COMPILER_MSVC) || defined(__CYGWIN__)
 
+#ifndef COMPILER_MSVC
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#endif  // not COMPILER_MSVC
+
 #include <string>
 
+#if defined(COMPILER_MSVC) || defined(__CYGWIN__)
 #include "src/main/cpp/util/errors.h"
-#include "src/main/cpp/util/file.h"
 #include "src/main/cpp/util/file_platform.h"
+#endif  // defined(COMPILER_MSVC) || defined(__CYGWIN__)
 
 namespace devtools_ijar {
 
@@ -77,30 +81,119 @@
 #endif  // defined(COMPILER_MSVC) || defined(__CYGWIN__)
 }
 
-bool write_file(const char* path, unsigned int perm, const void* data,
-                size_t size) {
-  return blaze_util::WriteFile(data, size, path, perm);
+bool write_file(const char* path, mode_t perm, const void* data, size_t size) {
+#ifdef COMPILER_MSVC
+  // TODO(laszlocsomor) 2016-12-01: implement this and other methods, in order
+  // to close https://github.com/bazelbuild/bazel/issues/2157.
+  fprintf(stderr, "Not yet implemented on Windows\n");
+  return false;
+#else   // not COMPILER_MSVC
+  int fd = open(path, O_CREAT | O_WRONLY, perm);
+  if (fd < 0) {
+    fprintf(stderr, "Cannot open file %s for writing: %s\n", path,
+            strerror(errno));
+    return false;
+  }
+  bool result = true;
+  int error = write(fd, data, size);
+  // Check for an error condition, or if we didn't write all of the data.
+  if (error < 0 || static_cast<size_t>(error) != size) {
+    fprintf(stderr, "Cannot write %zu bytes to file %s: %s\n", size, path,
+            strerror(errno));
+    result = false;
+  }
+  if (close(fd)) {
+    fprintf(stderr, "Cannot close file %s: %s\n", path, strerror(errno));
+    result = false;
+  }
+  return result;
+#endif  // COMPILER_MSVC
 }
 
 bool read_file(const char* path, void* buffer, size_t size) {
-  return blaze_util::ReadFile(path, buffer, size);
+#ifdef COMPILER_MSVC
+  // TODO(laszlocsomor) 2016-12-01: implement this and other methods, in order
+  // to close https://github.com/bazelbuild/bazel/issues/2157.
+  fprintf(stderr, "Not yet implemented on Windows\n");
+  return false;
+#else   // not COMPILER_MSVC
+  // read the input file
+  int fd = open(path, O_RDONLY);
+  if (fd < 0) {
+    fprintf(stderr, "Can't open file %s for reading: %s\n", path,
+            strerror(errno));
+    return false;
+  }
+  bool result = true;
+  size_t nb_read = 0;
+  while (nb_read < size) {
+    size_t to_read = size - nb_read;
+    if (to_read > 16384 /* 16K */) {
+      to_read = 16384;
+    }
+    ssize_t r = read(fd, static_cast<uint8_t*>(buffer) + nb_read, to_read);
+    if (r < 0) {
+      fprintf(stderr, "Can't read %zu bytes from file %s: %s\n", to_read, path,
+              strerror(errno));
+      result = false;
+      break;
+    }
+    nb_read += r;
+  }
+  if (close(fd)) {
+    fprintf(stderr, "Cannot close file %s: %s\n", path, strerror(errno));
+    result = false;
+  }
+  return result;
+#endif  // COMPILER_MSVC
 }
 
-string get_cwd() { return blaze_util::GetCwd(); }
+string get_cwd() {
+#ifdef COMPILER_MSVC
+  // TODO(laszlocsomor) 2016-12-01: implement this and other methods, in order
+  // to close https://github.com/bazelbuild/bazel/issues/2157.
+  fprintf(stderr, "Not yet implemented on Windows\n");
+  return "";
+#else   // not COMPILER_MSVC
+  char cwd[PATH_MAX];
+  if (getcwd(cwd, PATH_MAX) == NULL) {
+    fprintf(stderr, "getcwd() failed: %s\n", strerror(errno));
+    return "";
+  } else {
+    return string(cwd);
+  }
+#endif  // COMPILER_MSVC
+}
 
-bool make_dirs(const char* path, unsigned int mode) {
-#ifndef COMPILER_MSVC
-  // TODO(laszlocsomor): respect `mode` on Windows/MSVC.
+bool make_dirs(const char* path, mode_t mode) {
+#ifdef COMPILER_MSVC
+  // TODO(laszlocsomor) 2016-12-01: implement this and other methods, in order
+  // to close https://github.com/bazelbuild/bazel/issues/2157.
+  fprintf(stderr, "Not yet implemented on Windows\n");
+  return false;
+#else   // not COMPILER_MSVC
+  // Directories created must have executable bit set and be owner writeable.
+  // Otherwise, we cannot write or create any file inside.
   mode |= S_IWUSR | S_IXUSR;
-#endif  // not COMPILER_MSVC
-  string spath(path);
-  if (spath.empty()) {
-    return true;
+  char path_[PATH_MAX];
+  Stat file_stat;
+  strncpy(path_, path, PATH_MAX);
+  path_[PATH_MAX - 1] = 0;
+  char* pointer = path_;
+  while ((pointer = strchr(pointer, '/')) != NULL) {
+    if (path_ != pointer) {  // skip leading slash
+      *pointer = 0;
+      if (!stat_file(path_, &file_stat) && mkdir(path_, mode) < 0) {
+        fprintf(stderr, "Cannot create folder %s: %s\n", path_,
+                strerror(errno));
+        return false;
+      }
+      *pointer = '/';
+    }
+    pointer++;
   }
-  if (spath.back() != '/' && spath.back() != '\\') {
-    spath = blaze_util::Dirname(spath);
-  }
-  return blaze_util::MakeDirectories(spath, mode);
+  return true;
+#endif  // COMPILER_MSVC
 }
 
 }  // namespace devtools_ijar
diff --git a/third_party/ijar/platform_utils.h b/third_party/ijar/platform_utils.h
index 5cdab2d..2a2f37f 100644
--- a/third_party/ijar/platform_utils.h
+++ b/third_party/ijar/platform_utils.h
@@ -52,8 +52,7 @@
 // The file is created or overwritten and is set to have `perm` permissions.
 // Returns true upon success: file is created and all data is written.
 // Returns false upon failure and reports the error to stderr.
-bool write_file(const char* path, unsigned int perm, const void* data,
-                size_t size);
+bool write_file(const char* path, mode_t perm, const void* data, size_t size);
 
 // Reads at most `size` bytes into `buffer` from the file under `path`.
 // Returns true upon success: file is opened and all data is read.
@@ -70,7 +69,7 @@
 // openable by the current user.
 // Returns true if all directories were created and permissions set.
 // Returns false upon failure and reports the error to stderr.
-bool make_dirs(const char* path, unsigned int perm);
+bool make_dirs(const char* path, mode_t perm);
 
 }  // namespace devtools_ijar