Skip to content

Commit

Permalink
stat, lstat: Return timestamp in higher resolution if possible.
Browse files Browse the repository at this point in the history
* bootstrap.conf: Import gnulib module stat-time.
* liboctave/wrappers/stat-wrapper.h, liboctave/wrappers/stat-wrapper.c
(octave_stat_wrapper, octave_lstat_wrapper, octave_fstat_wrapper,
assign_stat_fields): Use nanosecond precision fraction of timestamps in stat
structure.
* liboctave/system/file-stat.cc (file_stat::update_internal,
file_fstat::update_internal): Use constructor of octave::sys::time objects with
microsecond fraction of timestamps.
  • Loading branch information
mmuetzel committed Jan 10, 2025
1 parent 0be306d commit f9543e2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions bootstrap.conf
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ gnulib_modules="
signal
sigprocmask
stat
stat-time
stddef
stdio
strcase
Expand Down
29 changes: 20 additions & 9 deletions liboctave/system/file-stat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,19 @@ file_stat::update_internal (bool force)
const char *cname = full_file_name.c_str ();

time_t sys_atime, sys_mtime, sys_ctime;
long int atime_nsec, mtime_nsec, ctime_nsec;

int status
= (m_follow_links
? octave_stat_wrapper (cname, &m_mode, &m_ino, &m_dev,
&m_nlink, &m_uid, &m_gid, &m_size,
&sys_atime, &sys_mtime, &sys_ctime,
&sys_atime, &atime_nsec, &sys_mtime,
&mtime_nsec, &sys_ctime, &ctime_nsec,
&m_rdev, &m_blksize, &m_blocks)
: octave_lstat_wrapper (cname, &m_mode, &m_ino, &m_dev,
&m_nlink, &m_uid, &m_gid, &m_size,
&sys_atime, &sys_mtime, &sys_ctime,
&sys_atime, &atime_nsec, &sys_mtime,
&mtime_nsec, &sys_ctime, &ctime_nsec,
&m_rdev, &m_blksize, &m_blocks));

if (status < 0)
Expand All @@ -235,9 +238,12 @@ file_stat::update_internal (bool force)
}
else
{
m_atime = sys::time (static_cast<OCTAVE_TIME_T> (sys_atime));
m_mtime = sys::time (static_cast<OCTAVE_TIME_T> (sys_mtime));
m_ctime = sys::time (static_cast<OCTAVE_TIME_T> (sys_ctime));
m_atime = sys::time (static_cast<OCTAVE_TIME_T> (sys_atime),
atime_nsec/1000);
m_mtime = sys::time (static_cast<OCTAVE_TIME_T> (sys_mtime),
mtime_nsec/1000);
m_ctime = sys::time (static_cast<OCTAVE_TIME_T> (sys_ctime),
ctime_nsec/1000);
}

m_initialized = true;
Expand All @@ -253,11 +259,13 @@ file_fstat::update_internal (bool force)
m_fail = false;

time_t sys_atime, sys_mtime, sys_ctime;
long int atime_nsec, mtime_nsec, ctime_nsec;

int status
= octave_fstat_wrapper (m_fid, &m_mode, &m_ino, &m_dev,
&m_nlink, &m_uid, &m_gid, &m_size,
&sys_atime, &sys_mtime, &sys_ctime,
&sys_atime, &atime_nsec, &sys_mtime,
&mtime_nsec, &sys_ctime, &ctime_nsec,
&m_rdev, &m_blksize, &m_blocks);

if (status < 0)
Expand All @@ -267,9 +275,12 @@ file_fstat::update_internal (bool force)
}
else
{
m_atime = sys::time (static_cast<OCTAVE_TIME_T> (sys_atime));
m_mtime = sys::time (static_cast<OCTAVE_TIME_T> (sys_mtime));
m_ctime = sys::time (static_cast<OCTAVE_TIME_T> (sys_ctime));
m_atime = sys::time (static_cast<OCTAVE_TIME_T> (sys_atime),
atime_nsec/1000);
m_mtime = sys::time (static_cast<OCTAVE_TIME_T> (sys_mtime),
mtime_nsec/1000);
m_ctime = sys::time (static_cast<OCTAVE_TIME_T> (sys_ctime),
ctime_nsec/1000);
}

m_initialized = true;
Expand Down
37 changes: 30 additions & 7 deletions liboctave/wrappers/stat-wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@
#include <sys/types.h>
#include <sys/stat.h>

// Silence compiler warning if stat doesn't support nanosecond-precision
// time stamps (e.g., on Windows).
#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

#include "stat-time.h"

#if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
# pragma GCC diagnostic pop
#endif

#include "stat-wrappers.h"
#include "uniconv-wrappers.h"

