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