opendir.c revision 165903
1153317Ssam/*
2153317Ssam * Copyright (c) 1983, 1993
3153317Ssam *	The Regents of the University of California.  All rights reserved.
4153317Ssam *
5153317Ssam * Redistribution and use in source and binary forms, with or without
6153317Ssam * modification, are permitted provided that the following conditions
7153317Ssam * are met:
8153317Ssam * 1. Redistributions of source code must retain the above copyright
9153317Ssam *    notice, this list of conditions and the following disclaimer.
10153317Ssam * 2. Redistributions in binary form must reproduce the above copyright
11153317Ssam *    notice, this list of conditions and the following disclaimer in the
12153317Ssam *    documentation and/or other materials provided with the distribution.
13153317Ssam * 4. Neither the name of the University nor the names of its contributors
14153317Ssam *    may be used to endorse or promote products derived from this software
15153317Ssam *    without specific prior written permission.
16153317Ssam *
17153317Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18153317Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19153317Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20153317Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21153317Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22153317Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23153317Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24153317Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25153317Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26153317Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27153317Ssam * SUCH DAMAGE.
28153317Ssam */
29153317Ssam
30153317Ssam#if defined(LIBC_SCCS) && !defined(lint)
31153317Ssamstatic char sccsid[] = "@(#)opendir.c	8.8 (Berkeley) 5/1/95";
32153317Ssam#endif /* LIBC_SCCS and not lint */
33153317Ssam#include <sys/cdefs.h>
34153317Ssam__FBSDID("$FreeBSD: head/lib/libc/gen/opendir.c 165903 2007-01-09 00:28:16Z imp $");
35153317Ssam
36153317Ssam#include "namespace.h"
37153317Ssam#include <sys/param.h>
38153317Ssam#include <sys/mount.h>
39153317Ssam#include <sys/stat.h>
40153317Ssam
41153317Ssam#include <dirent.h>
42153317Ssam#include <errno.h>
43153317Ssam#include <fcntl.h>
44153317Ssam#include <stdlib.h>
45153317Ssam#include <string.h>
46170534Ssam#include <unistd.h>
47153317Ssam#include "un-namespace.h"
48153317Ssam
49153317Ssam#include "telldir.h"
50170534Ssam/*
51153317Ssam * Open a directory.
52153317Ssam */
53153317SsamDIR *
54153317Ssamopendir(name)
55153317Ssam	const char *name;
56170534Ssam{
57153317Ssam
58153317Ssam	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
59153317Ssam}
60153317Ssam
61153317SsamDIR *
62153317Ssam__opendir2(name, flags)
63153317Ssam	const char *name;
64153317Ssam	int flags;
65153317Ssam{
66153317Ssam	DIR *dirp;
67153317Ssam	int fd;
68153317Ssam	int incr;
69153317Ssam	int saved_errno;
70153317Ssam	int unionstack;
71153317Ssam	struct stat statb;
72153317Ssam
73153317Ssam	/*
74153317Ssam	 * stat() before _open() because opening of special files may be
75153317Ssam	 * harmful.  _fstat() after open because the file may have changed.
76153317Ssam	 */
77153317Ssam	if (stat(name, &statb) != 0)
78153317Ssam		return (NULL);
79153317Ssam	if (!S_ISDIR(statb.st_mode)) {
80153317Ssam		errno = ENOTDIR;
81153317Ssam		return (NULL);
82164635Ssam	}
83178359Ssam	if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
84178359Ssam		return (NULL);
85178359Ssam	dirp = NULL;
86178359Ssam	if (_fstat(fd, &statb) != 0)
87153317Ssam		goto fail;
88153317Ssam	if (!S_ISDIR(statb.st_mode)) {
89153317Ssam		errno = ENOTDIR;
90153317Ssam		goto fail;
91153317Ssam	}
92170534Ssam	if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
93153317Ssam	    (dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
94153317Ssam		goto fail;
95153317Ssam
96153317Ssam	dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
97153317Ssam	LIST_INIT(&dirp->dd_td->td_locq);
98153317Ssam	dirp->dd_td->td_loccnt = 0;
99153317Ssam
100153317Ssam	/*
101153317Ssam	 * Use the system page size if that is a multiple of DIRBLKSIZ.
102153317Ssam	 * Hopefully this can be a big win someday by allowing page
103153317Ssam	 * trades to user space to be done by _getdirentries().
104153317Ssam	 */
105153317Ssam	incr = getpagesize();
106165963Ssam	if ((incr % DIRBLKSIZ) != 0)
107153317Ssam		incr = DIRBLKSIZ;
108153317Ssam
109153317Ssam	/*
110153317Ssam	 * Determine whether this directory is the top of a union stack.
111153317Ssam	 */
112153317Ssam	if (flags & DTF_NODUP) {
113153317Ssam		struct statfs sfb;
114153317Ssam
115153317Ssam		if (_fstatfs(fd, &sfb) < 0)
116153317Ssam			goto fail;
117153317Ssam		unionstack = !strcmp(sfb.f_fstypename, "unionfs")
118164635Ssam		    || (sfb.f_flags & MNT_UNION);
119178359Ssam	} else {
120178359Ssam		unionstack = 0;
121178359Ssam	}
122178359Ssam
123153317Ssam	if (unionstack) {
124153317Ssam		int len = 0;
125153317Ssam		int space = 0;
126153317Ssam		char *buf = 0;
127153317Ssam		char *ddptr = 0;
128153317Ssam		char *ddeptr;
129153317Ssam		int n;
130153317Ssam		struct dirent **dpv;
131153317Ssam
132153317Ssam		/*
133153317Ssam		 * The strategy here is to read all the directory
134153317Ssam		 * entries into a buffer, sort the buffer, and
135153317Ssam		 * remove duplicate entries by setting the inode
136153317Ssam		 * number to zero.
137153317Ssam		 */
138153317Ssam
139153317Ssam		do {
140153317Ssam			/*
141153317Ssam			 * Always make at least DIRBLKSIZ bytes
142153317Ssam			 * available to _getdirentries
143153317Ssam			 */
144153317Ssam			if (space < DIRBLKSIZ) {
145153317Ssam				space += incr;
146153317Ssam				len += incr;
147153317Ssam				buf = reallocf(buf, len);
148153317Ssam				if (buf == NULL)
149153317Ssam					goto fail;
150153317Ssam				ddptr = buf + (len - space);
151153317Ssam			}
152153317Ssam
153153317Ssam			n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
154153317Ssam			if (n > 0) {
155153317Ssam				ddptr += n;
156153317Ssam				space -= n;
157153317Ssam			}
158153317Ssam		} while (n > 0);
159178359Ssam
160178359Ssam		ddeptr = ddptr;
161178359Ssam		flags |= __DTF_READALL;
162178359Ssam
163178359Ssam		/*
164178359Ssam		 * Re-open the directory.
165178359Ssam		 * This has the effect of rewinding back to the
166178359Ssam		 * top of the union stack and is needed by
167178359Ssam		 * programs which plan to fchdir to a descriptor
168178359Ssam		 * which has also been read -- see fts.c.
169178359Ssam		 */
170178359Ssam		if (flags & DTF_REWIND) {
171178359Ssam			(void)_close(fd);
172178359Ssam			if ((fd = _open(name, O_RDONLY)) == -1) {
173153317Ssam				saved_errno = errno;
174153317Ssam				free(buf);
175153317Ssam				free(dirp);
176153317Ssam				errno = saved_errno;
177153317Ssam				return (NULL);
178170534Ssam			}
179153317Ssam		}
180153317Ssam
181178359Ssam		/*
182153317Ssam		 * There is now a buffer full of (possibly) duplicate
183153317Ssam		 * names.
184178359Ssam		 */
185153317Ssam		dirp->dd_buf = buf;
186153317Ssam
187153317Ssam		/*
188153317Ssam		 * Go round this loop twice...
189178359Ssam		 *
190178359Ssam		 * Scan through the buffer, counting entries.
191178359Ssam		 * On the second pass, save pointers to each one.
192153317Ssam		 * Then sort the pointers and remove duplicate names.
193153317Ssam		 */
194153317Ssam		for (dpv = 0;;) {
195153317Ssam			n = 0;
196153317Ssam			ddptr = buf;
197153317Ssam			while (ddptr < ddeptr) {
198153317Ssam				struct dirent *dp;
199153317Ssam
200153317Ssam				dp = (struct dirent *) ddptr;
201153317Ssam				if ((long)dp & 03L)
202153317Ssam					break;
203153317Ssam				if ((dp->d_reclen <= 0) ||
204153317Ssam				    (dp->d_reclen > (ddeptr + 1 - ddptr)))
205153317Ssam					break;
206153317Ssam				ddptr += dp->d_reclen;
207153317Ssam				if (dp->d_fileno) {
208153317Ssam					if (dpv)
209153317Ssam						dpv[n] = dp;
210153317Ssam					n++;
211153317Ssam				}
212153317Ssam			}
213153317Ssam
214153317Ssam			if (dpv) {
215153317Ssam				struct dirent *xp;
216153317Ssam
217153317Ssam				/*
218153317Ssam				 * This sort must be stable.
219153317Ssam				 */
220153317Ssam				mergesort(dpv, n, sizeof(*dpv), alphasort);
221153317Ssam
222153317Ssam				dpv[n] = NULL;
223170534Ssam				xp = NULL;
224170534Ssam
225153317Ssam				/*
226153317Ssam				 * Scan through the buffer in sort order,
227153317Ssam				 * zapping the inode number of any
228170534Ssam				 * duplicate names.
229153317Ssam				 */
230153317Ssam				for (n = 0; dpv[n]; n++) {
231153317Ssam					struct dirent *dp = dpv[n];
232153317Ssam
233153317Ssam					if ((xp == NULL) ||
234153317Ssam					    strcmp(dp->d_name, xp->d_name)) {
235153317Ssam						xp = dp;
236153317Ssam					} else {
237153317Ssam						dp->d_fileno = 0;
238153317Ssam					}
239153317Ssam					if (dp->d_type == DT_WHT &&
240153317Ssam					    (flags & DTF_HIDEW))
241153317Ssam						dp->d_fileno = 0;
242153317Ssam				}
243153317Ssam
244153317Ssam				free(dpv);
245153317Ssam				break;
246153317Ssam			} else {
247153317Ssam				dpv = malloc((n+1) * sizeof(struct dirent *));
248153317Ssam				if (dpv == NULL)
249153317Ssam					break;
250153317Ssam			}
251		}
252
253		dirp->dd_len = len;
254		dirp->dd_size = ddptr - dirp->dd_buf;
255	} else {
256		dirp->dd_len = incr;
257		dirp->dd_size = 0;
258		dirp->dd_buf = malloc(dirp->dd_len);
259		if (dirp->dd_buf == NULL)
260			goto fail;
261		dirp->dd_seek = 0;
262		flags &= ~DTF_REWIND;
263	}
264
265	dirp->dd_loc = 0;
266	dirp->dd_fd = fd;
267	dirp->dd_flags = flags;
268	dirp->dd_lock = NULL;
269
270	/*
271	 * Set up seek point for rewinddir.
272	 */
273	dirp->dd_rewind = telldir(dirp);
274
275	return (dirp);
276
277fail:
278	saved_errno = errno;
279	free(dirp);
280	(void)_close(fd);
281	errno = saved_errno;
282	return (NULL);
283}
284