Stat (Unix)
Encyclopedia
stat is a Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...

 that returns useful data about a file inode
Inode
In computing, an inode is a data structure on a traditional Unix-style file system such as UFS. An inode stores all the information about a regular file, directory, or other file system object, except its data and name....

. The semantics of stat vary between operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

s. As an example, the Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 command ls
Ls
In computing, ls is a command to list files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification.- History :An ls utility appeared in the original version of AT&T UNIX...

 uses it to retrieve information on (among many others): time of last modification (ls -l), time of last status change (ls -lc) and time of last access (ls -lu).

stat functions and stat structure

The POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 library header
C POSIX library
The C POSIX library is a specification of a C standard library for POSIX systems. It was developed at the same time as the ANSI C standard. Some effort was made to make POSIX compatible with standard C; POSIX includes additional functions to those introduced in standard C.- C POSIX library header...

 sys/stat.h, found on all POSIX-compliant and Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

 operating systems, declares the stat, fstat, and lstat routines:


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


and defines the struct stat structure as including at least the following members:


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 */
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 */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */

lstat

lstat is a library function that retrieves the status of a file
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...

. It is identical to stat, except when the file is a symbolic link
Symbolic link
In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...

, in which case information about the link itself is returned instead of the linked-to file.

fstat

fstat is a library function that retrieves the status of a file. It is identical to stat except that the file's identity is passed as a file descriptor
File descriptor
In computer programming, a file descriptor is an abstract indicator for accessing a file. The term is generally used in POSIX operating systems...

 instead of as a filename
Filename
The filename is metadata about a file; a string used to uniquely identify a file stored on the file system. Different file systems impose different restrictions on length and allowed characters on filenames.A filename includes one or more of these components:...

.

stat64

stat64 is a library function very similar to stat except that it returns a struct stat64 which represents the file size with a 64-bit type allowing it to work on files larger than 2 GB. It's a part of the large files extension.

Criticism of atime

Writing to a file changes its mtime and ctime, while reading a file changes its atime. As a result, on a POSIX-compliant system, reading a file causes a write, which has been criticized. This behaviour can usually be disabled by adding a mount option in /etc/fstab.

However, turning off atime updating breaks POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 compliance, and some applications, notably the mutt mail reader (in some configurations), and some file usage watching utilities, notably tmpwatch. In the worst case, not updating atime can cause some backup programs to fail to backup a file.

Linux kernel developer Ingo Molnár
Ingo Molnar
Ingo Molnár, currently employed by Red Hat, is a Hungarian Linux hacker. He is best known for his contributions to the operating system in terms of security and performance...

 called atime "perhaps the most stupid Unix design idea of all times," adding: "[T]hink about this a bit: 'For every file that is read from the disk, lets do a ... write to the disk! And, for every file that is already cached and which we read from the cache ... do a write to the disk!'"
He further emphasized the performance impact thus:
Atime updates are by far the biggest IO performance deficiency that Linux has today. Getting rid of atime updates would give us more everyday Linux performance than all the pagecache speedups of the past 10 years, _combined_.

Solutions

Current versions of Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

, Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

, Solaris, FreeBSD
FreeBSD
FreeBSD is a free Unix-like operating system descended from AT&T UNIX via BSD UNIX. Although for legal reasons FreeBSD cannot be called “UNIX”, as the direct descendant of BSD UNIX , FreeBSD’s internals and system APIs are UNIX-compliant...

, NetBSD
NetBSD
NetBSD is a freely available open source version of the Berkeley Software Distribution Unix operating system. It was the second open source BSD descendant to be formally released, after 386BSD, and continues to be actively developed. The NetBSD project is primarily focused on high quality design,...

, and OpenBSD
OpenBSD
OpenBSD is a Unix-like computer operating system descended from Berkeley Software Distribution , a Unix derivative developed at the University of California, Berkeley. It was forked from NetBSD by project leader Theo de Raadt in late 1995...

 support a mount option, which causes the atime field never to be updated. This breaks compliance with POSIX.

Current versions of Linux support four mount options, which can be specified in fstab
Fstab
The fstab file is a system configuration file commonly found on Unix systems. The fstab file typically lists all available disks and disk partitions, and indicates how they are to be initialized or otherwise integrated into the overall system's file system...

:
  • strictatime (formerly atime, and formerly the default; strictatime as of 2.6.30) – always update atime.
  • relatime – (relative atime), introduced in 2.6.20 and the default as of 2.6.30
  • nodiratime – do not update atime of directories ever
  • noatime – do not update atime ever; includes nodiratime; highest performance, least compatible


strictatime accords with POSIX; Alan Cox
Alan Cox
Alan Cox is a British computer programmer who formerly maintained the 2.2 branch of the Linux kernel and continues to be heavily involved in the development of the Linux kernel, an association that dates back to 1991...

 described the alternatives as:
Turn off atime and it is very non standards compliant, turn to relatime and it is not standards compliant but nobody will break (which is good)


File systems mounted with the noatime option do not update the atime on reads, and the relatime option provides for updates only if the previous atime is older than the mtime or ctime, or the previous atime is over 24 hours in the past. Many users use noatime without problem, so long as they do not use an application which depends on atime, and this offers some benefits over relatime (no writing of atime ever on read).

As of 2.6.30 (9 June 2009), Linux defaults to relatime, so that it will not update atime on all file reads. The behavior offers sufficient performance for most purposes and should not break any significant applications. Extended discussion of filesystem performance preceded decision. Indeed, relatime by default was the first patch Linux applied following the 2.6.29 release. In initial patches relatime only updated atime if atime < mtime or atime < ctime; this 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.

See further discussion at the references.

ctime

Note that ctime has nothing to do with file creation time. It is updated any time file content changes (together with mtime), and also by changes in metadata such as file permissions
Chmod
The chmod command is a Unix command that lets a user tell the system how much access it should permit to a file. It changes the file system modes of files and directories. The modes include permissions and special modes...

, file ownership
Chown
The chown command is used on Unix-like systems to change the owner of a file. In most implementations, it can only be executed by the superuser. Unprivileged users who wish to change the group of a file that they own may use chgrp.-Usage examples:These examples illustrate typical syntax and use...

, and creation and deletion of hard links
Hard link
In computing, a hard link is a directory entry that associates a name with a file on a file system. . The term is used in file systems which allow multiple hard links to be created for the same file. This has the effect of creating multiple names for the same file, causing an aliasing effect: e.g...

. In some implementations, ctime is affected by renaming a file (both original Unix and modern Linux tend to do this).

Unlike atime and mtime, ctime cannot be set with utime (as used e.g. by touch
Touch (Unix)
touch is a standard Unix program used to change a file's access and modification timestamps. It is also used to create a new empty file.- History :A touch utility appeared in Version 7 AT&T UNIX...

); the only way to set it to an arbitrary value is by changing the system clock.

Granularity of mtime etc.

time t provides times accurate to 1 second.

Some filesystems provide greater granularity. 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.

The FAT filesystem provides timestamps with a granularity of 2 seconds.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK