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"
49 #include "gen-private.h"
52 static DIR * __opendir_common(int, int, bool);
58 opendir(const char *name)
61 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
65 * Open a directory with existing file descriptor.
71 if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
73 return (__opendir_common(fd, DTF_HIDEW|DTF_NODUP, true));
77 __opendir2(const char *name, int flags)
83 if ((flags & (__DTF_READALL | __DTF_SKIPREAD)) != 0)
86 O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC)) == -1)
89 dir = __opendir_common(fd, flags, false);
99 opendir_compar(const void *p1, const void *p2)
102 return (strcmp((*(const struct dirent **)p1)->d_name,
103 (*(const struct dirent **)p2)->d_name));
107 * For a directory at the top of a unionfs stack, the entire directory's
108 * contents are read and cached locally until the next call to rewinddir().
109 * For the fdopendir() case, the initial seek position must be preserved.
110 * For rewinddir(), the full directory should always be re-read from the
113 * If an error occurs, the existing buffer and state of 'dirp' is left
117 _filldir(DIR *dirp, bool use_current_pos)
120 char *buf, *ddptr, *ddeptr;
122 int fd2, incr, len, n, saved_errno, space;
130 * Use the system page size if that is a multiple of DIRBLKSIZ.
131 * Hopefully this can be a big win someday by allowing page
132 * trades to user space to be done by _getdirentries().
134 incr = getpagesize();
135 if ((incr % DIRBLKSIZ) != 0)
139 * The strategy here is to read all the directory
140 * entries into a buffer, sort the buffer, and
141 * remove duplicate entries by setting the inode
144 * We reopen the directory because _getdirentries()
145 * on a MNT_UNION mount modifies the open directory,
146 * making it refer to the lower directory after the
147 * upper directory's entries are exhausted.
148 * This would otherwise break software that uses
149 * the directory descriptor for fchdir or *at
150 * functions, such as fts.c.
152 if ((fd2 = _openat(dirp->dd_fd, ".", O_RDONLY | O_CLOEXEC)) == -1)
155 if (use_current_pos) {
156 pos = lseek(dirp->dd_fd, 0, SEEK_CUR);
157 if (pos == -1 || lseek(fd2, pos, SEEK_SET) == -1) {
167 * Always make at least DIRBLKSIZ bytes
168 * available to _getdirentries
170 if (space < DIRBLKSIZ) {
173 buf = reallocf(buf, len);
180 ddptr = buf + (len - space);
183 n = _getdirentries(fd2, ddptr, space, &dirp->dd_seek);
200 * There is now a buffer full of (possibly) duplicate
206 * Go round this loop twice...
208 * Scan through the buffer, counting entries.
209 * On the second pass, save pointers to each one.
210 * Then sort the pointers and remove duplicate names.
215 while (ddptr < ddeptr) {
218 dp = (struct dirent *) ddptr;
221 if ((dp->d_reclen <= 0) ||
222 (dp->d_reclen > (ddeptr + 1 - ddptr)))
224 ddptr += dp->d_reclen;
236 * This sort must be stable.
238 mergesort(dpv, n, sizeof(*dpv), opendir_compar);
244 * Scan through the buffer in sort order,
245 * zapping the inode number of any
248 for (n = 0; dpv[n]; n++) {
249 struct dirent *dp = dpv[n];
252 strcmp(dp->d_name, xp->d_name)) {
257 if (dp->d_type == DT_WHT &&
258 (dirp->dd_flags & DTF_HIDEW))
265 dpv = malloc((n+1) * sizeof(struct dirent *));
272 dirp->dd_size = ddptr - dirp->dd_buf;
278 * Common routine for opendir(3), __opendir2(3) and fdopendir(3).
281 __opendir_common(int fd, int flags, bool use_current_pos)
288 if ((dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
293 dirp->dd_flags = flags;
295 dirp->dd_lock = NULL;
296 dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
297 LIST_INIT(&dirp->dd_td->td_locq);
298 dirp->dd_td->td_loccnt = 0;
301 * Use the system page size if that is a multiple of DIRBLKSIZ.
302 * Hopefully this can be a big win someday by allowing page
303 * trades to user space to be done by _getdirentries().
305 incr = getpagesize();
306 if ((incr % DIRBLKSIZ) != 0)
310 * Determine whether this directory is the top of a union stack.
312 if (flags & DTF_NODUP) {
315 if (_fstatfs(fd, &sfb) < 0)
317 unionstack = !strcmp(sfb.f_fstypename, "unionfs")
318 || (sfb.f_flags & MNT_UNION);
324 if (!_filldir(dirp, use_current_pos))
326 dirp->dd_flags |= __DTF_READALL;
329 dirp->dd_buf = malloc(dirp->dd_len);
330 if (dirp->dd_buf == NULL)
332 if (use_current_pos) {
334 * Read the first batch of directory entries
335 * to prime dd_seek. This also checks if the
336 * fd passed to fdopendir() is a directory.
338 dirp->dd_size = _getdirentries(dirp->dd_fd,
339 dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
340 if (dirp->dd_size < 0) {
345 dirp->dd_flags |= __DTF_SKIPREAD;