aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2017-01-20 10:10:56 +0000
committerGravatar Vladimir Moskva <vladmos@google.com>2017-01-20 12:21:46 +0000
commitfbc546b2850d95094ddbbded2e2bd36312e26248 (patch)
treecdf9c793506b475a9126555c090618a2471635da /src
parentc051fafd957dcd3c6f11db14589c10b924d90157 (diff)
Windows, JNI, refactor: co-locate all JNI code
Move JNI code from windows_file_operations.cc to windows_processes.cc, so all the JNI code is now in the latter. This lets us expose windows_file_operations.* to the Bazel client (via the ":windows_jni_lib" target), so we can finally share file handling logic between the Bazel client and the JNI library. See https://github.com/bazelbuild/bazel/issues/2107 -- PiperOrigin-RevId: 145063140 MOS_MIGRATED_REVID=145063140
Diffstat (limited to 'src')
-rw-r--r--src/main/native/windows_file_operations.cc64
-rw-r--r--src/main/native/windows_processes.cc45
2 files changed, 45 insertions, 64 deletions
diff --git a/src/main/native/windows_file_operations.cc b/src/main/native/windows_file_operations.cc
index 610ae95707..e9e561eeb8 100644
--- a/src/main/native/windows_file_operations.cc
+++ b/src/main/native/windows_file_operations.cc
@@ -12,12 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include <jni.h>
#include <windows.h>
#include <memory>
-#include <string>
-#include <type_traits> // static_assert
#include "src/main/native/windows_file_operations.h"
#include "src/main/native/windows_util.h"
@@ -26,11 +23,6 @@ namespace windows_util {
using std::unique_ptr;
-// Ensure we can safely cast (const) jchar* to LP(C)WSTR.
-// This is true with MSVC but usually not with GCC.
-static_assert(sizeof(jchar) == sizeof(WCHAR),
- "jchar and WCHAR should be the same size");
-
int IsJunctionOrDirectorySymlink(const WCHAR* path) {
DWORD attrs = ::GetFileAttributesW(path);
if (attrs == INVALID_FILE_ATTRIBUTES) {
@@ -55,60 +47,4 @@ bool GetLongPath(const WCHAR* path, unique_ptr<WCHAR[]>* result) {
return true;
}
-static void MaybeReportLastError(string reason, JNIEnv* env,
- jobjectArray error_msg_holder) {
- if (error_msg_holder != nullptr &&
- env->GetArrayLength(error_msg_holder) > 0) {
- std::string error_str = windows_util::GetLastErrorString(reason);
- jstring error_msg = env->NewStringUTF(error_str.c_str());
- env->SetObjectArrayElement(error_msg_holder, 0, error_msg);
- }
-}
-
} // namespace windows_util
-
-extern "C" JNIEXPORT jint JNICALL
-Java_com_google_devtools_build_lib_windows_WindowsFileOperations_nativeIsJunction(
- JNIEnv* env, jclass clazz, jstring path, jobjectArray error_msg_holder) {
- bool report_error =
- error_msg_holder != nullptr && env->GetArrayLength(error_msg_holder) > 0;
- const jchar* wpath = env->GetStringChars(path, nullptr);
- int result = windows_util::IsJunctionOrDirectorySymlink((LPCWSTR)wpath);
- env->ReleaseStringChars(path, wpath);
- if (result == windows_util::IS_JUNCTION_ERROR && report_error) {
- // Getting the string's characters again in UTF8 encoding is
- // easier than converting `wpath` using `wcstombs(3)`.
- const char* path_cstr = env->GetStringUTFChars(path, nullptr);
- windows_util::MaybeReportLastError(
- std::string("GetFileAttributes(") + path_cstr + ")", env,
- error_msg_holder);
- env->ReleaseStringUTFChars(path, path_cstr);
- }
- return result;
-}
-
-extern "C" JNIEXPORT jboolean JNICALL
-Java_com_google_devtools_build_lib_windows_WindowsFileOperations_nativeGetLongPath(
- JNIEnv* env, jclass clazz, jstring path, jobjectArray result_holder,
- jobjectArray error_msg_holder) {
- const jchar* cpath = nullptr;
- cpath = env->GetStringChars(path, nullptr);
- std::unique_ptr<WCHAR[]> result;
- bool success =
- windows_util::GetLongPath(reinterpret_cast<const WCHAR*>(cpath), &result);
- env->ReleaseStringChars(path, cpath);
- if (!success) {
- const char* path_cstr = env->GetStringUTFChars(path, nullptr);
- windows_util::MaybeReportLastError(
- std::string("GetLongPathName(") + path_cstr + ")", env,
- error_msg_holder);
- env->ReleaseStringUTFChars(path, path_cstr);
- return JNI_FALSE;
- }
- std::wstring wresult(result.get());
- env->SetObjectArrayElement(
- result_holder, 0,
- env->NewString(reinterpret_cast<const jchar*>(wresult.c_str()),
- wresult.size()));
- return JNI_TRUE;
-}
diff --git a/src/main/native/windows_processes.cc b/src/main/native/windows_processes.cc
index 284b4e8c60..0a085c2da1 100644
--- a/src/main/native/windows_processes.cc
+++ b/src/main/native/windows_processes.cc
@@ -19,6 +19,7 @@
#include <jni.h>
#include <stdlib.h>
#include <string.h>
+#include <wchar.h>
#include <windows.h>
#include <atomic>
@@ -27,6 +28,7 @@
#include <string>
#include <type_traits> // static_assert
+#include "src/main/native/windows_file_operations.h"
#include "src/main/native/windows_util.h"
// Ensure we can safely cast (const) jchar* to (const) WCHAR* and LP(C)WSTR.
@@ -118,6 +120,16 @@ static bool NestedJobsSupported() {
version_info.dwMajorVersion == 6 && version_info.dwMinorVersion >= 2;
}
+static void MaybeReportLastError(const std::string& reason, JNIEnv* env,
+ jobjectArray error_msg_holder) {
+ if (error_msg_holder != nullptr &&
+ env->GetArrayLength(error_msg_holder) > 0) {
+ std::string error_str = windows_util::GetLastErrorString(reason);
+ jstring error_msg = env->NewStringUTF(error_str.c_str());
+ env->SetObjectArrayElement(error_msg_holder, 0, error_msg);
+ }
+}
+
static std::string GetJavaUTFString(JNIEnv* env, jstring str) {
std::string result;
if (str != nullptr) {
@@ -580,3 +592,36 @@ Java_com_google_devtools_build_lib_windows_WindowsProcesses_nativeStreamGetLastE
stream->error_ = "";
return result;
}
+
+extern "C" JNIEXPORT jint JNICALL
+Java_com_google_devtools_build_lib_windows_WindowsFileOperations_nativeIsJunction(
+ JNIEnv* env, jclass clazz, jstring path, jobjectArray error_msg_holder) {
+ int result = windows_util::IsJunctionOrDirectorySymlink(
+ GetJavaWstring(env, path).c_str());
+ if (result == windows_util::IS_JUNCTION_ERROR) {
+ MaybeReportLastError(
+ std::string("GetFileAttributes(") + GetJavaUTFString(env, path) + ")",
+ env, error_msg_holder);
+ }
+ return result;
+}
+
+extern "C" JNIEXPORT jboolean JNICALL
+Java_com_google_devtools_build_lib_windows_WindowsFileOperations_nativeGetLongPath(
+ JNIEnv* env, jclass clazz, jstring path, jobjectArray result_holder,
+ jobjectArray error_msg_holder) {
+ std::unique_ptr<WCHAR[]> result;
+ bool success =
+ windows_util::GetLongPath(GetJavaWstring(env, path).c_str(), &result);
+ if (!success) {
+ MaybeReportLastError(
+ std::string("GetLongPathName(") + GetJavaUTFString(env, path) + ")",
+ env, error_msg_holder);
+ return JNI_FALSE;
+ }
+ env->SetObjectArrayElement(
+ result_holder, 0,
+ env->NewString(reinterpret_cast<const jchar*>(result.get()),
+ wcslen(result.get())));
+ return JNI_TRUE;
+}