Stat (system call)

Last updated

stat command line Coreutils stat screenshot.png
stat command line

stat() is a Unix system call that queries the file system for metadata about a file (including special files such as directories). The metadata contains many fields including type, size, ownership, permissions and timestamps.

Contents

For example, the ls command uses this system call to retrieve timestamps:

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]

Since at least 2004, the same-named shell command stat has been available for Linux to expose features of the system call via a command-line interface. [2]

Functions

The C POSIX library header sys/stat.h, found on POSIX and other Unix-like operating systems, declares stat() and related functions.

intstat(constchar*path,structstat*buf);intlstat(constchar*path,structstat*buf);intfstat(intfiledesc,structstat*buf);

Each function accepts a pointer to a struct stat buffer which the function loads with information about the specified file. As typical for system calls, each function returns 0 on success, or on failure, sets errno to indicate the failure condition and returns −1.

The stat() and lstat() functions accept a path argument that specifies a file. If the path identifies a symbolic link, stat() returns attributes of the link target, whereas lstat() returns attributes of the link itself. The fstat() function accepts a file descriptor argument instead of a path, and returns attributes of the file that it identifies.

The library has been extended to support large files. Functions stat64(), lstat64() and fstat64() load information into a struct stat64 buffer, which supports 64-bit sizes; allowing them to work with files 2 GiB and larger (up to 8 EiB). When the _FILE_OFFSET_BITS macro is defined as 64, the 64-bit functions are available as the original names.

Data structure

The metadata structure is defined in the sys/stat.h header. The following shows the base fields, but an implementation is free to include additional fields: [3]

structstat{mode_tst_mode;ino_tst_ino;dev_tst_dev;dev_tst_rdev;nlink_tst_nlink;uid_tst_uid;gid_tst_gid;off_tst_size;structtimespecst_atim;structtimespecst_mtim;structtimespecst_ctim;blksize_tst_blksize;blkcnt_tst_blocks;};

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. [3]

Fields include:

Example

The following C program reports metadata about each file passed via the command-line using stat() to query the system for the information.

#include<stdio.h>#include<stdlib.h>#include<time.h>#include<sys/types.h>#include<sys/stat.h>intmain(intargc,char*argv[]){structstatsb;for(inti=1;i<argc;i++){if(stat(argv[i],&sb)==-1){perror("stat failed");exit(EXIT_FAILURE);}printf("%s:\n",argv[i]);printf("\tinode: %u\n",sb.st_ino);printf("\tperms: %o\n",sb.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO));printf("\tlinks: %d\n",sb.st_nlink);printf("\tsize: %ld\n",sb.st_size);printf("\tatime: %s",ctime(&sb.st_atim.tv_sec));printf("\tmtime: %s",ctime(&sb.st_mtim.tv_sec));printf("\tctime: %s",ctime(&sb.st_ctim.tv_sec));printf("\n");}return0;}

References

  1. McIlroy, M. D. (1987). A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 (PDF) (Technical report). CSTR. Bell Labs. 139.
  2. tee, file, stat, find- correction - linuxchix.org - Wed Mar 10 11:04:07 EST 2004 (archived on April 30, 2006)
  3. 1 2 Stevens & Rago 2013, p. 94.
  4. "<sys/stat.h>". The Open Group Base Specifications Issue 6IEEE Std 1003.1, 2004 Edition. The Open Group. 2004.