Re: st_* variables of ctim vs ctime vs ctimespec; is there a history & recommended use?

From: Kurt Hackenberg <kh_at_panix.com>
Date: Sun, 01 Oct 2023 17:45:23 UTC
On Sat, Sep 30, 2023 at 06:50:29PM -0700, Edward Sanford Sutton, III wrote:

>  After recently finding stat(1) EXAMPLES error (I filed under 
>PR274189 with patch and reasoning), I was trying to learn the meanings 
>of these variables which lead me through reading stat(2) (found by `man 
>lstat`) which says it uses st_mtim and provides both st_mtime and 
>st_mtimespec for compatibility.

Have you looked at the definition of struct stat, in 
/usr/include/sys/stat.h?  The time members, st_mtim and such, are each 
a struct timespec[1], which has two members, seconds and nanoseconds.  
The compatibility macro st_mtime is:

#define	st_mtime		st_mtim.tv_sec

This is compatibility with old Unix and old, slow computers.  I think 
those file times were originally only precise to a second -- they were 
classic Unix time, signed 32-bit seconds since 1/1/1970, UTC.

Now the FreeBSD filesystem keeps those times to the nanosecond, and so 
needs more than 32 bits.  The compatibility macro st_mtime is there for 
old code written when that variable was a simple integer that contained 
only seconds.

Apparently stat(1) uses the compatibility macro, so only shows those 
file times to the second.  That program may be old.

Those times have also expanded in another way.  The original Unix time, 
signed 32-bit seconds, will run out in the year 2038.  Struct 
timespec's member tv_sec is of type time_t, which is 64 bits on a 
64-bit machine, to greatly extend the range of Unix time.  I guess 
people using 32-bit machines 15 years from now will be out of luck.


[1] Defined in /usr/include/sys/_timespec.h