]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/opendir.c
Libc-1272.200.26.tar.gz
[apple/libc.git] / gen / FreeBSD / opendir.c
CommitLineData
1f2f436a 1/*-
e9ce8d39
A
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.
e9ce8d39
A
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
9385eb3d
A
30#if defined(LIBC_SCCS) && !defined(lint)
31static char sccsid[] = "@(#)opendir.c 8.8 (Berkeley) 5/1/95";
32#endif /* LIBC_SCCS and not lint */
33#include <sys/cdefs.h>
23e20b00 34__FBSDID("$FreeBSD$");
e9ce8d39 35
9385eb3d 36#include "namespace.h"
e9ce8d39
A
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>
e9ce8d39 45#include <string.h>
9385eb3d 46#include <unistd.h>
ad3c9f2a 47#include <pthread.h>
9385eb3d
A
48#include "un-namespace.h"
49
50#include "telldir.h"
23e20b00 51
b061a43b 52static DIR * __opendir_common(int, int, bool);
23e20b00 53
9385eb3d
A
54/*
55 * Open a directory.
56 */
57DIR *
1f2f436a 58opendir(const char *name)
9385eb3d
A
59{
60
61 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
62}
e9ce8d39 63
23e20b00
A
64/*
65 * Open a directory with existing file descriptor.
66 */
67DIR *
68fdopendir(int fd)
69{
70 struct stat statb;
71
72 /* Check that fd is associated with a directory. */
73 if (_fstat(fd, &statb) != 0)
74 return (NULL);
75 if (!S_ISDIR(statb.st_mode)) {
76 errno = ENOTDIR;
77 return (NULL);
78 }
b061a43b 79 /* Make sure CLOEXEC is set on the fd */
23e20b00
A
80 if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
81 return (NULL);
b061a43b 82 return (__opendir_common(fd, DTF_HIDEW|DTF_NODUP, true));
23e20b00
A
83}
84
9385eb3d 85DIR *
1f2f436a 86__opendir2(const char *name, int flags)
e9ce8d39 87{
e9ce8d39 88 int fd;
b061a43b
A
89 DIR *dir;
90 int saved_errno;
23e20b00 91
b061a43b
A
92 if ((flags & (__DTF_READALL | __DTF_SKIPREAD)) != 0)
93 return (NULL);
23e20b00
A
94 if ((fd = _open(name,
95 O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC)) == -1)
96 return (NULL);
97
b061a43b
A
98 dir = __opendir_common(fd, flags, false);
99 if (dir == NULL) {
100 saved_errno = errno;
101 _close(fd);
102 errno = saved_errno;
103 }
104 return (dir);
23e20b00
A
105}
106
107static int
108opendir_compar(const void *p1, const void *p2)
109{
110
111 return (strcmp((*(const struct dirent **)p1)->d_name,
112 (*(const struct dirent **)p2)->d_name));
113}
114
b061a43b
A
115/*
116 * For a directory at the top of a unionfs stack, the entire directory's
117 * contents are read and cached locally until the next call to rewinddir().
118 * For the fdopendir() case, the initial seek position must be preserved.
119 * For rewinddir(), the full directory should always be re-read from the
120 * beginning.
121 *
122 * If an error occurs, the existing buffer and state of 'dirp' is left
123 * unchanged.
124 */
125bool
126_filldir(DIR *dirp, bool use_current_pos)
127{
128 struct dirent **dpv;
129 char *buf, *ddptr, *ddeptr;
130 off_t pos;
131 int fd2, incr, len, n, saved_errno, space;
132
133 len = 0;
134 space = 0;
135 buf = NULL;
136 ddptr = NULL;
137
138 /*
139 * Use the system page size if that is a multiple of DIRBLKSIZ.
140 * Hopefully this can be a big win someday by allowing page
141 * trades to user space to be done by _getdirentries().
142 */
143 incr = getpagesize();
144 if ((incr % DIRBLKSIZ) != 0)
145 incr = DIRBLKSIZ;
146
147 /*
148 * The strategy here is to read all the directory
149 * entries into a buffer, sort the buffer, and
150 * remove duplicate entries by setting the inode
151 * number to zero.
152 *
153 * We reopen the directory because _getdirentries()
154 * on a MNT_UNION mount modifies the open directory,
155 * making it refer to the lower directory after the
156 * upper directory's entries are exhausted.
157 * This would otherwise break software that uses
158 * the directory descriptor for fchdir or *at
159 * functions, such as fts.c.
160 */
161 if ((fd2 = openat(dirp->dd_fd, ".", O_RDONLY | O_CLOEXEC)) == -1)
162 return (false);
163
164 if (use_current_pos) {
165 pos = lseek(dirp->dd_fd, 0, SEEK_CUR);
166 if (pos == -1 || lseek(fd2, pos, SEEK_SET) == -1) {
167 saved_errno = errno;
168 _close(fd2);
169 errno = saved_errno;
170 return (false);
171 }
172 }
173
174 do {
175 /*
176 * Always make at least DIRBLKSIZ bytes
177 * available to _getdirentries
178 */
179 if (space < DIRBLKSIZ) {
180 space += incr;
181 len += incr;
182 buf = reallocf(buf, len);
183 if (buf == NULL) {
184 saved_errno = errno;
185 _close(fd2);
186 errno = saved_errno;
187 return (false);
188 }
189 ddptr = buf + (len - space);
190 }
191
192#if __DARWIN_64_BIT_INO_T
193 n = (int)__getdirentries64(fd2, ddptr, space, &dirp->dd_td->seekoff);
194#else /* !__DARWIN_64_BIT_INO_T */
195 n = _getdirentries(fd2, ddptr, space, &dirp->dd_seek);
196#endif /* __DARWIN_64_BIT_INO_T */
197 if (n > 0) {
198 ddptr += n;
199 space -= n;
200 }
201 if (n < 0) {
202 saved_errno = errno;
203 _close(fd2);
204 errno = saved_errno;
205 return (false);
206 }
207 } while (n > 0);
208 _close(fd2);
209
210 ddeptr = ddptr;
211
212 /*
213 * There is now a buffer full of (possibly) duplicate
214 * names.
215 */
216 dirp->dd_buf = buf;
217
218 /*
219 * Go round this loop twice...
220 *
221 * Scan through the buffer, counting entries.
222 * On the second pass, save pointers to each one.
223 * Then sort the pointers and remove duplicate names.
224 */
225 for (dpv = NULL;;) {
226 n = 0;
227 ddptr = buf;
228 while (ddptr < ddeptr) {
229 struct dirent *dp;
230
231 dp = (struct dirent *) ddptr;
232 if ((long)dp & 03L)
233 break;
234 if ((dp->d_reclen <= 0) ||
235 (dp->d_reclen > (ddeptr + 1 - ddptr)))
236 break;
237 ddptr += dp->d_reclen;
238 if (dp->d_fileno) {
239 if (dpv)
240 dpv[n] = dp;
241 n++;
242 }
243 }
244
245 if (dpv) {
246 struct dirent *xp;
247
248 /*
249 * This sort must be stable.
250 */
251 mergesort(dpv, n, sizeof(*dpv), opendir_compar);
252
253 dpv[n] = NULL;
254 xp = NULL;
255
256 /*
257 * Scan through the buffer in sort order,
258 * zapping the inode number of any
259 * duplicate names.
260 */
261 for (n = 0; dpv[n]; n++) {
262 struct dirent *dp = dpv[n];
263
264 if ((xp == NULL) ||
265 strcmp(dp->d_name, xp->d_name)) {
266 xp = dp;
267 } else {
268 dp->d_fileno = 0;
269 }
270 if (dp->d_type == DT_WHT &&
271 (dirp->dd_flags & DTF_HIDEW))
272 dp->d_fileno = 0;
273 }
274
275 free(dpv);
276 break;
277 } else {
278 dpv = malloc((n+1) * sizeof(struct dirent *));
279 if (dpv == NULL)
280 break;
281 }
282 }
283
284 dirp->dd_len = len;
285 dirp->dd_size = ddptr - dirp->dd_buf;
286 return (true);
287}
288
289
23e20b00
A
290/*
291 * Common routine for opendir(3), __opendir2(3) and fdopendir(3).
292 */
293static DIR *
b061a43b 294__opendir_common(int fd, int flags, bool use_current_pos)
23e20b00
A
295{
296 DIR *dirp;
e9ce8d39 297 int incr;
9385eb3d 298 int saved_errno;
e9ce8d39 299 int unionstack;
e9ce8d39 300
23e20b00 301 if ((dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
e9ce8d39 302 return (NULL);
9385eb3d 303
b061a43b
A
304 dirp->dd_buf = NULL;
305 dirp->dd_fd = fd;
306 dirp->dd_flags = flags;
307 dirp->dd_loc = 0;
308 dirp->dd_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
3d9156a7 309 dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
9385eb3d
A
310 LIST_INIT(&dirp->dd_td->td_locq);
311 dirp->dd_td->td_loccnt = 0;
e9ce8d39
A
312
313 /*
9385eb3d 314 * Use the system page size if that is a multiple of DIRBLKSIZ.
e9ce8d39 315 * Hopefully this can be a big win someday by allowing page
9385eb3d 316 * trades to user space to be done by _getdirentries().
e9ce8d39 317 */
9385eb3d
A
318 incr = getpagesize();
319 if ((incr % DIRBLKSIZ) != 0)
e9ce8d39
A
320 incr = DIRBLKSIZ;
321
322 /*
323 * Determine whether this directory is the top of a union stack.
324 */
325 if (flags & DTF_NODUP) {
326 struct statfs sfb;
327
9385eb3d
A
328 if (_fstatfs(fd, &sfb) < 0)
329 goto fail;
23e20b00
A
330 unionstack = !strcmp(sfb.f_fstypename, "unionfs")
331 || (sfb.f_flags & MNT_UNION);
e9ce8d39
A
332 } else {
333 unionstack = 0;
334 }
335
336 if (unionstack) {
b061a43b
A
337 if (!_filldir(dirp, use_current_pos))
338 goto fail;
339 dirp->dd_flags |= __DTF_READALL;
340 } else {
341 dirp->dd_len = incr;
342 dirp->dd_buf = malloc(dirp->dd_len);
343 if (dirp->dd_buf == NULL)
344 goto fail;
345 if (use_current_pos) {
e9ce8d39 346 /*
b061a43b
A
347 * Read the first batch of directory entries
348 * to prime dd_seek. This also checks if the
349 * fd passed to fdopendir() is a directory.
e9ce8d39 350 */
ad3c9f2a 351#if __DARWIN_64_BIT_INO_T
b061a43b
A
352 dirp->dd_size = (long)__getdirentries64(dirp->dd_fd,
353 dirp->dd_buf, dirp->dd_len, &dirp->dd_td->seekoff);
ad3c9f2a 354#else /* !__DARWIN_64_BIT_INO_T */
b061a43b
A
355 dirp->dd_size = _getdirentries(dirp->dd_fd,
356 dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
ad3c9f2a 357#endif /* __DARWIN_64_BIT_INO_T */
b061a43b
A
358 if (dirp->dd_size < 0) {
359 if (errno == EINVAL)
360 errno = ENOTDIR;
361 goto fail;
e9ce8d39 362 }
b061a43b
A
363 dirp->dd_flags |= __DTF_SKIPREAD;
364 } else {
365 dirp->dd_size = 0;
ad3c9f2a 366#if __DARWIN_64_BIT_INO_T
b061a43b 367 dirp->dd_td->seekoff = 0;
ad3c9f2a 368#else /* !__DARWIN_64_BIT_INO_T */
b061a43b 369 dirp->dd_seek = 0;
ad3c9f2a 370#endif /* __DARWIN_64_BIT_INO_T */
b061a43b 371 }
e9ce8d39
A
372 }
373
e9ce8d39 374 return (dirp);
e9ce8d39 375
9385eb3d
A
376fail:
377 saved_errno = errno;
b061a43b 378 free(dirp->dd_buf);
9385eb3d 379 free(dirp);
9385eb3d
A
380 errno = saved_errno;
381 return (NULL);
e9ce8d39 382}