Skip to content

Commit

Permalink
Windows JNI, refactor: move OpenDirectory to JNI
Browse files Browse the repository at this point in the history
Move the OpenDirectory helper method into the JNI
library. We'll need it there; a subsequent change
will make use of it there.

See #2107

--
PiperOrigin-RevId: 147448792
MOS_MIGRATED_REVID=147448792
  • Loading branch information
laszlocsomor authored and dslomov committed Feb 14, 2017
1 parent 015e595 commit 5efe4a1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 33 deletions.
10 changes: 3 additions & 7 deletions src/main/cpp/blaze_util_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 2 additions & 14 deletions src/main/cpp/util/file_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions src/main/native/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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__",
Expand Down
13 changes: 13 additions & 0 deletions src/main/native/windows_file_operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion src/main/native/windows_file_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
10 changes: 2 additions & 8 deletions src/test/cpp/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
}),
)
Expand Down
1 change: 0 additions & 1 deletion src/test/cpp/util/file_windows_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down

0 comments on commit 5efe4a1

Please sign in to comment.