Fix inconsistent behavior of java.nio.file.attribute.BasicFileAttributes.lastModifiedTime()
Schaef, Martin
schaef at amazon.com
Fri Nov 24 14:53:02 UTC 2017
We are experiencing a problem with the following test case:
https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java#L364
Depending on which gcc version we use to compile OpenJDK, the assertion in https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java#L397 fails.
That is, the methods java.nio.file.attribute.BasicFileAttributes.lastModifiedTime()
and java.io.File.lastModified() return results with a different precision.
We root-caused it to the following code block in UnixNativeDispatcher.c:
http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/07011844584f/src/solaris/native/sun/nio/fs/UnixNativeDispatcher.c#l456
The test case at the bottom of this email can be used to reproduce the behavior. When using a JDK compiled with gcc 4.1.2, the test prints:
nio 1511387180000
io 1511387180000
But when using a JDK compiled with gcc 4.4.6, the code returns:
nio 1511387187817
io 1511387187000
Exception in thread "main" java.lang.RuntimeException: 1511387187817 != 1511387187000
at Issue225.testLastModified(Issue225.java:33)
at Issue225.main(Issue225.java:11)
In comparison, the OracleJDK binaries as downloaded from the website behave like the gcc 4.1.2 compiled binaries. To avoid confusion, and to ensure that both methods behave the same, we propose to remove the block in UnixNativeDispatcher.c (see attached patch).
Cheers,
Martin
### Test code to witness the behavior difference ###
import java.io.File;
import java.io.IOException;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
public class Issue225 {
public static void main(String[] args) throws IOException {
(new Issue225()).testLastModified();
}
public void testLastModified() throws IOException {
File file = File.createTempFile("Test", ".txt");
file.deleteOnExit();
Path nioPath = file.toPath();
BasicFileAttributes readAttributes = nioPath
.getFileSystem().provider()
.getFileAttributeView(nioPath,
BasicFileAttributeView.class,
LinkOption.NOFOLLOW_LINKS
).readAttributes();
System.out.println(readAttributes.getClass());
System.out.println("nio " + readAttributes.lastModifiedTime().toMillis());
System.out.println("io " + file.lastModified() );
if (readAttributes.lastModifiedTime().toMillis() != file.lastModified()) {
throw new RuntimeException(readAttributes.lastModifiedTime().toMillis()
+ " != "
+ file.lastModified());
}
}
}
More information about the core-libs-dev
mailing list