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