1STAT(2)                    Linux Programmer's Manual                   STAT(2)
2
3
4

NAME

6       stat, fstat, lstat, fstatat - get file status
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/stat.h>
11       #include <unistd.h>
12
13       int stat(const char *pathname, struct stat *statbuf);
14       int fstat(int fd, struct stat *statbuf);
15       int lstat(const char *pathname, struct stat *statbuf);
16
17       #include <fcntl.h>           /* Definition of AT_* constants */
18       #include <sys/stat.h>
19
20       int fstatat(int dirfd, const char *pathname, struct stat *statbuf,
21                   int flags);
22
23   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
24
25       lstat():
26           /* glibc 2.19 and earlier */ _BSD_SOURCE
27               || /* Since glibc 2.20 */ _DEFAULT_SOURCE
28               || _XOPEN_SOURCE >= 500
29               || /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L
30
31       fstatat():
32           Since glibc 2.10:
33               _POSIX_C_SOURCE >= 200809L
34           Before glibc 2.10:
35               _ATFILE_SOURCE
36

DESCRIPTION

38       These  functions return information about a file, in the buffer pointed
39       to by statbuf.  No permissions are required on the file itself,  but—in
40       the  case of stat(), fstatat(), and lstat()—execute (search) permission
41       is required on all of the directories in  pathname  that  lead  to  the
42       file.
43
44       stat()  and fstatat() retrieve information about the file pointed to by
45       pathname; the differences for fstatat() are described below.
46
47       lstat() is identical to stat(), except that if pathname is  a  symbolic
48       link,  then  it returns information about the link itself, not the file
49       that the link refers to.
50
51       fstat() is identical to stat(), except that the file about which infor‐
52       mation is to be retrieved is specified by the file descriptor fd.
53
54   The stat structure
55       All  of  these system calls return a stat structure, which contains the
56       following fields:
57
58           struct stat {
59               dev_t     st_dev;         /* ID of device containing file */
60               ino_t     st_ino;         /* Inode number */
61               mode_t    st_mode;        /* File type and mode */
62               nlink_t   st_nlink;       /* Number of hard links */
63               uid_t     st_uid;         /* User ID of owner */
64               gid_t     st_gid;         /* Group ID of owner */
65               dev_t     st_rdev;        /* Device ID (if special file) */
66               off_t     st_size;        /* Total size, in bytes */
67               blksize_t st_blksize;     /* Block size for filesystem I/O */
68               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */
69
70               /* Since Linux 2.6, the kernel supports nanosecond
71                  precision for the following timestamp fields.
72                  For the details before Linux 2.6, see NOTES. */
73
74               struct timespec st_atim;  /* Time of last access */
75               struct timespec st_mtim;  /* Time of last modification */
76               struct timespec st_ctim;  /* Time of last status change */
77
78           #define st_atime st_atim.tv_sec      /* Backward compatibility */
79           #define st_mtime st_mtim.tv_sec
80           #define st_ctime st_ctim.tv_sec
81           };
82
83       Note: the order of fields in the stat structure varies somewhat  across
84       architectures.   In  addition,  the  definition above does not show the
85       padding bytes that may be present between some fields on various archi‐
86       tectures.  Consult the glibc and kernel source code if you need to know
87       the details.
88
89       Note: for performance and simplicity reasons, different fields  in  the
90       stat  structure  may  contain  state information from different moments
91       during the execution of the system call.  For example,  if  st_mode  or
92       st_uid  is  changed by another process by calling chmod(2) or chown(2),
93       stat() might return the old st_mode together with the  new  st_uid,  or
94       the old st_uid together with the new st_mode.
95
96       The fields in the stat structure are as follows:
97
98       st_dev This  field  describes  the  device  on which this file resides.
99              (The major(3) and minor(3) macros may be useful to decompose the
100              device ID in this field.)
101
102       st_ino This field contains the file's inode number.
103
104       st_mode
105              This  field  contains  the file type and mode.  See inode(7) for
106              further information.
107
108       st_nlink
109              This field contains the number of hard links to the file.
110
111       st_uid This field contains the user ID of the owner of the file.
112
113       st_gid This field contains the ID of the group owner of the file.
114
115       st_rdev
116              This field describes the device that this  file  (inode)  repre‐
117              sents.
118
119       st_size
120              This  field  gives the size of the file (if it is a regular file
121              or a symbolic link) in bytes.  The size of a  symbolic  link  is
122              the  length  of  the pathname it contains, without a terminating
123              null byte.
124
125       st_blksize
126              This field  gives  the  "preferred"  block  size  for  efficient
127              filesystem I/O.
128
129       st_blocks
130              This field indicates the number of blocks allocated to the file,
131              in 512-byte units.  (This may be smaller than  st_size/512  when
132              the file has holes.)
133
134       st_atime
135              This is the time of the last access of file data.
136
137       st_mtime
138              This is the time of last modification of file data.
139
140       st_ctime
141              This  is  the  file's last status change timestamp (time of last
142              change to the inode).
143
144       For further information on the above fields, see inode(7).
145
146   fstatat()
147       The fstatat() system call is a more  general  interface  for  accessing
148       file  information  which can still provide exactly the behavior of each
149       of stat(), lstat(), and fstat().
150
151       If the pathname given in pathname is relative, then it  is  interpreted
152       relative  to  the  directory  referred  to by the file descriptor dirfd
153       (rather than relative to the current working directory of  the  calling
154       process, as is done by stat() and lstat() for a relative pathname).
155
156       If  pathname  is relative and dirfd is the special value AT_FDCWD, then
157       pathname is interpreted relative to the current  working  directory  of
158       the calling process (like stat() and lstat()).
159
160       If pathname is absolute, then dirfd is ignored.
161
162       flags  can  either  be 0, or include one or more of the following flags
163       ORed:
164
165       AT_EMPTY_PATH (since Linux 2.6.39)
166              If pathname is an empty string, operate on the file referred  to
167              by  dirfd (which may have been obtained using the open(2) O_PATH
168              flag).  In this case, dirfd can refer to any type of  file,  not
169              just  a  directory,  and the behavior of fstatat() is similar to
170              that of fstat().  If dirfd is AT_FDCWD, the call operates on the
171              current  working directory.  This flag is Linux-specific; define
172              _GNU_SOURCE to obtain its definition.
173
174       AT_NO_AUTOMOUNT (since Linux 2.6.38)
175              Don't automount the terminal ("basename") component of  pathname
176              if  it  is  a directory that is an automount point.  This allows
177              the caller to gather attributes of an  automount  point  (rather
178              than the location it would mount).  Since Linux 4.14, also don't
179              instantiate a nonexistent name in an on-demand directory such as
180              used  for automounter indirect maps.  This flag has no effect if
181              the mount point has already been mounted over.
182
183              Both stat() and lstat() act as though AT_NO_AUTOMOUNT was set.
184
185              The AT_NO_AUTOMOUNT can be used in tools that  scan  directories
186              to prevent mass-automounting of a directory of automount points.
187
188              This  flag  is  Linux-specific; define _GNU_SOURCE to obtain its
189              definition.
190
191       AT_SYMLINK_NOFOLLOW
192              If pathname is a symbolic link, do not dereference  it:  instead
193              return information about the link itself, like lstat().  (By de‐
194              fault, fstatat() dereferences symbolic links, like stat().)
195
196       See openat(2) for an explanation of the need for fstatat().
197

