1STAT(2) Linux Programmer's Manual STAT(2)
2
3
4
6 stat, fstat, lstat, fstatat - get file status
7
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
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 it 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 file's last access timestamp.
136
137 st_mtime
138 This is the file's last modification timestamp.
139
140 st_ctime
141 This is the file's last status change timestamp.
142
143 For further information on the above fields, see inode(7).
144
145 fstatat()
146 The fstatat() system call is a more general interface for accessing
147 file information which can still provide exactly the behavior of each
148 of stat(), lstat(), and fstat().
149
150 If the pathname given in pathname is relative, then it is interpreted
151 relative to the directory referred to by the file descriptor dirfd
152 (rather than relative to the current working directory of the calling
153 process, as is done by stat() and lstat() for a relative pathname).
154
155 If pathname is relative and dirfd is the special value AT_FDCWD, then
156 pathname is interpreted relative to the current working directory of
157 the calling process (like stat() and lstat()).
158
159 If pathname is absolute, then dirfd is ignored.
160
161 flags can either be 0, or include one or more of the following flags
162 ORed:
163
164 AT_EMPTY_PATH (since Linux 2.6.39)
165 If pathname is an empty string, operate on the file referred to
166 by dirfd (which may have been obtained using the open(2) O_PATH
167 flag). In this case, dirfd can refer to any type of file, not
168 just a directory, and the behavior of fstatat() is similar to
169 that of fstat(). If dirfd is AT_FDCWD, the call operates on the
170 current working directory. This flag is Linux-specific; define
171 _GNU_SOURCE to obtain its definition.
172
173 AT_NO_AUTOMOUNT (since Linux 2.6.38)
174 Don't automount the terminal ("basename") component of pathname
175 if it is a directory that is an automount point. This allows
176 the caller to gather attributes of an automount point (rather
177 than the location it would mount). Since Linux 4.14, also don't
178 instantiate a nonexistent name in an on-demand directory such as
179 used for automounter indirect maps. This flag can be used in
180 tools that scan directories to prevent mass-automounting of a
181 directory of automount points. The AT_NO_AUTOMOUNT flag has no
182 effect if the mount point has already been mounted over. This
183 flag is Linux-specific; define _GNU_SOURCE to obtain its defini‐
184 tion. Both stat() and lstat() act as though AT_NO_AUTOMOUNT was
185 set.
186
187 AT_SYMLINK_NOFOLLOW
188 If pathname is a symbolic link, do not dereference it: instead
189 return information about the link itself, like lstat(). (By
190 default, fstatat() dereferences symbolic links, like stat().)
191
192 See openat(2) for an explanation of the need for fstatat().
193
195 On success, zero is returned. On error, -1 is returned, and errno is
196 set appropriately.
197
199 EACCES Search permission is denied for one of the directories in the
200 path prefix of pathname. (See also path_resolution(7).)
201
202 EBADF fd is not a valid open file descriptor.
203
204 EFAULT Bad address.
205
206 ELOOP Too many symbolic links encountered while traversing the path.
207
208 ENAMETOOLONG
209 pathname is too long.
210
211 ENOENT A component of pathname does not exist or is a dangling symbolic
212 link.
213
214 ENOENT pathname is an empty string and AT_EMPTY_PATH was not specified
215 in flags.
216
217 ENOMEM Out of memory (i.e., kernel memory).
218
219 ENOTDIR
220 A component of the path prefix of pathname is not a directory.
221
222 EOVERFLOW
223 pathname or fd refers to a file whose size, inode number, or
224 number of blocks cannot be represented in, respectively, the
225 types off_t, ino_t, or blkcnt_t. This error can occur when, for
226 example, an application compiled on a 32-bit platform without
227 -D_FILE_OFFSET_BITS=64 calls stat() on a file whose size exceeds
228 (1<<31)-1 bytes.
229
230 The following additional errors can occur for fstatat():
231
232 EBADF dirfd is not a valid file descriptor.
233
234 EINVAL Invalid flag specified in flags.
235
236 ENOTDIR
237 pathname is relative and dirfd is a file descriptor referring to
238 a file other than a directory.
239
241 fstatat() was added to Linux in kernel 2.6.16; library support was
242 added to glibc in version 2.4.
243
245 stat(), fstat(), lstat(): SVr4, 4.3BSD, POSIX.1-2001, POSIX.1.2008.
246
247 fstatat(): POSIX.1-2008.
248
249 According to POSIX.1-2001, lstat() on a symbolic link need return valid
250 information only in the st_size field and the file type of the st_mode
251 field of the stat structure. POSIX.1-2008 tightens the specification,
252 requiring lstat() to return valid information in all fields except the
253 mode bits in st_mode.
254
255 Use of the st_blocks and st_blksize fields may be less portable. (They
256 were introduced in BSD. The interpretation differs between systems,
257 and possibly on a single system when NFS mounts are involved.)
258
260 Timestamp fields
261 Older kernels and older standards did not support nanosecond timestamp
262 fields. Instead, there were three timestamp fields—st_atime, st_mtime,
263 and st_ctime—typed as time_t that recorded timestamps with one-second
264 precision.
265
266 Since kernel 2.5.48, the stat structure supports nanosecond resolution
267 for the three file timestamp fields. The nanosecond components of each
268 timestamp are available via names of the form st_atim.tv_nsec, if suit‐
269 able feature test macros are defined. Nanosecond timestamps were stan‐
270 dardized in POSIX.1-2008, and, starting with version 2.12, glibc
271 exposes the nanosecond component names if _POSIX_C_SOURCE is defined
272 with the value 200809L or greater, or _XOPEN_SOURCE is defined with the
273 value 700 or greater. Up to and including glibc 2.19, the definitions
274 of the nanoseconds components are also defined if _BSD_SOURCE or
275 _SVID_SOURCE is defined. If none of the aforementioned macros are
276 defined, then the nanosecond values are exposed with names of the form
277 st_atimensec.
278
279 C library/kernel differences
280 Over time, increases in the size of the stat structure have led to
281 three successive versions of stat(): sys_stat() (slot __NR_oldstat),
282 sys_newstat() (slot __NR_stat), and sys_stat64() (slot __NR_stat64) on
283 32-bit platforms such as i386. The first two versions were already
284 present in Linux 1.0 (albeit with different names); the last was added
285 in Linux 2.4. Similar remarks apply for fstat() and lstat().
286
287 The kernel-internal versions of the stat structure dealt with by the
288 different versions are, respectively:
289
290 __old_kernel_stat
291 The original structure, with rather narrow fields, and no pad‐
292 ding.
293
294 stat Larger st_ino field and padding added to various parts of the
295 structure to allow for future expansion.
296
297 stat64 Even larger st_ino field, larger st_uid and st_gid fields to
298 accommodate the Linux-2.4 expansion of UIDs and GIDs to 32 bits,
299 and various other enlarged fields and further padding in the
300 structure. (Various padding bytes were eventually consumed in
301 Linux 2.6, with the advent of 32-bit device IDs and nanosecond
302 components for the timestamp fields.)
303
304 The glibc stat() wrapper function hides these details from applica‐
305 tions, invoking the most recent version of the system call provided by
306 the kernel, and repacking the returned information if required for old
307 binaries.
308
309 On modern 64-bit systems, life is simpler: there is a single stat()
310 system call and the kernel deals with a stat structure that contains
311 fields of a sufficient size.
312
313 The underlying system call employed by the glibc fstatat() wrapper
314 function is actually called fstatat64() or, on some architectures,
315 newfstatat().
316
318 The following program calls lstat() and displays selected fields in the
319 returned stat structure.
320
321 #include <sys/types.h>
322 #include <sys/stat.h>
323 #include <time.h>
324 #include <stdio.h>
325 #include <stdlib.h>
326 #include <sys/sysmacros.h>
327
328 int
329 main(int argc, char *argv[])
330 {
331 struct stat sb;
332
333 if (argc != 2) {
334 fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
335 exit(EXIT_FAILURE);
336 }
337
338 if (lstat(argv[1], &sb) == -1) {
339 perror("lstat");
340 exit(EXIT_FAILURE);
341 }
342
343 printf("ID of containing device: [%lx,%lx]\n",
344 (long) major(sb.st_dev), (long) minor(sb.st_dev));
345
346 printf("File type: ");
347
348 switch (sb.st_mode & S_IFMT) {
349 case S_IFBLK: printf("block device\n"); break;
350 case S_IFCHR: printf("character device\n"); break;
351 case S_IFDIR: printf("directory\n"); break;
352 case S_IFIFO: printf("FIFO/pipe\n"); break;
353 case S_IFLNK: printf("symlink\n"); break;
354 case S_IFREG: printf("regular file\n"); break;
355 case S_IFSOCK: printf("socket\n"); break;
356 default: printf("unknown?\n"); break;
357 }
358
359 printf("I-node number: %ld\n", (long) sb.st_ino);
360
361 printf("Mode: %lo (octal)\n",
362 (unsigned long) sb.st_mode);
363
364 printf("Link count: %ld\n", (long) sb.st_nlink);
365 printf("Ownership: UID=%ld GID=%ld\n",
366 (long) sb.st_uid, (long) sb.st_gid);
367
368 printf("Preferred I/O block size: %ld bytes\n",
369 (long) sb.st_blksize);
370 printf("File size: %lld bytes\n",
371 (long long) sb.st_size);
372 printf("Blocks allocated: %lld\n",
373 (long long) sb.st_blocks);
374
375 printf("Last status change: %s", ctime(&sb.st_ctime));
376 printf("Last file access: %s", ctime(&sb.st_atime));
377 printf("Last file modification: %s", ctime(&sb.st_mtime));
378
379 exit(EXIT_SUCCESS);
380 }
381
383 ls(1), stat(1), access(2), chmod(2), chown(2), readlink(2), statx(2),
384 utime(2), capabilities(7), inode(7), symlink(7)
385
387 This page is part of release 5.02 of the Linux man-pages project. A
388 description of the project, information about reporting bugs, and the
389 latest version of this page, can be found at
390 https://www.kernel.org/doc/man-pages/.
391
392
393
394Linux 2019-03-06 STAT(2)