Skip to content

Commit 81d7b70

Browse files
author
SendaoYan
committed
8341881: [REDO] java/nio/file/attribute/BasicFileAttributeView/CreationTime.java#tmp fails on alinux3
8342145: File libCreationTimeHelper.c compile fails on Alpine Backport-of: c7c7280f6e25fb68950bad93aa20a96cfc9f35b3
1 parent 3bc06ab commit 81d7b70

File tree

3 files changed

+127
-19
lines changed

3 files changed

+127
-19
lines changed

test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -28,7 +29,7 @@
2829
* @library ../.. /test/lib
2930
* @build jdk.test.lib.Platform
3031
* @comment We see this failing with "UnsatisfiedLinkError: Native Library ...libCreationTimeHelper.so already loaded in another classloader". Thus run as othervm
31-
* @run main/othervm CreationTime
32+
* @run main/othervm/native CreationTime
3233
*/
3334

3435
/* @test id=cwd
@@ -37,7 +38,7 @@
3738
* scratch directory maybe at diff disk partition to /tmp on linux.
3839
* @library ../.. /test/lib
3940
* @build jdk.test.lib.Platform
40-
* @run main/native CreationTime .
41+
* @run main/othervm/native CreationTime .
4142
*/
4243

4344
import java.nio.file.Path;
@@ -51,8 +52,6 @@
5152

