Skip to content

Commit

Permalink
use high resolution file stat fields
Browse files Browse the repository at this point in the history
prepare for reworking #1635: nanosecond resolution in javalib FileTime
and posixlib sys/stat

This commit introduces breaking changes in public api.

This commit changes stat struct fields so that it can use high resolution file stat, which is
available since linux kernel 2.5.48 and later.
For more detail,see linux man page https://linux.die.net/man/2/stat
  • Loading branch information
i10416 committed Dec 25, 2022
1 parent 29cad8b commit cf57399
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions javalib/src/main/scala/java/io/File.scala
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ class File(_path: String) extends Serializable with Comparable[File] {
} else {
val buf = alloc[stat.stat]()
if (stat.stat(toCString(path), buf) == 0) {
buf._8 * 1000L
buf._8._1 * 1000L
} else {
0L
}
Expand Down Expand Up @@ -473,7 +473,7 @@ class File(_path: String) extends Serializable with Comparable[File] {
val statbuf = alloc[stat.stat]()
if (stat.stat(toCString(path), statbuf) == 0) {
val timebuf = alloc[utime.utimbuf]()
timebuf._1 = statbuf._8
timebuf._1 = statbuf._8._1
timebuf._2 = time.toSize / 1000
utime.utime(toCString(path), timebuf) == 0
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ final class PosixFileAttributeViewImpl(path: Path, options: Array[LinkOption])
val buf = alloc[utime.utimbuf]()
buf._1 =
if (lastAccessTime != null) lastAccessTime.to(TimeUnit.SECONDS).toSize
else sb._7
else sb._7._1
buf._2 =
if (lastModifiedTime != null) lastModifiedTime.to(TimeUnit.SECONDS).toSize
else sb._8
else sb._8._1
// createTime is ignored: No posix-y way to set it.
if (utime.utime(toCString(path.toString), buf) != 0)
throwIOException()
Expand Down Expand Up @@ -104,8 +104,8 @@ final class PosixFileAttributeViewImpl(path: Path, options: Array[LinkOption])
st_uid = buf._4
st_gid = buf._5
st_size = buf._6
st_atime = buf._7
st_mtime = buf._8
st_atime = buf._7._1
st_mtime = buf._8._1
st_mode = buf._13
}

Expand Down
19 changes: 13 additions & 6 deletions posixlib/src/main/resources/scala-native/sys/stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ struct scalanative_stat {
length in bytes. For a typed memory object,
the length in bytes. For other file types,
the use of this field is unspecified. */
scalanative_time_t _st_atime; /** Time of last access. */
scalanative_time_t _st_mtime; /** Time of last data modification. */
scalanative_time_t _st_ctime; /** Time of last status change. */
scalanative_timespec st_atim; /** Time of last access. */
scalanative_timespec st_mtim; /** Time of last data modification. */
scalanative_timespec st_ctim; /** Time of last status change. */
scalanative_blkcnt_t st_blocks; /** Number of blocks allocated for this
object. */
scalanative_blksize_t st_blksize; /** A file system-specific preferred I/O
Expand All @@ -42,9 +42,16 @@ void scalanative_stat_init(struct stat *stat,
my_stat->st_uid = stat->st_uid;
my_stat->st_gid = stat->st_gid;
my_stat->st_size = stat->st_size;
my_stat->_st_atime = stat->st_atime;
my_stat->_st_mtime = stat->st_mtime;
my_stat->_st_ctime = stat->st_ctime;
// see https://linux.die.net/man/2/stat
#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L || defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >=700
my_stat->st_atim = stat->st_atim;
my_stat->st_mtim = stat->st_mtim;
my_stat->st_ctim = stat->st_ctim;
#else
my_stat->st_atim = stat->st_atimespec;
my_stat->st_mtim = stat->st_mtimespec;
my_stat->st_ctim = stat->st_ctimespec;
#endif
my_stat->st_blksize = stat->st_blksize;
my_stat->st_blocks = stat->st_blocks;
my_stat->st_nlink = stat->st_nlink;
Expand Down
1 change: 1 addition & 0 deletions posixlib/src/main/resources/scala-native/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef unsigned int scalanative_uid_t;
typedef unsigned int scalanative_gid_t;
typedef long long scalanative_off_t;
typedef long int scalanative_time_t;
typedef struct timespec scalanative_timespec;
typedef long long scalanative_blkcnt_t;
typedef long scalanative_blksize_t;
typedef unsigned long scalanative_nlink_t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ object stat {
uid_t, // st_uid
gid_t, // st_gid
off_t, // st_size
time_t, // st_atime
time_t, // st_mtime
time_t, // st_ctime
timespec, // st_atim or st_atimespec
timespec, // st_mtim or st_mtimespec
timespec, // st_ctim or st_ctimespec
blkcnt_t, // st_blocks
blksize_t, // st_blksize
nlink_t, // st_nlink
Expand Down

0 comments on commit cf57399

Please sign in to comment.