xref: /netbsd-src/lib/libc/sys/stat.2 (revision a374ef689d591d7ee46526368abb86b78f7fdd51)
1.\"	$NetBSD: stat.2,v 1.60 2023/10/15 20:37:04 jschauma Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993, 1994
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)stat.2	8.4 (Berkeley) 5/1/95
31.\"
32.Dd October 15, 2023
33.Dt STAT 2
34.Os
35.Sh NAME
36.Nm stat ,
37.Nm lstat ,
38.Nm fstat ,
39.Nm fstatat
40.Nd get file status
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In sys/stat.h
45.Ft int
46.Fn stat "const char *path" "struct stat *sb"
47.Ft int
48.Fn lstat "const char *path" "struct stat *sb"
49.Ft int
50.Fn fstat "int fd" "struct stat *sb"
51.In sys/stat.h
52.In fcntl.h
53.Ft int
54.Fn fstatat "int fd" "const char *path" "struct stat *sb" "int flag"
55.Sh DESCRIPTION
56The
57.Fn stat
58function obtains information about the file pointed to by
59.Fa path .
60Read, write or execute
61permission of the named file is not required, but all directories
62listed in the path name leading to the file must be searchable.
63.Pp
64The function
65.Fn lstat
66is like
67.Fn stat
68except in the case where the named file is a symbolic link,
69in which case
70.Fn lstat
71returns information about the link,
72while
73.Fn stat
74returns information about the file the link references.
75The
76.Fn fstat
77function obtains the same information about an open file
78known by the file descriptor
79.Fa fd .
80.Pp
81.Fn fstatat
82works the same way as
83.Fn stat
84(or
85.Fn lstat
86if
87.Dv AT_SYMLINK_NOFOLLOW
88is set in
89.Fa flag )
90except if
91.Fa path
92is relative.
93In that case, it is looked up from a directory whose file
94descriptor was passed as
95.Fa fd .
96Search permission is required on this directory.
97.\"    (These alternatives await a decision about the semantics of O_SEARCH)
98.\" Search permission is required on this directory
99.\" except if
100.\" .Fa fd
101.\" was opened with the
102.\" .Dv O_SEARCH
103.\" flag.
104.\"    - or -
105.\" This file descriptor must have been opened with the
106.\" .Dv O_SEARCH
107.\" flag.
108.Fa fd
109can be set to
110.Dv AT_FDCWD
111in order to specify the current directory.
112.Pp
113The
114.Fa sb
115argument is a pointer to a
116.Fa stat
117structure
118as defined by
119.In sys/stat.h
120and into which information is placed concerning the file.
121.Ss The Standard Structure
122The following standards-compliant fields are defined in the structure:
123.Bl -column -offset indent \
124"nlink_t " "st_nlink " "Description"
125.It Sy Type Ta Sy Entry Ta Sy Description
126.It Vt dev_t Ta st_dev Ta device ID containing the file
127.It Vt ino_t Ta st_ino Ta serial number of the file (inode number)
128.It Vt mode_t Ta st_mode Ta mode of the file
129.It Vt nlink_t Ta st_nlink Ta number of hard links to the file
130.It Vt uid_t Ta st_uid Ta user ID of the owner
131.It Vt gid_t Ta st_gid Ta group ID of the owner
132.It Vt dev_t Ta st_rdev Ta device type (character or block special)
133.It Vt off_t Ta st_size Ta size of the file in bytes
134.It Vt time_t Ta st_atime Ta time of last access
135.It Vt time_t Ta st_mtime Ta time of last data modification
136.It Vt time_t Ta st_ctime Ta  time of last file status change
137.It Vt blksize_t Ta st_blksize Ta preferred I/O block size (fs-specific)
138.It Vt blkcnt_t Ta st_blocks Ta blocks allocated for the file
139.El
140.Pp
141These are specified in the
142.St -p1003.1-2004
143standard.
144The
145.Va st_ino
146and
147.Va st_dev
148fields taken together uniquely identify the file within the system.
149Most of the types are defined in
150.Xr types 3 .
151.Pp
152The time-related fields are:
153.Bl -tag -width st_blksize -offset indent
154.It Va st_atime
155Time when file data was last accessed.
156Changed by the
157.Xr mknod 2 ,
158.Xr utimes 2 ,
159and
160.Xr read 2
161system calls.
162.It Va st_mtime
163Time when file data was last modified.
164Changed by the
165.Xr mknod 2 ,
166.Xr utimes 2 ,
167and
168.Xr write 2
169system calls.
170.It Va st_ctime
171Time when file status was last changed (file metadata modification).
172Changed by the
173.Xr chflags 2 ,
174.Xr chmod 2 ,
175.Xr chown 2 ,
176.Xr link 2 ,
177.Xr mknod 2 ,
178.Xr rename 2 ,
179.Xr unlink 2 ,
180.Xr utimes 2 ,
181and
182.Xr write 2
183system calls.
184.El
185.Pp
186The size-related fields of the
187.Fa struct stat
188are as follows:
189.Bl -tag -width st_blksize -offset indent
190.It Va st_size
191The size of the file in bytes.
192The meaning of the size reported for a directory is file system
193dependent.
194Some file systems (e.g. FFS) return the total size used for the
195directory metadata, possibly including free slots; others (notably
196ZFS) return the number of entries in the directory.
197Some may also return other things or always report zero.
198.It Va st_blksize
199The optimal I/O block size for the file.
200.It Va st_blocks
201The actual number of blocks allocated for the file in 512-byte units.
202As short symbolic links are stored in the inode, this number may
203be zero.
204.El
205.Pp
206The status information word
207.Fa st_mode
208contains bits that define the access mode (see
209.Xr chmod 2 )
210and the type (see
211.Xr dirent 3 )
212of the file.
213The following macros can be used to test
214whether a file is of the specified type.
215The value
216.Fa m
217supplied to the macros is the value of
218.Va st_mode .
219.Bl -tag -width "S_ISSOCK(m)" -offset indent
220.It Fn S_ISBLK "m"
221Test for a block special file.
222.It Fn S_ISCHR "m"
223Test for a character special file.
224.It Fn S_ISDIR "m"
225Test for a directory.
226.It Fn S_ISFIFO "m"
227Test for a pipe or FIFO special file.
228.It Fn S_ISLNK "m"
229Test for a symbolic link.
230.It Fn S_ISREG "m"
231Test for a regular file.
232.It Fn S_ISSOCK "m"
233Test for a socket.
234.It Fn S_ISWHT "m"
235Test for a whiteout file.
236.El
237.Pp
238The macros evaluate to a non-zero value if the test
239is true or to the value 0 if the test is false.
240.Ss NetBSD Extensions
241The following additional
242.Nx
243specific fields are present:
244.Bl -column -offset indent \
245"uint32_t" "st_birthtimensec" "Description"
246.It Sy Type Ta Sy Entry Ta Sy Description
247.It Vt long Ta st_atimensec Ta last access (nanoseconds)
248.It Vt long Ta st_mtimensec Ta last modification (nanoseconds)
249.It Vt long Ta st_ctimensec Ta last status change (nanoseconds)
250.It Vt time_t Ta st_birthtime Ta time of inode creation
251.It Vt long Ta st_birthtimensec Ta inode creation (nanoseconds)
252.It Vt uint32_t Ta st_flags Ta user defined flags for the file
253.It Vt uint32_t Ta st_gen Ta file generation number
254.\"
255.\" XXX: What is this?
256.\"
257.It Vt uint32_t Ta st_spare[2] Ta implementation detail
258.El
259.Pp
260However, if
261_NETBSD_SOURCE
262is furthermore defined, instead of the above,
263the following are present in the structure:
264.Bl -column -offset indent \
265"struct timespec " "st_birthtimensec" "Description"
266.It Sy Type Ta Sy Entry Ta Sy Description
267.It Vt struct timespec Ta st_atimespec Ta time of last access
268.It Vt struct timespec Ta st_mtimespec Ta time of last modification
269.It Vt struct timespec Ta st_birthtimespec Ta time of creation
270.It Vt uint32_t Ta st_flags Ta user defined flags
271.It Vt uint32_t Ta st_gen Ta file generation number
272.\"
273.\" XXX: What is this?
274.\"
275.It Vt uint32_t Ta st_spare[2] Ta implementation detail
276.El
277.Pp
278In this case the following macros are provided for convenience:
279.Bd -literal -offset indent
280#if defined(_NETBSD_SOURCE)
281  #define st_atime                st_atimespec.tv_sec
282  #define st_atimensec            st_atimespec.tv_nsec
283  #define st_mtime                st_mtimespec.tv_sec
284  #define st_mtimensec            st_mtimespec.tv_nsec
285  #define st_ctime                st_ctimespec.tv_sec
286  #define st_ctimensec            st_ctimespec.tv_nsec
287  #define st_birthtime            st_birthtimespec.tv_sec
288  #define st_birthtimensec        st_birthtimespec.tv_nsec
289#endif
290.Ed
291.Pp
292The status information word
293.Fa st_flags
294has the following bits:
295.Bl -column -offset indent \
296"struct timespec " "st_birthtimensec"
297.It Sy Constant Ta Sy Description
298.It Dv UF_NODUMP Ta do not dump a file
299.It Dv UF_IMMUTABLE Ta file may not be changed
300.It Dv UF_APPEND Ta writes to file may only append
301.It Dv UF_OPAQUE Ta directory is opaque wrt. union
302.It Dv SF_ARCHIVED Ta file is archived
303.It Dv SF_IMMUTABLE Ta file may not be changed
304.It Dv SF_APPEND Ta writes to file may only append
305.El
306.Pp
307For a description of the flags, see
308.Xr chflags 2 .
309.Sh RETURN VALUES
310.Rv -std stat lstat fstat fstatat
311.Sh COMPATIBILITY
312Previous versions of the system used different types for the
313.Li st_dev ,
314.Li st_uid ,
315.Li st_gid ,
316.Li st_rdev ,
317.Li st_size ,
318.Li st_blksize
319and
320.Li st_blocks
321fields.
322.Sh ERRORS
323.Fn stat ,
324.Fn lstat
325and
326.Fn fstatat
327will fail if:
328.Bl -tag -width Er
329.It Bq Er EACCES
330Search permission is denied for a component of the path prefix.
331.It Bq Er EBADF
332A badly formed vnode was encountered.
333This can happen if a file system information node is incorrect.
334.It Bq Er EFAULT
335.Fa sb
336or
337.Fa path
338points to an invalid address.
339.It Bq Er EIO
340An I/O error occurred while reading from or writing to the file system.
341.It Bq Er ELOOP
342Too many symbolic links were encountered in translating the pathname.
343.It Bq Er ENAMETOOLONG
344A component of a pathname exceeded
345.Brq Dv NAME_MAX
346characters, or an entire path name exceeded
347.Brq Dv PATH_MAX
348characters.
349.It Bq Er ENOENT
350The named file does not exist.
351.It Bq Er ENOTDIR
352A component of the path prefix is not a directory.
353.It Bq Er ENXIO
354The named file is a character special or block
355special file, and the device associated with this special file
356does not exist.
357.El
358.Pp
359In addition,
360.Fn fstatat
361will fail if:
362.Bl -tag -width Er
363.It Bq Er EBADF
364.Fa path
365does not specify an absolute path and
366.Fa fd
367is neither
368.Dv AT_FDCWD
369nor a valid file descriptor open for reading or searching.
370.It Bq Er ENOTDIR
371.Fa path
372is not an absolute path and
373.Fa fd
374is a file descriptor associated with a non-directory file.
375.El
376.Pp
377.Fn fstat
378will fail if:
379.Bl -tag -width Er
380.It Bq Er EBADF
381.Fa fd
382is not a valid open file descriptor.
383.It Bq Er EFAULT
384.Fa sb
385points to an invalid address.
386.It Bq Er EIO
387An I/O error occurred while reading from or writing to the file system.
388.El
389.Sh SEE ALSO
390.Xr chflags 2 ,
391.Xr chmod 2 ,
392.Xr chown 2 ,
393.Xr utimes 2 ,
394.Xr dirent 3 ,
395.Xr types 3 ,
396.Xr symlink 7
397.Sh STANDARDS
398.Fn stat ,
399.Fn lstat ,
400and
401.Fn fstat
402conform to
403.St -p1003.1-2004 .
404.Fn fstatat
405conforms to
406.St -p1003.1-2008 .
407.Sh HISTORY
408The
409.Fn stat
410and
411.Fn fstat
412function calls appeared in
413.At v1 .
414A
415.Fn lstat
416function call appeared in
417.Bx 4.2 .
418.Sh BUGS
419Applying
420.Fn fstat
421to a socket (and thus to a pipe)
422returns a zero'd buffer,
423except for the blocksize field,
424and a unique device and file serial number.
425