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