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