< prev index next >

src/java.base/unix/native/libjava/UnixFileSystem_md.c

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

*** 86,95 **** --- 86,119 ---- if (!fileClass) return; ids.path = (*env)->GetFieldID(env, fileClass, "path", "Ljava/lang/String;"); } + /* extendExceptionsWithPath == -1: not yet initialized; + * 0: do not extend, 1: extend the exception message + */ + static int extendExceptionsWithPath = -1; + + static int checkExtendExceptionsWithPath(JNIEnv *env) { + if (extendExceptionsWithPath == -1) { + jboolean res; + jmethodID hasPathInExceptionsMethod; + jclass cls = (*env)->FindClass(env, "java/io/FileSystem"); + if (cls == NULL) return -1; + hasPathInExceptionsMethod = (*env)->GetStaticMethodID(env, cls, + "hasPathInExceptions", "()Z"); + if (hasPathInExceptionsMethod == NULL) return -1; + res = (*env)->CallStaticBooleanMethod(env, cls, hasPathInExceptionsMethod); + if (res == JNI_TRUE) { + extendExceptionsWithPath = 1; + } else { + extendExceptionsWithPath = 0; + } + } + return extendExceptionsWithPath; + } + /* -- Path operations -- */ extern int canonicalize(char *path, const char *out, int len); JNIEXPORT jstring JNICALL
*** 100,110 **** --- 124,138 ---- WITH_PLATFORM_STRING(env, pathname, path) { char canonicalPath[PATH_MAX]; if (canonicalize((char *)path, canonicalPath, PATH_MAX) < 0) { + if (checkExtendExceptionsWithPath(env) == 1) { + JNU_ThrowByNameWithTwoMessagesAndLastError(env, "java/io/IOException", "Bad pathname", (char*) path); + } else { JNU_ThrowIOExceptionWithLastError(env, "Bad pathname"); + } } else { #ifdef MACOSX rv = newStringPlatform(env, canonicalPath); #else rv = JNU_NewStringPlatform(env, canonicalPath);
*** 276,290 **** FD fd; /* The root directory always exists */ if (strcmp (path, "/")) { fd = handleOpen(path, O_RDWR | O_CREAT | O_EXCL, 0666); if (fd < 0) { ! if (errno != EEXIST) JNU_ThrowIOExceptionWithLastError(env, path); } else { - if (close(fd) == -1) JNU_ThrowIOExceptionWithLastError(env, path); rv = JNI_TRUE; } } } END_PLATFORM_STRING(env, path); return rv; --- 304,328 ---- FD fd; /* The root directory always exists */ if (strcmp (path, "/")) { fd = handleOpen(path, O_RDWR | O_CREAT | O_EXCL, 0666); if (fd < 0) { ! if (errno != EEXIST) { ! if (checkExtendExceptionsWithPath(env) == 1) { ! JNU_ThrowByNameWithTwoMessagesAndLastError(env, "java/io/IOException", "handleOpen failed", path); ! } else { JNU_ThrowIOExceptionWithLastError(env, path); + } + } + } else { + if (close(fd) == -1) { + if (checkExtendExceptionsWithPath(env) == 1) { + JNU_ThrowByNameWithTwoMessagesAndLastError(env, "java/io/IOException", "close failed", path); } else { JNU_ThrowIOExceptionWithLastError(env, path); + } + } rv = JNI_TRUE; } } } END_PLATFORM_STRING(env, path); return rv;
< prev index next >