RETURN VALUE

199       On success, zero is returned.  On error, -1 is returned, and  errno  is
200       set appropriately.
201

ERRORS

203       EACCES Search  permission  is  denied for one of the directories in the
204              path prefix of pathname.  (See also path_resolution(7).)
205
206       EBADF  fd is not a valid open file descriptor.
207
208       EFAULT Bad address.
209
210       ELOOP  Too many symbolic links encountered while traversing the path.
211
212       ENAMETOOLONG
213              pathname is too long.
214
215       ENOENT A component of pathname does not exist or is a dangling symbolic
216              link.
217
218       ENOENT pathname  is an empty string and AT_EMPTY_PATH was not specified
219              in flags.
220
221       ENOMEM Out of memory (i.e., kernel memory).
222
223       ENOTDIR
224              A component of the path prefix of pathname is not a directory.
225
226       EOVERFLOW
227              pathname or fd refers to a file whose  size,  inode  number,  or
228              number  of  blocks  cannot  be represented in, respectively, the
229              types off_t, ino_t, or blkcnt_t.  This error can occur when, for
230              example,  an  application  compiled on a 32-bit platform without
231              -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds
232              (1<<31)-1 bytes.
233
234       The following additional errors can occur for fstatat():
235
236       EBADF  dirfd is not a valid file descriptor.
237
238       EINVAL Invalid flag specified in flags.
239
240       ENOTDIR
241              pathname is relative and dirfd is a file descriptor referring to
242              a file other than a directory.
243

