Skip to content

Commit

Permalink
use "Foreign Function & Memory API" instead of JNI
Browse files Browse the repository at this point in the history
  • Loading branch information
sendaoYan committed Sep 5, 2024
1 parent f838ddf commit 8c03946
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdbool.h>
#else
#define bool int
#define true 1
#define false 0
#endif
#include <linux/fcntl.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 8c03946

Please sign in to comment.