test/jdk/java/nio/file/attribute/BasicFileAttributeView/SetTimesNanos.java
author bpb
Wed, 25 Sep 2019 11:44:52 -0700
changeset 58341 21a03fa2f6b6
parent 58024 6eb44470aa98
child 58679 9c3209ff7550
permissions -rw-r--r--
8231174: (fs) FileTime should have 100ns resolution (win) Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     1
/*
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     4
 *
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     8
 *
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    13
 * accompanied this code).
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    14
 *
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    18
 *
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    21
 * questions.
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    22
 */
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    23
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    24
/* @test
58341
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    25
 * @bug 8181493 8231174
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    26
 * @summary Verify that nanosecond precision is maintained for file timestamps
58341
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    27
 * @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris") | (os.family == "windows")
58024
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    28
 * @modules java.base/sun.nio.fs:+open
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    29
 */
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    30
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    31
import java.io.IOException;
58024
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    32
import java.lang.reflect.Method;
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    33
import java.nio.file.Files;
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    34
import java.nio.file.FileStore;
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    35
import java.nio.file.Path;
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    36
import java.nio.file.attribute.BasicFileAttributes;
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    37
import java.nio.file.attribute.BasicFileAttributeView;
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    38
import java.nio.file.attribute.FileTime;
58024
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    39
import java.util.Set;
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    40
import java.util.concurrent.TimeUnit;
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    41
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    42
public class SetTimesNanos {
58341
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    43
    private static final boolean IS_WINDOWS =
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    44
        System.getProperty("os.name").startsWith("Windows");
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    45
58024
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    46
    public static void main(String[] args) throws Exception {
58341
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    47
        if (!IS_WINDOWS) {
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    48
            // Check whether futimens() system call is supported
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    49
            Class unixNativeDispatcherClass =
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    50
                Class.forName("sun.nio.fs.UnixNativeDispatcher");
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    51
            Method futimensSupported =
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    52
                unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    53
            futimensSupported.setAccessible(true);
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    54
            if (!(boolean)futimensSupported.invoke(null)) {
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    55
                System.err.println("futimens() not supported; skipping test");
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    56
                return;
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    57
            }
58024
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    58
        }
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    59
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    60
        Path dirPath = Path.of("test");
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    61
        Path dir = Files.createDirectory(dirPath);
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    62
        FileStore store = Files.getFileStore(dir);
58024
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    63
        System.out.format("FileStore: \"%s\" on %s (%s)%n",
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    64
            dir, store.name(), store.type());
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    65
58341
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    66
        Set<String> testedTypes = IS_WINDOWS ?
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    67
            Set.of("NTFS") : Set.of("apfs", "ext4", "xfs", "zfs");
58024
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    68
        if (!testedTypes.contains(store.type())) {
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    69
            System.err.format("%s not in %s; skipping test", store.type(), testedTypes);
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    70
            return;
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    71
        }
58024
6eb44470aa98 8229280: Test failures on several linux hosts after JDK-8181493
bpb
parents: 57568
diff changeset
    72
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    73
        testNanos(dir);
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    74
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    75
        Path file = Files.createFile(dir.resolve("test.dat"));
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    76
        testNanos(file);
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    77
    }
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    78
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    79
    private static void testNanos(Path path) throws IOException {
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    80
        // Set modification and access times
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    81
        // Time stamp = "2017-01-01 01:01:01.123456789";
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    82
        long timeNanos = 1_483_261_261L*1_000_000_000L + 123_456_789L;
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    83
        FileTime pathTime = FileTime.from(timeNanos, TimeUnit.NANOSECONDS);
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    84
        BasicFileAttributeView view =
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    85
            Files.getFileAttributeView(path, BasicFileAttributeView.class);
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    86
        view.setTimes(pathTime, pathTime, null);
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    87
58341
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    88
        // Windows file time resolution is 100ns so truncate
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    89
        if (IS_WINDOWS) {
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    90
            timeNanos = 100L*(timeNanos/100L);
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    91
        }
21a03fa2f6b6 8231174: (fs) FileTime should have 100ns resolution (win)
bpb
parents: 58024
diff changeset
    92
57568
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    93
        // Read attributes
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    94
        BasicFileAttributes attrs =
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    95
            Files.readAttributes(path, BasicFileAttributes.class);
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    96
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    97
        // Check timestamps
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    98
        String[] timeNames = new String[] {"modification", "access"};
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
    99
        FileTime[] times = new FileTime[] {attrs.lastModifiedTime(),
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   100
            attrs.lastAccessTime()};
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   101
        for (int i = 0; i < timeNames.length; i++) {
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   102
            long nanos = times[i].to(TimeUnit.NANOSECONDS);
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   103
            if (nanos != timeNanos) {
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   104
                throw new RuntimeException("Expected " + timeNames[i] +
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   105
                    " timestamp to be '" + timeNanos + "', but was '" +
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   106
                    nanos + "'");
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   107
            }
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   108
        }
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   109
    }
460ac76019f4 8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
bpb
parents:
diff changeset
   110
}