Windows JNI, refactor: move OpenDirectory to JNI

Move the OpenDirectory helper method into the JNI
library. We'll need it there; a subsequent change
will make use of it there.

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

--
PiperOrigin-RevId: 147448792
MOS_MIGRATED_REVID=147448792
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc
index b9653ff..7fbfa00 100644
--- a/src/main/cpp/blaze_util_windows.cc
+++ b/src/main/cpp/blaze_util_windows.cc
@@ -51,13 +51,9 @@
 #include "src/main/cpp/util/md5.h"
 #include "src/main/cpp/util/numbers.h"
 #include "src/main/cpp/util/strings.h"
+#include "src/main/native/windows_file_operations.h"
 #include "src/main/native/windows_util.h"
 
-// Defined by file_windows.cc
-namespace blaze_util {
-HANDLE OpenDirectory(const WCHAR* path, bool read_write);
-}
-
 namespace blaze {
 
 // Ensure we can safely cast (const) wchar_t* to LP(C)WSTR.
@@ -879,7 +875,7 @@
     return false;
   }
 
-  HANDLE directory = blaze_util::OpenDirectory(wname.c_str(), true);
+  HANDLE directory = windows_util::OpenDirectory(wname.c_str(), true);
   if (directory == INVALID_HANDLE_VALUE) {
     return false;
   }
@@ -959,7 +955,7 @@
     return false;
   }
 
-  HANDLE directory = blaze_util::OpenDirectory(wname.c_str(), false);
+  HANDLE directory = windows_util::OpenDirectory(wname.c_str(), false);
   if (directory == INVALID_HANDLE_VALUE) {
     return false;
   }
diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc
index 8b7950a..9d22444 100644
--- a/src/main/cpp/util/file_windows.cc
+++ b/src/main/cpp/util/file_windows.cc
@@ -25,6 +25,7 @@
 #include "src/main/cpp/util/exit_code.h"
 #include "src/main/cpp/util/file.h"
 #include "src/main/cpp/util/strings.h"
+#include "src/main/native/windows_file_operations.h"
 #include "src/main/native/windows_util.h"
 
 namespace blaze_util {
@@ -647,19 +648,6 @@
   return UnlinkPathW(wpath);
 }
 
