xref: /dragonfly/sys/sys/dirent.h (revision 6b921297)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)dirent.h	8.3 (Berkeley) 8/10/94
30  * $FreeBSD: src/sys/sys/dirent.h,v 1.11 1999/12/29 04:24:39 peter Exp $
31  */
32 
33 #ifndef	_SYS_DIRENT_H_
34 #define	_SYS_DIRENT_H_
35 
36 /*
37  * The dirent structure defines the format of directory entries returned by
38  * the getdirentries(2) system call.
39  *
40  * A directory entry has a struct dirent at the front of it, containing its
41  * inode number, the length of the entry, and the length of the name
42  * contained in the entry.  These are followed by the name padded to a 8
43  * byte boundary with null bytes.  All names are guaranteed null terminated.
44  */
45 
46 /*
47  * XXX Temporary bandaids to keep changes small:
48  * XXX - for userland programs which don't specify any C or POSIX options,
49  * XXX   keep the old d_fileno and map d_ino via macro.  Everything else gets
50  * XXX   the POSIX d_ino and only that.
51  * XXX - d_name is declared with the current maximum directory entry length,
52  * XXX   instead of being incomplete. Code must allocate space for the
53  * XXX   directory itself.
54  */
55 
56 #include <sys/cdefs.h>
57 #include <machine/stdint.h>
58 
59 #ifndef _INO_T_DECLARED
60 typedef	__uint64_t	ino_t;		/* inode number */
61 #define	_INO_T_DECLARED
62 #endif
63 
64 struct dirent {
65 #if defined(_KERNEL) || !__BSD_VISIBLE
66 	ino_t		d_ino;		/* file number of entry */
67 #else
68 	ino_t		d_fileno;	/* file number of entry */
69 #endif
70 	__uint16_t	d_namlen;	/* strlen(d_name) */
71 	__uint8_t	d_type;		/* file type, see blow */
72 	__uint8_t	d_unused1;	/* padding, reserved */
73 	__uint32_t	d_unused2;	/* reserved */
74 	char		d_name[255 + 1];
75 					/* name, NUL-terminated */
76 };
77 
78 #if __BSD_VISIBLE
79 /*
80  * Linux compatibility, but its a good idea anyhow
81  */
82 #define _DIRENT_HAVE_D_NAMLEN
83 #define _DIRENT_HAVE_D_TYPE
84 
85 #if !defined(_KERNEL)
86 #define	d_ino		d_fileno
87 #endif
88 
89 /*
90  * File types
91  */
92 #define	DT_UNKNOWN	 0
93 #define	DT_FIFO		 1
94 #define	DT_CHR		 2
95 #define	DT_DIR		 4
96 #define	DT_BLK		 6
97 #define	DT_REG		 8
98 #define	DT_LNK		10
99 #define	DT_SOCK		12
100 #define	DT_WHT		14
101 #define DT_DBF		15	/* database record file */
102 
103 /*
104  * The _DIRENT_DIRSIZ macro gives the minimum record length which will hold
105  * the directory entry.  This requires the amount of space in struct dirent
106  * without the d_name field, plus enough space for the name with a terminating
107  * null byte (dp->d_namlen+1), rounded up to an 8 byte boundary.
108  *
109  * The _DIRENT_MINSIZ macro gives space needed for the directory entry without
110  * the padding _DIRENT_DIRSIZ adds at the end.
111  */
112 #define	_DIRENT_MINSIZ(dp) \
113 	(__offsetof(struct dirent, d_name) + (dp)->d_namlen + 1)
114 #define	_DIRENT_RECLEN(namelen) \
115 	((__offsetof(struct dirent, d_name) + (namelen) + 1 + 7) & ~7)
116 #define	_DIRENT_DIRSIZ(dp)	_DIRENT_RECLEN((dp)->d_namlen)
117 #define	_DIRENT_NEXT(dp) \
118 	((struct dirent *)((__uint8_t *)(dp) + _DIRENT_DIRSIZ(dp)))
119 #endif /* __BSD_VISIBLE */
120 
121 #endif /* !_SYS_DIRENT_H_ */
122