]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/stat.2
_exit.2, bpf.2, cacheflush.2, capget.2, chdir.2, chmod.2, chroot.2, clock_getres...
[thirdparty/man-pages.git] / man2 / stat.2
CommitLineData
fea681da 1'\" t
fea681da
MK
2.\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
3.\" Parts Copyright (c) 1995 Nicolai Langfeldt (janl@ifi.uio.no), 1/1/95
40084043 4.\" and Copyright (c) 2006, 2007, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 5.\"
93015253 6.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
7.\" Permission is granted to make and distribute verbatim copies of this
8.\" manual provided the copyright notice and this permission notice are
9.\" preserved on all copies.
10.\"
11.\" Permission is granted to copy and distribute modified versions of this
12.\" manual under the conditions for verbatim copying, provided that the
13.\" entire resulting derived work is distributed under the terms of a
14.\" permission notice identical to this one.
c13182ef 15.\"
fea681da
MK
16.\" Since the Linux kernel and libraries are constantly changing, this
17.\" manual page may be incorrect or out-of-date. The author(s) assume no
18.\" responsibility for errors or omissions, or for damages resulting from
19.\" the use of the information contained herein. The author(s) may not
20.\" have taken the same level of care in the production of this manual,
21.\" which is licensed free of charge, as they might when working
22.\" professionally.
c13182ef 23.\"
fea681da
MK
24.\" Formatted or processed versions of this manual, if unaccompanied by
25.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 26.\" %%%LICENSE_END
fea681da
MK
27.\"
28.\" Modified by Michael Haardt <michael@moria.de>
29.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
30.\" Modified 1995-05-18 by Todd Larason <jtl@molehill.org>
31.\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
32.\" Modified 1995-01-09 by Richard Kettlewell <richard@greenend.org.uk>
33.\" Modified 1998-05-13 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
34.\" Modified 1999-07-06 by aeb & Albert Cahalan
35.\" Modified 2000-01-07 by aeb
c11b1abf 36.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
bc7ff20e 37.\" 2007-06-08 mtk: Added example program
cd667553 38.\" 2007-07-05 mtk: Added details on underlying system call interfaces
c13182ef 39.\"
734882f4 40.TH STAT 2 2017-05-03 "Linux" "Linux Programmer's Manual"
fea681da 41.SH NAME
40084043 42stat, fstat, lstat, fstatat \- get file status
fea681da 43.SH SYNOPSIS
40084043 44.nf
fea681da
MK
45.B #include <sys/types.h>
46.br
47.B #include <sys/stat.h>
48.br
49.B #include <unistd.h>
68e4db0a 50.PP
9a38b2ce 51.BI "int stat(const char *" pathname ", struct stat *" statbuf );
fea681da 52.br
9a38b2ce 53.BI "int fstat(int " fd ", struct stat *" statbuf );
fea681da 54.br
9a38b2ce 55.BI "int lstat(const char *" pathname ", struct stat *" statbuf );
68e4db0a 56.PP
40084043
MK
57.BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
58.B #include <sys/stat.h>
68e4db0a 59.PP
40084043 60.BI "int fstatat(int " dirfd ", const char *" pathname ", struct stat *" \
9a38b2ce 61statbuf ,
40084043
MK
62.BI " int " flags );
63.fi
68e4db0a 64.PP
8179def1
MK
65.in -4n
66Feature Test Macro Requirements for glibc (see
67.BR feature_test_macros (7)):
68.in
68e4db0a 69.PP
43e0fe7d 70.ad l
8179def1 71.BR lstat ():
43e0fe7d 72.RS 4
cf7fa0a1 73/* glibc 2.19 and earlier */ _BSD_SOURCE
afb9bd5d 74.br
cf7fa0a1 75 || /* Since glibc 2.20 */ _DEFAULT_SOURCE
afb9bd5d 76.br
cf7fa0a1
MK
77 || _XOPEN_SOURCE\ >=\ 500
78.\" _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
3ba63d80 79.br
cf7fa0a1 80 || /* Since glibc 2.10: */ _POSIX_C_SOURCE\ >=\ 200112L
43e0fe7d 81.RE
68e4db0a 82.PP
40084043 83.BR fstatat ():
40084043
MK
84.ad l
85.RS 4
68e4db0a 86.PD 0
40084043
MK
87.TP 4
88Since glibc 2.10:
b0da7b8b 89_POSIX_C_SOURCE\ >=\ 200809L
40084043
MK
90.TP
91Before glibc 2.10:
92_ATFILE_SOURCE
93.RE
43e0fe7d
MK
94.PD
95.ad
fea681da
MK
96.SH DESCRIPTION
97.PP
e096bd70 98These functions return information about a file, in the buffer pointed to by
9a38b2ce 99.IR statbuf .
5503c85e 100No permissions are required on the file itself, but\(emin the case of
071d1c4f
MK
101.BR stat (),
102.BR fstatat (),
c13182ef 103and
827edbd2
MK
104.BR lstat ()\(emexecute
105(search) permission is required on all of the directories in
66cbeaf4 106.I pathname
da2d9dad 107that lead to the file.
fea681da 108.PP
da2d9dad 109.BR stat ()
071d1c4f
MK
110and
111.BR fstatat ()
e096bd70
MK
112retrieve information about the file pointed to by
113.IR pathname ;
071d1c4f
MK
114the differences for
115.BR fstatat ()
116are described below.
fea681da 117
da2d9dad 118.BR lstat ()
fea681da 119is identical to
da2d9dad
MK
120.BR stat (),
121except that if
66cbeaf4 122.I pathname
e096bd70 123is a symbolic link, then it returns information about the link itself,
fea681da
MK
124not the file that it refers to.
125
da2d9dad 126.BR fstat ()
fea681da 127is identical to
da2d9dad 128.BR stat (),
5201bb40 129except that the file about which information is to be retrieved
e096bd70 130is specified by the file descriptor
d3b03141 131.IR fd .
9152b8c9
MK
132.\"
133.SS The stat structure
da2d9dad 134All of these system calls return a
fea681da
MK
135.I stat
136structure, which contains the following fields:
137.PP
bd191423 138.in +4n
fea681da
MK
139.nf
140struct stat {
e9e9e87e 141 dev_t st_dev; /* ID of device containing file */
96dea201
MK
142 ino_t st_ino; /* Inode number */
143 mode_t st_mode; /* File type and mode */
144 nlink_t st_nlink; /* Number of hard links */
145 uid_t st_uid; /* User ID of owner */
146 gid_t st_gid; /* Group ID of owner */
147 dev_t st_rdev; /* Device ID (if special file) */
148 off_t st_size; /* Total size, in bytes */
149 blksize_t st_blksize; /* Block size for filesystem I/O */
150 blkcnt_t st_blocks; /* Number of 512B blocks allocated */
e9e9e87e
MK
151
152 /* Since Linux 2.6, the kernel supports nanosecond
153 precision for the following timestamp fields.
154 For the details before Linux 2.6, see NOTES. */
155
96dea201
MK
156 struct timespec st_atim; /* Time of last access */
157 struct timespec st_mtim; /* Time of last modification */
158 struct timespec st_ctim; /* Time of last status change */
e9e9e87e
MK
159
160#define st_atime st_atim.tv_sec /* Backward compatibility */
161#define st_mtime st_mtim.tv_sec
162#define st_ctime st_ctim.tv_sec
fea681da
MK
163};
164.fi
bd191423 165.in
496bdb56 166
b3deaf0f 167.IR Note :
496bdb56
MK
168the order of fields in the
169.I stat
170structure varies somewhat
171across architectures.
172In addition,
173the definition above does not show the padding bytes
174that may be present between some fields on various architectures.
409b0278 175Consult the glibc and kernel source code
496bdb56
MK
176if you need to know the details.
177
64639e9a
MK
178.\" Background: inode attributes are modified with i_mutex held, but
179.\" read by stat() without taking the mutex.
b3deaf0f 180.IR Note :
7ad5fb5d 181for performance and simplicity reasons, different fields in the
f240296d
JH
182.I stat
183structure may contain state information from different moments
64639e9a
MK
184during the execution of the system call.
185For example, if
addfd6c3
MK
186.IR st_mode
187or
f240296d 188.IR st_uid
addfd6c3
MK
189is changed by another process by calling
190.BR chmod (2)
191or
64639e9a 192.BR chown (2),
f240296d
JH
193.BR stat ()
194might return the old
195.I st_mode
196together with the new
197.IR st_uid ,
198or the old
199.I st_uid
200together with the new
201.IR st_mode .
202
53cb52e5
MK
203The fields in the
204.I stat
205structure are as follows:
206.TP
29de83af 207.I st_dev
53cb52e5 208This field describes the device on which this file resides.
e9ef777f
MK
209(The
210.BR major (3)
211and
212.BR minor (3)
213macros may be useful to decompose the device ID in this field.)
53cb52e5 214.TP
c61617be
MK
215.I st_ino
216This field contains the file's inode number.
217.TP
cafa9752 218.I st_mode
e8ff4f53
MK
219This field contains the file type and mode.
220See
221.BR inode (7)
222for further information.
cafa9752 223.TP
d41bfb20
MK
224.I st_nlink
225This field contains the number of hard links to the file.
226.TP
78909f3e
MK
227.I st_uid
228This field contains the user ID of the owner of the file.
229.TP
230.I st_gid
231This field contains the ID of the group owner of the file.
232.TP
da2d9dad 233.I st_rdev
53cb52e5
MK
234This field describes the device that this file (inode) represents.
235.TP
fea681da 236.I st_size
53cb52e5 237This field gives the size of the file (if it is a regular
c13182ef 238file or a symbolic link) in bytes.
280ff209
MK
239The size of a symbolic link is the length of the pathname
240it contains, without a terminating null byte.
53cb52e5 241.TP
28f042cc 242.I st_blksize
96dea201 243This field gives the "preferred" block size for efficient filesystem I/O.
28f042cc 244.TP
fea681da 245.I st_blocks
53cb52e5 246This field indicates the number of blocks allocated to the file,
e8ff4f53 247in 512-byte units.
fea681da 248(This may be smaller than
eee0a2ec
MK
249.IR st_size /512
250when the file has holes.)
53cb52e5 251.TP
fea681da 252.I st_atime
53cb52e5 253This is the file's last access timestamp.
53cb52e5 254.TP
fea681da 255.I st_mtime
53cb52e5 256This is the file's last modification timestamp.
53cb52e5 257.TP
fea681da 258.I st_ctime
53cb52e5 259This is the file's last status change timestamp.
fea681da 260.PP
e8ff4f53
MK
261For further information on the above fields, see
262.BR inode (7).
40084043 263.\"
1fef0fa3 264.SS fstatat()
40084043
MK
265The
266.BR fstatat ()
267system call operates in exactly the same way as
cadd38ba 268.BR stat (),
40084043
MK
269except for the differences described here.
270
271If the pathname given in
272.I pathname
273is relative, then it is interpreted relative to the directory
274referred to by the file descriptor
275.I dirfd
276(rather than relative to the current working directory of
277the calling process, as is done by
cadd38ba 278.BR stat ()
40084043
MK
279for a relative pathname).
280
281If
282.I pathname
283is relative and
284.I dirfd
285is the special value
286.BR AT_FDCWD ,
287then
288.I pathname
289is interpreted relative to the current working
290directory of the calling process (like
cadd38ba 291.BR stat ()).
40084043
MK
292
293If
294.I pathname
295is absolute, then
296.I dirfd
297is ignored.
298
299.I flags
300can either be 0, or include one or more of the following flags ORed:
301.TP
302.BR AT_EMPTY_PATH " (since Linux 2.6.39)"
303.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
304If
305.I pathname
306is an empty string, operate on the file referred to by
307.IR dirfd
308(which may have been obtained using the
309.BR open (2)
310.B O_PATH
311flag).
b78969ac
MÅš
312In this case,
313.I dirfd
314can refer to any type of file, not just a directory.
a6fcbdf8
MK
315If
316.I dirfd
317is
318.BR AT_FDCWD ,
319the call operates on the current working directory.
40084043
MK
320This flag is Linux-specific; define
321.B _GNU_SOURCE
322.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
323to obtain its definition.
324.TP
325.BR AT_NO_AUTOMOUNT " (since Linux 2.6.38)"
326Don't automount the terminal ("basename") component of
327.I pathname
328if it is a directory that is an automount point.
329This allows the caller to gather attributes of an automount point
330(rather than the location it would mount).
331This flag can be used in tools that scan directories
332to prevent mass-automounting of a directory of automount points.
333The
334.B AT_NO_AUTOMOUNT
335flag has no effect if the mount point has already been mounted over.
336This flag is Linux-specific; define
337.B _GNU_SOURCE
338.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
339to obtain its definition.
340.TP
341.B AT_SYMLINK_NOFOLLOW
342If
343.I pathname
344is a symbolic link, do not dereference it:
345instead return information about the link itself, like
cadd38ba 346.BR lstat ().
40084043
MK
347(By default,
348.BR fstatat ()
349dereferences symbolic links, like
cadd38ba 350.BR stat ().)
40084043
MK
351.PP
352See
353.BR openat (2)
354for an explanation of the need for
355.BR fstatat ().
47297adb 356.SH RETURN VALUE
c13182ef
MK
357On success, zero is returned.
358On error, \-1 is returned, and
fea681da
MK
359.I errno
360is set appropriately.
361.SH ERRORS
362.TP
363.B EACCES
364Search permission is denied for one of the directories
365in the path prefix of
66cbeaf4 366.IR pathname .
fea681da 367(See also
ad7cc990 368.BR path_resolution (7).)
fea681da
MK
369.TP
370.B EBADF
d3b03141 371.I fd
0fa890e0 372is not a valid open file descriptor.
fea681da
MK
373.TP
374.B EFAULT
375Bad address.
376.TP
377.B ELOOP
378Too many symbolic links encountered while traversing the path.
379.TP
380.B ENAMETOOLONG
66cbeaf4 381.I pathname
c066b169 382is too long.
fea681da
MK
383.TP
384.B ENOENT
5cf0b3b4 385A component of
66cbeaf4 386.I pathname
5cf0b3b4 387does not exist, or
66cbeaf4 388.I pathname
96dea201
MK
389is an empty string and
390.B AT_EMPTY_PATH
391was not specified in
392.IR flags .
fea681da
MK
393.TP
394.B ENOMEM
75b94dc3 395Out of memory (i.e., kernel memory).
fea681da
MK
396.TP
397.B ENOTDIR
94ea1e9d 398A component of the path prefix of
66cbeaf4 399.I pathname
5cf0b3b4 400is not a directory.
9dcc4605
MK
401.TP
402.B EOVERFLOW
66cbeaf4 403.I pathname
fec75159
SP
404or
405.I fd
406refers to a file whose size, inode number,
407or number of blocks cannot be represented in, respectively, the types
408.IR off_t ,
409.IR ino_t ,
410or
411.IR blkcnt_t .
412This error can occur when, for example,
413an application compiled on a 32-bit platform without
5e4dc269 414.I -D_FILE_OFFSET_BITS=64
9dcc4605
MK
415calls
416.BR stat ()
417on a file whose size exceeds
4d69ed2e 418.I (1<<31)-1
fec75159 419bytes.
40084043 420.PP
40084043
MK
421The following additional errors can occur for
422.BR fstatat ():
423.TP
424.B EBADF
425.I dirfd
426is not a valid file descriptor.
427.TP
428.B EINVAL
429Invalid flag specified in
430.IR flags .
431.TP
432.B ENOTDIR
433.I pathname
434is relative and
435.I dirfd
436is a file descriptor referring to a file other than a directory.
437.SH VERSIONS
438.BR fstatat ()
439was added to Linux in kernel 2.6.16;
440library support was added to glibc in version 2.4.
47297adb 441.SH CONFORMING TO
40084043
MK
442.BR stat (),
443.BR fstat (),
444.BR lstat ():
282daba9 445SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
97c1eac8
MK
446.\" SVr4 documents additional
447.\" .BR fstat ()
448.\" error conditions EINTR, ENOLINK, and EOVERFLOW. SVr4
449.\" documents additional
450.\" .BR stat ()
451.\" and
452.\" .BR lstat ()
453.\" error conditions EINTR, EMULTIHOP, ENOLINK, and EOVERFLOW.
454
40084043
MK
455.BR fstatat ():
456POSIX.1-2008.
40084043 457
d3e8b141
MK
458According to POSIX.1-2001,
459.BR lstat ()
460on a symbolic link need return valid information only in the
461.I st_size
44b1f5a0 462field and the file type of the
d3e8b141
MK
463.IR st_mode
464field of the
465.IR stat
466structure.
a03c016c 467POSIX.1-2008 tightens the specification, requiring
d3e8b141 468.BR lstat ()
7901c9c3 469to return valid information in all fields except the mode bits in
d3e8b141
MK
470.IR st_mode .
471
fea681da
MK
472Use of the
473.I st_blocks
474and
475.I st_blksize
c13182ef
MK
476fields may be less portable.
477(They were introduced in BSD.
478The interpretation differs between systems,
479and possibly on a single system when NFS mounts are involved.)
2b2581ee 480.SH NOTES
1ef5b280
MK
481On Linux,
482.BR lstat ()
483will generally not trigger automounter action, whereas
484.BR stat ()
5d5985b8
MK
485will (but see the description of
486.BR fstatat ()
487.B AT_NO_AUTOMOUNT
488fag, above).
9dbc4adb 489.\"
1ef5b280 490.SS Timestamp fields
e9e9e87e
MK
491Older kernels and older standards did not support nanosecond timestamp
492fields.
b8efc3ed 493Instead, there were three timestamp
e9e9e87e
MK
494.RI fields\(em st_atime ,
495.IR st_mtime ,
496and
497.IR st_ctime \(emtyped
b8efc3ed 498as
e9e9e87e
MK
499.IR time_t
500that recorded timestamps with one-second precision.
1ef5b280 501
2b2581ee
MK
502Since kernel 2.5.48, the
503.I stat
f5935752 504structure supports nanosecond resolution for the three file timestamp fields.
e9e9e87e
MK
505The nanosecond components of each timestamp are available
506via names of the form
2a0bd971
MK
507.IR st_atim.tv_nsec ,
508if suitable feature test macros are defined.
509Nanosecond timestamps were standardized in POSIX.1-2008,
510and, starting with version 2.12,
511glibc exposes the nanosecond component names if
f5935752
MK
512.BR _POSIX_C_SOURCE
513is defined with the value 200809L or greater, or
514.BR _XOPEN_SOURCE
515is defined with the value 700 or greater.
2a0bd971
MK
516Up to and including glibc 2.19,
517the definitions of the nanoseconds components are also defined if
518.B _BSD_SOURCE
519or
520.B _SVID_SOURCE
521is defined.
f5935752
MK
522If none of the aforementioned macros are defined,
523then the nanosecond values are exposed with names of the form
524.IR st_atimensec .
e8ff4f53 525.\"
0722a578 526.SS C library/kernel differences
b8f9d9dd
MK
527Over time, increases in the size of the
528.I stat
0c8dd254 529structure have led to three successive versions of
b8f9d9dd
MK
530.BR stat ():
531.IR sys_stat ()
532(slot
533.IR __NR_oldstat ),
534.IR sys_newstat ()
535(slot
536.IR __NR_stat ),
537and
0daa9e92 538.I sys_stat64()
a39f2f4d 539(slot
94971420
MK
540.IR __NR_stat64 )
541on 32-bit platforms such as i386.
542The first two versions were already present in Linux 1.0
543(albeit with different names);
cd358be3 544.\" See include/asm-i386/stat.h in the Linux 2.4 source code for the
94971420
MK
545.\" various versions of the structure definitions
546the last was added in Linux 2.4.
547Similar remarks apply for
548.BR fstat ()
549and
550.BR lstat ().
551
552The kernel-internal versions of the
553.I stat
554structure dealt with by the different versions are, respectively:
94971420
MK
555.TP
556.IR __old_kernel_stat
557The original structure, with rather narrow fields, and no padding.
558.TP
559.IR stat
560Larger
561.I st_ino
562field and padding added to various parts of the structure to
563allow for future expansion.
564.TP
565.IR stat64
566Even larger
567.I st_ino
568field,
569larger
570.I st_uid
571and
572.I st_gid
573fields to accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
574and various other enlarged fields and further padding in the structure.
575(Various padding bytes were eventually consumed in Linux 2.6,
576with the advent of 32-bit device IDs and nanosecond components
577for the timestamp fields.)
94971420 578.PP
d28a0b77 579The glibc
b8f9d9dd 580.BR stat ()
d28a0b77 581wrapper function hides these details from applications,
6f1a1e61
MK
582invoking the most recent version of the system call provided by the kernel,
583and repacking the returned information if required for old binaries.
8179def1
MK
584.\"
585.\" A note from Andries Brouwer, July 2007
e0bf9127
MK
586.\"
587.\" > Is the story not rather more complicated for some calls like
8179def1 588.\" > stat(2)?
e0bf9127 589.\"
8179def1 590.\" Yes and no, mostly no. See /usr/include/sys/stat.h .
e0bf9127 591.\"
8179def1
MK
592.\" The idea is here not so much that syscalls change, but that
593.\" the definitions of struct stat and of the types dev_t and mode_t change.
594.\" This means that libc (even if it does not call the kernel
595.\" but only calls some internal function) must know what the
596.\" format of dev_t or of struct stat is.
597.\" The communication between the application and libc goes via
598.\" the include file <sys/stat.h> that defines a _STAT_VER and
599.\" _MKNOD_VER describing the layout of the data that user space
600.\" uses. Each (almost each) occurrence of stat() is replaced by
601.\" an occurrence of xstat() where the first parameter of xstat()
602.\" is this version number _STAT_VER.
e0bf9127 603.\"
8179def1
MK
604.\" Now, also the definitions used by the kernel change.
605.\" But glibc copes with this in the standard way, and the
606.\" struct stat as returned by the kernel is repacked into
607.\" the struct stat as expected by the application.
608.\" Thus, _STAT_VER and this setup cater for the application-libc
609.\" interface, rather than the libc-kernel interface.
e0bf9127 610.\"
8179def1 611.\" (Note that the details depend on gcc being used as c compiler.)
40084043 612
94971420
MK
613On modern 64-bit systems, life is simpler: there is a single
614.BR stat ()
615system call and the kernel deals with a
616.I stat
617structure that contains fields of a sufficient size.
618
40084043
MK
619The underlying system call employed by the glibc
620.BR fstatat ()
621wrapper function is actually called
cad6df32
MK
622.BR fstatat64 ()
623or, on some architectures,
624.\" strace(1) shows the name "newfstatat" on x86-64
625.BR newfstatat ().
bc7ff20e 626.SH EXAMPLE
988db661 627The following program calls
2777b1ca 628.BR stat ()
bc7ff20e
MK
629and displays selected fields in the returned
630.I stat
631structure.
632.nf
633
634#include <sys/types.h>
635#include <sys/stat.h>
636#include <time.h>
637#include <stdio.h>
988db661 638#include <stdlib.h>
51dbffc5 639#include <sys/sysmacros.h>
bc7ff20e
MK
640
641int
642main(int argc, char *argv[])
643{
644 struct stat sb;
645
646 if (argc != 2) {
647 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
648 exit(EXIT_FAILURE);
649 }
650
29059a65 651 if (stat(argv[1], &sb) == \-1) {
bc7ff20e 652 perror("stat");