comparison src/windows/native/java/io/WinNTFileSystem_md.c @ 10983:935758609767

Added tag jdk8u60-b28 for changeset 48e79820c798
author asaha
date Tue, 13 Oct 2015 08:18:35 -0700
parents 7a3d9b221f8a
children
comparison
equal deleted inserted replaced
19:7523fa773c67 20:6fa4a3a3b026
57 57
58 JNIEXPORT void JNICALL 58 JNIEXPORT void JNICALL
59 Java_java_io_WinNTFileSystem_initIDs(JNIEnv *env, jclass cls) 59 Java_java_io_WinNTFileSystem_initIDs(JNIEnv *env, jclass cls)
60 { 60 {
61 HMODULE handle; 61 HMODULE handle;
62 jclass fileClass = (*env)->FindClass(env, "java/io/File"); 62 jclass fileClass;
63 if (!fileClass) return; 63
64 ids.path = 64 fileClass = (*env)->FindClass(env, "java/io/File");
65 (*env)->GetFieldID(env, fileClass, "path", "Ljava/lang/String;"); 65 CHECK_NULL(fileClass);
66 ids.path = (*env)->GetFieldID(env, fileClass, "path", "Ljava/lang/String;");
67 CHECK_NULL(ids.path);
66 68
67 // GetFinalPathNameByHandle requires Windows Vista or newer 69 // GetFinalPathNameByHandle requires Windows Vista or newer
68 if (GetModuleHandleExW((GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | 70 if (GetModuleHandleExW((GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
69 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT), 71 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT),
70 (LPCWSTR)&CreateFileW, &handle) != 0) 72 (LPCWSTR)&CreateFileW, &handle) != 0)
245 { 247 {
246 jstring rv = NULL; 248 jstring rv = NULL;
247 WCHAR canonicalPath[MAX_PATH_LENGTH]; 249 WCHAR canonicalPath[MAX_PATH_LENGTH];
248 250
249 WITH_UNICODE_STRING(env, pathname, path) { 251 WITH_UNICODE_STRING(env, pathname, path) {
250 /*we estimate the max length of memory needed as 252 /* we estimate the max length of memory needed as
251 "currentDir. length + pathname.length" 253 "currentDir. length + pathname.length"
252 */ 254 */
253 int len = (int)wcslen(path); 255 int len = (int)wcslen(path);
254 len += currentDirLength(path, len); 256 len += currentDirLength(path, len);
255 if (len > MAX_PATH_LENGTH - 1) { 257 if (len > MAX_PATH_LENGTH - 1) {
256 WCHAR *cp = (WCHAR*)malloc(len * sizeof(WCHAR)); 258 WCHAR *cp = (WCHAR*)malloc(len * sizeof(WCHAR));
260 } 262 }
261 free(cp); 263 free(cp);
262 } else { 264 } else {
263 JNU_ThrowOutOfMemoryError(env, "native memory allocation failed"); 265 JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
264 } 266 }
265 } else 267 } else if (wcanonicalize(path, canonicalPath, MAX_PATH_LENGTH) >= 0) {
266 if (wcanonicalize(path, canonicalPath, MAX_PATH_LENGTH) >= 0) {
267 rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath)); 268 rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath));
268 } 269 }
269 } END_UNICODE_STRING(env, path); 270 } END_UNICODE_STRING(env, path);
270 if (rv == NULL) { 271 if (rv == NULL && !(*env)->ExceptionCheck(env)) {
271 JNU_ThrowIOExceptionWithLastError(env, "Bad pathname"); 272 JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
272 } 273 }
273 return rv; 274 return rv;
274 } 275 }
275 276
294 } 295 }
295 free(cp); 296 free(cp);
296 } else { 297 } else {
297 JNU_ThrowOutOfMemoryError(env, "native memory allocation failed"); 298 JNU_ThrowOutOfMemoryError(env, "native memory allocation failed");
298 } 299 }
299 } else 300 } else if (wcanonicalizeWithPrefix(canonicalPrefix,
300 if (wcanonicalizeWithPrefix(canonicalPrefix, 301 pathWithCanonicalPrefix,
301 pathWithCanonicalPrefix, 302 canonicalPath, MAX_PATH_LENGTH) >= 0) {
302 canonicalPath, MAX_PATH_LENGTH) >= 0) {
303 rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath)); 303 rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath));
304 } 304 }
305 } END_UNICODE_STRING(env, pathWithCanonicalPrefix); 305 } END_UNICODE_STRING(env, pathWithCanonicalPrefix);
306 } END_UNICODE_STRING(env, canonicalPrefix); 306 } END_UNICODE_STRING(env, canonicalPrefix);
307 if (rv == NULL) { 307 if (rv == NULL && !(*env)->ExceptionCheck(env)) {
308 JNU_ThrowIOExceptionWithLastError(env, "Bad pathname"); 308 JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
309 } 309 }
310 return rv; 310 return rv;
311 } 311 }
312 312
622 WIN32_FIND_DATAW find_data; 622 WIN32_FIND_DATAW find_data;
623 int len, maxlen; 623 int len, maxlen;
624 jobjectArray rv, old; 624 jobjectArray rv, old;
625 DWORD fattr; 625 DWORD fattr;
626 jstring name; 626 jstring name;
627 627 jclass str_class;
628 WCHAR *pathbuf = fileToNTPath(env, file, ids.path); 628 WCHAR *pathbuf;
629
630 str_class = JNU_ClassString(env);
631 CHECK_NULL_RETURN(str_class, NULL);
632
633 pathbuf = fileToNTPath(env, file, ids.path);
629 if (pathbuf == NULL) 634 if (pathbuf == NULL)
630 return NULL; 635 return NULL;
631 search_path = (WCHAR*)malloc(2*wcslen(pathbuf) + 6); 636 search_path = (WCHAR*)malloc(2*wcslen(pathbuf) + 6);
632 if (search_path == 0) { 637 if (search_path == 0) {
633 free (pathbuf); 638 free (pathbuf);
671 if (GetLastError() != ERROR_FILE_NOT_FOUND) { 676 if (GetLastError() != ERROR_FILE_NOT_FOUND) {
672 // error 677 // error
673 return NULL; 678 return NULL;
674 } else { 679 } else {
675 // No files found - return an empty array 680 // No files found - return an empty array
676 rv = (*env)->NewObjectArray(env, 0, JNU_ClassString(env), NULL); 681 rv = (*env)->NewObjectArray(env, 0, str_class, NULL);
677 return rv; 682 return rv;
678 } 683 }
679 } 684 }
680 685
681 /* Allocate an initial String array */ 686 /* Allocate an initial String array */
682 len = 0; 687 len = 0;
683 maxlen = 16; 688 maxlen = 16;
684 rv = (*env)->NewObjectArray(env, maxlen, JNU_ClassString(env), NULL); 689 rv = (*env)->NewObjectArray(env, maxlen, str_class, NULL);
685 if (rv == NULL) // Couldn't allocate an array 690 if (rv == NULL) // Couldn't allocate an array
686 return NULL; 691 return NULL;
687 /* Scan the directory */ 692 /* Scan the directory */
688 do { 693 do {
689 if (!wcscmp(find_data.cFileName, L".") 694 if (!wcscmp(find_data.cFileName, L".")
693 (jsize)wcslen(find_data.cFileName)); 698 (jsize)wcslen(find_data.cFileName));
694 if (name == NULL) 699 if (name == NULL)
695 return NULL; // error; 700 return NULL; // error;
696 if (len == maxlen) { 701 if (len == maxlen) {
697 old = rv; 702 old = rv;
698 rv = (*env)->NewObjectArray(env, maxlen <<= 1, 703 rv = (*env)->NewObjectArray(env, maxlen <<= 1, str_class, NULL);
699 JNU_ClassString(env), NULL); 704 if (rv == NULL || JNU_CopyObjectArray(env, rv, old, len) < 0)
700 if ( rv == NULL
701 || JNU_CopyObjectArray(env, rv, old, len) < 0)
702 return NULL; // error 705 return NULL; // error
703 (*env)->DeleteLocalRef(env, old); 706 (*env)->DeleteLocalRef(env, old);
704 } 707 }
705 (*env)->SetObjectArrayElement(env, rv, len++, name); 708 (*env)->SetObjectArrayElement(env, rv, len++, name);
706 (*env)->DeleteLocalRef(env, name); 709 (*env)->DeleteLocalRef(env, name);
711 return NULL; // error 714 return NULL; // error
712 FindClose(handle); 715 FindClose(handle);
713 716
714 /* Copy the final results into an appropriately-sized array */ 717 /* Copy the final results into an appropriately-sized array */
715 old = rv; 718 old = rv;
716 rv = (*env)->NewObjectArray(env, len, JNU_ClassString(env), NULL); 719 rv = (*env)->NewObjectArray(env, len, str_class, NULL);
717 if (rv == NULL) 720 if (rv == NULL)
718 return NULL; /* error */ 721 return NULL; /* error */
719 if (JNU_CopyObjectArray(env, rv, old, len) < 0) 722 if (JNU_CopyObjectArray(env, rv, old, len) < 0)
720 return NULL; /* error */ 723 return NULL; /* error */
721 return rv; 724 return rv;