Skip to content

Commit

Permalink
upload JNI solution temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
sendaoYan committed Sep 4, 2024
1 parent 78fbf83 commit 614603e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
import java.nio.file.Files;
import java.nio.file.attribute.*;
import java.time.Instant;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import jdk.test.lib.Platform;
import jtreg.SkippedException;
Expand Down Expand Up @@ -109,7 +107,7 @@ static void test(Path top) throws IOException {
}
} else if (Platform.isLinux()) {
// Creation time read depends on statx system call support
supportsCreationTimeRead = CreationTimeHelper.linuxIsCreationTimeSupported();
supportsCreationTimeRead = CreationTimeHelper.linuxIsCreationTimeSupported(file.toAbsolutePath().toString());
// Creation time updates are not supported on Linux
supportsCreationTimeWrite = false;
}
Expand All @@ -124,8 +122,11 @@ static void test(Path top) throws IOException {
Instant plusHour = Instant.now().plusSeconds(60L * 60L);
Files.setLastModifiedTime(file, FileTime.from(plusHour));
FileTime current = creationTime(file);
if (!current.equals(creationTime))
if (!current.equals(creationTime)) {
System.out.println("current = " + current);
System.out.println("creationTime = " + creationTime);
throw new RuntimeException("Creation time should not have changed");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,5 +27,5 @@ public class CreationTimeHelper {
}

// Helper so as to determine 'statx' support on the runtime system
static native boolean linuxIsCreationTimeSupported();
static native boolean linuxIsCreationTimeSupported(String file);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -21,17 +21,110 @@
* questions.
*/
#include "jni.h"
#if defined(__linux__)
#include <linux/fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <bits/types.h>
#include <dlfcn.h>
#ifndef STATX_BASIC_STATS
#define STATX_BASIC_STATS 0x000007ffU
#endif
#ifndef STATX_BTIME
#define STATX_BTIME 0x00000800U
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef RTLD_DEFAULT
#define RTLD_DEFAULT RTLD_LOCAL
#endif

/*
* Timestamp structure for the timestamps in struct statx.
*/
struct my_statx_timestamp {
__int64_t tv_sec;
__uint32_t tv_nsec;
__int32_t __reserved;
};

/*
* struct statx used by statx system call on >= glibc 2.28
* systems
*/
struct my_statx
{
__uint32_t stx_mask;
__uint32_t stx_blksize;
__uint64_t stx_attributes;
__uint32_t stx_nlink;
__uint32_t stx_uid;
__uint32_t stx_gid;
__uint16_t stx_mode;
__uint16_t __statx_pad1[1];
__uint64_t stx_ino;
__uint64_t stx_size;
__uint64_t stx_blocks;
__uint64_t stx_attributes_mask;
struct my_statx_timestamp stx_atime;
struct my_statx_timestamp stx_btime;
struct my_statx_timestamp stx_ctime;
struct my_statx_timestamp stx_mtime;
__uint32_t stx_rdev_major;
__uint32_t stx_rdev_minor;
__uint32_t stx_dev_major;
__uint32_t stx_dev_minor;
__uint64_t __statx_pad2[14];
};

#if defined(__linux__)
typedef int statx_func(int dirfd, const char *restrict pathname, int flags,
unsigned int mask, struct my_statx *restrict statxbuf);
#endif
#if defined(__linux__)
static statx_func* my_statx_func = NULL;
#endif

// static native boolean linuxIsCreationTimeSupported()
JNIEXPORT jboolean JNICALL
Java_CreationTimeHelper_linuxIsCreationTimeSupported(JNIEnv *env, jclass cls)
{
Java_CreationTimeHelper_linuxIsCreationTimeSupported(JNIEnv *env, jclass cls, jstring file) {
#if defined(__linux__)
void* statx_func = dlsym(RTLD_DEFAULT, "statx");
return statx_func != NULL ? JNI_TRUE : JNI_FALSE;
struct my_statx stx;
int ret, atflag = AT_SYMLINK_NOFOLLOW;
memset(&stx, 0xbf, sizeof(stx));
unsigned int mask = STATX_BASIC_STATS | STATX_BTIME;

my_statx_func = (statx_func*) dlsym(RTLD_DEFAULT, "statx");
if (my_statx_func == NULL) {
return JNI_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;
}

ret = my_statx_func(AT_FDCWD, utfChars, atflag, mask, &stx);

if (file != NULL && utfChars != NULL) {
(*env)->ReleaseStringUTFChars(env, file, utfChars);
}

#ifdef DEBUG
printf("birth time = %ld\n", stx.stx_btime.tv_sec);
#endif
if (ret != 0) {
return JNI_FALSE;
}
if (stx.stx_mask & STATX_BTIME)
return JNI_TRUE;
return JNI_FALSE;
#else
return JNI_FALSE;
#endif
Expand Down

0 comments on commit 614603e

Please sign in to comment.