
is a
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
system call
In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
that queries the
file system for
metadata
Metadata (or metainformation) is "data that provides information about other data", but not the content of the data itself, such as the text of a message or the image itself. There are many distinct types of metadata, including:
* Descriptive ...
about a
file (including
special files such as
directories). The metadata contains many fields including
type,
size
Size in general is the Magnitude (mathematics), magnitude or dimensions of a thing. More specifically, ''geometrical size'' (or ''spatial size'') can refer to three geometrical measures: length, area, or volume. Length can be generalized ...
, ownership,
permissions and
timestamps.
For example, the
command uses this system call to retrieve timestamps:
* mtime: when last modified ()
* atime: when last accessed ()
* ctime: when last status changed ()
appeared in
Version 1 Unix. It is among the few original Unix
system call
In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
s to change, with
Version 4's addition of
group permissions and larger
file size
File size is a measure of how much data a computer file contains or how much storage space it is allocated. Typically, file size is expressed in units based on byte. A large value is often expressed with a metric prefix (as in megabyte and giga ...
.
Since at least 2004, the same-named
shell
Shell may refer to:
Architecture and design
* Shell (structure), a thin structure
** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses
Science Biology
* Seashell, a hard outer layer of a marine ani ...
command
Command may refer to:
Computing
* Command (computing), a statement in a computer language
* command (Unix), a Unix command
* COMMAND.COM, the default operating system shell and command-line interpreter for DOS
* Command key, a modifier key on A ...
stat
has been available for
Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
to expose features of the system call via a
command-line interface
A command-line interface (CLI) is a means of interacting with software via command (computing), commands each formatted as a line of text. Command-line interfaces emerged in the mid-1960s, on computer terminals, as an interactive and more user ...
.
Functions
The
C POSIX library header , found on
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
and other
Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
operating system
An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ...
s, declares
stat()
and related functions.
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. As typical for system calls, each function returns 0 on success, or on failure, sets
errno
errno.h is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno (short form for "error number").International Standard for Programming Langu ...
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
In computing, a symbolic link (also symlink or soft link) is a file whose purpose is to point to a file or directory (called the "target") by specifying a path thereto.
Symbolic links are supported by POSIX and by most Unix-like operating syste ...
,
stat()
returns attributes of the link target, whereas
lstat()
returns attributes of the link itself. The
fstat()
function accepts a
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
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
The metadata structure is defined in the header. The following shows the base fields, but an implementation is 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
.
Fields include:
*
st_dev
identifier of
device containing file
*
st_ino
inode
An inode (index node) is a data structure in a Unix-style file system that describes a file-system object such as a file or a directory. Each inode stores the attributes and disk block locations of the object's data. File-system object attribu ...
number
*
st_mode
a
bit field
A bit field is a data structure that maps to one or more adjacent bits which have been allocated for specific purposes, so that any single bit or group of bits within the structure can be set or inspected. A bit field is most commonly used to repre ...
containing file access
modes and
special file type; see
Unix permissions
*
st_nlink
reference count of
hard link
In computing, a hard link is a directory entry (in a Directory (computing), directory-based file system) that associates a name with a Computer file, file. Thus, each file must have at least one hard link. Creating additional hard links for a fil ...
s
*
st_uid
user identifier
Unix-like operating systems identify a user by a value called a user identifier, often abbreviated to user ID or UID. The UID, along with the group identifier (GID) and other access control criteria, is used to determine which system resources a us ...
of owner
*
st_gid
group identifier of owner
*
st_rdev
device identifier (if
special file)
*
st_size
total
file size
File size is a measure of how much data a computer file contains or how much storage space it is allocated. Typically, file size is expressed in units based on byte. A large value is often expressed with a metric prefix (as in megabyte and giga ...
, in bytes
*
st_atime
time of last access
*
st_mtime
time of last modification
*
st_ctime
time of last status change
*
st_blksize
preferred
block size for file system I/O, which can depend upon both the system and the type of file system
*
st_blocks
number of blocks allocated in multiples of
DEV_BSIZE
(usually 512 bytes).
Example
An example
C application that logs information about each path passed via the command-line. It uses to query the system for the information.
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
References
External links
atime and relatime Retrieved 2012-06-07.
stat() in PHPstat(2) Linux man page Retrieved 2012-06-07.
*{{cite book, first1=W. Richard, last1=Stevens, first2=Stephen A., last2=Rago, title=Advanced Programming in the UNIX Environment, date=May 24, 2013, publisher=
Addison-Wesley Professional, isbn=978-0321637734, edition=Third, url=http://www.kohala.com/start/apue.html, accessdate=27 February 2015
C POSIX library
POSIX
Unix file system-related software
System calls