]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 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 | /* | |
23 | * Copyright (c) 1983, 1993 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | */ | |
54 | ||
55 | ||
56 | #include <sys/param.h> | |
57 | #include <sys/mount.h> | |
58 | #include <sys/stat.h> | |
59 | ||
60 | #include <dirent.h> | |
61 | #include <errno.h> | |
62 | #include <fcntl.h> | |
63 | #include <stdlib.h> | |
64 | #include <unistd.h> | |
65 | #include <string.h> | |
66 | ||
67 | DIR *__opendir2(const char *name, int flags) | |
68 | { | |
69 | DIR *dirp; | |
70 | int fd; | |
71 | int incr; | |
72 | int unionstack; | |
73 | struct stat statb; | |
74 | ||
75 | if ((fd = open(name, O_RDONLY)) == -1) | |
76 | return (NULL); | |
77 | if (fstat(fd, &statb) || !S_ISDIR(statb.st_mode)) { | |
78 | errno = ENOTDIR; | |
79 | close(fd); | |
80 | return (NULL); | |
81 | } | |
82 | if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || | |
83 | (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) { | |
84 | close(fd); | |
85 | return (NULL); | |
86 | } | |
87 | ||
88 | /* | |
89 | * If CLBYTES is an exact multiple of DIRBLKSIZ, use a CLBYTES | |
90 | * buffer that it cluster boundary aligned. | |
91 | * Hopefully this can be a big win someday by allowing page | |
92 | * trades to user space to be done by getdirentries() | |
93 | */ | |
94 | if ((CLBYTES % DIRBLKSIZ) == 0) | |
95 | incr = CLBYTES; | |
96 | else | |
97 | incr = DIRBLKSIZ; | |
98 | ||
99 | /* | |
100 | * Determine whether this directory is the top of a union stack. | |
101 | */ | |
102 | if (flags & DTF_NODUP) { | |
103 | struct statfs sfb; | |
104 | ||
105 | if (fstatfs(fd, &sfb) < 0) { | |
106 | free(dirp); | |
107 | close(fd); | |
108 | return (NULL); | |
109 | } | |
110 | unionstack = (!strcmp(sfb.f_fstypename, "union") || (sfb.f_flags & MNT_UNION)); | |
111 | } else { | |
112 | unionstack = 0; | |
113 | } | |
114 | ||
115 | if (unionstack) { | |
116 | int len = 0; | |
117 | int space = 0; | |
118 | char *buf = 0; | |
119 | char *ddptr = 0; | |
120 | char *ddeptr; | |
121 | int n; | |
122 | struct dirent **dpv; | |
123 | ||
124 | /* | |
125 | * The strategy here is to read all the directory | |
126 | * entries into a buffer, sort the buffer, and | |
127 | * remove duplicate entries by setting the inode | |
128 | * number to zero. | |
129 | */ | |
130 | ||
131 | do { | |
132 | /* | |
133 | * Always make at least DIRBLKSIZ bytes | |
134 | * available to getdirentries | |
135 | */ | |
136 | if (space < DIRBLKSIZ) { | |
137 | space += incr; | |
138 | len += incr; | |
139 | buf = realloc(buf, len); | |
140 | if (buf == NULL) { | |
141 | free(dirp); | |
142 | close(fd); | |
143 | return (NULL); | |
144 | } | |
145 | ddptr = buf + (len - space); | |
146 | } | |
147 | ||
148 | n = getdirentries(fd, ddptr, space, &dirp->dd_seek); | |
149 | if (n > 0) { | |
150 | ddptr += n; | |
151 | space -= n; | |
152 | } | |
153 | } while (n > 0); | |
154 | ||
155 | ddeptr = ddptr; | |
156 | flags |= __DTF_READALL; | |
157 | ||
158 | /* | |
159 | * Re-open the directory. | |
160 | * This has the effect of rewinding back to the | |
161 | * top of the union stack and is needed by | |
162 | * programs which plan to fchdir to a descriptor | |
163 | * which has also been read -- see fts.c. | |
164 | */ | |
165 | if (flags & DTF_REWIND) { | |
166 | (void) close(fd); | |
167 | if ((fd = open(name, O_RDONLY)) == -1) { | |
168 | free(buf); | |
169 | free(dirp); | |
170 | return (NULL); | |
171 | } | |
172 | } | |
173 | ||
174 | /* | |
175 | * There is now a buffer full of (possibly) duplicate | |
176 | * names. | |
177 | */ | |
178 | dirp->dd_buf = buf; | |
179 | ||
180 | /* | |
181 | * Go round this loop twice... | |
182 | * | |
183 | * Scan through the buffer, counting entries. | |
184 | * On the second pass, save pointers to each one. | |
185 | * Then sort the pointers and remove duplicate names. | |
186 | */ | |
187 | for (dpv = 0;;) { | |
188 | n = 0; | |
189 | ddptr = buf; | |
190 | while (ddptr < ddeptr) { | |
191 | struct dirent *dp; | |
192 | ||
193 | dp = (struct dirent *) ddptr; | |
194 | if ((int)dp & 03) | |
195 | break; | |
196 | if ((dp->d_reclen <= 0) || | |
197 | (dp->d_reclen > (ddeptr + 1 - ddptr))) | |
198 | break; | |
199 | ddptr += dp->d_reclen; | |
200 | if (dp->d_fileno) { | |
201 | if (dpv) | |
202 | dpv[n] = dp; | |
203 | n++; | |
204 | } | |
205 | } | |
206 | ||
207 | if (dpv) { | |
208 | struct dirent *xp; | |
209 | ||
210 | /* | |
211 | * This sort must be stable. | |
212 | */ | |
213 | mergesort(dpv, n, sizeof(*dpv), alphasort); | |
214 | ||
215 | dpv[n] = NULL; | |
216 | xp = NULL; | |
217 | ||
218 | /* | |
219 | * Scan through the buffer in sort order, | |
220 | * zapping the inode number of any | |
221 | * duplicate names. | |
222 | */ | |
223 | for (n = 0; dpv[n]; n++) { | |
224 | struct dirent *dp = dpv[n]; | |
225 | ||
226 | if ((xp == NULL) || | |
227 | strcmp(dp->d_name, xp->d_name)) { | |
228 | xp = dp; | |
229 | } else { | |
230 | dp->d_fileno = 0; | |
231 | } | |
232 | if (dp->d_type == DT_WHT && | |
233 | (flags & DTF_HIDEW)) | |
234 | dp->d_fileno = 0; | |
235 | } | |
236 | ||
237 | free(dpv); | |
238 | break; | |
239 | } else { | |
240 | dpv = malloc((n+1) * sizeof(struct dirent *)); | |
241 | if (dpv == NULL) | |
242 | break; | |
243 | } | |
244 | } | |
245 | ||
246 | dirp->dd_len = len; | |
247 | dirp->dd_size = ddptr - dirp->dd_buf; | |
248 | } else { | |
249 | dirp->dd_len = incr; | |
250 | dirp->dd_buf = malloc(dirp->dd_len); | |
251 | if (dirp->dd_buf == NULL) { | |
252 | free(dirp); | |
253 | close (fd); | |
254 | return (NULL); | |
255 | } | |
256 | dirp->dd_seek = 0; | |
257 | flags &= ~DTF_REWIND; | |
258 | } | |
259 | ||
260 | dirp->dd_loc = 0; | |
261 | dirp->dd_fd = fd; | |
262 | dirp->dd_flags = flags; | |
263 | ||
264 | /* | |
265 | * Set up seek point for rewinddir. | |
266 | */ | |
267 | dirp->dd_rewind = telldir(dirp); | |
268 | ||
269 | return (dirp); | |
270 | } | |
271 | ||
272 | /* | |
273 | * Open a directory. | |
274 | */ | |
275 | DIR *opendir(const char *name) | |
276 | { | |
277 | return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); | |
278 | } |