2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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$");
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/mount.h>
47 #include "un-namespace.h"
51 static DIR * __opendir_common(int, const char *, int);
57 opendir(const char *name)
60 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
64 * Open a directory with existing file descriptor.
70 return (__opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP));
74 __opendir2(const char *name, int flags)
80 * stat() before _open() because opening of special files may be
83 if (stat(name, &statb) != 0)
85 if (!S_ISDIR(statb.st_mode)) {
89 if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
92 return __opendir_common(fd, name, flags);
96 opendir_compar(const void *p1, const void *p2)
99 return (strcmp((*(const struct dirent **)p1)->d_name,
100 (*(const struct dirent **)p2)->d_name));
104 * Common routine for opendir(3), __opendir2(3) and fdopendir(3).
107 __opendir_common(int fd, const char *name, int flags)
116 /* _fstat() the open handler because the file may have changed. */
117 if (_fstat(fd, &statb) != 0)
119 if (!S_ISDIR(statb.st_mode)) {
123 if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
124 (dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
127 dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
128 LIST_INIT(&dirp->dd_td->td_locq);
129 dirp->dd_td->td_loccnt = 0;
132 * Use the system page size if that is a multiple of DIRBLKSIZ.
133 * Hopefully this can be a big win someday by allowing page
134 * trades to user space to be done by _getdirentries().
136 incr = getpagesize();
137 if ((incr % DIRBLKSIZ) != 0)
141 * Determine whether this directory is the top of a union stack.
143 if (flags & DTF_NODUP) {
146 if (_fstatfs(fd, &sfb) < 0)
148 unionstack = !strcmp(sfb.f_fstypename, "unionfs")
149 || (sfb.f_flags & MNT_UNION);
164 * The strategy here is to read all the directory
165 * entries into a buffer, sort the buffer, and
166 * remove duplicate entries by setting the inode
172 * Always make at least DIRBLKSIZ bytes
173 * available to _getdirentries
175 if (space < DIRBLKSIZ) {
178 buf = reallocf(buf, len);
181 ddptr = buf + (len - space);
184 n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
192 flags |= __DTF_READALL;
195 * Re-open the directory.
196 * This has the effect of rewinding back to the
197 * top of the union stack and is needed by
198 * programs which plan to fchdir to a descriptor
199 * which has also been read -- see fts.c.
201 if (flags & DTF_REWIND) {
203 if ((fd = _open(name, O_RDONLY)) == -1) {
213 * There is now a buffer full of (possibly) duplicate
219 * Go round this loop twice...
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.
228 while (ddptr < ddeptr) {
231 dp = (struct dirent *) ddptr;
234 if ((dp->d_reclen <= 0) ||
235 (dp->d_reclen > (ddeptr + 1 - ddptr)))
237 ddptr += dp->d_reclen;
249 * This sort must be stable.
251 mergesort(dpv, n, sizeof(*dpv),
258 * Scan through the buffer in sort order,
259 * zapping the inode number of any
262 for (n = 0; dpv[n]; n++) {
263 struct dirent *dp = dpv[n];
266 strcmp(dp->d_name, xp->d_name)) {
271 if (dp->d_type == DT_WHT &&
279 dpv = malloc((n+1) * sizeof(struct dirent *));
286 dirp->dd_size = ddptr - dirp->dd_buf;
290 dirp->dd_buf = malloc(dirp->dd_len);
291 if (dirp->dd_buf == NULL)
294 flags &= ~DTF_REWIND;
299 dirp->dd_flags = flags;
300 dirp->dd_lock = NULL;
303 * Set up seek point for rewinddir.
305 dirp->dd_rewind = telldir(dirp);