Jump to content

stat (system call)

From Wikipedia, the free encyclopedia
(Redirected from Rdev)

stat command line

stat() is a Unix system call that queries the file system for metadata about a file or directory or more precisely an inode. The metadata contains many fields including size, ownership, permissions and timestamps.

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

  • mtime: when last modified (ls -l)
  • atime: when last accessed (ls -lu)
  • ctime: when last status changed (ls -lc)

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

[edit]

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

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

Each function accepts a pointer to a struct stat buffer which the function loads with information about the specified file or directory. 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 or directory. 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 functions was extended to support large files. Functions stat64(), lstat64() and fstat64() load information into 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 to 64, the 64-bit functions are available as the original names.

Data structure

[edit]

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

struct stat {
	mode_t			st_mode;
	ino_t			st_ino;
	dev_t			st_dev;
	dev_t			st_rdev;
	nlink_t			st_nlink;
	uid_t			st_uid;
	gid_t			st_gid;
	off_t			st_size;
	struct timespec	st_atim;
	struct timespec	st_mtim;
	struct timespec st_ctim;
	blksize_t		st_blksize;
	blkcnt_t		st_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:

Criticism of atime

[edit]

Reading a file changes its atime 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,[5][6] and in 2009, the relatime mount option had become the default, which addresses this criticism.[7] The behavior behind the relatime mount option offers sufficient performance for most purposes and should not break any significant applications, as it has been extensively discussed.[8] Initially, relatime 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 tmpwatch and Debian's popularity counter (popcon) would behave properly.[9]

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

  • strictatime (formerly atime, and formerly the default; strictatime as of 2.6.30) – always update atime, which conforms to the behavior defined by POSIX
  • relatime ("relative atime", introduced in 2.6.20 and the default as of 2.6.30) – only update atime under certain circumstances: if the previous atime is older than the mtime or ctime, or the previous atime is over 24 hours in the past
  • nodiratime – never update atime of directories, but do update atime of other files
  • noatime – never update atime of any file or directory; implies nodiratime; highest performance, but least compatible
  • lazytime – update atime according to specific circumstances laid out below

Current versions of Linux, macOS, Solaris, FreeBSD, and NetBSD support a noatime 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,[10] and some file usage watching utilities, notably tmpwatch.

The noatime option on OpenBSD behaves more like Linux relatime.[11]

Version 4.0 of the Linux kernel mainline, which was released on April 12, 2015, introduced the new mount option lazytime. 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.[12][13]

ctime

[edit]

It is tempting to believe that ctime originally meant creation time;[14] 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 ctime. The file systems retained just the access time (atime) and modification time (mtime) through 6th edition Unix. The ctime 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 ctime (last status change) update with nonzero write() (file modification).[15] In some implementations, ctime 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 ctime) and then unlinking the old name (updating ctime again) and modern Linux tend to do this.

Unlike atime and mtime, ctime cannot be set to an arbitrary value with utime(), as used by the touch utility, for example. Instead, when utime() is used, or for any other change to the inode other than an update to atime caused by accessing the file, the ctime value is set to the current time.

Time granularity

[edit]
  • time_t provides times accurate to one second.
  • Some filesystems provide finer granularity. Solaris 2.1 introduced a microsecond resolution with UFS in 1992[citation needed] and a nanosecond resolution with ZFS.[citation needed]
  • In Linux kernels 2.5.48 and above, the stat structure supports nanosecond resolution for the three file timestamp fields. These are exposed as additional fields in the stat structure.[16][17]
  • The resolution of create time on FAT filesystem is 10 milliseconds, while resolution of its write time is two seconds, and access time has a resolution of one day thus it acts as the access date.[18]

Example

[edit]

An example C application that logs information about each path passed via the command-line. It uses stat() to query the system for the information.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
	struct stat sb;
	struct passwd *pwuser;
	struct group *grpnam;

	if (argc < 2)
	{
		fprintf(stderr, "Usage: %s: file ...\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	for (int i = 1; i < argc; i++)
	{
		if (-1 == stat(argv[i], &sb))
		{
			perror("stat()");
			exit(EXIT_FAILURE);
		}

		if (NULL == (pwuser = getpwuid(sb.st_uid)))
		{
			perror("getpwuid()");
			exit(EXIT_FAILURE);
		}

		if (NULL == (grpnam = getgrgid(sb.st_gid)))
		{
			perror("getgrgid()");
			exit(EXIT_FAILURE);
		}

		printf("%s:\n", argv[i]);
		printf("\tinode: %u\n", sb.st_ino);
		printf("\towner: %u (%s)\n", sb.st_uid, pwuser->pw_name);
		printf("\tgroup: %u (%s)\n", sb.st_gid, grpnam->gr_name);
		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); /* you may use %lld */
		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");
	}

	return 0;
}

References

[edit]
  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. ^ a b Stevens & Rago 2013, p. 94.
  4. ^ "<sys/stat.h>". The Open Group Base Specifications Issue 6—IEEE Std 1003.1, 2004 Edition. The Open Group. 2004.
  5. ^ Kernel Trap: Linux: Replacing atime With relatime, by Jeremy, August 7, 2007
  6. ^ Once upon atime, LWN, by Jonathan Corbet, August 8, 2007
  7. ^ Linux kernel 2.6.30, Linux Kernel Newbies
  8. ^ That massive filesystem thread, LWN, by Jonathan Corbet, March 31, 2009
  9. ^ Relatime Recap, Valerie Aurora
  10. ^ 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)"
  11. ^ "mount(2) - OpenBSD manual pages". openbsd.org. April 27, 2018. Retrieved September 26, 2018.
  12. ^ "Linux kernel 4.0, Section 1.5. 'lazytime' option for better update of file timestamps". kernelnewbies.org. May 1, 2015. Retrieved May 2, 2015.
  13. ^ Jonathan Corbet (November 19, 2014). "Introducing lazytime". LWN.net. Retrieved May 2, 2015.
  14. ^ "BSTJ version of C.ACM Unix paper".
  15. ^ "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
  16. ^ "stat(2) - Linux manual page". man7.org. Retrieved February 27, 2015.
  17. ^ Andreas Jaeger (December 2, 2002), struct stat.h with nanosecond resolution, mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.
  18. ^ MSDN: File Times
[edit]