< prev index next >

src/java.base/windows/native/libjava/WinNTFileSystem_md.c

Print this page
rev 52921 : 8211752: JNU_ThrowIOExceptionWithLastErrorAndPath - enhance some IOExceptions with path causing the issue

@@ -76,10 +76,29 @@
         GetFinalPathNameByHandle_func = (GetFinalPathNameByHandleProc)
             GetProcAddress(handle, "GetFinalPathNameByHandleW");
     }
 }
 
+static int extendExceptionsWithPath = -1;
+
+static int checkExtendExceptionsWithPath(JNIEnv *env) {
+    if (extendExceptionsWithPath == -1) {
+        jclass cls = (*env)->FindClass(env, "java/io/FileSystem");
+        if (cls == NULL) return -1;
+        jmethodID hasPathInExceptionsMethod = (*env)->GetStaticMethodID(env, cls,
+                                     "hasPathInExceptions", "()Z");
+        if (hasPathInExceptionsMethod == NULL) return -1;
+        jboolean res = (*env)->CallStaticBooleanMethod(env, cls, hasPathInExceptionsMethod);
+        if (res == JNI_TRUE) {
+            extendExceptionsWithPath = 1;
+        } else {
+            extendExceptionsWithPath = 0;
+        }
+    }
+    return extendExceptionsWithPath;
+}
+
 /* -- Path operations -- */
 
 extern int wcanonicalize(const WCHAR *path, WCHAR *out, int len);
 extern int wcanonicalizeWithPrefix(const WCHAR *canonicalPrefix, const WCHAR *pathWithCanonicalPrefix, WCHAR *out, int len);
 

@@ -254,12 +273,18 @@
         } else if (wcanonicalize(path, canonicalPath, MAX_PATH_LENGTH) >= 0) {
             rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath));
         }
     } END_UNICODE_STRING(env, path);
     if (rv == NULL && !(*env)->ExceptionCheck(env)) {
+            if (checkExtendExceptionsWithPath(env) == 1) {
+                WITH_PLATFORM_STRING(env, pathname, pathstr) {
+                    JNU_ThrowByNameWithTwoMessagesAndLastError(env, "java/io/IOException", "Bad pathname", pathstr);
+                } END_PLATFORM_STRING(env, pathstr);
+            } else {
         JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
     }
+    }
     return rv;
 }
 
 
 JNIEXPORT jstring JNICALL

@@ -290,12 +315,18 @@
                 rv = (*env)->NewString(env, canonicalPath, (jsize)wcslen(canonicalPath));
             }
         } END_UNICODE_STRING(env, pathWithCanonicalPrefix);
     } END_UNICODE_STRING(env, canonicalPrefix);
     if (rv == NULL && !(*env)->ExceptionCheck(env)) {
+        if (checkExtendExceptionsWithPath(env) == 1) {
+            WITH_PLATFORM_STRING(env, pathWithCanonicalPrefixString, path) {
+                JNU_ThrowByNameWithTwoMessagesAndLastError(env, "java/io/IOException", "Bad pathname", path);
+            } END_PLATFORM_STRING(env, path);
+        } else {
         JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
     }
+    }
     return rv;
 }
 
 /* -- Attribute accessors -- */
 

@@ -582,13 +613,19 @@
             // return false rather than throwing an exception when there is
             // an existing file.
             DWORD a = GetFileAttributesW(pathbuf);
             if (a == INVALID_FILE_ATTRIBUTES) {
                 SetLastError(error);
+                if (checkExtendExceptionsWithPath(env) == 1) {
+                    WITH_PLATFORM_STRING(env, path, pathstr) {
+                        JNU_ThrowByNameWithTwoMessagesAndLastError(env, "java/io/IOException", "Could not open file", pathstr);
+                    } END_PLATFORM_STRING(env, pathstr);
+                } else {
                 JNU_ThrowIOExceptionWithLastError(env, "Could not open file");
             }
         }
+        }
         free(pathbuf);
         return JNI_FALSE;
     }
     free(pathbuf);
     CloseHandle(h);
< prev index next >