VERSIONS

245       fstatat() was added to Linux in  kernel  2.6.16;  library  support  was
246       added to glibc in version 2.4.
247

CONFORMING TO

249       stat(), fstat(), lstat(): SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
250
251       fstatat(): POSIX.1-2008.
252
253       According to POSIX.1-2001, lstat() on a symbolic link need return valid
254       information only in the st_size field and the file type of the  st_mode
255       field  of the stat structure.  POSIX.1-2008 tightens the specification,
256       requiring lstat() to return valid information in all fields except  the
257       mode bits in st_mode.
258
259       Use of the st_blocks and st_blksize fields may be less portable.  (They
260       were introduced in BSD.  The interpretation  differs  between  systems,
261       and possibly on a single system when NFS mounts are involved.)
262

NOTES

264   Timestamp fields
265       Older  kernels and older standards did not support nanosecond timestamp
266       fields.  Instead, there were three timestamp fields—st_atime, st_mtime,
267       and  st_ctime—typed  as time_t that recorded timestamps with one-second
268       precision.
269
270       Since kernel 2.5.48, the stat structure supports nanosecond  resolution
271       for the three file timestamp fields.  The nanosecond components of each
272       timestamp are available via names of the form st_atim.tv_nsec, if suit‐
273       able feature test macros are defined.  Nanosecond timestamps were stan‐
274       dardized in POSIX.1-2008, and, starting with version  2.12,  glibc  ex‐
275       poses the nanosecond component names if _POSIX_C_SOURCE is defined with
276       the value 200809L or greater, or  _XOPEN_SOURCE  is  defined  with  the
277       value  700 or greater.  Up to and including glibc 2.19, the definitions
278       of the nanoseconds  components  are  also  defined  if  _BSD_SOURCE  or
279       _SVID_SOURCE  is defined.  If none of the aforementioned macros are de‐
280       fined, then the nanosecond values are exposed with names  of  the  form
281       st_atimensec.
282
283   C library/kernel differences
284       Over  time,  increases  in  the  size of the stat structure have led to
285       three successive versions of stat():  sys_stat()  (slot  __NR_oldstat),
286       sys_newstat()  (slot __NR_stat), and sys_stat64() (slot __NR_stat64) on
287       32-bit platforms such as i386.  The first  two  versions  were  already
288       present  in Linux 1.0 (albeit with different names); the last was added
289       in Linux 2.4.  Similar remarks apply for fstat() and lstat().
290
291       The kernel-internal versions of the stat structure dealt  with  by  the
292       different versions are, respectively:
293
294       __old_kernel_stat
295              The  original  structure, with rather narrow fields, and no pad‐
296              ding.
297
298       stat   Larger st_ino field and padding added to various  parts  of  the
299              structure to allow for future expansion.
300
301       stat64 Even larger st_ino field, larger st_uid and st_gid fields to ac‐
302              commodate the Linux-2.4 expansion of UIDs and GIDs to  32  bits,
303              and  various  other  enlarged  fields and further padding in the
304              structure.  (Various padding bytes were eventually  consumed  in
305              Linux  2.6,  with the advent of 32-bit device IDs and nanosecond
306              components for the timestamp fields.)
307
308       The glibc stat() wrapper function hides  these  details  from  applica‐
309       tions,  invoking the most recent version of the system call provided by
310       the kernel, and repacking the returned information if required for  old
311       binaries.
312
313       On  modern  64-bit  systems,  life is simpler: there is a single stat()
314       system call and the kernel deals with a stat  structure  that  contains
315       fields of a sufficient size.
316
317       The  underlying  system  call  employed  by the glibc fstatat() wrapper
318       function is actually called  fstatat64()  or,  on  some  architectures,
319       newfstatat().
320

