2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "namespace.h"
33 #include <sys/param.h>
41 #include "un-namespace.h"
43 #include "gen-private.h"
46 static DIR * __opendir_common(int, int, bool);
52 opendir(const char *name)
55 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
59 * Open a directory with existing file descriptor.
65 if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
67 return (__opendir_common(fd, DTF_HIDEW|DTF_NODUP, true));
71 __opendir2(const char *name, int flags)
77 if ((flags & (__DTF_READALL | __DTF_SKIPREAD)) != 0)
80 O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC)) == -1)
83 dir = __opendir_common(fd, flags, false);
93 opendir_compar(const void *p1, const void *p2)
96 return (strcmp((*(const struct dirent * const *)p1)->d_name,
97 (*(const struct dirent * const *)p2)->d_name));
101 * For a directory at the top of a unionfs stack, the entire directory's
102 * contents are read and cached locally until the next call to rewinddir().
103 * For the fdopendir() case, the initial seek position must be preserved.
104 * For rewinddir(), the full directory should always be re-read from the
107 * If an error occurs, the existing buffer and state of 'dirp' is left
111 _filldir(DIR *dirp, bool use_current_pos)
114 char *buf, *ddptr, *ddeptr;
116 int fd2, incr, len, n, saved_errno, space;
124 * Use the system page size if that is a multiple of DIRBLKSIZ.
125 * Hopefully this can be a big win someday by allowing page
126 * trades to user space to be done by _getdirentries().
128 incr = getpagesize();
129 if ((incr % DIRBLKSIZ) != 0)
133 * The strategy here is to read all the directory
134 * entries into a buffer, sort the buffer, and
135 * remove duplicate entries by setting the inode
138 * We reopen the directory because _getdirentries()
139 * on a MNT_UNION mount modifies the open directory,
140 * making it refer to the lower directory after the
141 * upper directory's entries are exhausted.
142 * This would otherwise break software that uses
143 * the directory descriptor for fchdir or *at
144 * functions, such as fts.c.
146 if ((fd2 = _openat(dirp->dd_fd, ".", O_RDONLY | O_CLOEXEC)) == -1)
149 if (use_current_pos) {
150 pos = lseek(dirp->dd_fd, 0, SEEK_CUR);
151 if (pos == -1 || lseek(fd2, pos, SEEK_SET) == -1) {
161 * Always make at least DIRBLKSIZ bytes
162 * available to _getdirentries
164 if (space < DIRBLKSIZ) {
167 buf = reallocf(buf, len);
174 ddptr = buf + (len - space);
177 n = _getdirentries(fd2, ddptr, space, &dirp->dd_seek);
194 * There is now a buffer full of (possibly) duplicate
200 * Go round this loop twice...
202 * Scan through the buffer, counting entries.
203 * On the second pass, save pointers to each one.
204 * Then sort the pointers and remove duplicate names.
209 while (ddptr < ddeptr) {
212 dp = (struct dirent *) ddptr;
215 if ((dp->d_reclen <= 0) ||
216 (dp->d_reclen > (ddeptr + 1 - ddptr)))
218 ddptr += dp->d_reclen;
230 * This sort must be stable.
232 mergesort(dpv, n, sizeof(*dpv), opendir_compar);
238 * Scan through the buffer in sort order,
239 * zapping the inode number of any
242 for (n = 0; dpv[n]; n++) {
243 struct dirent *dp = dpv[n];
246 strcmp(dp->d_name, xp->d_name)) {
251 if (dp->d_type == DT_WHT &&
252 (dirp->dd_flags & DTF_HIDEW))
259 dpv = malloc((n+1) * sizeof(struct dirent *));
266 dirp->dd_size = ddptr - dirp->dd_buf;
271 is_unionstack(int fd)
275 unionstack = _fcntl(fd, F_ISUNIONSTACK, 0);
276 if (unionstack != -1)
280 * Should not happen unless running on a kernel without the op,
281 * but no use rendering the system useless in such a case.
287 * Common routine for opendir(3), __opendir2(3) and fdopendir(3).
290 __opendir_common(int fd, int flags, bool use_current_pos)
297 if ((dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
302 dirp->dd_flags = flags;
304 dirp->dd_lock = NULL;
305 dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
306 LIST_INIT(&dirp->dd_td->td_locq);
307 dirp->dd_td->td_loccnt = 0;
308 dirp->dd_compat_de = NULL;
311 * Use the system page size if that is a multiple of DIRBLKSIZ.
312 * Hopefully this can be a big win someday by allowing page
313 * trades to user space to be done by _getdirentries().
315 incr = getpagesize();
316 if ((incr % DIRBLKSIZ) != 0)
320 * Determine whether this directory is the top of a union stack.
323 if (flags & DTF_NODUP) {
324 unionstack = is_unionstack(fd);
328 if (!_filldir(dirp, use_current_pos))
330 dirp->dd_flags |= __DTF_READALL;
333 dirp->dd_buf = malloc(dirp->dd_len);
334 if (dirp->dd_buf == NULL)
336 if (use_current_pos) {
338 * Read the first batch of directory entries
339 * to prime dd_seek. This also checks if the
340 * fd passed to fdopendir() is a directory.
342 dirp->dd_size = _getdirentries(dirp->dd_fd,
343 dirp->dd_buf, dirp->dd_len, &dirp->dd_seek);
344 if (dirp->dd_size < 0) {
349 dirp->dd_flags |= __DTF_SKIPREAD;