diff --git a/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java b/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java index ecb54a5879450..9f1c12c8e2634 100644 --- a/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java +++ b/test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java @@ -44,7 +44,9 @@ 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; @@ -107,12 +109,10 @@ static void test(Path top) throws IOException { } } 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 etc. file system type does not store - // the birth time. + // 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 && - !Files.getFileStore(file).type().contentEquals("tmpfs") && - !Files.getFileStore(file).type().contains("nfs")) { + if (statxIsPresent && supportBirthTimeOnLinux(file)) { supportsCreationTimeRead = true; } // Creation time updates are not supported on Linux @@ -148,6 +148,24 @@ 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;