Stat (system call) explained

is a Unix system call that returns file attributes about an inode. The semantics of vary between operating systems. As an example, Unix command uses this system call to retrieve information on files that includes:

stat appeared in Version 1 Unix. It is among the few original Unix system calls to change, with Version 4's addition of group permissions and larger file size.[1]

stat functions

The C POSIX library header, found on POSIX and other Unix-like operating systems, declares the stat functions, as well as related functions called fstat and lstat. The functions take a pointer to a struct stat buffer argument, which is used to return the file attributes. On success, the functions return zero, and on error, −1 is returned and errno is set appropriately.

The stat and lstat functions take a filename argument. If the file is a symbolic link, stat returns attributes of the eventual target of the link, while lstat returns attributes of the link itself. The fstat function takes a file descriptor argument instead, and returns attributes of the file that it identifies.

The family of functions was extended to implement large file support. Functions named stat64, lstat64 and fstat64 return attributes in a struct stat64 structure, which represents file sizes with a 64-bit type, allowing the functions to work on files 2 GiB and larger (up to 8 EiB). When the _FILE_OFFSET_BITS macro is defined to 64, these 64-bit functions are available under the original names.

The functions are defined as:

int stat(const char *filename, struct stat *buf);int lstat(const char *filename, struct stat *buf);int fstat(int filedesc, struct stat *buf);

stat structure

This structure is defined in header file as follows, although implementations are free to define additional fields:

struct stat ;

POSIX.1 does not require st_rdev, st_blocks and st_blksize members; these fields are defined as part of XSI option in the Single Unix Specification.

In older versions of POSIX.1 standard, the time-related fields were defined as st_atime, st_mtime and st_ctime, and were of type time_t. Since the 2008 version of the standard, these fields were renamed to st_atim, st_mtim and st_ctim, respectively, of type struct timespec, since this structure provides a higher resolution time unit. For the sake of compatibility, implementations can define the old names in terms of the tv_sec member of struct timespec. For example, st_atime can be defined as st_atim.tv_sec.

The struct stat structure includes at least the following members:

The st_mode field is a bit field. It combines the file access modes and also indicates any special file type. There are many macros to work with the different mode flags and file types.

Criticism of atime

Reading a file changes its eventually requiring a disk write, which has been criticized as it is inconsistent with a read only file system. File system cache may significantly reduce this activity to one disk write per cache flush.

Linux kernel developer Ingo Molnár publicly criticized the concept and performance impact of atime in 2007,[3] [4] and in 2009, the mount option had become the default, which addresses this criticism.[5] The behavior behind the mount option offers sufficient performance for most purposes and should not break any significant applications, as it has been extensively discussed.[6] Initially, only updated atime if atime < mtime or atime < ctime; that was subsequently modified to update atimes that were 24 hours old or older, so that and Debian's popularity counter (popcon) would behave properly.[7]

Current versions of the Linux kernel support four mount options, which can be specified in fstab:

Current versions of Linux, macOS, Solaris, FreeBSD, and NetBSD support a mount option in /etc/fstab, which causes the atime field never to be updated. Turning off atime updating breaks POSIX compliance, and some applications, such as mbox-driven "new mail" notifications,[8] and some file usage watching utilities, notably tmpwatch.

The option on OpenBSD behaves more like Linux .[9]

Version 4.0 of the Linux kernel mainline, which was released on April 12, 2015, introduced the new mount option . It allows POSIX-style atime updates to be performed in-memory and flushed to disk together with some non-time-related I/O operations on the same file; atime updates are also flushed to disk when some of the sync system calls are executed, or before the file's in-memory inode is evicted from the filesystem cache. Additionally, it is possible to configure for how long atime modifications can remain unflushed. That way, lazytime retains POSIX compatibility while offering performance improvements.[10] [11]

ctime

It is tempting to believe that originally meant creation time;[12] however, while early Unix did have modification and creation times, the latter was changed to be access time before there was any C structure in which to call anything . The file systems retained just the access time and modification time through 6th edition Unix. The timestamp was added in the file system restructuring that occurred with Version 7 Unix, and has always referred to inode change time. It is updated any time file metadata stored in the inode changes, such as file permissions, file ownership, and creation and deletion of hard links. POSIX also mandates (last status change) update with nonzero (file modification).[13] In some implementations, is affected by renaming a file, despite filenames not being stored in inodes: Both original Unix, which implemented a renaming by making a link (updating) and then unlinking the old name (updating again) and modern Linux tend to do this.

Unlike and, cannot be set to an arbitrary value with, as used by the utility, for example. Instead, when is used, or for any other change to the inode other than an update to caused byaccessing the file, the value is set to the current time.

Time granularity

Example

  1. include
  2. include
  3. include
  4. include
  5. include
  6. include
  7. include

intmain(int argc, char *argv[])

References

External links

Notes and References

  1. M. D. . McIlroy . Doug McIlroy . 1987 . A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 . CSTR . 139 . Bell Labs.
  2. Web site: . The Open Group Base Specifications Issue 6 - IEEE Std 1003.1, 2004 Edition . The Open Group . 2004 .
  3. Kernel Trap: Linux: Replacing atime With relatime, by Jeremy, August 7, 2007
  4. https://lwn.net/Articles/244829/ Once upon atime
  5. http://kernelnewbies.org/Linux_2_6_30 Linux kernel 2.6.30
  6. https://lwn.net/Articles/326471/ That massive filesystem thread
  7. https://archive.today/20120720170552/http://valerieaurora.wordpress.com/2009/03/27/relatime-recap/ Relatime Recap
  8. http://www.mail-archive.com/mutt-users@mutt.org/msg24912.html "the shell's $MAIL monitor ... depends on atime, pronouncing new email with atime($MAIL) < mtime($MAIL)"
  9. Web site: mount(2) - OpenBSD manual pages . April 27, 2018 . September 26, 2018 . openbsd.org.
  10. Web site: Linux kernel 4.0, Section 1.5. 'lazytime' option for better update of file timestamps . May 1, 2015 . May 2, 2015 . kernelnewbies.org.
  11. Web site: Introducing lazytime . November 19, 2014 . May 2, 2015 . Jonathan Corbet . LWN.net.
  12. Web site: BSTJ version of C.ACM Unix paper.
  13. Web site: pwrite, write - write on a file. Upon successful completion, where nbyte is greater than 0, write shall mark for update the last data modification and last file status change timestamps.
  14. Web site: stat(2) - Linux manual page . February 27, 2015 . man7.org .
  15. http://msdn.microsoft.com/en-us/library/windows/desktop/ms724290%28v=vs.85%29.aspx MSDN: File Times