EXAMPLES

322       The following program calls lstat() and displays selected fields in the
323       returned stat structure.
324
325       #include <sys/types.h>
326       #include <sys/stat.h>
327       #include <stdint.h>
328       #include <time.h>
329       #include <stdio.h>
330       #include <stdlib.h>
331       #include <sys/sysmacros.h>
332
333       int
334       main(int argc, char *argv[])
335       {
336           struct stat sb;
337
338           if (argc != 2) {
339               fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
340               exit(EXIT_FAILURE);
341           }
342
343           if (lstat(argv[1], &sb) == -1) {
344               perror("lstat");
345               exit(EXIT_FAILURE);
346           }
347
348           printf("ID of containing device:  [%jx,%jx]\n",
349                   (uintmax_t) major(sb.st_dev),
350                   (uintmax_t) minor(sb.st_dev));
351
352           printf("File type:                ");
353
354           switch (sb.st_mode & S_IFMT) {
355           case S_IFBLK:  printf("block device\n");            break;
356           case S_IFCHR:  printf("character device\n");        break;
357           case S_IFDIR:  printf("directory\n");               break;
358           case S_IFIFO:  printf("FIFO/pipe\n");               break;
359           case S_IFLNK:  printf("symlink\n");                 break;
360           case S_IFREG:  printf("regular file\n");            break;
361           case S_IFSOCK: printf("socket\n");                  break;
362           default:       printf("unknown?\n");                break;
363           }
364
365           printf("I-node number:            %ju\n", (uintmax_t) sb.st_ino);
366
367           printf("Mode:                     %jo (octal)\n",
368                   (uintmax_t) sb.st_mode);
369
370           printf("Link count:               %ju\n", (uintmax_t) sb.st_nlink);
371           printf("Ownership:                UID=%ju   GID=%ju\n",
372                   (uintmax_t) sb.st_uid, (uintmax_t) sb.st_gid);
373
374           printf("Preferred I/O block size: %jd bytes\n",
375                   (intmax_t) sb.st_blksize);
376           printf("File size:                %jd bytes\n",
377                   (intmax_t) sb.st_size);
378           printf("Blocks allocated:         %jd\n",
379                   (intmax_t) sb.st_blocks);
380
381           printf("Last status change:       %s", ctime(&sb.st_ctime));
382           printf("Last file access:         %s", ctime(&sb.st_atime));
383           printf("Last file modification:   %s", ctime(&sb.st_mtime));
384
385           exit(EXIT_SUCCESS);
386       }
387

SEE ALSO

389       ls(1), stat(1), access(2), chmod(2), chown(2),  readlink(2),  statx(2),
390       utime(2), capabilities(7), inode(7), symlink(7)
391

COLOPHON

393       This  page  is  part of release 5.10 of the Linux man-pages project.  A
394       description of the project, information about reporting bugs,  and  the
395       latest     version     of     this    page,    can    be    found    at
396       https://www.kernel.org/doc/man-pages/.
397
398
399
400Linux                             2020-08-13                           STAT(2)
Impressum