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