mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8181493: (fs) Files.readAttributes(path, BasicFileAttributes.class) should preserve nano second time stamps
Reviewed-by: alanb, lancea
This commit is contained in:
parent
1ed3649f48
commit
20ee82bf0a
5 changed files with 159 additions and 14 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -143,6 +143,7 @@ typedef int fstatat64_func(int, const char *, struct stat64 *, int);
|
|||
typedef int unlinkat_func(int, const char*, int);
|
||||
typedef int renameat_func(int, const char*, int, const char*);
|
||||
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 DIR* fdopendir_func(int);
|
||||
|
||||
|
@ -151,6 +152,7 @@ static fstatat64_func* my_fstatat64_func = NULL;
|
|||
static unlinkat_func* my_unlinkat_func = NULL;
|
||||
static renameat_func* my_renameat_func = NULL;
|
||||
static futimesat_func* my_futimesat_func = NULL;
|
||||
static futimens_func* my_futimens_func = NULL;
|
||||
static lutimes_func* my_lutimes_func = NULL;
|
||||
static fdopendir_func* my_fdopendir_func = NULL;
|
||||
|
||||
|
@ -275,6 +277,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
|
|||
my_futimesat_func = (futimesat_func*) dlsym(RTLD_DEFAULT, "futimesat");
|
||||
my_lutimes_func = (lutimes_func*) dlsym(RTLD_DEFAULT, "lutimes");
|
||||
#endif
|
||||
my_futimens_func = (futimens_func*) dlsym(RTLD_DEFAULT, "futimens");
|
||||
#if defined(_AIX)
|
||||
my_fdopendir_func = (fdopendir_func*) dlsym(RTLD_DEFAULT, "fdopendir64");
|
||||
#else
|
||||
|
@ -287,7 +290,7 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
|
|||
my_fstatat64_func = (fstatat64_func*)&fstatat64_wrapper;
|
||||
#endif
|
||||
|
||||
/* supports futimes or futimesat and/or lutimes */
|
||||
/* supports futimes or futimesat, futimens, and/or lutimes */
|
||||
|
||||
#ifdef _ALLBSD_SOURCE
|
||||
capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMES;
|
||||
|
@ -298,6 +301,8 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
|
|||
if (my_lutimes_func != NULL)
|
||||
capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_LUTIMES;
|
||||
#endif
|
||||
if (my_futimens_func != NULL)
|
||||
capabilities |= sun_nio_fs_UnixNativeDispatcher_SUPPORTS_FUTIMENS;
|
||||
|
||||
/* supports openat, etc. */
|
||||
|
||||
|
@ -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, ×[0]), err);
|
||||
if (err == -1) {
|
||||
throwUnixException(env, errno);
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_sun_nio_fs_UnixNativeDispatcher_lutimes0(JNIEnv* env, jclass this,
|
||||
jlong pathAddress, jlong accessTime, jlong modificationTime)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue