Skip to content

Commit 12178ec

Browse files
author
Brian Burkhalter
committed
8231174: (fs) FileTime should have 100ns resolution (win)
Reviewed-by: alanb
1 parent 6ccf335 commit 12178ec

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

src/java.base/windows/classes/sun/nio/fs/WindowsFileAttributes.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -108,8 +108,9 @@ class WindowsFileAttributes
108108
private static final short OFFSETOF_FIND_DATA_SIZELOW = 32;
109109
private static final short OFFSETOF_FIND_DATA_RESERVED0 = 36;
110110

111-
// used to adjust values between Windows and java epoch
112-
private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
111+
// used to adjust values between Windows and java epochs
112+
private static final long WINDOWS_EPOCH_IN_MICROS = -11644473600000000L;
113+
private static final long WINDOWS_EPOCH_IN_100NS = -116444736000000000L;
113114

114115
// indicates if accurate metadata is required (interesting on NTFS only)
115116
private static final boolean ensureAccurateMetadata;
@@ -137,24 +138,23 @@ class WindowsFileAttributes
137138
* since January 1, 1601 to a FileTime.
138139
*/
139140
static FileTime toFileTime(long time) {
140-
// 100ns -> us
141-
time /= 10L;
142-
// adjust to java epoch
143-
time += WINDOWS_EPOCH_IN_MICROSECONDS;
144-
return FileTime.from(time, TimeUnit.MICROSECONDS);
141+
try {
142+
long adjusted = Math.addExact(time, WINDOWS_EPOCH_IN_100NS);
143+
long nanos = Math.multiplyExact(adjusted, 100L);
144+
return FileTime.from(nanos, TimeUnit.NANOSECONDS);
145+
} catch (ArithmeticException e) {
146+
long micros = Math.addExact(time/10L, WINDOWS_EPOCH_IN_MICROS);
147+
return FileTime.from(micros, TimeUnit.MICROSECONDS);
148+
}
145149
}
146150

147151
/**
148-
* Convert FileTime to 64-bit value representing the number of 100-nanosecond
149-
* intervals since January 1, 1601.
152+
* Convert FileTime to 64-bit value representing the number of
153+
* 100-nanosecond intervals since January 1, 1601.
150154
*/
151155
static long toWindowsTime(FileTime time) {
152-
long value = time.to(TimeUnit.MICROSECONDS);
153-
// adjust to Windows epoch+= 11644473600000000L;
154-
value -= WINDOWS_EPOCH_IN_MICROSECONDS;
155-
// us -> 100ns
156-
value *= 10L;
157-
return value;
156+
long adjusted = time.to(TimeUnit.NANOSECONDS)/100L;
157+
return adjusted - WINDOWS_EPOCH_IN_100NS;
158158
}
159159

160160
/**

test/jdk/java/nio/file/attribute/BasicFileAttributeView/SetTimesNanos.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323

2424
/* @test
25-
* @bug 8181493
25+
* @bug 8181493 8231174
2626
* @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")
2828
* @modules java.base/sun.nio.fs:+open
2929
*/
3030

@@ -40,14 +40,21 @@
4040
import java.util.concurrent.TimeUnit;
4141

4242
public class SetTimesNanos {
43+
private static final boolean IS_WINDOWS =
44+
System.getProperty("os.name").startsWith("Windows");
45+
4346
public static void main(String[] args) throws Exception {
44-
// Check whether futimens() system call is supported
45-
Class unixNativeDispatcherClass = Class.forName("sun.nio.fs.UnixNativeDispatcher");
46-
Method futimensSupported = unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");
47-
futimensSupported.setAccessible(true);
48-
if (!(boolean)futimensSupported.invoke(null)) {
49-
System.err.println("futimens() system call not supported; skipping test");
50-
return;
47+
if (!IS_WINDOWS) {
48+
// Check whether futimens() system call is supported
49+
Class unixNativeDispatcherClass =
50+
Class.forName("sun.nio.fs.UnixNativeDispatcher");
51+
Method futimensSupported =
52+
unixNativeDispatcherClass.getDeclaredMethod("futimensSupported");
53+
futimensSupported.setAccessible(true);
54+
if (!(boolean)futimensSupported.invoke(null)) {
55+
System.err.println("futimens() not supported; skipping test");
56+
return;
57+
}
5158
}
5259

5360
Path dirPath = Path.of("test");
@@ -56,7 +63,8 @@ public static void main(String[] args) throws Exception {
5663
System.out.format("FileStore: \"%s\" on %s (%s)%n",
5764
dir, store.name(), store.type());
5865

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");
6068
if (!testedTypes.contains(store.type())) {
6169
System.err.format("%s not in %s; skipping test", store.type(), testedTypes);
6270
return;
@@ -77,6 +85,11 @@ private static void testNanos(Path path) throws IOException {
7785
Files.getFileAttributeView(path, BasicFileAttributeView.class);
7886
view.setTimes(pathTime, pathTime, null);
7987

88+
// Windows file time resolution is 100ns so truncate
89+
if (IS_WINDOWS) {
90+
timeNanos = 100L*(timeNanos/100L);
91+
}
92+
8093
// Read attributes
8194
BasicFileAttributes attrs =
8295
Files.readAttributes(path, BasicFileAttributes.class);

0 commit comments

Comments
 (0)