diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributes.java b/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributes.java index 48dc4c07d0d..55661ff12a3 100644 --- a/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributes.java +++ b/src/java.base/unix/classes/sun/nio/fs/UnixFileAttributes.java @@ -83,18 +83,8 @@ class UnixFileAttributes // get the UnixFileAttributes for a given file. // Returns null if the file does not exist. - static UnixFileAttributes getIfExists(UnixPath path) - throws UnixException - { - UnixFileAttributes attrs = new UnixFileAttributes(); - int errno = UnixNativeDispatcher.stat2(path, attrs); - if (errno == 0) { - return attrs; - } else if (errno == UnixConstants.ENOENT) { - return null; - } else { - throw new UnixException(errno); - } + static UnixFileAttributes getIfExists(UnixPath path) throws UnixException { + return getIfExists(path, true); } // get the UnixFileAttributes for a given file, optionally following links. @@ -104,17 +94,14 @@ class UnixFileAttributes { UnixFileAttributes attrs = new UnixFileAttributes(); int flag = (followLinks) ? 0 : UnixConstants.AT_SYMLINK_NOFOLLOW; - try { - UnixNativeDispatcher.fstatat(UnixConstants.AT_FDCWD, - path.asByteArray(), flag, attrs); - } catch (UnixException x) { - if (x.errno() == UnixConstants.ENOENT) - return null; - - throw x; - } - - return attrs; + int errno = UnixNativeDispatcher.fstatat2(UnixConstants.AT_FDCWD, + path, flag, attrs); + if (errno == 0) + return attrs; + else if (errno == UnixConstants.ENOENT) + return null; + else + throw new UnixException(errno); } // get the UnixFileAttributes for an open file @@ -130,7 +117,7 @@ class UnixFileAttributes { UnixFileAttributes attrs = new UnixFileAttributes(); int flag = (followLinks) ? 0 : UnixConstants.AT_SYMLINK_NOFOLLOW; - UnixNativeDispatcher.fstatat(dfd, path.asByteArray(), flag, attrs); + UnixNativeDispatcher.fstatat(dfd, path, flag, attrs); return attrs; } diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java index 09611f23298..77dc1851478 100644 --- a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java +++ b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java @@ -256,15 +256,15 @@ class UnixNativeDispatcher { } } } + private static native int stat0(long pathAddress, UnixFileAttributes attrs); + // Variant of stat() returning errno instead of throwing UnixException static int stat2(UnixPath path, UnixFileAttributes attrs) { try (NativeBuffer buffer = copyToNativeBuffer(path)) { return stat0(buffer.address(), attrs); } } - private static native int stat0(long pathAddress, UnixFileAttributes attrs); - /** * lstat(const char* path, struct stat* buf) */ @@ -288,15 +288,25 @@ class UnixNativeDispatcher { /** * fstatat(int filedes,const char* path, struct stat* buf, int flag) */ - static void fstatat(int dfd, byte[] path, int flag, UnixFileAttributes attrs) + static void fstatat(int dfd, UnixPath path, int flag, UnixFileAttributes attrs) throws UnixException { - try (NativeBuffer buffer = NativeBuffers.asNativeBuffer(path)) { - fstatat0(dfd, buffer.address(), flag, attrs); + try (NativeBuffer buffer = copyToNativeBuffer(path)) { + int errno = fstatat0(dfd, buffer.address(), flag, attrs); + if (errno != 0) { + throw new UnixException(errno); + } + } + } + private static native int fstatat0(int dfd, long pathAddress, int flag, + UnixFileAttributes attrs); + + // Variant of fstatat() returning errno instead of throwing UnixException + static int fstatat2(int dfd, UnixPath path, int flag, UnixFileAttributes attrs) { + try (NativeBuffer buffer = copyToNativeBuffer(path)) { + return fstatat0(dfd, buffer.address(), flag, attrs); } } - private static native void fstatat0(int dfd, long pathAddress, int flag, - UnixFileAttributes attrs) throws UnixException; /** * chown(const char* path, uid_t owner, gid_t group) diff --git a/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c b/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c index 5f81241b7dd..60ccdfc45fc 100644 --- a/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c +++ b/src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c @@ -741,7 +741,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_fstat0(JNIEnv* env, jclass this, jint fd, } } -JNIEXPORT void JNICALL +JNIEXPORT jint JNICALL Java_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv* env, jclass this, jint dfd, jlong pathAddress, jint flag, jobject attrs) { @@ -761,23 +761,23 @@ Java_sun_nio_fs_UnixNativeDispatcher_fstatat0(JNIEnv* env, jclass this, jint dfd RESTARTABLE(statx_wrapper((int)dfd, path, flags, mask, &statx_buf), err); if (err == 0) { copy_statx_attributes(env, &statx_buf, attrs); + return 0; } else { - throwUnixException(env, errno); + return errno; } - // statx was available, so return now - return; } #endif if (my_fstatat_func == NULL) { JNU_ThrowInternalError(env, "should not reach here"); - return; + return ENOTSUP; } RESTARTABLE((*my_fstatat_func)((int)dfd, path, &buf, (int)flag), err); - if (err == -1) { - throwUnixException(env, errno); - } else { + if (err == 0) { copy_stat_attributes(env, &buf, attrs); + return 0; + } else { + return errno; } }