Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GetTempPath2 API instead of GetTempPath #21

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ typedef HRESULT (WINAPI *GetThreadDescriptionFnPtr)(HANDLE, PWSTR*);
static SetThreadDescriptionFnPtr _SetThreadDescription = nullptr;
DEBUG_ONLY(static GetThreadDescriptionFnPtr _GetThreadDescription = nullptr;)

// For dynamic lookup of GetTempPath2 API
typedef DWORD (WINAPI *GetTempPath2AFnPtr)(DWORD, LPSTR);
static GetTempPath2AFnPtr _GetTempPath2A = nullptr;

// forward decl.
static errno_t convert_to_unicode(char const* char_path, LPWSTR* unicode_path);

Expand Down Expand Up @@ -1515,12 +1519,15 @@ int os::closedir(DIR *dirp) {
// directory not the java application's temp directory, ala java.io.tmpdir.
const char* os::get_temp_directory() {
static char path_buf[MAX_PATH];
if (GetTempPath(MAX_PATH, path_buf) > 0) {
if (_GetTempPath2A != nullptr) {
if (_GetTempPath2A(MAX_PATH, path_buf) > 0) {
return path_buf;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the code indentation here is not correct. Is it?

} else {
path_buf[0] = '\0';
}
} else if (GetTempPath(MAX_PATH, path_buf) > 0) {
return path_buf;
}
path_buf[0] = '\0';
return path_buf;
}

// Needs to be in os specific directory because windows requires another
Expand Down Expand Up @@ -4711,6 +4718,14 @@ jint os::init_2(void) {
}
log_info(os, thread)("The SetThreadDescription API is%s available.", _SetThreadDescription == nullptr ? " not" : "");

// Lookup GetTempPath2
if (_kernelbase != nullptr) {
_GetTempPath2A =
reinterpret_cast<GetTempPath2AFnPtr>(
GetProcAddress(_kernelbase,
"GetTempPath2A"));
}
log_info(os, thread)("The _GetTempPath2A API is%s available.", _GetTempPath2A == nullptr ? " not" : "");

return JNI_OK;
}
Expand Down
22 changes: 21 additions & 1 deletion src/java.base/windows/native/libjava/java_props_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,26 @@ SetupI18nProps(LCID lcid, char** language, char** script, char** country,
return TRUE;
}

// For dynamic lookup of GetTempPath2 API
typedef DWORD (WINAPI *GetTempPath2WFnPtr)(DWORD, LPWSTR);
static GetTempPath2WFnPtr _GetTempPath2W = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code should use "nullptr" instead of NULL? I'm OK with it being "NULL" if other places in the file also uses NULL.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why all these leading "_" ? Is that a coding guideline for that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using NULL as it is C code and others are C++.

static BOOL _GetTempPath2WInitialized = FALSE;

DWORD _GetTempPathW(DWORD nBufferLength, LPWSTR lpBuffer)
{
if (!_GetTempPath2WInitialized) {
HINSTANCE _kernelbase = LoadLibrary(TEXT("kernelbase.dll"));
if (_kernelbase != NULL) {
_GetTempPath2W = (GetTempPath2WFnPtr)GetProcAddress(_kernelbase, "GetTempPath2W");
}
_GetTempPath2WInitialized = TRUE;
}
if (_GetTempPath2W != NULL) {
return _GetTempPath2W(nBufferLength, lpBuffer);
}
return GetTempPathW(nBufferLength, lpBuffer);
}

// GetVersionEx is deprecated; disable the warning until a replacement is found
#pragma warning(disable : 4996)
java_props_t *
Expand All @@ -339,7 +359,7 @@ GetJavaProperties(JNIEnv* env)
{
WCHAR tmpdir[MAX_PATH + 1];
/* we might want to check that this succeed */
GetTempPathW(MAX_PATH + 1, tmpdir);
_GetTempPathW(MAX_PATH + 1, tmpdir);
sprops.tmp_dir = _wcsdup(tmpdir);
}

Expand Down
24 changes: 22 additions & 2 deletions src/jdk.attach/windows/native/libattach/AttachProviderImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@

#include "sun_tools_attach_AttachProviderImpl.h"

// For dynamic lookup of GetTempPath2 API
typedef DWORD (WINAPI *GetTempPath2AFnPtr)(DWORD, LPSTR);
static GetTempPath2AFnPtr _GetTempPath2A = NULL;
static BOOL _GetTempPath2AInitialized = FALSE;

DWORD _GetTempPathA(DWORD nBufferLength, LPSTR lpBuffer)
{
if (!_GetTempPath2AInitialized) {
HINSTANCE _kernelbase = LoadLibrary(TEXT("kernelbase.dll"));
if (_kernelbase != NULL) {
_GetTempPath2A = (GetTempPath2AFnPtr)GetProcAddress(_kernelbase, "GetTempPath2A");
}
_GetTempPath2AInitialized = TRUE;
}
if (_GetTempPath2A != NULL) {
return _GetTempPath2A(nBufferLength, lpBuffer);
}
return GetTempPathA(nBufferLength, lpBuffer);
}

/*
* Class: sun_tools_attach_AttachProviderImpl
* Method: tempPath
Expand All @@ -45,13 +65,13 @@ Java_sun_tools_attach_AttachProviderImpl_tempPath(JNIEnv *env, jclass cls)
jstring result = NULL;

bufLen = sizeof(buf) / sizeof(char);
actualLen = GetTempPath(bufLen, buf);
actualLen = _GetTempPathA(bufLen, buf);
if (actualLen > 0) {
char* bufP = buf;
if (actualLen > bufLen) {
actualLen += sizeof(char);
bufP = (char*)malloc(actualLen * sizeof(char));
actualLen = GetTempPath(actualLen, bufP);
actualLen = _GetTempPathA(actualLen, bufP);
}
if (actualLen > 0) {
result = JNU_NewStringPlatform(env, bufP);
Expand Down
26 changes: 24 additions & 2 deletions src/jdk.jpackage/windows/native/common/WinSysInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,36 @@

// Requires linking with Shell32

// For dynamic lookup of GetTempPath2 API
typedef DWORD (WINAPI *GetTempPath2WFnPtr)(DWORD, LPWSTR);
static GetTempPath2WFnPtr _GetTempPath2W = nullptr;
static bool _GetTempPath2WInitialized = false;

DWORD _GetTempPathW(DWORD nBufferLength, LPWSTR lpBuffer)
{
if (!_GetTempPath2WInitialized) {
HINSTANCE _kernelbase = LoadLibrary(TEXT("kernelbase.dll"));
if (_kernelbase != nullptr) {
_GetTempPath2W =
reinterpret_cast<GetTempPath2WFnPtr>(
GetProcAddress(_kernelbase, "GetTempPath2W"));
}
_GetTempPath2WInitialized = true;
}
if (_GetTempPath2W != nullptr) {
return _GetTempPath2W(nBufferLength, lpBuffer);
}
return GetTempPathW(nBufferLength, lpBuffer);
}

namespace SysInfo {

tstring getTempDir() {
std::vector<TCHAR> buffer(MAX_PATH);
DWORD res = GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data());
DWORD res = _GetTempPathW(static_cast<DWORD>(buffer.size()), buffer.data());
if (res > buffer.size()) {
buffer.resize(res);
GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data());
_GetTempPathW(static_cast<DWORD>(buffer.size()), buffer.data());
}
return FileUtils::removeTrailingSlash(buffer.data());
}
Expand Down