]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/stat.2
Import libxo-1.3.0:
[FreeBSD/FreeBSD.git] / lib / libc / sys / stat.2
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 December 5, 2018
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
53 The
54 .Fn stat
55 system call obtains information about the file pointed to by
56 .Fa path .
57 Read, write or execute
58 permission of the named file is not required, but all directories
59 listed in the path name leading to the file must be searchable.
60 .Pp
61 The
62 .Fn lstat
63 system call is like
64 .Fn stat
65 except when the named file is a symbolic link,
66 in which case
67 .Fn lstat
68 returns information about the link,
69 while
70 .Fn stat
71 returns information about the file the link references.
72 .Pp
73 The
74 .Fn fstat
75 system call obtains the same information about an open file
76 known by the file descriptor
77 .Fa fd .
78 .Pp
79 The
80 .Fn fstatat
81 system call is equivalent to
82 .Fn stat
83 and
84 .Fn lstat
85 except when the
86 .Fa path
87 specifies a relative path, or the
88 .Dv AT_BENEATH
89 flag is provided.
90 For
91 .Fn fstatat
92 and relative
93 .Fa path ,
94 the status is retrieved from a file relative to
95 the directory associated with the file descriptor
96 .Fa fd
97 instead of the current working directory.
98 For
99 .Dv AT_BENEATH
100 and absolute
101 .Fa path ,
102 the status is retrieved from a file specified by the
103 .Fa path ,
104 but additional permission checks are performed, see below.
105 .Pp
106 The values for the
107 .Fa flag
108 are constructed by a bitwise-inclusive OR of flags from this list,
109 defined in
110 .In fcntl.h :
111 .Bl -tag -width indent
112 .It Dv AT_SYMLINK_NOFOLLOW
113 If
114 .Fa path
115 names a symbolic link, the status of the symbolic link is returned.
116 .It Dv AT_BENEATH
117 Only stat files and directories below the topping directory.
118 See the description of the
119 .Dv O_BENEATH
120 flag in the
121 .Xr open 2
122 manual page.
123 .El
124 .Pp
125 If
126 .Fn fstatat
127 is passed the special value
128 .Dv AT_FDCWD
129 in the
130 .Fa fd
131 parameter, the current working directory is used and the behavior is
132 identical to a call to
133 .Fn stat
134 or
135 .Fn lstat
136 respectively, depending on whether or not the
137 .Dv AT_SYMLINK_NOFOLLOW
138 bit is set in
139 .Fa flag .
140 .Pp
141 When
142 .Fn fstatat
143 is called with an absolute
144 .Fa path
145 without the
146 .Dv AT_BENEATH
147 flag, it ignores the
148 .Fa fd
149 argument.
150 When
151 .Dv AT_BENEATH
152 is specified with an absolute
153 .Fa path ,
154 a directory passed by the
155 .Fa fd
156 argument is used as the topping point for the resolution.
157 .Pp
158 The
159 .Fa sb
160 argument is a pointer to a
161 .Vt stat
162 structure
163 as defined by
164 .In sys/stat.h
165 and into which information is placed concerning the file.
166 .Pp
167 The fields of
168 .Vt "struct stat"
169 related to the file system are:
170 .Bl -tag -width ".Va st_nlink"
171 .It Va st_dev
172 Numeric ID of the device containing the file.
173 .It Va st_ino
174 The file's inode number.
175 .It Va st_nlink
176 Number of hard links to the file.
177 .It Va st_flags
178 Flags enabled for the file.
179 See
180 .Xr chflags 2
181 for the list of flags and their description.
182 .El
183 .Pp
184 The
185 .Va st_dev
186 and
187 .Va st_ino
188 fields together identify the file uniquely within the system.
189 .Pp
190 The time-related fields of
191 .Vt "struct stat"
192 are:
193 .Bl -tag -width ".Va st_birthtim"
194 .It Va st_atim
195 Time when file data was last accessed.
196 Changed implicitly by syscalls such as
197 .Xr read 2
198 and
199 .Xr readv 2 ,
200 and explicitly by
201 .Xr utimes 2 .
202 .It Va st_mtim
203 Time when file data was last modified.
204 Changed implicitly by syscalls such as
205 .Xr truncate 2 ,
206 .Xr write 2 ,
207 and
208 .Xr writev 2 ,
209 and explicitly by
210 .Xr utimes 2 .
211 Also, any syscall which modifies directory content changes the
212 .Va st_mtim
213 for the affected directory.
214 For instance,
215 .Xr creat 2 ,
216 .Xr mkdir 2 ,
217 .Xr rename 2 ,
218 .Xr link 2 ,
219 and
220 .Xr unlink 2 .
221 .It Va st_ctim
222 Time when file status was last changed (inode data modification).
223 Changed implicitly by any syscall that affects file metadata, including
224 .Va st_mtim ,
225 such as
226 .Xr chflags 2 ,
227 .Xr chmod 2 ,
228 .Xr chown 2 ,
229 .Xr truncate 2 ,
230 .Xr utimes 2 ,
231 and
232 .Xr write 2 .
233 Also, any syscall which modifies directory content changes the
234 .Va st_ctim
235 for the affected directory.
236 For instance,
237 .Xr creat 2 ,
238 .Xr mkdir 2 ,
239 .Xr rename 2 ,
240 .Xr link 2 ,
241 and
242 .Xr unlink 2 .
243 .It Va st_birthtim
244 Time when the inode was created.
245 .El
246 .Pp
247 These 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
264 Size-related fields of the
265 .Vt "struct stat"
266 are:
267 .Bl -tag -width ".Va st_blksize"
268 .It Va st_size
269 File size in bytes.
270 .It Va st_blksize
271 Optimal I/O block size for the file.
272 .It Va st_blocks
273 Actual number of blocks allocated for the file in 512-byte units.
274 As short symbolic links are stored in the inode, this number may
275 be zero.
276 .El
277 .Pp
278 The access-related fields of
279 .Vt "struct stat"
280 are:
281 .Bl -tag -width ".Va st_mode"
282 .It Va st_uid
283 User ID of the file's owner.
284 .It Va st_gid
285 Group ID of the file.
286 .It Va st_mode
287 Status of the file (see below).
288 .El
289 .Pp
290 The status information word
291 .Fa st_mode
292 has 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
320 For a list of access modes, see
321 .In sys/stat.h ,
322 .Xr access 2
323 and
324 .Xr chmod 2 .
325 These macros are available to test whether a
326 .Va st_mode
327 value passed in the
328 .Fa m
329 argument corresponds to a file of the specified type:
330 .Bl -tag -width ".Fn S_ISFIFO m"
331 .It Fn S_ISBLK m
332 Test for a block special file.
333 .It Fn S_ISCHR m
334 Test for a character special file.
335 .It Fn S_ISDIR m
336 Test for a directory.
337 .It Fn S_ISFIFO m
338 Test for a pipe or FIFO special file.
339 .It Fn S_ISLNK m
340 Test for a symbolic link.
341 .It Fn S_ISREG m
342 Test for a regular file.
343 .It Fn S_ISSOCK m
344 Test for a socket.
345 .It Fn S_ISWHT m
346 Test for a whiteout.
347 .El
348 .Pp
349 The macros evaluate to a non-zero value if the test is true
350 or to the value 0 if the test is false.
351 .Sh RETURN VALUES
352 .Rv -std
353 .Sh COMPATIBILITY
354 Previous 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
361 and
362 .Va st_blocks
363 fields.
364 .Sh ERRORS
365 The
366 .Fn stat
367 and
368 .Fn lstat
369 system calls will fail if:
370 .Bl -tag -width Er
371 .It Bq Er EACCES
372 Search permission is denied for a component of the path prefix.
373 .It Bq Er EFAULT
374 The
375 .Fa sb
376 or
377 .Fa path
378 argument
379 points to an invalid address.
380 .It Bq Er EIO
381 An I/O error occurred while reading from or writing to the file system.
382 .It Bq Er ELOOP
383 Too many symbolic links were encountered in translating the pathname.
384 .It Bq Er ENAMETOOLONG
385 A component of a pathname exceeded 255 characters,
386 or an entire path name exceeded 1023 characters.
387 .It Bq Er ENOENT
388 The named file does not exist.
389 .It Bq Er ENOTDIR
390 A component of the path prefix is not a directory.
391 .It Bq Er EOVERFLOW
392 The file size in bytes cannot be
393 represented correctly in the structure pointed to by
394 .Fa sb .
395 .El
396 .Pp
397 The
398 .Fn fstat
399 system call will fail if:
400 .Bl -tag -width Er
401 .It Bq Er EBADF
402 The
403 .Fa fd
404 argument
405 is not a valid open file descriptor.
406 .It Bq Er EFAULT
407 The
408 .Fa sb
409 argument
410 points to an invalid address.
411 .It Bq Er EIO
412 An I/O error occurred while reading from or writing to the file system.
413 .It Bq Er EOVERFLOW
414 The file size in bytes cannot be
415 represented correctly in the structure pointed to by
416 .Fa sb .
417 .El
418 .Pp
419 In addition to the errors returned by the
420 .Fn lstat ,
421 the
422 .Fn fstatat
423 may fail if:
424 .Bl -tag -width Er
425 .It Bq Er EBADF
426 The
427 .Fa path
428 argument does not specify an absolute path and the
429 .Fa fd
430 argument is neither
431 .Dv AT_FDCWD
432 nor a valid file descriptor open for searching.
433 .It Bq Er EINVAL
434 The value of the
435 .Fa flag
436 argument is not valid.
437 .It Bq Er ENOTDIR
438 The
439 .Fa path
440 argument is not an absolute path and
441 .Fa fd
442 is neither
443 .Dv AT_FDCWD
444 nor a file descriptor associated with a directory.
445 .It Bq Er ENOTCAPABLE
446 .Fa path
447 is an absolute path,
448 or contained a ".." component leading to a
449 directory outside of the directory hierarchy specified by
450 .Fa fd ,
451 and the process is in capability mode.
452 .It Bq Er ENOTCAPABLE
453 The
454 .Dv AT_BENEATH
455 flag was provided to
456 .Fn fstatat ,
457 and the absolute
458 .Fa path
459 does not have its tail fully contained under the topping directory,
460 or the relative
461 .Fa path
462 escapes it.
463 .El
464 .Sh SEE ALSO
465 .Xr access 2 ,
466 .Xr chmod 2 ,
467 .Xr chown 2 ,
468 .Xr fhstat 2 ,
469 .Xr statfs 2 ,
470 .Xr utimes 2 ,
471 .Xr sticky 7 ,
472 .Xr symlink 7
473 .Sh STANDARDS
474 The
475 .Fn stat
476 and
477 .Fn fstat
478 system calls are expected to conform to
479 .St -p1003.1-90 .
480 The
481 .Fn fstatat
482 system call follows The Open Group Extended API Set 2 specification.
483 .Sh HISTORY
484 The
485 .Fn stat
486 and
487 .Fn fstat
488 system calls appeared in
489 .At v1 .
490 The
491 .Fn lstat
492 system call appeared in
493 .Bx 4.2 .
494 The
495 .Fn fstatat
496 system call appeared in
497 .Fx 8.0 .
498 .Sh BUGS
499 Applying
500 .Fn fstat
501 to a socket
502 returns a zeroed buffer,
503 except for the blocksize field,
504 and a unique device and inode number.