Path: blob/master/test/jdk/java/nio/file/attribute/BasicFileAttributeView/SetTimesNanos.java
41155 views
/*1* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 8181493 823117425* @summary Verify that nanosecond precision is maintained for file timestamps26* @requires (os.family == "linux") | (os.family == "mac") | (os.family == "windows")27* @modules java.base/sun.nio.fs:+open28*/2930import java.io.IOException;31import java.lang.reflect.Method;32import java.nio.file.Files;33import java.nio.file.FileStore;34import java.nio.file.Path;35import java.nio.file.attribute.BasicFileAttributes;36import java.nio.file.attribute.BasicFileAttributeView;37import java.nio.file.attribute.FileTime;38import java.util.Set;39import java.util.concurrent.TimeUnit;4041public class SetTimesNanos {42private static final boolean IS_WINDOWS =43System.getProperty("os.name").startsWith("Windows");4445public static void main(String[] args) throws Exception {46if (!IS_WINDOWS) {47// Check whether futimens() system call is supported48Class unixNativeDispatcherClass =49Class.forName("sun.nio.fs.UnixNativeDispatcher");50Method futimensSupported =51unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");52futimensSupported.setAccessible(true);53if (!(boolean)futimensSupported.invoke(null)) {54System.err.println("futimens() not supported; skipping test");55return;56}57}5859Path dirPath = Path.of("test");60Path dir = Files.createDirectory(dirPath);61FileStore store = Files.getFileStore(dir);62System.out.format("FileStore: \"%s\" on %s (%s)%n",63dir, store.name(), store.type());6465Set<String> testedTypes = IS_WINDOWS ?66Set.of("NTFS") : Set.of("apfs", "ext4", "xfs", "zfs");67if (!testedTypes.contains(store.type())) {68System.err.format("%s not in %s; skipping test", store.type(), testedTypes);69return;70}7172testNanos(dir);7374Path file = Files.createFile(dir.resolve("test.dat"));75testNanos(file);76}7778private static void testNanos(Path path) throws IOException {79// Set modification and access times80// Time stamp = "2017-01-01 01:01:01.123456789";81long timeNanos = 1_483_261_261L*1_000_000_000L + 123_456_789L;82FileTime pathTime = FileTime.from(timeNanos, TimeUnit.NANOSECONDS);83BasicFileAttributeView view =84Files.getFileAttributeView(path, BasicFileAttributeView.class);85view.setTimes(pathTime, pathTime, null);8687// Windows file time resolution is 100ns so truncate88if (IS_WINDOWS) {89timeNanos = 100L*(timeNanos/100L);90}9192// Read attributes93BasicFileAttributes attrs =94Files.readAttributes(path, BasicFileAttributes.class);9596// Check timestamps97String[] timeNames = new String[] {"modification", "access"};98FileTime[] times = new FileTime[] {attrs.lastModifiedTime(),99attrs.lastAccessTime()};100for (int i = 0; i < timeNames.length; i++) {101long nanos = times[i].to(TimeUnit.NANOSECONDS);102if (nanos != timeNanos) {103throw new RuntimeException("Expected " + timeNames[i] +104" timestamp to be '" + timeNanos + "', but was '" +105nanos + "'");106}107}108}109}110111112