Skip to content
Snippets Groups Projects
Commit 20ee82bf authored by Brian Burkhalter's avatar Brian Burkhalter
Browse files

8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should...

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

Reviewed-by: alanb, lancea
parent 1ed3649f
No related branches found
No related tags found
No related merge requests found
...@@ -73,6 +73,7 @@ class UnixFileAttributeViews { ...@@ -73,6 +73,7 @@ class UnixFileAttributeViews {
boolean haveFd = false; boolean haveFd = false;
boolean useFutimes = false; boolean useFutimes = false;
boolean useFutimens = false;
boolean useLutimes = false; boolean useLutimes = false;
int fd = -1; int fd = -1;
try { try {
...@@ -84,9 +85,11 @@ class UnixFileAttributeViews { ...@@ -84,9 +85,11 @@ class UnixFileAttributeViews {
fd = file.openForAttributeAccess(followLinks); fd = file.openForAttributeAccess(followLinks);
if (fd != -1) { if (fd != -1) {
haveFd = true; haveFd = true;
if (!(useFutimens = futimensSupported())) {
useFutimes = futimesSupported(); useFutimes = futimesSupported();
} }
} }
}
} catch (UnixException x) { } catch (UnixException x) {
if (!(x.errno() == UnixConstants.ENXIO || if (!(x.errno() == UnixConstants.ENXIO ||
(x.errno() == UnixConstants.ELOOP && useLutimes))) { (x.errno() == UnixConstants.ELOOP && useLutimes))) {
...@@ -112,13 +115,17 @@ class UnixFileAttributeViews { ...@@ -112,13 +115,17 @@ class UnixFileAttributeViews {
} }
} }
// uptime times // update times
long modValue = lastModifiedTime.to(TimeUnit.MICROSECONDS); TimeUnit timeUnit = useFutimens ?
long accessValue= lastAccessTime.to(TimeUnit.MICROSECONDS); TimeUnit.NANOSECONDS : TimeUnit.MICROSECONDS;
long modValue = lastModifiedTime.to(timeUnit);
long accessValue= lastAccessTime.to(timeUnit);
boolean retry = false; boolean retry = false;
try { try {
if (useFutimes) { if (useFutimens) {
futimens(fd, accessValue, modValue);
} else if (useFutimes) {
futimes(fd, accessValue, modValue); futimes(fd, accessValue, modValue);
} else if (useLutimes) { } else if (useLutimes) {
lutimes(file, accessValue, modValue); lutimes(file, accessValue, modValue);
...@@ -139,7 +146,9 @@ class UnixFileAttributeViews { ...@@ -139,7 +146,9 @@ class UnixFileAttributeViews {
if (modValue < 0L) modValue = 0L; if (modValue < 0L) modValue = 0L;
if (accessValue < 0L) accessValue= 0L; if (accessValue < 0L) accessValue= 0L;
try { try {
if (useFutimes) { if (useFutimens) {
futimens(fd, accessValue, modValue);
} else if (useFutimes) {
futimes(fd, accessValue, modValue); futimes(fd, accessValue, modValue);
} else if (useLutimes) { } else if (useLutimes) {
lutimes(file, accessValue, modValue); lutimes(file, accessValue, modValue);
......
/* /*
* 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. * 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
...@@ -109,13 +109,17 @@ class UnixFileAttributes ...@@ -109,13 +109,17 @@ class UnixFileAttributes
if (nsec == 0) { if (nsec == 0) {
return FileTime.from(sec, TimeUnit.SECONDS); return FileTime.from(sec, TimeUnit.SECONDS);
} else { } else {
// truncate to microseconds to avoid overflow with timestamps try {
// way out into the future. We can re-visit this if FileTime long nanos = Math.addExact(nsec,
// is updated to define a from(secs,nsecs) method. Math.multiplyExact(sec, 1_000_000_000L));
long micro = sec*1000000L + nsec/1000L; 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); return FileTime.from(micro, TimeUnit.MICROSECONDS);
} }
} }
}
FileTime ctime() { FileTime ctime() {
return toFileTime(st_ctime_sec, st_ctime_nsec); return toFileTime(st_ctime_sec, st_ctime_nsec);
......
...@@ -418,6 +418,11 @@ class UnixNativeDispatcher { ...@@ -418,6 +418,11 @@ class UnixNativeDispatcher {
*/ */
static native void futimes(int fd, long times0, long times1) throws UnixException; 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]) * lutimes(const char* path, const struct timeval times[2])
*/ */
...@@ -593,7 +598,8 @@ class UnixNativeDispatcher { ...@@ -593,7 +598,8 @@ class UnixNativeDispatcher {
*/ */
private static final int SUPPORTS_OPENAT = 1 << 1; // syscalls private static final int SUPPORTS_OPENAT = 1 << 1; // syscalls
private static final int SUPPORTS_FUTIMES = 1 << 2; 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 SUPPORTS_BIRTHTIME = 1 << 16; // other features
private static final int capabilities; private static final int capabilities;
...@@ -611,6 +617,13 @@ class UnixNativeDispatcher { ...@@ -611,6 +617,13 @@ class UnixNativeDispatcher {
return (capabilities & SUPPORTS_FUTIMES) != 0; return (capabilities & SUPPORTS_FUTIMES) != 0;
} }
/**
* Supports futimens
*/
static boolean futimensSupported() {
return (capabilities & SUPPORTS_FUTIMENS) != 0;
}
/** /**
* Supports lutimes * Supports lutimes
*/ */
......
...@@ -143,6 +143,7 @@ typedef int fstatat64_func(int, const char *, struct stat64 *, int); ...@@ -143,6 +143,7 @@ typedef int fstatat64_func(int, const char *, struct stat64 *, int);
typedef int unlinkat_func(int, const char*, int); typedef int unlinkat_func(int, const char*, int);
typedef int renameat_func(int, const char*, int, const char*); typedef int renameat_func(int, const char*, int, const char*);
typedef int futimesat_func(int, const char *, const struct timeval *); typedef int futimesat_func(int, const char *, const struct timeval *);
typedef int futimens_func(int, const struct timespec *);
typedef int lutimes_func(const char *, const struct timeval *); typedef int lutimes_func(const char *, const struct timeval *);
typedef DIR* fdopendir_func(int); typedef DIR* fdopendir_func(int);
...@@ -151,6 +152,7 @@ static fstatat64_func* my_fstatat64_func = NULL; ...@@ -151,6 +152,7 @@ static fstatat64_func* my_fstatat64_func = NULL;
static unlinkat_func* my_unlinkat_func = NULL; static unlinkat_func* my_unlinkat_func = NULL;
static renameat_func* my_renameat_func = NULL; static renameat_func* my_renameat_func = NULL;
static futimesat_func* my_futimesat_func = NULL; static futimesat_func* my_futimesat_func = NULL;
static futimens_func* my_futimens_func = NULL;
static lutimes_func* my_lutimes_func = NULL; static lutimes_func* my_lutimes_func = NULL;
static fdopendir_func* my_fdopendir_func = NULL; static fdopendir_func* my_fdopendir_func = NULL;
...@@ -275,6 +277,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this) ...@@ -275,6 +277,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
my_futimesat_func = (futimesat_func*) dlsym(RTLD_DEFAULT, "futimesat"); my_futimesat_func = (futimesat_func*) dlsym(RTLD_DEFAULT, "futimesat");
my_lutimes_func = (lutimes_func*) dlsym(RTLD_DEFAULT, "lutimes"); my_lutimes_func = (lutimes_func*) dlsym(RTLD_DEFAULT, "lutimes");
#endif #endif
my_futimens_func = (futimens_func*) dlsym(RTLD_DEFAULT, "futimens");
#if defined(_AIX) #if defined(_AIX)
my_fdopendir_func = (fdopendir_func*) dlsym(RTLD_DEFAULT, "fdopendir64"); my_fdopendir_func = (fdopendir_func*) dlsym(RTLD_DEFAULT, "fdopendir64");
#else #else
...@@ -287,7 +290,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this) ...@@ -287,7 +290,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
my_fstatat64_func = (fstatat64_func*)&fstatat64_wrapper; my_fstatat64_func = (fstatat64_func*)&fstatat64_wrapper;
#endif #endif
/* supports futimes or futimesat and/or lutimes */ /* supports futimes or futimesat, futimens, and/or lutimes */
#ifdef _ALLBSD_SOURCE #ifdef _ALLBSD_SOURCE
capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES; capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES;
...@@ -298,6 +301,8 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this) ...@@ -298,6 +301,8 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
if (my_lutimes_func != NULL) if (my_lutimes_func != NULL)
capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_LUTIMES; capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_LUTIMES;
#endif #endif
if (my_futimens_func != NULL)
capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMENS;
/* supports openat, etc. */ /* supports openat, etc. */
...@@ -693,6 +698,29 @@ Java_sun_nio_fs_UnixNativeDispatcher_futimes(JNIEnv* env, jclass this, jint file ...@@ -693,6 +698,29 @@ Java_sun_nio_fs_UnixNativeDispatcher_futimes(JNIEnv* env, jclass this, jint file
} }
} }
JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_futimens(JNIEnv* env, jclass this, jint filedes,
jlong accessTime, jlong modificationTime)
{
struct timespec times[2];
int err = 0;
times[0].tv_sec = accessTime / 1000000000;
times[0].tv_nsec = accessTime % 1000000000;
times[1].tv_sec = modificationTime / 1000000000;
times[1].tv_nsec = modificationTime % 1000000000;
if (my_futimens_func == NULL) {
JNU_ThrowInternalError(env, "my_futimens_func is NULL");
return;
}
RESTARTABLE((*my_futimens_func)(filedes, &times[0]), err);
if (err == -1) {
throwUnixException(env, errno);
}
}
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_lutimes0(JNIEnv* env, jclass this, Java_sun_nio_fs_UnixNativeDispatcher_lutimes0(JNIEnv* env, jclass this,
jlong pathAddress, jlong accessTime, jlong modificationTime) jlong pathAddress, jlong accessTime, jlong modificationTime)
......
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8181493
* @summary Verify that nanosecond precision is maintained for file timestamps
* @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris")
*/
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.FileStore;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
public class SetTimesNanos {
public static void main(String[] args) throws IOException,
InterruptedException {
Path dirPath = Path.of("test");
Path dir = Files.createDirectory(dirPath);
FileStore store = Files.getFileStore(dir);
System.out.format("FileStore: %s on %s (%s)%n", dir, store.name(),
store.type());
if (System.getProperty("os.name").toLowerCase().startsWith("mac") &&
store.type().equalsIgnoreCase("hfs")) {
System.err.println
("HFS on macOS does not have nsec timestamps: skipping test");
return;
}
testNanos(dir);
Path file = Files.createFile(dir.resolve("test.dat"));
testNanos(file);
}
private static void testNanos(Path path) throws IOException {
// Set modification and access times
// Time stamp = "2017-01-01 01:01:01.123456789";
long timeNanos = 1_483_261_261L*1_000_000_000L + 123_456_789L;
FileTime pathTime = FileTime.from(timeNanos, TimeUnit.NANOSECONDS);
BasicFileAttributeView view =
Files.getFileAttributeView(path, BasicFileAttributeView.class);
view.setTimes(pathTime, pathTime, null);
// Read attributes
BasicFileAttributes attrs =
Files.readAttributes(path, BasicFileAttributes.class);
// Check timestamps
String[] timeNames = new String[] {"modification", "access"};
FileTime[] times = new FileTime[] {attrs.lastModifiedTime(),
attrs.lastAccessTime()};
for (int i = 0; i < timeNames.length; i++) {
long nanos = times[i].to(TimeUnit.NANOSECONDS);
if (nanos != timeNanos) {
throw new RuntimeException("Expected " + timeNames[i] +
" timestamp to be '" + timeNanos + "', but was '" +
nanos + "'");
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment