aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2017-02-14 10:16:23 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2017-02-14 14:23:52 +0000
commit5efe4a116027ae45edd4b8b5eb6ba58670b77f0f (patch)
tree8cd1b2bc5ea2afd799512fb00ab2e9549ba20c1e
parent015e5954157a6c071b6118b3d9b9f51676ccc6f3 (diff)
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
-rw-r--r--src/main/cpp/blaze_util_windows.cc10
-rw-r--r--src/main/cpp/util/file_windows.cc16
-rw-r--r--src/main/native/BUILD10
-rw-r--r--src/main/native/windows_file_operations.cc13
-rw-r--r--src/main/native/windows_file_operations.h8
-rw-r--r--src/test/cpp/util/BUILD10
-rw-r--r--src/test/cpp/util/file_windows_test.cc1
7 files changed, 35 insertions, 33 deletions
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc
index b9653ff74a..7fbfa004db 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 @@ bool SymlinkDirectories(const string &posix_target, const string &posix_name) {
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 @@ bool ReadDirectorySymlink(const string &posix_name, string* result) {
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 8b7950a608..9d22444dc8 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 @@ bool UnlinkPath(const string& file_path) {
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 @@ bool JunctionResolver::Resolve(const WCHAR* path, unique_ptr<WCHAR[]>* result,
}
// 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 ba605707c1..653b595ad2 100644
--- a/src/main/native/BUILD
+++ b/src/main/native/BUILD
@@ -64,8 +64,14 @@ cc_binary(
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 e9e561eeb8..b5780b2c86 100644
--- a/src/main/native/windows_file_operations.cc
+++ b/src/main/native/windows_file_operations.cc
@@ -47,4 +47,17 @@ bool GetLongPath(const WCHAR* path, unique_ptr<WCHAR[]>* result) {
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 e28200f359..ded07b0a1f 100644
--- a/src/main/native/windows_file_operations.h
+++ b/src/main/native/windows_file_operations.h
@@ -50,11 +50,17 @@ int IsJunctionOrDirectorySymlink(const WCHAR* path);
// 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 64e9e19518..c8b2f57caf 100644
--- a/src/test/cpp/util/BUILD
+++ b/src/test/cpp/util/BUILD
@@ -38,14 +38,8 @@ cc_test(
"//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 0e777d88fd..ec4e0f5b53 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__)