mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 12:04:39 +02:00
8231174: (fs) FileTime should have 100ns resolution (win)
Reviewed-by: alanb
This commit is contained in:
parent
6ccf3351d7
commit
12178ec008
2 changed files with 39 additions and 26 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -108,8 +108,9 @@ class WindowsFileAttributes
|
||||||
private static final short OFFSETOF_FIND_DATA_SIZELOW = 32;
|
private static final short OFFSETOF_FIND_DATA_SIZELOW = 32;
|
||||||
private static final short OFFSETOF_FIND_DATA_RESERVED0 = 36;
|
private static final short OFFSETOF_FIND_DATA_RESERVED0 = 36;
|
||||||
|
|
||||||
// used to adjust values between Windows and java epoch
|
// used to adjust values between Windows and java epochs
|
||||||
private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
|
private static final long WINDOWS_EPOCH_IN_MICROS = -11644473600000000L;
|
||||||
|
private static final long WINDOWS_EPOCH_IN_100NS = -116444736000000000L;
|
||||||
|
|
||||||
// indicates if accurate metadata is required (interesting on NTFS only)
|
// indicates if accurate metadata is required (interesting on NTFS only)
|
||||||
private static final boolean ensureAccurateMetadata;
|
private static final boolean ensureAccurateMetadata;
|
||||||
|
@ -137,24 +138,23 @@ class WindowsFileAttributes
|
||||||
* since January 1, 1601 to a FileTime.
|
* since January 1, 1601 to a FileTime.
|
||||||
*/
|
*/
|
||||||
static FileTime toFileTime(long time) {
|
static FileTime toFileTime(long time) {
|
||||||
// 100ns -> us
|
try {
|
||||||
time /= 10L;
|
long adjusted = Math.addExact(time, WINDOWS_EPOCH_IN_100NS);
|
||||||
// adjust to java epoch
|
long nanos = Math.multiplyExact(adjusted, 100L);
|
||||||
time += WINDOWS_EPOCH_IN_MICROSECONDS;
|
return FileTime.from(nanos, TimeUnit.NANOSECONDS);
|
||||||
return FileTime.from(time, TimeUnit.MICROSECONDS);
|
} catch (ArithmeticException e) {
|
||||||
|
long micros = Math.addExact(time/10L, WINDOWS_EPOCH_IN_MICROS);
|
||||||
|
return FileTime.from(micros, TimeUnit.MICROSECONDS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert FileTime to 64-bit value representing the number of 100-nanosecond
|
* Convert FileTime to 64-bit value representing the number of
|
||||||
* intervals since January 1, 1601.
|
* 100-nanosecond intervals since January 1, 1601.
|
||||||
*/
|
*/
|
||||||
static long toWindowsTime(FileTime time) {
|
static long toWindowsTime(FileTime time) {
|
||||||
long value = time.to(TimeUnit.MICROSECONDS);
|
long adjusted = time.to(TimeUnit.NANOSECONDS)/100L;
|
||||||
// adjust to Windows epoch+= 11644473600000000L;
|
return adjusted - WINDOWS_EPOCH_IN_100NS;
|
||||||
value -= WINDOWS_EPOCH_IN_MICROSECONDS;
|
|
||||||
// us -> 100ns
|
|
||||||
value *= 10L;
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -22,9 +22,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* @test
|
/* @test
|
||||||
* @bug 8181493
|
* @bug 8181493 8231174
|
||||||
* @summary Verify that nanosecond precision is maintained for file timestamps
|
* @summary Verify that nanosecond precision is maintained for file timestamps
|
||||||
* @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris")
|
* @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris") | (os.family == "windows")
|
||||||
* @modules java.base/sun.nio.fs:+open
|
* @modules java.base/sun.nio.fs:+open
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -40,15 +40,22 @@ import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class SetTimesNanos {
|
public class SetTimesNanos {
|
||||||
|
private static final boolean IS_WINDOWS =
|
||||||
|
System.getProperty("os.name").startsWith("Windows");
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
if (!IS_WINDOWS) {
|
||||||
// Check whether futimens() system call is supported
|
// Check whether futimens() system call is supported
|
||||||
Class unixNativeDispatcherClass = Class.forName("sun.nio.fs.UnixNativeDispatcher");
|
Class unixNativeDispatcherClass =
|
||||||
Method futimensSupported = unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");
|
Class.forName("sun.nio.fs.UnixNativeDispatcher");
|
||||||
|
Method futimensSupported =
|
||||||
|
unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");
|
||||||
futimensSupported.setAccessible(true);
|
futimensSupported.setAccessible(true);
|
||||||
if (!(boolean)futimensSupported.invoke(null)) {
|
if (!(boolean)futimensSupported.invoke(null)) {
|
||||||
System.err.println("futimens() system call not supported; skipping test");
|
System.err.println("futimens() not supported; skipping test");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Path dirPath = Path.of("test");
|
Path dirPath = Path.of("test");
|
||||||
Path dir = Files.createDirectory(dirPath);
|
Path dir = Files.createDirectory(dirPath);
|
||||||
|
@ -56,7 +63,8 @@ public class SetTimesNanos {
|
||||||
System.out.format("FileStore: \"%s\" on %s (%s)%n",
|
System.out.format("FileStore: \"%s\" on %s (%s)%n",
|
||||||
dir, store.name(), store.type());
|
dir, store.name(), store.type());
|
||||||
|
|
||||||
Set<String> testedTypes = Set.of("apfs", "ext4", "xfs", "zfs");
|
Set<String> testedTypes = IS_WINDOWS ?
|
||||||
|
Set.of("NTFS") : Set.of("apfs", "ext4", "xfs", "zfs");
|
||||||
if (!testedTypes.contains(store.type())) {
|
if (!testedTypes.contains(store.type())) {
|
||||||
System.err.format("%s not in %s; skipping test", store.type(), testedTypes);
|
System.err.format("%s not in %s; skipping test", store.type(), testedTypes);
|
||||||
return;
|
return;
|
||||||
|
@ -77,6 +85,11 @@ public class SetTimesNanos {
|
||||||
Files.getFileAttributeView(path, BasicFileAttributeView.class);
|
Files.getFileAttributeView(path, BasicFileAttributeView.class);
|
||||||
view.setTimes(pathTime, pathTime, null);
|
view.setTimes(pathTime, pathTime, null);
|
||||||
|
|
||||||
|
// Windows file time resolution is 100ns so truncate
|
||||||
|
if (IS_WINDOWS) {
|
||||||
|
timeNanos = 100L*(timeNanos/100L);
|
||||||
|
}
|
||||||
|
|
||||||
// Read attributes
|
// Read attributes
|
||||||
BasicFileAttributes attrs =
|
BasicFileAttributes attrs =
|
||||||
Files.readAttributes(path, BasicFileAttributes.class);
|
Files.readAttributes(path, BasicFileAttributes.class);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue