6939260: (fs) BasicFileAttributes.lastModifiedTime() should return last modified time with higher precision
Reviewed-by: chegar
This commit is contained in:
parent
9bad85cb03
commit
69b0c6aad4
@ -45,9 +45,12 @@ class UnixFileAttributes
|
|||||||
private int st_uid;
|
private int st_uid;
|
||||||
private int st_gid;
|
private int st_gid;
|
||||||
private long st_size;
|
private long st_size;
|
||||||
private long st_atime;
|
private long st_atime_sec;
|
||||||
private long st_mtime;
|
private long st_atime_nsec;
|
||||||
private long st_ctime;
|
private long st_mtime_sec;
|
||||||
|
private long st_mtime_nsec;
|
||||||
|
private long st_ctime_sec;
|
||||||
|
private long st_ctime_nsec;
|
||||||
|
|
||||||
// created lazily
|
// created lazily
|
||||||
private volatile UserPrincipal owner;
|
private volatile UserPrincipal owner;
|
||||||
@ -101,8 +104,20 @@ class UnixFileAttributes
|
|||||||
int uid() { return st_uid; }
|
int uid() { return st_uid; }
|
||||||
int gid() { return st_gid; }
|
int gid() { return st_gid; }
|
||||||
|
|
||||||
|
private static FileTime toFileTime(long sec, long nsec) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
FileTime ctime() {
|
FileTime ctime() {
|
||||||
return FileTime.from(st_ctime, TimeUnit.SECONDS);
|
return toFileTime(st_ctime_sec, st_ctime_nsec);
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean isDevice() {
|
boolean isDevice() {
|
||||||
@ -114,12 +129,12 @@ class UnixFileAttributes
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileTime lastModifiedTime() {
|
public FileTime lastModifiedTime() {
|
||||||
return FileTime.from(st_mtime, TimeUnit.SECONDS);
|
return toFileTime(st_mtime_sec, st_mtime_nsec);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileTime lastAccessTime() {
|
public FileTime lastAccessTime() {
|
||||||
return FileTime.from(st_atime, TimeUnit.SECONDS);
|
return toFileTime(st_atime_sec, st_atime_nsec);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -90,9 +90,12 @@ static jfieldID attrs_st_nlink;
|
|||||||
static jfieldID attrs_st_uid;
|
static jfieldID attrs_st_uid;
|
||||||
static jfieldID attrs_st_gid;
|
static jfieldID attrs_st_gid;
|
||||||
static jfieldID attrs_st_size;
|
static jfieldID attrs_st_size;
|
||||||
static jfieldID attrs_st_atime;
|
static jfieldID attrs_st_atime_sec;
|
||||||
static jfieldID attrs_st_mtime;
|
static jfieldID attrs_st_atime_nsec;
|
||||||
static jfieldID attrs_st_ctime;
|
static jfieldID attrs_st_mtime_sec;
|
||||||
|
static jfieldID attrs_st_mtime_nsec;
|
||||||
|
static jfieldID attrs_st_ctime_sec;
|
||||||
|
static jfieldID attrs_st_ctime_nsec;
|
||||||
|
|
||||||
static jfieldID attrs_f_frsize;
|
static jfieldID attrs_f_frsize;
|
||||||
static jfieldID attrs_f_blocks;
|
static jfieldID attrs_f_blocks;
|
||||||
@ -183,9 +186,12 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
|
|||||||
attrs_st_uid = (*env)->GetFieldID(env, clazz, "st_uid", "I");
|
attrs_st_uid = (*env)->GetFieldID(env, clazz, "st_uid", "I");
|
||||||
attrs_st_gid = (*env)->GetFieldID(env, clazz, "st_gid", "I");
|
attrs_st_gid = (*env)->GetFieldID(env, clazz, "st_gid", "I");
|
||||||
attrs_st_size = (*env)->GetFieldID(env, clazz, "st_size", "J");
|
attrs_st_size = (*env)->GetFieldID(env, clazz, "st_size", "J");
|
||||||
attrs_st_atime = (*env)->GetFieldID(env, clazz, "st_atime", "J");
|
attrs_st_atime_sec = (*env)->GetFieldID(env, clazz, "st_atime_sec", "J");
|
||||||
attrs_st_mtime = (*env)->GetFieldID(env, clazz, "st_mtime", "J");
|
attrs_st_atime_nsec = (*env)->GetFieldID(env, clazz, "st_atime_nsec", "J");
|
||||||
attrs_st_ctime = (*env)->GetFieldID(env, clazz, "st_ctime", "J");
|
attrs_st_mtime_sec = (*env)->GetFieldID(env, clazz, "st_mtime_sec", "J");
|
||||||
|
attrs_st_mtime_nsec = (*env)->GetFieldID(env, clazz, "st_mtime_nsec", "J");
|
||||||
|
attrs_st_ctime_sec = (*env)->GetFieldID(env, clazz, "st_ctime_sec", "J");
|
||||||
|
attrs_st_ctime_nsec = (*env)->GetFieldID(env, clazz, "st_ctime_nsec", "J");
|
||||||
|
|
||||||
clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileStoreAttributes");
|
clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileStoreAttributes");
|
||||||
if (clazz == NULL) {
|
if (clazz == NULL) {
|
||||||
@ -395,9 +401,15 @@ static void prepAttributes(JNIEnv* env, struct stat64* buf, jobject attrs) {
|
|||||||
(*env)->SetIntField(env, attrs, attrs_st_uid, (jint)buf->st_uid);
|
(*env)->SetIntField(env, attrs, attrs_st_uid, (jint)buf->st_uid);
|
||||||
(*env)->SetIntField(env, attrs, attrs_st_gid, (jint)buf->st_gid);
|
(*env)->SetIntField(env, attrs, attrs_st_gid, (jint)buf->st_gid);
|
||||||
(*env)->SetLongField(env, attrs, attrs_st_size, (jlong)buf->st_size);
|
(*env)->SetLongField(env, attrs, attrs_st_size, (jlong)buf->st_size);
|
||||||
(*env)->SetLongField(env, attrs, attrs_st_atime, (jlong)buf->st_atime);
|
(*env)->SetLongField(env, attrs, attrs_st_atime_sec, (jlong)buf->st_atime);
|
||||||
(*env)->SetLongField(env, attrs, attrs_st_mtime, (jlong)buf->st_mtime);
|
(*env)->SetLongField(env, attrs, attrs_st_mtime_sec, (jlong)buf->st_mtime);
|
||||||
(*env)->SetLongField(env, attrs, attrs_st_ctime, (jlong)buf->st_ctime);
|
(*env)->SetLongField(env, attrs, attrs_st_ctime_sec, (jlong)buf->st_ctime);
|
||||||
|
|
||||||
|
#if (_POSIX_C_SOURCE >= 200809L) || defined(__solaris__)
|
||||||
|
(*env)->SetLongField(env, attrs, attrs_st_atime_nsec, (jlong)buf->st_atim.tv_nsec);
|
||||||
|
(*env)->SetLongField(env, attrs, attrs_st_mtime_nsec, (jlong)buf->st_mtim.tv_nsec);
|
||||||
|
(*env)->SetLongField(env, attrs, attrs_st_ctime_nsec, (jlong)buf->st_ctim.tv_nsec);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL
|
JNIEXPORT void JNICALL
|
||||||
|
@ -49,9 +49,9 @@ public class Basic {
|
|||||||
check(!attrs.isSymbolicLink(), "is not a link");
|
check(!attrs.isSymbolicLink(), "is not a link");
|
||||||
check(!attrs.isOther(), "is not other");
|
check(!attrs.isOther(), "is not other");
|
||||||
|
|
||||||
// last-modified-time should match java.io.File
|
// last-modified-time should match java.io.File in seconds
|
||||||
File f = new File(dir.toString());
|
File f = new File(dir.toString());
|
||||||
check(f.lastModified() == attrs.lastModifiedTime().toMillis(),
|
check(f.lastModified()/1000 == attrs.lastModifiedTime().to(TimeUnit.SECONDS),
|
||||||
"last-modified time should be the same");
|
"last-modified time should be the same");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,10 +64,10 @@ public class Basic {
|
|||||||
check(!attrs.isSymbolicLink(), "is not a link");
|
check(!attrs.isSymbolicLink(), "is not a link");
|
||||||
check(!attrs.isOther(), "is not other");
|
check(!attrs.isOther(), "is not other");
|
||||||
|
|
||||||
// size and last-modified-time should match java.io.File
|
// size and last-modified-time should match java.io.File in seconds
|
||||||
File f = new File(file.toString());
|
File f = new File(file.toString());
|
||||||
check(f.length() == attrs.size(), "size should be the same");
|
check(f.length() == attrs.size(), "size should be the same");
|
||||||
check(f.lastModified() == attrs.lastModifiedTime().toMillis(),
|
check(f.lastModified()/1000 == attrs.lastModifiedTime().to(TimeUnit.SECONDS),
|
||||||
"last-modified time should be the same");
|
"last-modified time should be the same");
|
||||||
|
|
||||||
// copy last-modified time and file create time from directory to file,
|
// copy last-modified time and file create time from directory to file,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user