C stat struct does not have st_ctime field, but only st_ctim

Now I have been doing this for about two hours, but could not find the answers that helped.

The definition of "stat", as indicated in the manpage, states that the st_ctime field exists.

struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ }; 

However, this is not like my system, although I use gcc (which should behave according to the standard).

In fact, all time fields (atime, mtime, ctime) are missing, and therefore the structure contains some atim, mtim, and ctim values ​​that return the timepec value instead of the required time_t value.

Now my questions are:

  • Why is this so? I may have included the wrong header, but I'm really sure it should be sys / stat.h.
  • I did not find too much information about timespec, what is it and why is it back here?
  • Even if I find a workaround, will that help m, or will any other system not be able to execute my code?

I am using Ubuntu 11.10 and gcc 4.6.1.

My code (partially):

 struct stat file_info; time_t t; if( lstat( path, &file_info ) == 0 ) { struct tm* timeinfo; t = file_info.st_ctime; timeinfo = localtime( &t ); 

I would be very happy if you could help with this, I really did not know why I could not compile using the st_ctime field of my structure, and, as usual, gcc does not help much when it comes to talking about errors; -)

He probably should do something with C # include problems, but I cannot determine what.

+4
source share
2 answers

POSIX 2008 requires stat () to return struct timespec, so that the fractional part of the timestamp is available for the specified precision of the timestamps - a single resolution is not enough for the file time.

ADDED:

From the man page

Starting with kernel 2.5.48, the stat structure supports nanosecond resolution for three file timestamp fields. Glibc provides the nanosecond component of each field using the st_atim.tv_nsec form names if the _BSD_SOURCE or _SVID_SOURCE test macro is defined. These fields are specified in POSIX.1-2008, and starting with version 2.12, glibc also provides these field names if _POSIX_C_SOURCE is defined with a value of 200809L or higher, or _XOPEN_SOURCE is defined with a value of 700 or higher. If none of the above macros is defined, then nanosecond values ​​are displayed with the names of the form st_atimensec. On file systems that do not support subsecond timestamps, nanosecond fields are returned with a value of 0.

+2
source

My documentation ( man lstat ) lists the headers to include and the requirements of the function check macro to define before the headers

 #define _POSIX_C_SOURCE 200112L #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> /* use stat, fstat, or lstat */ 
0
source

Source: https://habr.com/ru/post/1384961/


All Articles