]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/dir.h
9a86ec824b34fdc81111959b1e70bfa53054f5cf
[apple/xnu.git] / bsd / ufs / ufs / dir.h
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)dir.h 8.5 (Berkeley) 4/27/95
61 */
62
63 #ifndef _DIR_H_
64 #define _DIR_H_
65
66 #include <sys/appleapiopts.h>
67
68 #ifdef __APPLE_API_UNSTABLE
69 /*
70 * Theoretically, directories can be more than 2Gb in length, however, in
71 * practice this seems unlikely. So, we define the type doff_t as a 32-bit
72 * quantity to keep down the cost of doing lookup on a 32-bit machine.
73 */
74 #define doff_t int32_t
75 #define MAXDIRSIZE (0x7fffffff)
76
77 /*
78 * A directory consists of some number of blocks of DIRBLKSIZ
79 * bytes, where DIRBLKSIZ is chosen such that it can be transferred
80 * to disk in a single atomic operation (e.g. 512 bytes on most machines).
81 *
82 * Each DIRBLKSIZ byte block contains some number of directory entry
83 * structures, which are of variable length. Each directory entry has
84 * a struct direct at the front of it, containing its inode number,
85 * the length of the entry, and the length of the name contained in
86 * the entry. These are followed by the name padded to a 4 byte boundary
87 * with null bytes. All names are guaranteed null terminated.
88 * The maximum length of a name in a directory is MAXNAMLEN.
89 *
90 * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
91 * a directory entry. Free space in a directory is represented by
92 * entries which have dp->d_reclen > DIRSIZ(fmt, dp). All DIRBLKSIZ bytes
93 * in a directory block are claimed by the directory entries. This
94 * usually results in the last entry in a directory having a large
95 * dp->d_reclen. When entries are deleted from a directory, the
96 * space is returned to the previous entry in the same directory
97 * block by increasing its dp->d_reclen. If the first entry of
98 * a directory block is free, then its dp->d_ino is set to 0.
99 * Entries other than the first in a directory do not normally have
100 * dp->d_ino set to 0.
101 */
102 #ifdef __APPLE__
103 #define DIRBLKSIZ 1024
104 #else
105 #define DIRBLKSIZ DEV_BSIZE
106 #endif
107 #define MAXNAMLEN 255
108
109 struct direct {
110 u_int32_t d_ino; /* inode number of entry */
111 u_int16_t d_reclen; /* length of this record */
112 u_int8_t d_type; /* file type, see below */
113 u_int8_t d_namlen; /* length of string in d_name */
114 char d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */
115 };
116
117 /*
118 * File types
119 */
120 #define DT_UNKNOWN 0
121 #define DT_FIFO 1
122 #define DT_CHR 2
123 #define DT_DIR 4
124 #define DT_BLK 6
125 #define DT_REG 8
126 #define DT_LNK 10
127 #define DT_SOCK 12
128 #define DT_WHT 14
129
130 /*
131 * Convert between stat structure types and directory types.
132 */
133 #define IFTODT(mode) (((mode) & 0170000) >> 12)
134 #define DTTOIF(dirtype) ((dirtype) << 12)
135
136 /*
137 * The DIRSIZ macro gives the minimum record length which will hold
138 * the directory entry. This requires the amount of space in struct direct
139 * without the d_name field, plus enough space for the name with a terminating
140 * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
141 */
142 #if (BYTE_ORDER == LITTLE_ENDIAN)
143 #define DIRSIZ(oldfmt, dp) \
144 ((oldfmt) ? \
145 ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_type+1 + 3) &~ 3)) : \
146 ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3)))
147 #else
148 #define DIRSIZ(oldfmt, dp) \
149 ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
150 #endif
151 #define OLDDIRFMT 1
152 #define NEWDIRFMT 0
153
154 /*
155 * Template for manipulating directories. Should use struct direct's,
156 * but the name field is MAXNAMLEN - 1, and this just won't do.
157 */
158 struct dirtemplate {
159 u_int32_t dot_ino;
160 int16_t dot_reclen;
161 u_int8_t dot_type;
162 u_int8_t dot_namlen;
163 char dot_name[4]; /* must be multiple of 4 */
164 u_int32_t dotdot_ino;
165 int16_t dotdot_reclen;
166 u_int8_t dotdot_type;
167 u_int8_t dotdot_namlen;
168 char dotdot_name[4]; /* ditto */
169 };
170
171 /*
172 * This is the old format of directories, sanz type element.
173 */
174 struct odirtemplate {
175 u_int32_t dot_ino;
176 int16_t dot_reclen;
177 u_int16_t dot_namlen;
178 char dot_name[4]; /* must be multiple of 4 */
179 u_int32_t dotdot_ino;
180 int16_t dotdot_reclen;
181 u_int16_t dotdot_namlen;
182 char dotdot_name[4]; /* ditto */
183 };
184 #endif /* __APPLE_API_UNSTABLE */
185 #endif /* !_DIR_H_ */