Expand Down Expand Up @@ -75,7 +88,8 @@ static inline void
assign_stat_fields (struct stat *buf, mode_t *mode, ino_t *ino,
dev_t *dev, nlink_t *nlink, uid_t *uid,
gid_t *gid, off_t *size, time_t *atime,
time_t *mtime, time_t *ctime, dev_t *rdev,
long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
time_t *ctime, long int *ctime_nsec, dev_t *rdev,
long *blksize, long *blocks)
{
*mode = buf->st_mode;
Expand All @@ -86,8 +100,11 @@ assign_stat_fields (struct stat *buf, mode_t *mode, ino_t *ino,
*gid = buf->st_gid;
*size = buf->st_size;
*atime = buf->st_atime;
*atime_nsec = get_stat_atime_ns (buf);
*mtime = buf->st_mtime;
*mtime_nsec = get_stat_mtime_ns (buf);
*ctime = buf->st_ctime;
*ctime_nsec = get_stat_ctime_ns (buf);

#if defined (HAVE_STRUCT_STAT_ST_RDEV)
*rdev = buf->st_rdev;
Expand All @@ -112,7 +129,8 @@ int
octave_stat_wrapper (const char *fname, mode_t *mode, ino_t *ino,
dev_t *dev, nlink_t *nlink, uid_t *uid,
gid_t *gid, off_t *size, time_t *atime,
time_t *mtime, time_t *ctime, dev_t *rdev,
long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
time_t *ctime, long int *ctime_nsec, dev_t *rdev,
long *blksize, long *blocks)
{
struct stat buf;
Expand All @@ -126,7 +144,8 @@ octave_stat_wrapper (const char *fname, mode_t *mode, ino_t *ino,
#endif

assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size,
atime, mtime, ctime, rdev, blksize, blocks);
atime, atime_nsec, mtime, mtime_nsec, ctime, ctime_nsec,
rdev, blksize, blocks);

return status;
}
Expand All @@ -135,7 +154,8 @@ int
octave_lstat_wrapper (const char *lname, mode_t *mode, ino_t *ino,
dev_t *dev, nlink_t *nlink, uid_t *uid,
gid_t *gid, off_t *size, time_t *atime,
time_t *mtime, time_t *ctime, dev_t *rdev,
long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
time_t *ctime, long int *ctime_nsec, dev_t *rdev,
long *blksize, long *blocks)
{
struct stat buf;
Expand All @@ -150,7 +170,8 @@ octave_lstat_wrapper (const char *lname, mode_t *mode, ino_t *ino,
#endif

assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size,
atime, mtime, ctime, rdev, blksize, blocks);
atime, atime_nsec, mtime, mtime_nsec, ctime, ctime_nsec,
rdev, blksize, blocks);

return status;
}
Expand All @@ -159,15 +180,17 @@ int
octave_fstat_wrapper (int fid, mode_t *mode, ino_t *ino,
dev_t *dev, nlink_t *nlink, uid_t *uid,
gid_t *gid, off_t *size, time_t *atime,
time_t *mtime, time_t *ctime, dev_t *rdev,
long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
time_t *ctime, long int *ctime_nsec, dev_t *rdev,
long *blksize, long *blocks)
{
struct stat buf;

int status = fstat (fid, &buf);

assign_stat_fields (&buf, mode, ino, dev, nlink, uid, gid, size,
atime, mtime, ctime, rdev, blksize, blocks);
atime, atime_nsec, mtime, mtime_nsec, ctime, ctime_nsec,
rdev, blksize, blocks);

return status;
}
Expand Down
9 changes: 6 additions & 3 deletions liboctave/wrappers/stat-wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@ extern OCTAVE_API int
octave_stat_wrapper (const char *fname, mode_t *mode, ino_t *ino,
dev_t *dev, nlink_t *nlink, uid_t *uid,
gid_t *gid, off_t *size, time_t *atime,
time_t *mtime, time_t *ctime, dev_t *rdev,
long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
time_t *ctime, long int *ctime_nsec, dev_t *rdev,
long *blksize, long *blocks);

extern OCTAVE_API int
octave_lstat_wrapper (const char *lname, mode_t *mode, ino_t *ino,
dev_t *dev, nlink_t *nlink, uid_t *uid,
gid_t *gid, off_t *size, time_t *atime,
time_t *mtime, time_t *ctime, dev_t *rdev,
long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
time_t *ctime, long int *ctime_nsec, dev_t *rdev,
long *blksize, long *blocks);

extern OCTAVE_API int
octave_fstat_wrapper (int fid, mode_t *mode, ino_t *ino,
dev_t *dev, nlink_t *nlink, uid_t *uid,
gid_t *gid, off_t *size, time_t *atime,
time_t *mtime, time_t *ctime, dev_t *rdev,
long int *atime_nsec, time_t *mtime, long int *mtime_nsec,
time_t *ctime, long int *ctime_nsec, dev_t *rdev,
long *blksize, long *blocks);

extern OCTAVE_API bool octave_is_blk_wrapper (mode_t mode);
Expand Down

0 comments on commit f9543e2

Please sign in to comment.