]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/stat.2
xnu-4903.270.47.tar.gz
[apple/xnu.git] / bsd / man / man2 / stat.2
CommitLineData
9bccf70c
A
1.\" $OpenBSD: stat.2,v 1.3 1997/02/13 05:20:55 millert 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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)stat.2 8.3 (Berkeley) 4/19/94
35.\"
b0d623f7 36.Dd May 15, 2008
9bccf70c
A
37.Dt STAT 2
38.Os BSD 4
39.Sh NAME
2d21ac55 40.Nm fstat ,
b0d623f7 41.Nm fstat64 ,
9bccf70c 42.Nm lstat ,
b0d623f7
A
43.Nm lstat64 ,
44.Nm stat ,
fe8ab488
A
45.Nm stat64 ,
46.Nm fstatat
9bccf70c
A
47.Nd get file status
48.Sh SYNOPSIS
9bccf70c
A
49.Fd #include <sys/stat.h>
50.Ft int
2d21ac55
A
51.Fo fstat
52.Fa "int fildes"
53.Fa "struct stat *buf"
54.Fc
55.Ft int
2d21ac55
A
56.Fo lstat
57.Fa "const char *restrict path"
58.Fa "struct stat *restrict buf"
59.Fc
9bccf70c 60.Ft int
2d21ac55
A
61.Fo stat
62.Fa "const char *restrict path"
63.Fa "struct stat *restrict buf"
64.Fc
fe8ab488
A
65.Ft int
66.Fn fstatat "int fd" "const char *path" "struct stat *buf" "int flag"
b0d623f7
A
67.Sh TRANSITIIONAL SYNOPSIS (NOW DEPRECATED)
68.Ft int
69.br
70.Fo fstat64
71.Fa "int fildes"
72.Fa "struct stat64 *buf"
73.Fc ;
74.sp
75.Ft int
76.br
77.Fo lstat64
78.Fa "const char *restrict path"
79.Fa "struct stat64 *restrict buf"
80.Fc ;
81.sp
82.Ft int
83.br
84.Fo stat64
85.Fa "const char *restrict path"
86.Fa "struct stat64 *restrict buf"
87.Fc ;
9bccf70c 88.Sh DESCRIPTION
b0d623f7 89The
2d21ac55 90.Fn stat
9bccf70c
A
91function obtains information about the file pointed to by
92.Fa path .
93Read, write or execute
94permission of the named file is not required, but all directories
95listed in the path name leading to the file must be searchable.
96.Pp
b0d623f7
A
97The
98.Fn lstat
99function
9bccf70c
A
100is like
101.Fn stat
b0d623f7 102except in the case where the named file is a symbolic link;
9bccf70c
A
103.Fn lstat
104returns information about the link,
105while
106.Fn stat
107returns information about the file the link references.
813fb2f6
A
108For symbolic links, the st_mode member contains meaningful information
109when used with the file type macros, and the st_size member contains
110the length of the pathname contained in the symbolic link. File mode
111bits and the contents of the remaining members of the stat structure
112are unspecified. The value returned in the st_size member is the
113length of the contents of the symbolic link, and does not count any
114trailing null.
9bccf70c
A
115.Pp
116The
117.Fn fstat
118obtains the same information about an open file
119known by the file descriptor
2d21ac55 120.Fa fildes .
9bccf70c
A
121.Pp
122The
fe8ab488
A
123.Fn fstatat
124system call is equivalent to
125.Fn stat
126and
127.Fn lstat
128except in the case where the
129.Fa path
130specifies a relative path.
131In this case the status is retrieved from a file relative to
132the directory associated with the file descriptor
133.Fa fd
134instead of the current working directory.
135.Pp
136The values for the
137.Fa flag
138are constructed by a bitwise-inclusive OR of flags from the following list,
139defined in
140.In fcntl.h :
141.Bl -tag -width indent
142.It Dv AT_SYMLINK_NOFOLLOW
143If
144.Fa path
145names a symbolic link, the status of the symbolic link is returned.
146.El
147.Pp
148If
149.Fn fstatat
150is passed the special value
151.Dv AT_FDCWD
152in the
153.Fa fd
154parameter, the current working directory is used and the behavior is
155identical to a call to
156.Fn stat
157or
158.Fn lstat
159respectively, depending on whether or not the
160.Dv AT_SYMLINK_NOFOLLOW
161bit is set in
162.Fa flag .
163.Pp
164The
2d21ac55 165.Fa buf
9bccf70c 166argument is a pointer to a
2d21ac55 167.Fa stat
2d21ac55 168structure
9bccf70c
A
169as defined by
170.Aq Pa sys/stat.h
9bccf70c 171and into which information is placed concerning the file.
b0d623f7
A
172When the macro
173.Dv _DARWIN_FEATURE_64_BIT_INODE
6d2010ae 174is not defined (see below for more information about this macro), the
b0d623f7
A
175.Fa stat
176structure is defined as:
177.Bd -literal
178struct stat { /* when _DARWIN_FEATURE_64_BIT_INODE is NOT defined */
179 dev_t st_dev; /* device inode resides on */
180 ino_t st_ino; /* inode's number */
181 mode_t st_mode; /* inode protection mode */
6d2010ae 182 nlink_t st_nlink; /* number of hard links to the file */
b0d623f7
A
183 uid_t st_uid; /* user-id of owner */
184 gid_t st_gid; /* group-id of owner */
185 dev_t st_rdev; /* device type, for special file inode */
186 struct timespec st_atimespec; /* time of last access */
187 struct timespec st_mtimespec; /* time of last data modification */
188 struct timespec st_ctimespec; /* time of last file status change */
189 off_t st_size; /* file size, in bytes */
190 quad_t st_blocks; /* blocks allocated for file */
191 u_long st_blksize;/* optimal file sys I/O ops blocksize */
192 u_long st_flags; /* user defined flags for file */
193 u_long st_gen; /* file generation number */
194};
195.Ed
196.Pp
197However, when the macro
198.Dv _DARWIN_FEATURE_64_BIT_INODE
199is defined, the
b0d623f7
A
200.Fa stat
201structure will now be defined as:
9bccf70c 202.Bd -literal
b0d623f7 203struct stat { /* when _DARWIN_FEATURE_64_BIT_INODE is defined */
2d21ac55
A
204 dev_t st_dev; /* ID of device containing file */
205 mode_t st_mode; /* Mode of file (see below) */
206 nlink_t st_nlink; /* Number of hard links */
593a1d5f 207 ino_t st_ino; /* File serial number */
2d21ac55
A
208 uid_t st_uid; /* User ID of the file */
209 gid_t st_gid; /* Group ID of the file */
210 dev_t st_rdev; /* Device ID */
211 struct timespec st_atimespec; /* time of last access */
212 struct timespec st_mtimespec; /* time of last data modification */
213 struct timespec st_ctimespec; /* time of last status change */
214 struct timespec st_birthtimespec; /* time of file creation(birth) */
215 off_t st_size; /* file size, in bytes */
216 blkcnt_t st_blocks; /* blocks allocated for file */
217 blksize_t st_blksize; /* optimal blocksize for I/O */
218 uint32_t st_flags; /* user defined flags for file */
219 uint32_t st_gen; /* file generation number */
220 int32_t st_lspare; /* RESERVED: DO NOT USE! */
221 int64_t st_qspare[2]; /* RESERVED: DO NOT USE! */
222};
9bccf70c
A
223.Ed
224.Pp
225The time-related fields of
226.Fa struct stat
227are as follows:
2d21ac55 228.Bl -tag -width XXXst_birthtime
9bccf70c
A
229.It st_atime
230Time when file data last accessed.
231Changed by the
232.Xr mknod 2 ,
233.Xr utimes 2
234and
235.Xr read 2
236system calls.
237.It st_mtime
238Time when file data last modified.
239Changed by the
240.Xr mknod 2 ,
241.Xr utimes 2
242and
243.Xr write 2
244system calls.
245.It st_ctime
246Time when file status was last changed (inode data modification).
247Changed by the
248.Xr chmod 2 ,
249.Xr chown 2 ,
250.Xr link 2 ,
251.Xr mknod 2 ,
252.Xr rename 2 ,
253.Xr unlink 2 ,
254.Xr utimes 2
255and
256.Xr write 2
257system calls.
2d21ac55 258.It st_birthtime
b0d623f7
A
259Time of file creation. Only set once when the file is created. This field is
260only available in the 64 bit inode variants. On filesystems where birthtime is
3e170ce0 261not available, this field is set to 0 (i.e. epoch).
9bccf70c
A
262.El
263.Pp
2d21ac55 264The size-related fields of the structures are as follows:
9bccf70c
A
265.Bl -tag -width XXXst_blksize
266.It st_blksize
267The optimal I/O block size for the file.
268.It st_blocks
269The actual number of blocks allocated for the file in 512-byte units.
270As short symbolic links are stored in the inode, this number may
271be zero.
272.El
273.Pp
274The status information word
275.Fa st_mode
276has the following bits:
277.Bd -literal
278#define S_IFMT 0170000 /* type of file */
279#define S_IFIFO 0010000 /* named pipe (fifo) */
280#define S_IFCHR 0020000 /* character special */
281#define S_IFDIR 0040000 /* directory */
282#define S_IFBLK 0060000 /* block special */
283#define S_IFREG 0100000 /* regular */
284#define S_IFLNK 0120000 /* symbolic link */
285#define S_IFSOCK 0140000 /* socket */
286#define S_IFWHT 0160000 /* whiteout */
287#define S_ISUID 0004000 /* set user id on execution */
288#define S_ISGID 0002000 /* set group id on execution */
289#define S_ISVTX 0001000 /* save swapped text even after use */
290#define S_IRUSR 0000400 /* read permission, owner */
291#define S_IWUSR 0000200 /* write permission, owner */
292#define S_IXUSR 0000100 /* execute/search permission, owner */
293.Ed
294.Pp
295For a list of access modes, see
296.Aq Pa sys/stat.h ,
297.Xr access 2
298and
299.Xr chmod 2 .
2d21ac55
A
300.Pp
301For a list of the file flags in the
302.Fa st_flags
303field, see
304.Aq Pa sys/stat.h
305and
306.Xr chflags 2 .
6d2010ae
A
307.Sh _DARWIN_FEATURE_64_BIT_INODE
308In order to accommodate advanced capabilities of newer file systems, the
309.Fa struct stat ,
310.Fa struct statfs ,
311and
312.Fa struct dirent
313data structures were updated in Mac OSX 10.5.
314.Pp
315The most obvious change is the increased size of
316.Fa ino_t
317from 32 bits to 64 bits. As a consequence, storing an ino_t in an int is
318no longer safe, and file formats storing ino_t as 32-bit values may need to
319be updated. There are other changes as well, such as the widening of
320.Fa f_fstypename ,
321.Fa f_mntonname ,
322and
323.Fa f_mntfromname
324in
325.Fa struct statfs .
326Please refer to
327.Xr stat 2
328and
329.Xr dir 5
330for more detail on the specific changes to the other affected data structures.
331.Pp
332On platforms that existed before these updates were available, ABI
333compatibility is achieved by providing two implementations for related
334functions: one using the legacy data structures and one using the updated
335data structures. Variants which make use of the newer structures have their
336symbols suffixed with $INODE64. These $INODE64 suffixes are automatically
337appended by the compiler tool-chain and should not be used directly.
338.Pp
339Platforms that were released after these updates only have the newer variants
340available to them. These platforms have the macro
341.Dv _DARWIN_FEATURE_ONLY_64_BIT_INODE
342defined.
343.Pp
344The
345.Dv _DARWIN_FEATURE_64_BIT_INODE
346macro should not be set directly. Instead, developers should make use of the
347.Dv _DARWIN_NO_64_BIT_INODE
348or
349.Dv _DARWIN_USE_64_BIT_INODE
350macros when the default variant is not desired. The following table details
351the effects of defining these macros for different deployment targets.
352.Pp
353.TS
354center;
355c s s s
356l | c s s
357c | c c c
358c | c c c
359l | c c c.
360T{
361.Dv _DARWIN_FEATURE_ONLY_64_BIT_INODE Sy not defined
362T}
363=
364 Deployment Target
365user defines: < 10.5 10.5 > 10.5
366_
367T{
368.Em (none)
369T} 32-bit 32-bit 64-bit
370T{
371.Dv _DARWIN_NO_64_BIT_INODE
372T} 32-bit 32-bit 32-bit
373T{
374.Dv _DARWIN_USE_64_BIT_INODE
375T} 32-bit 64-bit 64-bit
376_
377.T&
378c s s s
379c s s s
380c | l s s
381c | c c c
382l | c c c.
383
384T{
385.Dv _DARWIN_FEATURE_ONLY_64_BIT_INODE Sy defined
386T}
387=
388user defines: Any Deployment Target
389_
390T{
391.Em (none)
392T} 64-bit-only
393T{
394.Dv _DARWIN_NO_64_BIT_INODE
395T} T{
396.Em (error)
397T}
398T{
399.Dv _DARWIN_USE_64_BIT_INODE
400T} 64-bit-only
401_
402.TE
403.Pp
404.Bl -tag -width 64-bit-only -offset indent
405.It 32-bit
40632-bit inode values are enabled, and the legacy structures involving the
407.Vt ino_t
408type are in use.
409The macro
410.Dv _DARWIN_FEATURE_64_BIT_INODE
411is not defined.
412.It 64-bit
41364-bit inode values are enabled, and the expanded structures involving the
414.Vt ino_t
415type are in use.
416The macro
417.Dv _DARWIN_FEATURE_64_BIT_INODE
418is defined, and loader symbols will contain the
419.Li $INODE64
420suffix.
421.It 64-bit-only
422Like 64-bit, except loader symbols do not have the
423.Li $INODE64
424suffix.
425.It Em (error)
426A compile time error is generated.
427.El
428.Pp
429Due to the increased benefits of the larger structure, it is highly
430recommended that developers not define
431.Dv _DARWIN_NO_64_BIT_INODE
432and make use of
433.Dv _DARWIN_USE_64_BIT_INODE
434when targeting Mac OSX 10.5.
435.Pp
436In addition to the $INODE64 suffixed symbols, variants suffixed with 64 are
437also available for related functions. These functions were provided as a way
438for developers to use the updated structures in code that also made use of
439the legacy structures. The enlarged stat structures were also prefixed with
44064 to distinguish them from their legacy variants. These functions have been
441deprecated and should be avoided.
9bccf70c
A
442.Sh RETURN VALUES
443Upon successful completion a value of 0 is returned.
444Otherwise, a value of -1 is returned and
445.Va errno
446is set to indicate the error.
447.Sh COMPATIBILITY
448Previous versions of the system used different types for the
449.Li st_dev ,
450.Li st_uid ,
451.Li st_gid ,
452.Li st_rdev ,
453.Li st_size ,
454.Li st_blksize
455and
456.Li st_blocks
457fields.
458.Sh ERRORS
2d21ac55
A
459.Bl -tag -width Er
460The
461.Fn fstat
462system call will fail if:
463.\" ===========
464.It Bq Er EBADF
465.Fa fildes
466is not a valid open file descriptor.
467.\" ===========
468.It Bq Er EFAULT
469.Fa Sb
470points to an invalid address.
471.\" ===========
472.It Bq Er EIO
473An I/O error occurs while reading from or writing to the file system.
474.El
475.Pp
476The
9bccf70c 477.Fn lstat
2d21ac55
A
478and
479.Fn stat
480system calls will fail if:
9bccf70c 481.Bl -tag -width Er
2d21ac55 482.\" ===========
9bccf70c
A
483.It Bq Er EACCES
484Search permission is denied for a component of the path prefix.
2d21ac55 485.\" ===========
9bccf70c
A
486.It Bq Er EFAULT
487.Fa Sb
488or
489.Em name
490points to an invalid address.
2d21ac55 491.\" ===========
9bccf70c 492.It Bq Er EIO
2d21ac55
A
493An I/O error occurs while reading from or writing to the file system.
494.\" ===========
495.It Bq Er ELOOP
496Too many symbolic links are encountered in translating the pathname.
497This is taken to be indicative of a looping symbolic link.
498.\" ===========
499.It Bq Er ENAMETOOLONG
500A component of a pathname exceeds
501.Dv {NAME_MAX}
502characters, or an entire path name exceeds
503.Dv {PATH_MAX}
504characters.
505.\" ===========
506.It Bq Er ENOENT
507The named file does not exist.
508.\" ===========
509.It Bq Er ENOTDIR
510A component of the path prefix is not a directory.
9bccf70c
A
511.El
512.Pp
2d21ac55
A
513The
514.Fn fstat ,
515.Fn lstat ,
516and
517.Fn stat
518system calls will fail if:
9bccf70c 519.Bl -tag -width Er
2d21ac55
A
520.\" ===========
521.It Bq Er EOVERFLOW
522The file size in bytes
523or the number of blocks allocated to the file
524or the file serial number cannot be represented correctly
525in the structure pointed to by
526.Fa buf .
9bccf70c 527.El
fe8ab488
A
528.Pp
529In addition to the errors returned by the
530.Fn stat
531and
532.Fn lstat ,
533.Fn fstatat
534may fail if:
535.Bl -tag -width Er
536.It Bq Er EBADF
537The
538.Fa path
539argument does not specify an absolute path and the
540.Fa fd
541argument is neither
542.Dv AT_FDCWD
543nor a valid file descriptor open for searching.
544.It Bq Er EINVAL
545The value of the
546.Fa flag
547argument is not valid.
548.It Bq Er ENOTDIR
549The
550.Fa path
551argument is not an absolute path and
552.Fa fd
553is neither
554.Dv AT_FDCWD
555nor a file descriptor associated with a directory.
556.El
9bccf70c
A
557.Sh CAVEATS
558The file generation number,
559.Fa st_gen ,
560is only available to the super-user.
b0d623f7
A
561.br
562The fields in the stat structure currently marked
563.Fa st_spare1 ,
564.Fa st_spare2 ,
565and
566.Fa st_spare3
567are present in preparation for inode time stamps expanding
568to 64 bits. This, however, can break certain programs that
569depend on the time stamps being contiguous (in calls to
570.Xr utimes 2 ) .
571.Sh TRANSITIONAL DESCRIPTION (NOW DEPRECATED)
572The
573.Fa fstat64 ,
574.Fa lstat64
575and
576.Fa stat64
577routines are equivalent to their corresponding non-64-suffixed routine,
578when 64-bit inodes are in effect.
579They were added before there was support for the symbol variants, and so are
580now deprecated.
581Instead of using these, set the
582.Dv _DARWIN_USE_64_BIT_INODE
583macro before including header files to force 64-bit inode support.
584.Pp
585The
586.Fa stat64
587structure used by these deprecated routines is the same as the
588.Fa stat
589structure when 64-bit inodes are in effect (see above).
9bccf70c 590.Sh SEE ALSO
2d21ac55 591.Xr chflags 2 ,
9bccf70c
A
592.Xr chmod 2 ,
593.Xr chown 2 ,
2d21ac55
A
594.Xr utimes 2 ,
595.Xr compat 5 ,
6d2010ae 596.Xr statfs 2 ,
9bccf70c
A
597.Xr symlink 7
598.Sh BUGS
599Applying
600.Xr fstat
601to a socket (and thus to a pipe)
602returns a zero'd buffer,
603except for the blocksize field,
604and a unique device and inode number.
605.Sh STANDARDS
606The
607.Fn stat
608and
609.Fn fstat
610function calls are expected to conform to
611.St -p1003.1-88 .
fe8ab488
A
612The
613.Fn fstatat
614system call is expected to conform to POSIX.1-2008 .
9bccf70c
A
615.Sh HISTORY
616An
617.Fn lstat
618function call appeared in
619.Bx 4.2 .
b0d623f7
A
620The
621.Fn stat64 ,
622.Fn fstat64 ,
623and
624.Fn lstat64
625system calls first appeared in Mac OS X 10.5 (Leopard) and are now deprecated
626in favor of the corresponding symbol variants.
fe8ab488
A
627The
628.Fn fstatat
629system call appeared in OS X 10.10