--- old/src/hotspot/os/windows/os_windows.cpp 2017-10-25 11:01:40.092591235 -0700 +++ new/src/hotspot/os/windows/os_windows.cpp 2017-10-25 11:01:39.725556744 -0700 @@ -4060,15 +4060,85 @@ } } +// combine the high and low DWORD into a ULONGLONG +static ULONGLONG eval_find_data(DWORD high_word, DWORD low_word) { + ULONGLONG value = high_word; + value <<= sizeof(high_word) * 8; + value |= low_word; + return value; +} + +// setup the struct stat using the data from WIN32_FIND_DATAW structure +static void setup_stat(struct stat* sbuf, WIN32_FIND_DATAW find_data) { + sbuf->st_size = (_off_t)eval_find_data(find_data.nFileSizeHigh, find_data.nFileSizeLow); + sbuf->st_mtime = eval_find_data(find_data.ftLastWriteTime.dwHighDateTime, + find_data.ftLastWriteTime.dwLowDateTime); + sbuf->st_ctime = eval_find_data(find_data.ftCreationTime.dwHighDateTime, + find_data.ftCreationTime.dwLowDateTime); + sbuf->st_atime = eval_find_data(find_data.ftLastAccessTime.dwHighDateTime, + find_data.ftLastAccessTime.dwLowDateTime); + if (find_data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { + sbuf->st_mode |= S_IFDIR; + } + if (find_data.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) { + sbuf->st_mode |= S_IFREG; + } +} + +// The following function is adapted from java.base/windows/native/libjava/canonicalize_md.c +// copy \\?\ or \\?\UNC\ to the front of path +static void prefixed_path(WCHAR* wpath, size_t pathlen) { + if (wpath[0] == L'\\' && wpath[1] == L'\\') { + /* if it already has a \\?\ don't do the prefix */ + if (!(wpath[2] == L'?' && wpath[3] == L'\\')) { + /* only UNC pathname includes double slashes here */ + ::wmemmove(wpath + 7, wpath + 1, pathlen - 1); + ::wmemcpy_s(wpath, pathlen, L"\\\\?\\UNC", 7); + wpath[pathlen + 6] = L'\0'; + } + } + else { + ::wmemmove(wpath + 4, wpath, pathlen); + ::wmemcpy_s(wpath, pathlen, L"\\\\?\\", 4); + wpath[pathlen + 4] = L'\0'; + } +} int os::stat(const char *path, struct stat *sbuf) { - char pathbuf[MAX_PATH]; - if (strlen(path) > MAX_PATH - 1) { - errno = ENAMETOOLONG; + size_t path_len = strlen(path) + 1; + char* pathbuf = (char*)os::malloc(path_len * sizeof(char), mtInternal); + if (pathbuf == NULL) { + errno = ENOMEM; return -1; } os::native_path(strcpy(pathbuf, path)); - int ret = ::stat(pathbuf, sbuf); + int ret; + if (strlen(path) < MAX_PATH) { + ret = ::stat(pathbuf, sbuf); + } else { + WCHAR* wpathbuf = (WCHAR*)os::malloc((path_len + 10) * sizeof (WCHAR), mtInternal); + if (wpathbuf == NULL) { + os::free(pathbuf); + errno = ENOMEM; + return -1; + } + size_t converted_chars = 0; + ::mbstowcs_s(&converted_chars, wpathbuf, path_len + 10, pathbuf, strlen(path)); + prefixed_path(wpathbuf, strlen(path)); + WIN32_FIND_DATAW find_data; + HANDLE h_find; + h_find = ::FindFirstFileW(wpathbuf, &find_data); + if (h_find == INVALID_HANDLE_VALUE) { + errno = ::GetLastError(); + os::free(wpathbuf); + ret = -1; + } else { + setup_stat(sbuf, find_data); + ret = 0; + ::FindClose(h_find); + } + os::free(wpathbuf); + } if (sbuf != NULL && UseUTCFileTimestamp) { // Fix for 6539723. st_mtime returned from stat() is dependent on // the system timezone and so can return different values for the @@ -4092,9 +4162,10 @@ TIME_ZONE_INFORMATION tz; DWORD tzid = GetTimeZoneInformation(&tz); int daylightBias = - (tzid == TIME_ZONE_ID_DAYLIGHT) ? tz.DaylightBias : tz.StandardBias; + (tzid == TIME_ZONE_ID_DAYLIGHT) ? tz.DaylightBias : tz.StandardBias; sbuf->st_mtime += (tz.Bias + daylightBias) * 60; } + os::free(pathbuf); return ret; } @@ -4207,14 +4278,34 @@ // from src/windows/hpi/src/sys_api_md.c int os::open(const char *path, int oflag, int mode) { - char pathbuf[MAX_PATH]; - - if (strlen(path) > MAX_PATH - 1) { - errno = ENAMETOOLONG; + size_t path_len = strlen(path) + 1; + char* pathbuf = (char*)os::malloc(path_len * sizeof(char), mtInternal); + if (pathbuf == NULL) { + errno = ENOMEM; return -1; } os::native_path(strcpy(pathbuf, path)); - return ::open(pathbuf, oflag | O_BINARY | O_NOINHERIT, mode); + int ret; + if (strlen(path) < MAX_PATH) { + ret = ::open(pathbuf, oflag | O_BINARY | O_NOINHERIT, mode); + } else { + WCHAR* wpathbuf = (WCHAR*)os::malloc((path_len + 10) * sizeof (WCHAR), mtInternal); + if (wpathbuf == NULL) { + os::free(pathbuf); + errno = ENOMEM; + return -1; + } + size_t converted_chars = 0; + ::mbstowcs_s(&converted_chars, wpathbuf, path_len + 10, pathbuf, strlen(path)); + prefixed_path(wpathbuf, strlen(path)); + ret = ::_wopen(wpathbuf, oflag | O_BINARY | O_NOINHERIT, mode); + if (ret == -1) { + errno = ::GetLastError(); + } + os::free(wpathbuf); + } + os::free(pathbuf); + return ret; } FILE* os::open(int fd, const char* mode) {