opendir.c revision 73632
1/*
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.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/lib/libc/gen/opendir.c 73632 2001-03-05 09:21:44Z obrien $
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char sccsid[] = "@(#)opendir.c	8.8 (Berkeley) 5/1/95";
38#endif /* LIBC_SCCS and not lint */
39
40#include "namespace.h"
41#include <sys/param.h>
42#include <sys/mount.h>
43#include <sys/stat.h>
44
45#include <dirent.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <stdlib.h>
49#include <unistd.h>
50#include "un-namespace.h"
51
52#include "telldir.h"
53/*
54 * Open a directory.
55 */
56DIR *
57opendir(name)
58	const char *name;
59{
60
61	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
62}
63
64DIR *
65__opendir2(name, flags)
66	const char *name;
67	int flags;
68{
69	DIR *dirp;
70	int fd;
71	int incr;
72	int saved_errno;
73	int unionstack;
74	struct stat statb;
75
76	/*
77	 * stat() before _open() because opening of special files may be
78	 * harmful.  _fstat() after open because the file may have changed.
79	 */
80	if (stat(name, &statb) != 0)
81		return (NULL);
82	if (!S_ISDIR(statb.st_mode)) {
83		errno = ENOTDIR;
84		return (NULL);
85	}
86	if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
87		return (NULL);
88	dirp = NULL;
89	if (_fstat(fd, &statb) != 0)
90		goto fail;
91	if (!S_ISDIR(statb.st_mode)) {
92		errno = ENOTDIR;
93		goto fail;
94	}
95	if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
96	    (dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
97		goto fail;
98
99	dirp->dd_td = (void *)dirp + sizeof(DIR);
100	LIST_INIT(&dirp->dd_td->td_locq);
101	dirp->dd_td->td_loccnt = 0;
102
103	/*
104	 * Use the system page size if that is a multiple of DIRBLKSIZ.
105	 * Hopefully this can be a big win someday by allowing page
106	 * trades to user space to be done by _getdirentries().
107	 */
108	incr = getpagesize();
109	if ((incr % DIRBLKSIZ) != 0)
110		incr = DIRBLKSIZ;
111
112	/*
113	 * Determine whether this directory is the top of a union stack.
114	 */
115	if (flags & DTF_NODUP) {
116		struct statfs sfb;
117
118		if (_fstatfs(fd, &sfb) < 0)
119			goto fail;
120		unionstack = !strcmp(sfb.f_fstypename, "union");
121	} else {
122		unionstack = 0;
123	}
124
125	if (unionstack) {
126		int len = 0;
127		int space = 0;
128		char *buf = 0;
129		char *ddptr = 0;
130		char *ddeptr;
131		int n;
132		struct dirent **dpv;
133
134		/*
135		 * The strategy here is to read all the directory
136		 * entries into a buffer, sort the buffer, and
137		 * remove duplicate entries by setting the inode
138		 * number to zero.
139		 */
140
141		do {
142			/*
143			 * Always make at least DIRBLKSIZ bytes
144			 * available to _getdirentries
145			 */
146			if (space < DIRBLKSIZ) {
147				space += incr;
148				len += incr;
149				buf = reallocf(buf, len);
150				if (buf == NULL)
151					goto fail;
152				ddptr = buf + (len - space);
153			}
154
155			n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
156			if (n > 0) {
157				ddptr += n;
158				space -= n;
159			}
160		} while (n > 0);
161
162		ddeptr = ddptr;
163		flags |= __DTF_READALL;
164
165		/*
166		 * Re-open the directory.
167		 * This has the effect of rewinding back to the
168		 * top of the union stack and is needed by
169		 * programs which plan to fchdir to a descriptor
170		 * which has also been read -- see fts.c.
171		 */
172		if (flags & DTF_REWIND) {
173			(void)_close(fd);
174			if ((fd = _open(name, O_RDONLY)) == -1) {
175				saved_errno = errno;
176				free(buf);
177				free(dirp);
178				errno = saved_errno;
179				return (NULL);
180			}
181		}
182
183		/*
184		 * There is now a buffer full of (possibly) duplicate
185		 * names.
186		 */
187		dirp->dd_buf = buf;
188
189		/*
190		 * Go round this loop twice...
191		 *
192		 * Scan through the buffer, counting entries.
193		 * On the second pass, save pointers to each one.
194		 * Then sort the pointers and remove duplicate names.
195		 */
196		for (dpv = 0;;) {
197			n = 0;
198			ddptr = buf;
199			while (ddptr < ddeptr) {
200				struct dirent *dp;
201
202				dp = (struct dirent *) ddptr;
203				if ((long)dp & 03L)
204					break;
205				if ((dp->d_reclen <= 0) ||
206				    (dp->d_reclen > (ddeptr + 1 - ddptr)))
207					break;
208				ddptr += dp->d_reclen;
209				if (dp->d_fileno) {
210					if (dpv)
211						dpv[n] = dp;
212					n++;
213				}
214			}
215
216			if (dpv) {
217				struct dirent *xp;
218
219				/*
220				 * This sort must be stable.
221				 */
222				mergesort(dpv, n, sizeof(*dpv), alphasort);
223
224				dpv[n] = NULL;
225				xp = NULL;
226
227				/*
228				 * Scan through the buffer in sort order,
229				 * zapping the inode number of any
230				 * duplicate names.
231				 */
232				for (n = 0; dpv[n]; n++) {
233					struct dirent *dp = dpv[n];
234
235					if ((xp == NULL) ||
236					    strcmp(dp->d_name, xp->d_name)) {
237						xp = dp;
238					} else {
239						dp->d_fileno = 0;
240					}
241					if (dp->d_type == DT_WHT &&
242					    (flags & DTF_HIDEW))
243						dp->d_fileno = 0;
244				}
245
246				free(dpv);
247				break;
248			} else {
249				dpv = malloc((n+1) * sizeof(struct dirent *));
250				if (dpv == NULL)
251					break;
252			}
253		}
254
255		dirp->dd_len = len;
256		dirp->dd_size = ddptr - dirp->dd_buf;
257	} else {
258		dirp->dd_len = incr;
259		dirp->dd_buf = malloc(dirp->dd_len);
260		if (dirp->dd_buf == NULL)
261			goto fail;
262		dirp->dd_seek = 0;
263		flags &= ~DTF_REWIND;
264	}
265
266	dirp->dd_loc = 0;
267	dirp->dd_fd = fd;
268	dirp->dd_flags = flags;
269	dirp->dd_lock = NULL;
270
271	/*
272	 * Set up seek point for rewinddir.
273	 */
274	dirp->dd_rewind = telldir(dirp);
275
276	return (dirp);
277
278fail:
279	saved_errno = errno;
280	free(dirp);
281	(void)_close(fd);
282	errno = saved_errno;
283	return (NULL);
284}
285