xref: /freebsd/lib/libsys/stat.2 (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1.\" Copyright (c) 1980, 1991, 1993, 1994
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd March 30, 2021
29.Dt STAT 2
30.Os
31.Sh NAME
32.Nm stat ,
33.Nm lstat ,
34.Nm fstat ,
35.Nm fstatat
36.Nd get file status
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In sys/stat.h
41.Ft int
42.Fn stat "const char * restrict path" "struct stat * restrict sb"
43.Ft int
44.Fn lstat "const char * restrict path" "struct stat * restrict sb"
45.Ft int
46.Fn fstat "int fd" "struct stat *sb"
47.Ft int
48.Fn fstatat "int fd" "const char *path" "struct stat *sb" "int flag"
49.Sh DESCRIPTION
50The
51.Fn stat
52system call obtains information about the file pointed to by
53.Fa path .
54Read, write or execute
55permission of the named file is not required, but all directories
56listed in the path name leading to the file must be searchable.
57.Pp
58The
59.Fn lstat
60system call is like
61.Fn stat
62except when the named file is a symbolic link,
63in which case
64.Fn lstat
65returns information about the link,
66while
67.Fn stat
68returns information about the file the link references.
69.Pp
70The
71.Fn fstat
72system call obtains the same information about an open file
73known by the file descriptor
74.Fa fd .
75.Pp
76The
77.Fn fstatat
78system call is equivalent to
79.Fn stat
80and
81.Fn lstat
82except when the
83.Fa path
84specifies a relative path.
85For
86.Fn fstatat
87and relative
88.Fa path ,
89the status is retrieved from a file relative to
90the directory associated with the file descriptor
91.Fa fd
92instead of the current working directory.
93.Pp
94The values for the
95.Fa flag
96are constructed by a bitwise-inclusive OR of flags from this list,
97defined in
98.In fcntl.h :
99.Bl -tag -width indent
100.It Dv AT_SYMLINK_NOFOLLOW
101If
102.Fa path
103names a symbolic link, the status of the symbolic link is returned.
104.It Dv AT_RESOLVE_BENEATH
105Only walk paths below the starting directory.
106See the description of the
107.Dv O_RESOLVE_BENEATH
108flag in the
109.Xr open 2
110manual page.
111.It Dv AT_EMPTY_PATH
112If the
113.Fa path
114argument is an empty string, operate on the file or directory
115referenced by the descriptor
116.Fa fd .
117If
118.Fa fd
119is equal to
120.Dv AT_FDCWD ,
121operate on the current working directory.
122.El
123.Pp
124If
125.Fn fstatat
126is passed the special value
127.Dv AT_FDCWD
128in the
129.Fa fd
130parameter, the current working directory is used and the behavior is
131identical to a call to
132.Fn stat
133or
134.Fn lstat
135respectively, depending on whether or not the
136.Dv AT_SYMLINK_NOFOLLOW
137bit is set in
138.Fa flag .
139.Pp
140When
141.Fn fstatat
142is called with an absolute
143.Fa path ,
144it ignores the
145.Fa fd
146argument.
147.Pp
148The
149.Fa sb
150argument is a pointer to a
151.Vt stat
152structure
153as defined by
154.In sys/stat.h
155and into which information is placed concerning the file.
156.Pp
157The fields of
158.Vt "struct stat"
159related to the file system are:
160.Bl -tag -width ".Va st_nlink"
161.It Va st_dev
162Numeric ID of the device containing the file.
163.It Va st_ino
164The file's inode number.
165.It Va st_nlink
166Number of hard links to the file.
167.It Va st_flags
168Flags enabled for the file.
169See
170.Xr chflags 2
171for the list of flags and their description.
172.El
173.Pp
174The
175.Va st_dev
176and
177.Va st_ino
178fields together identify the file uniquely within the system.
179.Pp
180The time-related fields of
181.Vt "struct stat"
182are:
183.Bl -tag -width ".Va st_birthtim"
184.It Va st_atim
185Time when file data was last accessed.
186Changed implicitly by syscalls such as
187.Xr read 2
188and
189.Xr readv 2 ,
190and explicitly by
191.Xr utimes 2 .
192.It Va st_mtim
193Time when file data was last modified.
194Changed implicitly by syscalls such as
195.Xr truncate 2 ,
196.Xr write 2 ,
197and
198.Xr writev 2 ,
199and explicitly by
200.Xr utimes 2 .
201Also, any syscall which modifies directory content changes the
202.Va st_mtim
203for the affected directory.
204For instance,
205.Xr creat 2 ,
206.Xr mkdir 2 ,
207.Xr rename 2 ,
208.Xr link 2 ,
209and
210.Xr unlink 2 .
211.It Va st_ctim
212Time when file status was last changed (inode data modification).
213Changed implicitly by any syscall that affects file metadata, including
214.Va st_mtim ,
215such as
216.Xr chflags 2 ,
217.Xr chmod 2 ,
218.Xr chown 2 ,
219.Xr truncate 2 ,
220.Xr utimes 2 ,
221and
222.Xr write 2 .
223Also, any syscall which modifies directory content changes the
224.Va st_ctim
225for the affected directory.
226For instance,
227.Xr creat 2 ,
228.Xr mkdir 2 ,
229.Xr rename 2 ,
230.Xr link 2 ,
231and
232.Xr unlink 2 .
233.It Va st_birthtim
234Time when the inode was created.
235.El
236.Pp
237These time-related macros are defined for compatibility:
238.Bd -literal
239#define	st_atime		st_atim.tv_sec
240#define	st_mtime		st_mtim.tv_sec
241#define	st_ctime		st_ctim.tv_sec
242#ifndef _POSIX_SOURCE
243#define	st_birthtime		st_birthtim.tv_sec
244#endif
245
246#ifndef _POSIX_SOURCE
247#define	st_atimespec		st_atim
248#define	st_mtimespec		st_mtim
249#define	st_ctimespec		st_ctim
250#define	st_birthtimespec	st_birthtim
251#endif
252.Ed
253.Pp
254Size-related fields of the
255.Vt "struct stat"
256are:
257.Bl -tag -width ".Va st_blksize"
258.It Va st_size
259File size in bytes.
260.It Va st_blksize
261Optimal I/O block size for the file.
262.It Va st_blocks
263Actual number of blocks allocated for the file in 512-byte units.
264As short symbolic links are stored in the inode, this number may
265be zero.
266.El
267.Pp
268The access-related fields of
269.Vt "struct stat"
270are:
271.Bl -tag -width ".Va st_mode"
272.It Va st_uid
273User ID of the file's owner.
274.It Va st_gid
275Group ID of the file.
276.It Va st_mode
277Status of the file (see below).
278.El
279.Pp
280The status information word
281.Fa st_mode
282has these bits:
283.Bd -literal
284#define S_IFMT   0170000  /* type of file mask */
285#define S_IFIFO  0010000  /* named pipe (fifo) */
286#define S_IFCHR  0020000  /* character special */
287#define S_IFDIR  0040000  /* directory */
288#define S_IFBLK  0060000  /* block special */
289#define S_IFREG  0100000  /* regular */
290#define S_IFLNK  0120000  /* symbolic link */
291#define S_IFSOCK 0140000  /* socket */
292#define S_IFWHT  0160000  /* whiteout */
293#define S_ISUID  0004000  /* set user id on execution */
294#define S_ISGID  0002000  /* set group id on execution */
295#define S_ISVTX  0001000  /* save swapped text even after use */
296#define S_IRWXU  0000700  /* RWX mask for owner */
297#define S_IRUSR  0000400  /* read permission, owner */
298#define S_IWUSR  0000200  /* write permission, owner */
299#define S_IXUSR  0000100  /* execute/search permission, owner */
300#define S_IRWXG  0000070  /* RWX mask for group */
301#define S_IRGRP  0000040  /* read permission, group */
302#define S_IWGRP  0000020  /* write permission, group */
303#define S_IXGRP  0000010  /* execute/search permission, group */
304#define S_IRWXO  0000007  /* RWX mask for other */
305#define S_IROTH  0000004  /* read permission, other */
306#define S_IWOTH  0000002  /* write permission, other */
307#define S_IXOTH  0000001  /* execute/search permission, other */
308.Ed
309.Pp
310For a list of access modes, see
311.In sys/stat.h ,
312.Xr access 2
313and
314.Xr chmod 2 .
315These macros are available to test whether a
316.Va st_mode
317value passed in the
318.Fa m
319argument corresponds to a file of the specified type:
320.Bl -tag -width ".Fn S_ISFIFO m"
321.It Fn S_ISBLK m
322Test for a block special file.
323.It Fn S_ISCHR m
324Test for a character special file.
325.It Fn S_ISDIR m
326Test for a directory.
327.It Fn S_ISFIFO m
328Test for a pipe or FIFO special file.
329.It Fn S_ISLNK m
330Test for a symbolic link.
331.It Fn S_ISREG m
332Test for a regular file.
333.It Fn S_ISSOCK m
334Test for a socket.
335.It Fn S_ISWHT m
336Test for a whiteout.
337.El
338.Pp
339The macros evaluate to a non-zero value if the test is true
340or to the value 0 if the test is false.
341.Sh RETURN VALUES
342.Rv -std
343.Sh COMPATIBILITY
344Previous versions of the system used different types for the
345.Va st_dev ,
346.Va st_uid ,
347.Va st_gid ,
348.Va st_rdev ,
349.Va st_size ,
350.Va st_blksize
351and
352.Va st_blocks
353fields.
354.Sh ERRORS
355The
356.Fn stat
357and
358.Fn lstat
359system calls will fail if:
360.Bl -tag -width Er
361.It Bq Er EACCES
362Search permission is denied for a component of the path prefix.
363.It Bq Er EFAULT
364The
365.Fa sb
366or
367.Fa path
368argument
369points to an invalid address.
370.It Bq Er EIO
371An I/O error occurred while reading from or writing to the file system.
372.It Bq Er EINTEGRITY
373Corrupted data was detected while reading from the file system.
374.It Bq Er ELOOP
375Too many symbolic links were encountered in translating the pathname.
376.It Bq Er ENAMETOOLONG
377A component of a pathname exceeded 255 characters,
378or an entire path name exceeded 1023 characters.
379.It Bq Er ENOENT
380The named file does not exist.
381.It Bq Er ENOTDIR
382A component of the path prefix is not a directory.
383.It Bq Er EOVERFLOW
384The file size in bytes cannot be
385represented correctly in the structure pointed to by
386.Fa sb .
387.El
388.Pp
389The
390.Fn fstat
391system call will fail if:
392.Bl -tag -width Er
393.It Bq Er EBADF
394The
395.Fa fd
396argument
397is not a valid open file descriptor.
398.It Bq Er EFAULT
399The
400.Fa sb
401argument
402points to an invalid address.
403.It Bq Er EIO
404An I/O error occurred while reading from or writing to the file system.
405.It Bq Er EINTEGRITY
406Corrupted data was detected while reading from the file system.
407.It Bq Er EOVERFLOW
408The file size in bytes cannot be
409represented correctly in the structure pointed to by
410.Fa sb .
411.El
412.Pp
413In addition to the errors returned by the
414.Fn lstat ,
415the
416.Fn fstatat
417may fail if:
418.Bl -tag -width Er
419.It Bq Er EBADF
420The
421.Fa path
422argument does not specify an absolute path and the
423.Fa fd
424argument is neither
425.Dv AT_FDCWD
426nor a valid file descriptor open for searching.
427.It Bq Er EINVAL
428The value of the
429.Fa flag
430argument is not valid.
431.It Bq Er ENOTDIR
432The
433.Fa path
434argument is not an absolute path and
435.Fa fd
436is neither
437.Dv AT_FDCWD
438nor a file descriptor associated with a directory.
439.It Bq Er ENOTCAPABLE
440.Fa path
441is an absolute path,
442or contained a ".." component leading to a
443directory outside of the directory hierarchy specified by
444.Fa fd ,
445and the process is in capability mode or the
446.Dv AT_RESOLVE_BENEATH
447flag was specified.
448.El
449.Sh SEE ALSO
450.Xr access 2 ,
451.Xr chmod 2 ,
452.Xr chown 2 ,
453.Xr fhstat 2 ,
454.Xr statfs 2 ,
455.Xr utimes 2 ,
456.Xr sticky 7 ,
457.Xr symlink 7
458.Sh STANDARDS
459The
460.Fn stat
461and
462.Fn fstat
463system calls are expected to conform to
464.St -p1003.1-90 .
465The
466.Fn fstatat
467system call follows The Open Group Extended API Set 2 specification.
468.Sh HISTORY
469The
470.Fn stat
471and
472.Fn fstat
473system calls appeared in
474.At v1 .
475The
476.Fn lstat
477system call appeared in
478.Bx 4.2 .
479The
480.Fn fstatat
481system call appeared in
482.Fx 8.0 .
483.Sh BUGS
484Applying
485.Fn fstat
486to a socket
487returns a zeroed buffer,
488except for the blocksize field,
489and a unique device and inode number.
490