test/jdk/java/nio/file/attribute/BasicFileAttributeView/SetTimesNanos.java
changeset 58341 21a03fa2f6b6
parent 58024 6eb44470aa98
child 58679 9c3209ff7550
equal deleted inserted replaced
58340:f4abe950c3b0 58341:21a03fa2f6b6
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25  * @bug 8181493
    25  * @bug 8181493 8231174
    26  * @summary Verify that nanosecond precision is maintained for file timestamps
    26  * @summary Verify that nanosecond precision is maintained for file timestamps
    27  * @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris")
    27  * @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris") | (os.family == "windows")
    28  * @modules java.base/sun.nio.fs:+open
    28  * @modules java.base/sun.nio.fs:+open
    29  */
    29  */
    30 
    30 
    31 import java.io.IOException;
    31 import java.io.IOException;
    32 import java.lang.reflect.Method;
    32 import java.lang.reflect.Method;
    38 import java.nio.file.attribute.FileTime;
    38 import java.nio.file.attribute.FileTime;
    39 import java.util.Set;
    39 import java.util.Set;
    40 import java.util.concurrent.TimeUnit;
    40 import java.util.concurrent.TimeUnit;
    41 
    41 
    42 public class SetTimesNanos {
    42 public class SetTimesNanos {
       
    43     private static final boolean IS_WINDOWS =
       
    44         System.getProperty("os.name").startsWith("Windows");
       
    45 
    43     public static void main(String[] args) throws Exception {
    46     public static void main(String[] args) throws Exception {
    44         // Check whether futimens() system call is supported
    47         if (!IS_WINDOWS) {
    45         Class unixNativeDispatcherClass = Class.forName("sun.nio.fs.UnixNativeDispatcher");
    48             // Check whether futimens() system call is supported
    46         Method futimensSupported = unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");
    49             Class unixNativeDispatcherClass =
    47         futimensSupported.setAccessible(true);
    50                 Class.forName("sun.nio.fs.UnixNativeDispatcher");
    48         if (!(boolean)futimensSupported.invoke(null)) {
    51             Method futimensSupported =
    49             System.err.println("futimens() system call not supported; skipping test");
    52                 unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");
    50             return;
    53             futimensSupported.setAccessible(true);
       
    54             if (!(boolean)futimensSupported.invoke(null)) {
       
    55                 System.err.println("futimens() not supported; skipping test");
       
    56                 return;
       
    57             }
    51         }
    58         }
    52 
    59 
    53         Path dirPath = Path.of("test");
    60         Path dirPath = Path.of("test");
    54         Path dir = Files.createDirectory(dirPath);
    61         Path dir = Files.createDirectory(dirPath);
    55         FileStore store = Files.getFileStore(dir);
    62         FileStore store = Files.getFileStore(dir);
    56         System.out.format("FileStore: \"%s\" on %s (%s)%n",
    63         System.out.format("FileStore: \"%s\" on %s (%s)%n",
    57             dir, store.name(), store.type());
    64             dir, store.name(), store.type());
    58 
    65 
    59         Set<String> testedTypes = Set.of("apfs", "ext4", "xfs", "zfs");
    66         Set<String> testedTypes = IS_WINDOWS ?
       
    67             Set.of("NTFS") : Set.of("apfs", "ext4", "xfs", "zfs");
    60         if (!testedTypes.contains(store.type())) {
    68         if (!testedTypes.contains(store.type())) {
    61             System.err.format("%s not in %s; skipping test", store.type(), testedTypes);
    69             System.err.format("%s not in %s; skipping test", store.type(), testedTypes);
    62             return;
    70             return;
    63         }
    71         }
    64 
    72 
    75         FileTime pathTime = FileTime.from(timeNanos, TimeUnit.NANOSECONDS);
    83         FileTime pathTime = FileTime.from(timeNanos, TimeUnit.NANOSECONDS);
    76         BasicFileAttributeView view =
    84         BasicFileAttributeView view =
    77             Files.getFileAttributeView(path, BasicFileAttributeView.class);
    85             Files.getFileAttributeView(path, BasicFileAttributeView.class);
    78         view.setTimes(pathTime, pathTime, null);
    86         view.setTimes(pathTime, pathTime, null);
    79 
    87 
       
    88         // Windows file time resolution is 100ns so truncate
       
    89         if (IS_WINDOWS) {
       
    90             timeNanos = 100L*(timeNanos/100L);
       
    91         }
       
    92 
    80         // Read attributes
    93         // Read attributes
    81         BasicFileAttributes attrs =
    94         BasicFileAttributes attrs =
    82             Files.readAttributes(path, BasicFileAttributes.class);
    95             Files.readAttributes(path, BasicFileAttributes.class);
    83 
    96 
    84         // Check timestamps
    97         // Check timestamps