1STAT(2) Linux Programmer's Manual STAT(2)
2
3
4
6 stat, fstat, lstat, fstatat - get file status
7
9 #include <sys/stat.h>
10
11 int stat(const char *restrict pathname,
12 struct stat *restrict statbuf);
13 int fstat(int fd, struct stat *statbuf);
14 int lstat(const char *restrict pathname,
15 struct stat *restrict statbuf);
16
17 #include <fcntl.h> /* Definition of AT_* constants */
18 #include <sys/stat.h>
19
20 int fstatat(int dirfd, const char *restrict pathname,
21 struct stat *restrict statbuf, int flags);
22
23 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
24
25 lstat():
26 /* Since glibc 2.20 */ _DEFAULT_SOURCE
27 || _XOPEN_SOURCE >= 500
28 || /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L
29 || /* Glibc 2.19 and earlier */ _BSD_SOURCE
30
31 fstatat():
32 Since glibc 2.10:
33 _POSIX_C_SOURCE >= 200809L
34 Before glibc 2.10:
35 _ATFILE_SOURCE
36
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
199 On success, zero is returned. On error, -1 is returned, and errno is
200 set to indicate the error.
201
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 EBADF (fstatat()) pathname is relative but dirfd is neither AT_FDCWD
209 nor a valid file descriptor.
210
211 EFAULT Bad address.
212
213 EINVAL (fstatat()) Invalid flag specified in flags.
214
215 ELOOP Too many symbolic links encountered while traversing the path.
216
217 ENAMETOOLONG
218 pathname is too long.
219
220 ENOENT A component of pathname does not exist or is a dangling symbolic
221 link.
222
223 ENOENT pathname is an empty string and AT_EMPTY_PATH was not specified
224 in flags.
225
226 ENOMEM Out of memory (i.e., kernel memory).
227
228 ENOTDIR
229 A component of the path prefix of pathname is not a directory.
230
231 ENOTDIR
232 (fstatat()) pathname is relative and dirfd is a file descriptor
233 referring to a file other than a directory.
234
235 EOVERFLOW
236 pathname or fd refers to a file whose size, inode number, or
237 number of blocks cannot be represented in, respectively, the
238 types off_t, ino_t, or blkcnt_t. This error can occur when, for
239 example, an application compiled on a 32-bit platform without
240 -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds
241 (1<<31)-1 bytes.
242
244 fstatat() was added to Linux in kernel 2.6.16; library support was
245 added to glibc in version 2.4.
246
248 stat(), fstat(), lstat(): SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
249
250 fstatat(): POSIX.1-2008.
251
252 According to POSIX.1-2001, lstat() on a symbolic link need return valid
253 information only in the st_size field and the file type of the st_mode
254 field of the stat structure. POSIX.1-2008 tightens the specification,
255 requiring lstat() to return valid information in all fields except the
256 mode bits in st_mode.
257
258 Use of the st_blocks and st_blksize fields may be less portable. (They
259 were introduced in BSD. The interpretation differs between systems,
260 and possibly on a single system when NFS mounts are involved.)
261
263 Timestamp fields
264 Older kernels and older standards did not support nanosecond timestamp
265 fields. Instead, there were three timestamp fields—st_atime, st_mtime,
266 and st_ctime—typed as time_t that recorded timestamps with one-second
267 precision.
268
269 Since kernel 2.5.48, the stat structure supports nanosecond resolution
270 for the three file timestamp fields. The nanosecond components of each
271 timestamp are available via names of the form st_atim.tv_nsec, if suit‐
272 able feature test macros are defined. Nanosecond timestamps were stan‐
273 dardized in POSIX.1-2008, and, starting with version 2.12, glibc ex‐
274 poses the nanosecond component names if _POSIX_C_SOURCE is defined with
275 the value 200809L or greater, or _XOPEN_SOURCE is defined with the
276 value 700 or greater. Up to and including glibc 2.19, the definitions
277 of the nanoseconds components are also defined if _BSD_SOURCE or
278 _SVID_SOURCE is defined. If none of the aforementioned macros are de‐
279 fined, then the nanosecond values are exposed with names of the form
280 st_atimensec.
281
282 C library/kernel differences
283 Over time, increases in the size of the stat structure have led to
284 three successive versions of stat(): sys_stat() (slot __NR_oldstat),
285 sys_newstat() (slot __NR_stat), and sys_stat64() (slot __NR_stat64) on
286 32-bit platforms such as i386. The first two versions were already
287 present in Linux 1.0 (albeit with different names); the last was added
288 in Linux 2.4. Similar remarks apply for fstat() and lstat().
289
290 The kernel-internal versions of the stat structure dealt with by the
291 different versions are, respectively:
292
293 __old_kernel_stat
294 The original structure, with rather narrow fields, and no pad‐
295 ding.
296
297 stat Larger st_ino field and padding added to various parts of the
298 structure to allow for future expansion.
299
300 stat64 Even larger st_ino field, larger st_uid and st_gid fields to ac‐
301 commodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
302 and various other enlarged fields and further padding in the
303 structure. (Various padding bytes were eventually consumed in
304 Linux 2.6, with the advent of 32-bit device IDs and nanosecond
305 components for the timestamp fields.)
306
307 The glibc stat() wrapper function hides these details from applica‐
308 tions, invoking the most recent version of the system call provided by
309 the kernel, and repacking the returned information if required for old
310 binaries.
311
312 On modern 64-bit systems, life is simpler: there is a single stat()
313 system call and the kernel deals with a stat structure that contains
314 fields of a sufficient size.
315
316 The underlying system call employed by the glibc fstatat() wrapper
317 function is actually called fstatat64() or, on some architectures,
318 newfstatat().
319
321 The following program calls lstat() and displays selected fields in the
322 returned stat structure.
323
324 #include <sys/types.h>
325 #include <sys/stat.h>
326 #include <stdint.h>
327 #include <time.h>
328 #include <stdio.h>
329 #include <stdlib.h>
330 #include <sys/sysmacros.h>
331
332 int
333 main(int argc, char *argv[])
334 {
335 struct stat sb;
336
337 if (argc != 2) {
338 fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
339 exit(EXIT_FAILURE);
340 }
341
342 if (lstat(argv[1], &sb) == -1) {
343 perror("lstat");
344 exit(EXIT_FAILURE);
345 }
346
347 printf("ID of containing device: [%jx,%jx]\n",
348 (uintmax_t) major(sb.st_dev),
349 (uintmax_t) minor(sb.st_dev));
350
351 printf("File type: ");
352
353 switch (sb.st_mode & S_IFMT) {
354 case S_IFBLK: printf("block device\n"); break;
355 case S_IFCHR: printf("character device\n"); break;
356 case S_IFDIR: printf("directory\n"); break;
357 case S_IFIFO: printf("FIFO/pipe\n"); break;
358 case S_IFLNK: printf("symlink\n"); break;
359 case S_IFREG: printf("regular file\n"); break;
360 case S_IFSOCK: printf("socket\n"); break;
361 default: printf("unknown?\n"); break;
362 }
363
364 printf("I-node number: %ju\n", (uintmax_t) sb.st_ino);
365
366 printf("Mode: %jo (octal)\n",
367 (uintmax_t) sb.st_mode);
368
369 printf("Link count: %ju\n", (uintmax_t) sb.st_nlink);
370 printf("Ownership: UID=%ju GID=%ju\n",
371 (uintmax_t) sb.st_uid, (uintmax_t) sb.st_gid);
372
373 printf("Preferred I/O block size: %jd bytes\n",
374 (intmax_t) sb.st_blksize);
375 printf("File size: %jd bytes\n",
376 (intmax_t) sb.st_size);
377 printf("Blocks allocated: %jd\n",
378 (intmax_t) sb.st_blocks);
379
380 printf("Last status change: %s", ctime(&sb.st_ctime));
381 printf("Last file access: %s", ctime(&sb.st_atime));
382 printf("Last file modification: %s", ctime(&sb.st_mtime));
383
384 exit(EXIT_SUCCESS);
385 }
386
388 ls(1), stat(1), access(2), chmod(2), chown(2), readlink(2), statx(2),
389 utime(2), capabilities(7), inode(7), symlink(7)
390
392 This page is part of release 5.13 of the Linux man-pages project. A
393 description of the project, information about reporting bugs, and the
394 latest version of this page, can be found at
395 https://www.kernel.org/doc/man-pages/.
396
397
398
399Linux 2021-08-27 STAT(2)