Skip to content

Commit

Permalink
use jni to determine the tested file support birth time or not
Browse files Browse the repository at this point in the history
  • Loading branch information
sendaoYan committed Sep 3, 2024
1 parent fe3fbc1 commit 78fbf83
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 25 deletions.
2 changes: 2 additions & 0 deletions make/test/JtregNativeJdk.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ ifeq ($(call isTargetOs, linux), true)
# stripping during the test libraries' build.
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libFib := -g
BUILD_JDK_JTREG_LIBRARIES_STRIP_SYMBOLS_libFib := false
# nio tests' libCreationTimeHelper native needs -ldl linker flag
BUILD_JDK_JTREG_LIBRARIES_LDFLAGS_libCreationTimeHelper := -ldl
endif

ifeq ($(ASAN_ENABLED), true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,8 @@ static void test(Path top) throws IOException {
supportsCreationTimeWrite = true;
}
} else if (Platform.isLinux()) {
// Creation time read depends on statx system call support and on the file
// system storing the birth time. The tmpfs/nfs/ext3 etc. file system type does not store
// the birth time, that depends the linux kernel version.
boolean statxIsPresent = Linker.nativeLinker().defaultLookup().find("statx").isPresent();
if (statxIsPresent && supportBirthTimeOnLinux(file)) {
supportsCreationTimeRead = true;
}
// Creation time read depends on statx system call support
supportsCreationTimeRead = CreationTimeHelper.linuxIsCreationTimeSupported();
// Creation time updates are not supported on Linux
supportsCreationTimeWrite = false;
}
Expand Down Expand Up @@ -148,24 +143,6 @@ static void test(Path top) throws IOException {
}
}

/**
* read the output of linux command `stat -c "%w" file`, if the output is "-",
* then the file system doesn't support birth time
*/
public static boolean supportBirthTimeOnLinux(Path file) {
try {
String filePath = file.toAbsolutePath().toString();
ProcessBuilder pb = new ProcessBuilder("stat", "-c", "%w", filePath);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String l = b.readLine();
if (l != null && l.equals("-")) { return false; }
} catch(Exception e) {
}
return true;
}

public static void main(String[] args) throws IOException {
// create temporary directory to run tests
Path dir;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
public class CreationTimeHelper {

static {
System.loadLibrary("CreationTimeHelper");
}

// Helper so as to determine 'statx' support on the runtime system
static native boolean linuxIsCreationTimeSupported();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "jni.h"
#if defined(__linux__)
#include <dlfcn.h>
#endif

// static native boolean linuxIsCreationTimeSupported()
JNIEXPORT jboolean JNICALL
Java_CreationTimeHelper_linuxIsCreationTimeSupported(JNIEnv *env, jclass cls)
{
#if defined(__linux__)
void* statx_func = dlsym(RTLD_DEFAULT, "statx");
return statx_func != NULL ? JNI_TRUE : JNI_FALSE;
#else
return JNI_FALSE;
#endif
}

0 comments on commit 78fbf83

Please sign in to comment.