]> git.saurik.com Git - apple/libc.git/blame - gen/opendir-fbsd.c
Libc-594.1.4.tar.gz
[apple/libc.git] / gen / opendir-fbsd.c
CommitLineData
224c7076
A
1/*
2 * Copyright (c) 1983, 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
34#if defined(LIBC_SCCS) && !defined(lint)
35static char sccsid[] = "@(#)opendir.c 8.8 (Berkeley) 5/1/95";
36#endif /* LIBC_SCCS and not lint */
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: src/lib/libc/gen/opendir.c,v 1.22 2004/08/14 17:46:10 stefanf Exp $");
39
40#include "namespace.h"
41#include <sys/param.h>
42#include <sys/mount.h>
43#include <sys/stat.h>
44
45#include <dirent.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <stdlib.h>
49#include <string.h>
50#include <unistd.h>
51#include <pthread.h>
52#include "un-namespace.h"
53
54#include "telldir.h"
55/*
56 * Open a directory.
57 */
58DIR *
59opendir(name)
60 const char *name;
61{
62
63 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
64}
65
66DIR *
67__opendir2(name, flags)
68 const char *name;
69 int flags;
70{
71 DIR *dirp;
72 int fd;
73 int incr;
74 int saved_errno;
75 int unionstack;
76
77 /*
78 * Use O_DIRECTORY to only open directories (because opening of
79 * special files may be harmful). errno is set to ENOTDIR if
80 * not a directory.
81 */
82 if ((fd = _open(name, O_RDONLY | O_NONBLOCK | O_DIRECTORY)) == -1)
83 return (NULL);
84 dirp = NULL;
85 if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
86 (dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
87 goto fail;
88
89 dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
90 LIST_INIT(&dirp->dd_td->td_locq);
91 dirp->dd_td->td_loccnt = 0;
92
93 /*
94 * Use the system page size if that is a multiple of DIRBLKSIZ.
95 * Hopefully this can be a big win someday by allowing page
96 * trades to user space to be done by _getdirentries().
97 */
98 incr = getpagesize();
99 if ((incr % DIRBLKSIZ) != 0)
100 incr = DIRBLKSIZ;
101
102 /*
103 * Determine whether this directory is the top of a union stack.
104 */
105 if (flags & DTF_NODUP) {
106 struct statfs sfb;
107
108 if (_fstatfs(fd, &sfb) < 0)
109 goto fail;
110 unionstack = !strcmp(sfb.f_fstypename, "unionfs")
111 || (sfb.f_flags & MNT_UNION);
112 } else {
113 unionstack = 0;
114 }
115
116 if (unionstack) {
117 int len = 0;
118 int space = 0;
119 char *buf = 0;
120 char *ddptr = 0;
121 char *ddeptr;
122 int n;
123 struct dirent **dpv;
124
125 /*
126 * The strategy here is to read all the directory
127 * entries into a buffer, sort the buffer, and
128 * remove duplicate entries by setting the inode
129 * number to zero.
130 */
131
132 do {
133 /*
134 * Always make at least DIRBLKSIZ bytes
135 * available to _getdirentries
136 */
137 if (space < DIRBLKSIZ) {
138 space += incr;
139 len += incr;
140 buf = reallocf(buf, len);
141 if (buf == NULL)
142 goto fail;
143 ddptr = buf + (len - space);
144 }
145
146#if __DARWIN_64_BIT_INO_T
147 n = __getdirentries64(fd, ddptr, space, &dirp->dd_td->seekoff);
148#else /* !__DARWIN_64_BIT_INO_T */
149 n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
150#endif /* __DARWIN_64_BIT_INO_T */
151 if (n > 0) {
152 ddptr += n;
153 space -= n;
154 }
155 } while (n > 0);
156
157 ddeptr = ddptr;
158 flags |= __DTF_READALL;
159
160 /*
161 * Re-open the directory.
162 * This has the effect of rewinding back to the
163 * top of the union stack and is needed by
164 * programs which plan to fchdir to a descriptor
165 * which has also been read -- see fts.c.
166 */
167 if (flags & DTF_REWIND) {
168 (void)_close(fd);
169 if ((fd = _open(name, O_RDONLY)) == -1) {
170 saved_errno = errno;
171 free(buf);
172 free(dirp);
173 errno = saved_errno;
174 return (NULL);
175 }
176 }
177
178 /*
179 * There is now a buffer full of (possibly) duplicate
180 * names.
181 */
182 dirp->dd_buf = buf;
183
184 /*
185 * Go round this loop twice...
186 *
187 * Scan through the buffer, counting entries.
188 * On the second pass, save pointers to each one.
189 * Then sort the pointers and remove duplicate names.
190 */
191 for (dpv = 0;;) {
192 n = 0;
193 ddptr = buf;
194 while (ddptr < ddeptr) {
195 struct dirent *dp;
196
197 dp = (struct dirent *) ddptr;
198 if ((long)dp & 03L)
199 break;
200 if ((dp->d_reclen <= 0) ||
201 (dp->d_reclen > (ddeptr + 1 - ddptr)))
202 break;
203 ddptr += dp->d_reclen;
204 if (dp->d_fileno) {
205 if (dpv)
206 dpv[n] = dp;
207 n++;
208 }
209 }
210
211 if (dpv) {
212 struct dirent *xp;
213
214 /*
215 * This sort must be stable.
216 */
217 mergesort(dpv, n, sizeof(*dpv), alphasort);
218
219 dpv[n] = NULL;
220 xp = NULL;
221
222 /*
223 * Scan through the buffer in sort order,
224 * zapping the inode number of any
225 * duplicate names.
226 */
227 for (n = 0; dpv[n]; n++) {
228 struct dirent *dp = dpv[n];
229
230 if ((xp == NULL) ||
231 strcmp(dp->d_name, xp->d_name)) {
232 xp = dp;
233 } else {
234 dp->d_fileno = 0;
235 }
236 if (dp->d_type == DT_WHT &&
237 (flags & DTF_HIDEW))
238 dp->d_fileno = 0;
239 }
240
241 free(dpv);
242 break;
243 } else {
244 dpv = malloc((n+1) * sizeof(struct dirent *));
245 if (dpv == NULL)
246 break;
247 }
248 }
249
250 dirp->dd_len = len;
251 dirp->dd_size = ddptr - dirp->dd_buf;
252 } else {
253 dirp->dd_len = incr;
254 dirp->dd_size = 0;
255 dirp->dd_buf = malloc(dirp->dd_len);
256 if (dirp->dd_buf == NULL)
257 goto fail;
258#if __DARWIN_64_BIT_INO_T
259 dirp->dd_td->seekoff = 0;
260#else /* !__DARWIN_64_BIT_INO_T */
261 dirp->dd_seek = 0;
262#endif /* __DARWIN_64_BIT_INO_T */
263 flags &= ~DTF_REWIND;
264 }
265
266 dirp->dd_loc = 0;
267 dirp->dd_fd = fd;
268 dirp->dd_flags = flags;
269 dirp->dd_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
270
271 /*
272 * Set up seek point for rewinddir.
273 */
274 dirp->dd_rewind = telldir(dirp);
275
276 return (dirp);
277
278fail:
279 saved_errno = errno;
280 free(dirp);
281 (void)_close(fd);
282 errno = saved_errno;
283 return (NULL);
284}