]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/opendir.c
Libc-825.40.1.tar.gz
[apple/libc.git] / gen / FreeBSD / opendir.c
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)opendir.c 8.8 (Berkeley) 5/1/95";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: src/lib/libc/gen/opendir.c,v 1.24 2008/04/16 18:40:52 delphij Exp $");
35
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40
41 #include <dirent.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <pthread.h>
48 #include "un-namespace.h"
49
50 #include "telldir.h"
51 /*
52 * Open a directory.
53 */
54 DIR *
55 opendir(const char *name)
56 {
57
58 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
59 }
60
61 DIR *
62 __opendir2(const char *name, int flags)
63 {
64 DIR *dirp;
65 int fd;
66 int incr;
67 int saved_errno;
68 int unionstack;
69
70 /*
71 * Use O_DIRECTORY to only open directories (because opening of
72 * special files may be harmful). errno is set to ENOTDIR if
73 * not a directory.
74 */
75 if ((fd = _open(name, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC)) == -1)
76 return (NULL);
77 dirp = malloc(sizeof(DIR) + sizeof(struct _telldir));
78 if (dirp == NULL)
79 goto fail;
80
81 dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
82 LIST_INIT(&dirp->dd_td->td_locq);
83 dirp->dd_td->td_loccnt = 0;
84
85 /*
86 * Use the system page size if that is a multiple of DIRBLKSIZ.
87 * Hopefully this can be a big win someday by allowing page
88 * trades to user space to be done by _getdirentries().
89 */
90 incr = getpagesize();
91 if ((incr % DIRBLKSIZ) != 0)
92 incr = DIRBLKSIZ;
93
94 /*
95 * Determine whether this directory is the top of a union stack.
96 */
97 if (flags & DTF_NODUP) {
98 struct statfs sfb;
99
100 if (_fstatfs(fd, &sfb) < 0)
101 goto fail;
102 unionstack = (sfb.f_flags & MNT_UNION);
103 } else {
104 unionstack = 0;
105 }
106
107 if (unionstack) {
108 int len = 0;
109 int space = 0;
110 char *buf = 0;
111 char *ddptr = 0;
112 char *ddeptr;
113 int n;
114 struct dirent **dpv;
115
116 /*
117 * The strategy here is to read all the directory
118 * entries into a buffer, sort the buffer, and
119 * remove duplicate entries by setting the inode
120 * number to zero.
121 */
122
123 do {
124 /*
125 * Always make at least DIRBLKSIZ bytes
126 * available to _getdirentries
127 */
128 if (space < DIRBLKSIZ) {
129 space += incr;
130 len += incr;
131 buf = reallocf(buf, len);
132 if (buf == NULL)
133 goto fail;
134 ddptr = buf + (len - space);
135 }
136
137 #if __DARWIN_64_BIT_INO_T
138 n = __getdirentries64(fd, ddptr, space, &dirp->dd_td->seekoff);
139 #else /* !__DARWIN_64_BIT_INO_T */
140 n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
141 #endif /* __DARWIN_64_BIT_INO_T */
142 if (n > 0) {
143 ddptr += n;
144 space -= n;
145 }
146 } while (n > 0);
147
148 ddeptr = ddptr;
149 flags |= __DTF_READALL;
150
151 /*
152 * Re-open the directory.
153 * This has the effect of rewinding back to the
154 * top of the union stack and is needed by
155 * programs which plan to fchdir to a descriptor
156 * which has also been read -- see fts.c.
157 */
158 if (flags & DTF_REWIND) {
159 (void)_close(fd);
160 if ((fd = _open(name, O_RDONLY)) == -1) {
161 saved_errno = errno;
162 free(buf);
163 free(dirp);
164 errno = saved_errno;
165 return (NULL);
166 }
167 }
168
169 /*
170 * There is now a buffer full of (possibly) duplicate
171 * names.
172 */
173 dirp->dd_buf = buf;
174
175 /*
176 * Go round this loop twice...
177 *
178 * Scan through the buffer, counting entries.
179 * On the second pass, save pointers to each one.
180 * Then sort the pointers and remove duplicate names.
181 */
182 for (dpv = 0;;) {
183 n = 0;
184 ddptr = buf;
185 while (ddptr < ddeptr) {
186 struct dirent *dp;
187
188 dp = (struct dirent *) ddptr;
189 if ((long)dp & 03L)
190 break;
191 if ((dp->d_reclen <= 0) ||
192 (dp->d_reclen > (ddeptr + 1 - ddptr)))
193 break;
194 ddptr += dp->d_reclen;
195 if (dp->d_fileno) {
196 if (dpv)
197 dpv[n] = dp;
198 n++;
199 }
200 }
201
202 if (dpv) {
203 struct dirent *xp;
204
205 /*
206 * This sort must be stable.
207 */
208 mergesort(dpv, n, sizeof(*dpv), alphasort);
209
210 dpv[n] = NULL;
211 xp = NULL;
212
213 /*
214 * Scan through the buffer in sort order,
215 * zapping the inode number of any
216 * duplicate names.
217 */
218 for (n = 0; dpv[n]; n++) {
219 struct dirent *dp = dpv[n];
220
221 if ((xp == NULL) ||
222 strcmp(dp->d_name, xp->d_name)) {
223 xp = dp;
224 } else {
225 dp->d_fileno = 0;
226 }
227 if (dp->d_type == DT_WHT &&
228 (flags & DTF_HIDEW))
229 dp->d_fileno = 0;
230 }
231
232 free(dpv);
233 break;
234 } else {
235 dpv = malloc((n+1) * sizeof(struct dirent *));
236 if (dpv == NULL)
237 break;
238 }
239 }
240
241 dirp->dd_len = len;
242 dirp->dd_size = ddptr - dirp->dd_buf;
243 } else {
244 dirp->dd_len = incr;
245 dirp->dd_size = 0;
246 dirp->dd_buf = malloc(dirp->dd_len);
247 if (dirp->dd_buf == NULL)
248 goto fail;
249 #if __DARWIN_64_BIT_INO_T
250 dirp->dd_td->seekoff = 0;
251 #else /* !__DARWIN_64_BIT_INO_T */
252 dirp->dd_seek = 0;
253 #endif /* __DARWIN_64_BIT_INO_T */
254 flags &= ~DTF_REWIND;
255 }
256
257 dirp->dd_loc = 0;
258 dirp->dd_fd = fd;
259 dirp->dd_flags = flags;
260 dirp->dd_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
261
262 /*
263 * Set up seek point for rewinddir.
264 */
265 dirp->dd_rewind = telldir(dirp);
266
267 return (dirp);
268
269 fail:
270 saved_errno = errno;
271 free(dirp);
272 (void)_close(fd);
273 errno = saved_errno;
274 return (NULL);
275 }