Skip to content

Commit

Permalink
Add support for nano seconds for btime on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
jerboaa committed Oct 3, 2023
1 parent 7aa251b commit 643251d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/java.base/unix/classes/sun/nio/fs/UnixFileAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@

package sun.nio.fs;

import java.nio.file.attribute.*;
import java.util.concurrent.TimeUnit;
import java.util.Set;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
* Unix implementation of PosixFileAttributes.
Expand All @@ -52,6 +57,7 @@ class UnixFileAttributes
private long st_ctime_sec;
private long st_ctime_nsec;
private long st_birthtime_sec;
private long st_birthtime_nsec;

// created lazily
private volatile UserPrincipal owner;
Expand Down Expand Up @@ -158,7 +164,7 @@ public FileTime lastAccessTime() {
@Override
public FileTime creationTime() {
if (UnixNativeDispatcher.birthtimeSupported()) {
return FileTime.from(st_birthtime_sec, TimeUnit.SECONDS);
return toFileTime(st_birthtime_sec, st_birthtime_nsec);
} else {
// return last modified when birth time not supported
return lastModifiedTime();
Expand Down
9 changes: 9 additions & 0 deletions src/java.base/unix/native/libnio/fs/UnixNativeDispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ static jfieldID attrs_st_ctime_nsec;
#if defined(_DARWIN_FEATURE_64_BIT_INODE) || defined(__linux__)
static jfieldID attrs_st_birthtime_sec;
#endif
#if defined(__linux__) // Linux has nsec granularity if supported
static jfieldID attrs_st_birthtime_nsec;
#endif

static jfieldID attrs_f_frsize;
static jfieldID attrs_f_blocks;
Expand Down Expand Up @@ -342,6 +345,10 @@ Java_sun_nio_fs_UnixNativeDispatcher_init(JNIEnv* env, jclass this)
attrs_st_birthtime_sec = (*env)->GetFieldID(env, clazz, "st_birthtime_sec", "J");
CHECK_NULL_RETURN(attrs_st_birthtime_sec, 0);
#endif
#if defined (__linux__) // Linux has nsec granularity
attrs_st_birthtime_nsec = (*env)->GetFieldID(env, clazz, "st_birthtime_nsec", "J");
CHECK_NULL_RETURN(attrs_st_birthtime_sec, 0);
#endif

clazz = (*env)->FindClass(env, "sun/nio/fs/UnixFileStoreAttributes");
CHECK_NULL_RETURN(clazz, 0);
Expand Down Expand Up @@ -620,6 +627,7 @@ static void copy_statx_attributes(JNIEnv* env, struct statx* buf, jobject attrs)
(*env)->SetLongField(env, attrs, attrs_st_mtime_sec, (jlong)buf->stx_mtime.tv_sec);
(*env)->SetLongField(env, attrs, attrs_st_ctime_sec, (jlong)buf->stx_ctime.tv_sec);
(*env)->SetLongField(env, attrs, attrs_st_birthtime_sec, (jlong)buf->stx_btime.tv_sec);
(*env)->SetLongField(env, attrs, attrs_st_birthtime_nsec, (jlong)buf->stx_btime.tv_nsec);
(*env)->SetLongField(env, attrs, attrs_st_atime_nsec, (jlong)buf->stx_atime.tv_nsec);
(*env)->SetLongField(env, attrs, attrs_st_mtime_nsec, (jlong)buf->stx_mtime.tv_nsec);
(*env)->SetLongField(env, attrs, attrs_st_ctime_nsec, (jlong)buf->stx_ctime.tv_nsec);
Expand Down Expand Up @@ -649,6 +657,7 @@ static void copy_stat64_attributes(JNIEnv* env, struct stat64* buf, jobject attr

#ifdef _DARWIN_FEATURE_64_BIT_INODE
(*env)->SetLongField(env, attrs, attrs_st_birthtime_sec, (jlong)buf->st_birthtime);
// rely on default value of 0 for st_birthtime_nsec field on Darwin
#endif

#ifndef MACOSX
Expand Down

0 comments on commit 643251d

Please sign in to comment.