5253
public class CreationTime {
5354

54-
private static final java.io.PrintStream err = System.err;
55-
5655
/**
5756
* Reads the creationTime attribute
5857
*/
@@ -78,14 +77,9 @@ static void test(Path top) throws IOException {
7877
FileTime creationTime = creationTime(file);
7978
Instant now = Instant.now();
8079
if (Math.abs(creationTime.toMillis()-now.toEpochMilli()) > 10000L) {
81-
System.out.println("creationTime.toMillis() == " + creationTime.toMillis());
82-
// If the file system doesn't support birth time, then skip this test
83-
if (creationTime.toMillis() == 0) {
84-
throw new SkippedException("birth time not support for: " + file);
85-
} else {
86-
err.println("File creation time reported as: " + creationTime);
87-
throw new RuntimeException("Expected to be close to: " + now);
88-
}
80+
System.err.println("creationTime.toMillis() == " + creationTime.toMillis());
81+
System.err.println("File creation time reported as: " + creationTime);
82+
throw new RuntimeException("Expected to be close to: " + now);
8983
}
9084

9185
/**
@@ -104,7 +98,12 @@ static void test(Path top) throws IOException {
10498
}
10599
} else if (Platform.isLinux()) {
106100
// Creation time read depends on statx system call support
107-
supportsCreationTimeRead = CreationTimeHelper.linuxIsCreationTimeSupported();
101+
try {
102+
supportsCreationTimeRead = CreationTimeHelper.
103+
linuxIsCreationTimeSupported(file.toAbsolutePath().toString());
104+
} catch (Throwable e) {
105+
supportsCreationTimeRead = false;
106+
}
108107
// Creation time updates are not supported on Linux
109108
supportsCreationTimeWrite = false;
110109
}
@@ -119,8 +118,11 @@ static void test(Path top) throws IOException {
119118
Instant plusHour = Instant.now().plusSeconds(60L * 60L);
120119
Files.setLastModifiedTime(file, FileTime.from(plusHour));
121120
FileTime current = creationTime(file);
122-
if (!current.equals(creationTime))
121+
if (!current.equals(creationTime)) {
122+
System.err.println("current = " + current);
123+
System.err.println("creationTime = " + creationTime);
123124
throw new RuntimeException("Creation time should not have changed");
125+
}
124126
}
125127

126128
/**

test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTimeHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2023, Red Hat, Inc.
3+
* Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -20,12 +21,13 @@
2021
* or visit www.oracle.com if you need additional information or have any
2122
* questions.
2223
*/
24+
2325
public class CreationTimeHelper {
2426

2527
static {
2628
System.loadLibrary("CreationTimeHelper");
2729
}
2830

2931
// Helper so as to determine 'statx' support on the runtime system
30-
static native boolean linuxIsCreationTimeSupported();
32+
static native boolean linuxIsCreationTimeSupported(String file);
3133
}

test/jdk/java/nio/file/attribute/BasicFileAttributeView/libCreationTimeHelper.c

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2023, Red Hat, Inc.
3+
* Copyright (c) 2024 Alibaba Group Holding Limited. All Rights Reserved.
34
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45
*
56
* This code is free software; you can redistribute it and/or modify it
@@ -21,17 +22,120 @@
2122
* questions.
2223
*/
2324
#include "jni.h"
25+
#include <stdbool.h>
2426
#if defined(__linux__)
27+
#include <stdio.h>
28+
#include <string.h>
29+
#include <sys/types.h>
30+
#include <sys/stat.h>
2531
#include <dlfcn.h>
32+
#ifndef STATX_BASIC_STATS
33+
#define STATX_BASIC_STATS 0x000007ffU
34+
#endif
35+
#ifndef STATX_BTIME
36+
#define STATX_BTIME 0x00000800U
37+
#endif
38+
#ifndef RTLD_DEFAULT
39+
#define RTLD_DEFAULT RTLD_LOCAL
40+
#endif
41+
#ifndef AT_SYMLINK_NOFOLLOW
42+
#define AT_SYMLINK_NOFOLLOW 0x100
43+
#endif
44+
#ifndef AT_FDCWD
45+
#define AT_FDCWD -100
46+
#endif
47+
48+
#ifndef __GLIBC__
49+
// Alpine doesn't know these types, define them
50+
typedef unsigned int __uint32_t;
51+
typedef unsigned short __uint16_t;
52+
typedef unsigned long int __uint64_t;
2653
#endif
2754

55+
/*
56+
* Timestamp structure for the timestamps in struct statx.
57+
*/
58+
struct my_statx_timestamp {
59+
int64_t tv_sec;
60+
__uint32_t tv_nsec;
61+
int32_t __reserved;
62+
};
63+
64+
/*
65+
* struct statx used by statx system call on >= glibc 2.28
66+
* systems
67+
*/
68+
struct my_statx
69+
{
70+
__uint32_t stx_mask;
71+
__uint32_t stx_blksize;
72+
__uint64_t stx_attributes;
73+
__uint32_t stx_nlink;
74+
__uint32_t stx_uid;
75+
__uint32_t stx_gid;
76+
__uint16_t stx_mode;
77+
__uint16_t __statx_pad1[1];
78+
__uint64_t stx_ino;
79+
__uint64_t stx_size;
80+
__uint64_t stx_blocks;
81+
__uint64_t stx_attributes_mask;
82+
struct my_statx_timestamp stx_atime;
83+
struct my_statx_timestamp stx_btime;
84+
struct my_statx_timestamp stx_ctime;
85+
struct my_statx_timestamp stx_mtime;
86+
__uint32_t stx_rdev_major;
87+
__uint32_t stx_rdev_minor;
88+
__uint32_t stx_dev_major;
89+
__uint32_t stx_dev_minor;
90+
__uint64_t __statx_pad2[14];
91+
};
92+
93+
typedef int statx_func(int dirfd, const char *restrict pathname, int flags,
94+
unsigned int mask, struct my_statx *restrict statxbuf);
95+
96+
static statx_func* my_statx_func = NULL;
97+
#endif //#defined(__linux__)
98+
2899
// static native boolean linuxIsCreationTimeSupported()
29100
JNIEXPORT jboolean JNICALL
30-
Java_CreationTimeHelper_linuxIsCreationTimeSupported(JNIEnv *env, jclass cls)
31-
{
101+
Java_CreationTimeHelper_linuxIsCreationTimeSupported(JNIEnv *env, jclass cls, jstring file) {
32102
#if defined(__linux__)
33-
void* statx_func = dlsym(RTLD_DEFAULT, "statx");
34-
return statx_func != NULL ? JNI_TRUE : JNI_FALSE;
103+
struct my_statx stx = {0};
104+
int ret, atflag = AT_SYMLINK_NOFOLLOW;
105+
unsigned int mask = STATX_BASIC_STATS | STATX_BTIME;
106+
107+
my_statx_func = (statx_func*) dlsym(RTLD_DEFAULT, "statx");
108+
if (my_statx_func == NULL) {
109+
return false;
110+
}
111+
112+
if (file == NULL) {
113+
printf("input file error!\n");
114+
return JNI_FALSE;
115+
}
116+
const char *utfChars = (*env)->GetStringUTFChars(env, file, NULL);
117+
if (utfChars == NULL) {
118+
printf("jstring convert to char array error!\n");
119+
return JNI_FALSE;
120+
}
121+
122+
ret = my_statx_func(AT_FDCWD, utfChars, atflag, mask, &stx);
123+
124+
if (file != NULL && utfChars != NULL) {
125+
(*env)->ReleaseStringUTFChars(env, file, utfChars);
126+
}
127+
128+
if (ret != 0) {
129+
return JNI_FALSE;
130+
}
131+
132+
// On some systems where statx is available but birth time might still not
133+
// be supported as it's file system specific. The only reliable way to
134+
// check for supported or not is looking at the filled in STATX_BTIME bit
135+
// in the returned statx buffer mask.
136+
if ((stx.stx_mask & STATX_BTIME) != 0)
137+
return JNI_TRUE;
138+
return JNI_FALSE;
35139
#else
36140
return JNI_FALSE;
37141
#endif

0 commit comments

Comments
 (0)