diff --git a/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java b/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java index fb673161f17ab..4e23b4828bdca 100644 --- a/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java +++ b/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java @@ -25,18 +25,18 @@ * @bug 8011536 8151430 8316304 8334339 * @summary Basic test for creationTime attribute on platforms/file systems * that support it, tests using /tmp directory. - * @library ../.. /test/lib - * @build jdk.test.lib.Platform - * @run main/native CreationTime + * @library ../.. /test/lib /java/foreign + * @build jdk.test.lib.Platform NativeTestHelper + * @run main/othervm --enable-native-access=ALL-UNNAMED CreationTime */ /* @test id=cwd * @summary Basic test for creationTime attribute on platforms/file systems * that support it, tests using the test scratch directory, the test * scratch directory maybe at diff disk partition to /tmp on linux. - * @library ../.. /test/lib - * @build jdk.test.lib.Platform - * @run main/native CreationTime . + * @library ../.. /test/lib /java/foreign + * @build jdk.test.lib.Platform NativeTestHelper + * @run main/othervm --enable-native-access=ALL-UNNAMED CreationTime . */ import java.lang.foreign.Linker; @@ -107,7 +107,11 @@ static void test(Path top) throws IOException { } } else if (Platform.isLinux()) { // Creation time read depends on statx system call support - supportsCreationTimeRead = CreationTimeHelper.linuxIsCreationTimeSupported(file.toAbsolutePath().toString()); + try { + supportsCreationTimeRead = CreationTimeHelper.linuxIsCreationTimeSupported(file.toAbsolutePath().toString()); + } catch (Throwable e) { + supportsCreationTimeRead = false; + } // Creation time updates are not supported on Linux supportsCreationTimeWrite = false; } diff --git a/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTimeHelper.java b/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTimeHelper.java index f57f6a6892541..43d737c2ba003 100644 --- a/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTimeHelper.java +++ b/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTimeHelper.java @@ -20,12 +20,32 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ -public class CreationTimeHelper { + +import java.lang.foreign.Arena; +import java.lang.foreign.FunctionDescriptor; +import java.lang.foreign.Linker; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.SymbolLookup; +import java.lang.foreign.ValueLayout; +import java.lang.invoke.MethodHandle; + +public class CreationTimeHelper extends NativeTestHelper { static { System.loadLibrary("CreationTimeHelper"); } + final static Linker abi = Linker.nativeLinker(); + static final SymbolLookup lookup = SymbolLookup.loaderLookup(); + final static MethodHandle methodHandle = abi.downcallHandle(lookup.findOrThrow("linuxIsCreationTimeSupported"), + FunctionDescriptor.of(C_BOOL, C_POINTER)); + // Helper so as to determine 'statx' support on the runtime system - static native boolean linuxIsCreationTimeSupported(String file); + // static boolean linuxIsCreationTimeSupported(String file); + static boolean linuxIsCreationTimeSupported(String file) throws Throwable { + try (var arena = Arena.ofConfined()) { + MemorySegment s = arena.allocateFrom(file); + return (boolean)methodHandle.invokeExact(s); + } + } } diff --git a/test/jdk/java/nio/file/attribute/BasicFileAttributeView/libCreationTimeHelper.c b/test/jdk/java/nio/file/attribute/BasicFileAttributeView/libCreationTimeHelper.c index 8bee748e9f4e5..52c2976129038 100644 --- a/test/jdk/java/nio/file/attribute/BasicFileAttributeView/libCreationTimeHelper.c +++ b/test/jdk/java/nio/file/attribute/BasicFileAttributeView/libCreationTimeHelper.c @@ -20,8 +20,15 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ -#include "jni.h" +#include "export.h" #if defined(__linux__) +#if (__STDC_VERSION__ >= 199901L) + #include +#else + #define bool int + #define true 1 + #define false 0 +#endif #include #include #include @@ -85,9 +92,8 @@ typedef int statx_func(int dirfd, const char *restrict pathname, int flags, static statx_func* my_statx_func = NULL; #endif //#defined(__linux__) -// static native boolean linuxIsCreationTimeSupported() -JNIEXPORT jboolean JNICALL -Java_CreationTimeHelper_linuxIsCreationTimeSupported(JNIEnv *env, jclass cls, jstring file) { +// static boolean linuxIsCreationTimeSupported(char* file) +EXPORT bool linuxIsCreationTimeSupported(char* file) { #if defined(__linux__) struct my_statx stx; int ret, atflag = AT_SYMLINK_NOFOLLOW; @@ -96,34 +102,25 @@ Java_CreationTimeHelper_linuxIsCreationTimeSupported(JNIEnv *env, jclass cls, js my_statx_func = (statx_func*) dlsym(RTLD_DEFAULT, "statx"); if (my_statx_func == NULL) { - return JNI_FALSE; + return false; } if (file == NULL) { printf("input file error!\n"); - return JNI_FALSE; - } - const char *utfChars = (*env)->GetStringUTFChars(env, file, NULL); - if (utfChars == NULL) { - printf("jstring convert to char array error!\n"); - return JNI_FALSE; + return false; } - ret = my_statx_func(AT_FDCWD, utfChars, atflag, mask, &stx); - - if (file != NULL && utfChars != NULL) { - (*env)->ReleaseStringUTFChars(env, file, utfChars); - } + ret = my_statx_func(AT_FDCWD, file, atflag, mask, &stx); #ifdef DEBUG printf("birth time = %ld\n", stx.stx_btime.tv_sec); #endif if (ret != 0) { - return JNI_FALSE; + return false; } if (stx.stx_mask & STATX_BTIME) - return JNI_TRUE; - return JNI_FALSE; + return true; + return false; #else return JNI_FALSE; #endif