8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps

Reviewed-by: alanb, lancea
This commit is contained in:
Brian Burkhalter 2019-07-29 08:48:52 -07:00
parent 1ed3649f48
commit 20ee82bf0a
5 changed files with 159 additions and 14 deletions

View file

@ -73,6 +73,7 @@ class UnixFileAttributeViews {
boolean haveFd = false;
boolean useFutimes = false;
boolean useFutimens = false;
boolean useLutimes = false;
int fd = -1;
try {
@ -84,7 +85,9 @@ class UnixFileAttributeViews {
fd = file.openForAttributeAccess(followLinks);
if (fd != -1) {
haveFd = true;
useFutimes = futimesSupported();
if (!(useFutimens = futimensSupported())) {
useFutimes = futimesSupported();
}
}
}
} catch (UnixException x) {
@ -112,13 +115,17 @@ class UnixFileAttributeViews {
}
}
// uptime times
long modValue = lastModifiedTime.to(TimeUnit.MICROSECONDS);
long accessValue= lastAccessTime.to(TimeUnit.MICROSECONDS);
// update times
TimeUnit timeUnit = useFutimens ?
TimeUnit.NANOSECONDS : TimeUnit.MICROSECONDS;
long modValue = lastModifiedTime.to(timeUnit);
long accessValue= lastAccessTime.to(timeUnit);
boolean retry = false;
try {
if (useFutimes) {
if (useFutimens) {
futimens(fd, accessValue, modValue);
} else if (useFutimes) {
futimes(fd, accessValue, modValue);
} else if (useLutimes) {
lutimes(file, accessValue, modValue);
@ -139,7 +146,9 @@ class UnixFileAttributeViews {
if (modValue < 0L) modValue = 0L;
if (accessValue < 0L) accessValue= 0L;
try {
if (useFutimes) {
if (useFutimens) {
futimens(fd, accessValue, modValue);
} else if (useFutimes) {
futimes(fd, accessValue, modValue);
} else if (useLutimes) {
lutimes(file, accessValue, modValue);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, 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.
*
* This code is free software; you can redistribute it and/or modify it
@ -109,11 +109,15 @@ class UnixFileAttributes
if (nsec == 0) {
return FileTime.from(sec, TimeUnit.SECONDS);
} else {
// truncate to microseconds to avoid overflow with timestamps
// way out into the future. We can re-visit this if FileTime
// is updated to define a from(secs,nsecs) method.
long micro = sec*1000000L + nsec/1000L;
return FileTime.from(micro, TimeUnit.MICROSECONDS);
try {
long nanos = Math.addExact(nsec,
Math.multiplyExact(sec, 1_000_000_000L));
return FileTime.from(nanos, TimeUnit.NANOSECONDS);
} catch (ArithmeticException ignore) {
// truncate to microseconds if nanoseconds overflow
long micro = sec*1_000_000L + nsec/1_000L;
return FileTime.from(micro, TimeUnit.MICROSECONDS);
}
}
}

View file

@ -418,6 +418,11 @@ class UnixNativeDispatcher {
*/
static native void futimes(int fd, long times0, long times1) throws UnixException;
/**
* futimens(int fildes, const struct timespec times[2])
*/
static native void futimens(int fd, long times0, long times1) throws UnixException;
/**
* lutimes(const char* path, const struct timeval times[2])
*/
@ -593,7 +598,8 @@ class UnixNativeDispatcher {
*/
private static final int SUPPORTS_OPENAT = 1 << 1; // syscalls
private static final int SUPPORTS_FUTIMES = 1 << 2;
private static final int SUPPORTS_LUTIMES = 1 << 4;
private static final int SUPPORTS_FUTIMENS = 1 << 4;
private static final int SUPPORTS_LUTIMES = 1 << 8;
private static final int SUPPORTS_BIRTHTIME = 1 << 16; // other features
private static final int capabilities;
@ -611,6 +617,13 @@ class UnixNativeDispatcher {
return (capabilities & SUPPORTS_FUTIMES) != 0;
}
/**
* Supports futimens
*/
static boolean futimensSupported() {
return (capabilities & SUPPORTS_FUTIMENS) != 0;
}
/**
* Supports lutimes
*/