-HANDLE OpenDirectory(const WCHAR* path, bool read_write) {
-  return ::CreateFileW(
-      /* lpFileName */ path,
-      /* dwDesiredAccess */ read_write ? (GENERIC_READ | GENERIC_WRITE)
-                                       : GENERIC_READ,
-      /* dwShareMode */ 0,
-      /* lpSecurityAttributes */ NULL,
-      /* dwCreationDisposition */ OPEN_EXISTING,
-      /* dwFlagsAndAttributes */ FILE_FLAG_OPEN_REPARSE_POINT |
-          FILE_FLAG_BACKUP_SEMANTICS,
-      /* hTemplateFile */ NULL);
-}
-
 class JunctionResolver {
  public:
   JunctionResolver();
@@ -748,7 +736,7 @@
       }
       // Get a handle to the directory.
       windows_util::AutoHandle handle(
-          OpenDirectory(path, /* read_write */ false));
+          windows_util::OpenDirectory(path, /* read_write */ false));
       if (!handle.IsValid()) {
         // Opening the junction failed for whatever reason. For all intents and
         // purposes we can treat this file as if it didn't exist.
diff --git a/src/main/native/BUILD b/src/main/native/BUILD
index ba60570..653b595 100644
--- a/src/main/native/BUILD
+++ b/src/main/native/BUILD
@@ -64,8 +64,14 @@
 
 cc_library(
     name = "windows_jni_lib",
-    srcs = ["windows_util.cc"],
-    hdrs = ["windows_util.h"],
+    srcs = [
+        "windows_file_operations.cc",
+        "windows_util.cc",
+    ],
+    hdrs = [
+        "windows_file_operations.h",
+        "windows_util.h",
+    ],
     visibility = [
         "//src/main/cpp:__subpackages__",
         "//src/test/cpp:__subpackages__",
diff --git a/src/main/native/windows_file_operations.cc b/src/main/native/windows_file_operations.cc
index e9e561e..b5780b2 100644
--- a/src/main/native/windows_file_operations.cc
+++ b/src/main/native/windows_file_operations.cc
@@ -47,4 +47,17 @@
   return true;
 }
 
+HANDLE OpenDirectory(const WCHAR* path, bool read_write) {
+  return ::CreateFileW(
+      /* lpFileName */ path,
+      /* dwDesiredAccess */
+      read_write ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_READ,
+      /* dwShareMode */ 0,
+      /* lpSecurityAttributes */ NULL,
+      /* dwCreationDisposition */ OPEN_EXISTING,
+      /* dwFlagsAndAttributes */ FILE_FLAG_OPEN_REPARSE_POINT |
+          FILE_FLAG_BACKUP_SEMANTICS,
+      /* hTemplateFile */ NULL);
+}
+
 }  // namespace windows_util
diff --git a/src/main/native/windows_file_operations.h b/src/main/native/windows_file_operations.h
index e28200f..ded07b0 100644
--- a/src/main/native/windows_file_operations.h
+++ b/src/main/native/windows_file_operations.h
@@ -50,11 +50,17 @@
 // Computes the long version of `path` if it has any 8dot3 style components.
 // Returns true upon success and sets `result` to point to the buffer.
 // `path` must be an absolute, normalized, Windows style path, with a "\\?\"
-// prefix if its longer than MAX_PATH. The result will have a "\\?\" prefix if
+// prefix if it's longer than MAX_PATH. The result will have a "\\?\" prefix if
 // and only if `path` had one as well. (It's the caller's responsibility to keep
 // or remove this prefix.)
 bool GetLongPath(const WCHAR* path, unique_ptr<WCHAR[]>* result);
 
+// Opens a directory using CreateFileW.
+// `path` must be a valid Windows path, with "\\?\" prefix if it's long.
+// If `read_write` is true then the directory is opened for reading and writing,
+// otherwise only for reading.
+HANDLE OpenDirectory(const WCHAR* path, bool read_write);
+
 }  // namespace windows_util
 
 #endif  // BAZEL_SRC_MAIN_NATIVE_WINDOWS_FILE_OPERATIONS_H_
diff --git a/src/test/cpp/util/BUILD b/src/test/cpp/util/BUILD
index 64e9e19..c8b2f57 100644
--- a/src/test/cpp/util/BUILD
+++ b/src/test/cpp/util/BUILD
@@ -38,14 +38,8 @@
         "//src/main/cpp/util:file",
         "//third_party:gtest",
     ] + select({
-        "//src:windows": [
-            "//src/main/native:windows_jni_lib",
-            ":windows_test_util",
-        ],
-        "//src:windows_msvc": [
-            "//src/main/native:windows_jni_lib",
-            ":windows_test_util",
-        ],
+        "//src:windows": [":windows_test_util"],
+        "//src:windows_msvc": [":windows_test_util"],
         "//conditions:default": [],
     }),
 )
diff --git a/src/test/cpp/util/file_windows_test.cc b/src/test/cpp/util/file_windows_test.cc
index 0e777d8..ec4e0f5 100644
--- a/src/test/cpp/util/file_windows_test.cc
+++ b/src/test/cpp/util/file_windows_test.cc
@@ -22,7 +22,6 @@
 #include "gtest/gtest.h"
 #include "src/main/cpp/util/file.h"
 #include "src/main/cpp/util/file_platform.h"
-#include "src/main/native/windows_util.h"
 #include "src/test/cpp/util/windows_test_util.h"
 
 #if !defined(COMPILER_MSVC) && !defined(__CYGWIN__)