]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/dir.h
2b8527ddc10c48cffc11ea144ca4bc1bb7c4a459
[apple/xnu.git] / bsd / ufs / ufs / dir.h
1 /*
2 * Copyright (c) 2000 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 /*
67 * Theoretically, directories can be more than 2Gb in length, however, in
68 * practice this seems unlikely. So, we define the type doff_t as a 32-bit
69 * quantity to keep down the cost of doing lookup on a 32-bit machine.
70 */
71 #define doff_t int32_t
72 #define MAXDIRSIZE (0x7fffffff)
73
74 /*
75 * A directory consists of some number of blocks of DIRBLKSIZ
76 * bytes, where DIRBLKSIZ is chosen such that it can be transferred
77 * to disk in a single atomic operation (e.g. 512 bytes on most machines).
78 *
79 * Each DIRBLKSIZ byte block contains some number of directory entry
80 * structures, which are of variable length. Each directory entry has
81 * a struct direct at the front of it, containing its inode number,
82 * the length of the entry, and the length of the name contained in
83 * the entry. These are followed by the name padded to a 4 byte boundary
84 * with null bytes. All names are guaranteed null terminated.
85 * The maximum length of a name in a directory is MAXNAMLEN.
86 *
87 * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
88 * a directory entry. Free space in a directory is represented by
89 * entries which have dp->d_reclen > DIRSIZ(fmt, dp). All DIRBLKSIZ bytes
90 * in a directory block are claimed by the directory entries. This
91 * usually results in the last entry in a directory having a large
92 * dp->d_reclen. When entries are deleted from a directory, the
93 * space is returned to the previous entry in the same directory
94 * block by increasing its dp->d_reclen. If the first entry of
95 * a directory block is free, then its dp->d_ino is set to 0.
96 * Entries other than the first in a directory do not normally have
97 * dp->d_ino set to 0.
98 */
99 #ifdef __APPLE__
100 #define DIRBLKSIZ 1024
101 #else
102 #define DIRBLKSIZ DEV_BSIZE
103 #endif
104 #define MAXNAMLEN 255
105
106 struct direct {
107 u_int32_t d_ino; /* inode number of entry */
108 u_int16_t d_reclen; /* length of this record */
109 u_int8_t d_type; /* file type, see below */
110 u_int8_t d_namlen; /* length of string in d_name */
111 char d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */
112 };
113
114 /*
115 * File types
116 */
117 #define DT_UNKNOWN 0
118 #define DT_FIFO 1
119 #define DT_CHR 2
120 #define DT_DIR 4
121 #define DT_BLK 6
122 #define DT_REG 8
123 #define DT_LNK 10
124 #define DT_SOCK 12
125 #define DT_WHT 14
126
127 /*
128 * Convert between stat structure types and directory types.
129 */
130 #define IFTODT(mode) (((mode) & 0170000) >> 12)
131 #define DTTOIF(dirtype) ((dirtype) << 12)
132
133 /*
134 * The DIRSIZ macro gives the minimum record length which will hold
135 * the directory entry. This requires the amount of space in struct direct
136 * without the d_name field, plus enough space for the name with a terminating
137 * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
138 */
139 #if (BYTE_ORDER == LITTLE_ENDIAN)
140 #define DIRSIZ(oldfmt, dp) \
141 ((oldfmt) ? \
142 ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_type+1 + 3) &~ 3)) : \
143 ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3)))
144 #else
145 #define DIRSIZ(oldfmt, dp) \
146 ((sizeof(struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
147 #endif
148 #define OLDDIRFMT 1
149 #define NEWDIRFMT 0
150
151 /*
152 * Template for manipulating directories. Should use struct direct's,
153 * but the name field is MAXNAMLEN - 1, and this just won't do.
154 */
155 struct dirtemplate {
156 u_int32_t dot_ino;
157 int16_t dot_reclen;
158 u_int8_t dot_type;
159 u_int8_t dot_namlen;
160 char dot_name[4]; /* must be multiple of 4 */
161 u_int32_t dotdot_ino;
162 int16_t dotdot_reclen;
163 u_int8_t dotdot_type;
164 u_int8_t dotdot_namlen;
165 char dotdot_name[4]; /* ditto */
166 };
167
168 /*
169 * This is the old format of directories, sanz type element.
170 */
171 struct odirtemplate {
172 u_int32_t dot_ino;
173 int16_t dot_reclen;
174 u_int16_t dot_namlen;
175 char dot_name[4]; /* must be multiple of 4 */
176 u_int32_t dotdot_ino;
177 int16_t dotdot_reclen;
178 u_int16_t dotdot_namlen;
179 char dotdot_name[4]; /* ditto */
180 };
181 #endif /* !_DIR_H_ */