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