opendir.c revision 178253
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1983, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
301556Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
311556Srgrimesstatic char sccsid[] = "@(#)opendir.c	8.8 (Berkeley) 5/1/95";
321556Srgrimes#endif /* LIBC_SCCS and not lint */
331556Srgrimes#include <sys/cdefs.h>
3450471Speter__FBSDID("$FreeBSD: head/lib/libc/gen/opendir.c 178253 2008-04-16 18:40:52Z delphij $");
351556Srgrimes
361556Srgrimes#include "namespace.h"
371556Srgrimes#include <sys/param.h>
381556Srgrimes#include <sys/mount.h>
391556Srgrimes#include <sys/stat.h>
401556Srgrimes
411556Srgrimes#include <dirent.h>
421556Srgrimes#include <errno.h>
431556Srgrimes#include <fcntl.h>
4446684Skris#include <stdlib.h>
451556Srgrimes#include <string.h>
461556Srgrimes#include <unistd.h>
471556Srgrimes#include "un-namespace.h"
481556Srgrimes
491556Srgrimes#include "telldir.h"
5046684Skris/*
511556Srgrimes * Open a directory.
521556Srgrimes */
531556SrgrimesDIR *
541556Srgrimesopendir(const char *name)
551556Srgrimes{
561556Srgrimes
571556Srgrimes	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
581556Srgrimes}
591556Srgrimes
601556SrgrimesDIR *
611556Srgrimes__opendir2(const char *name, int flags)
621556Srgrimes{
631556Srgrimes	DIR *dirp;
641556Srgrimes	int fd;
651556Srgrimes	int incr;
661556Srgrimes	int saved_errno;
671556Srgrimes	int unionstack;
681556Srgrimes	struct stat statb;
691556Srgrimes
701556Srgrimes	/*
711556Srgrimes	 * stat() before _open() because opening of special files may be
721556Srgrimes	 * harmful.  _fstat() after open because the file may have changed.
731556Srgrimes	 */
741556Srgrimes	if (stat(name, &statb) != 0)
751556Srgrimes		return (NULL);
761556Srgrimes	if (!S_ISDIR(statb.st_mode)) {
771556Srgrimes		errno = ENOTDIR;
781556Srgrimes		return (NULL);
791556Srgrimes	}
801556Srgrimes	if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
811556Srgrimes		return (NULL);
821556Srgrimes	dirp = NULL;
831556Srgrimes	if (_fstat(fd, &statb) != 0)
841556Srgrimes		goto fail;
851556Srgrimes	if (!S_ISDIR(statb.st_mode)) {
861556Srgrimes		errno = ENOTDIR;
871556Srgrimes		goto fail;
881556Srgrimes	}
891556Srgrimes	if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
901556Srgrimes	    (dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
911556Srgrimes		goto fail;
921556Srgrimes
931556Srgrimes	dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
941556Srgrimes	LIST_INIT(&dirp->dd_td->td_locq);
951556Srgrimes	dirp->dd_td->td_loccnt = 0;
961556Srgrimes
971556Srgrimes	/*
981556Srgrimes	 * Use the system page size if that is a multiple of DIRBLKSIZ.
99203613Simp	 * Hopefully this can be a big win someday by allowing page
1001556Srgrimes	 * trades to user space to be done by _getdirentries().
1011556Srgrimes	 */
1021556Srgrimes	incr = getpagesize();
1031556Srgrimes	if ((incr % DIRBLKSIZ) != 0)
1041556Srgrimes		incr = DIRBLKSIZ;
1051556Srgrimes
1061556Srgrimes	/*
1071556Srgrimes	 * Determine whether this directory is the top of a union stack.
1081556Srgrimes	 */
1091556Srgrimes	if (flags & DTF_NODUP) {
1101556Srgrimes		struct statfs sfb;
1111556Srgrimes
1121556Srgrimes		if (_fstatfs(fd, &sfb) < 0)
1131556Srgrimes			goto fail;
1141556Srgrimes		unionstack = !strcmp(sfb.f_fstypename, "unionfs")
1151556Srgrimes		    || (sfb.f_flags & MNT_UNION);
1161556Srgrimes	} else {
1171556Srgrimes		unionstack = 0;
1181556Srgrimes	}
1191556Srgrimes
1201556Srgrimes	if (unionstack) {
1211556Srgrimes		int len = 0;
1221556Srgrimes		int space = 0;
1231556Srgrimes		char *buf = 0;
1241556Srgrimes		char *ddptr = 0;
1251556Srgrimes		char *ddeptr;
1261556Srgrimes		int n;
1271556Srgrimes		struct dirent **dpv;
1281556Srgrimes
1291556Srgrimes		/*
1301556Srgrimes		 * The strategy here is to read all the directory
1311556Srgrimes		 * entries into a buffer, sort the buffer, and
1321556Srgrimes		 * remove duplicate entries by setting the inode
1331556Srgrimes		 * number to zero.
1341556Srgrimes		 */
1351556Srgrimes
1361556Srgrimes		do {
1371556Srgrimes			/*
1381556Srgrimes			 * Always make at least DIRBLKSIZ bytes
1391556Srgrimes			 * available to _getdirentries
1401556Srgrimes			 */
1411556Srgrimes			if (space < DIRBLKSIZ) {
1421556Srgrimes				space += incr;
1431556Srgrimes				len += incr;
1441556Srgrimes				buf = reallocf(buf, len);
145203613Simp				if (buf == NULL)
146					goto fail;
147				ddptr = buf + (len - space);
148			}
149
150			n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
151			if (n > 0) {
152				ddptr += n;
153				space -= n;
154			}
155		} while (n > 0);
156
157		ddeptr = ddptr;
158		flags |= __DTF_READALL;
159
160		/*
161		 * Re-open the directory.
162		 * This has the effect of rewinding back to the
163		 * top of the union stack and is needed by
164		 * programs which plan to fchdir to a descriptor
165		 * which has also been read -- see fts.c.
166		 */
167		if (flags & DTF_REWIND) {
168			(void)_close(fd);
169			if ((fd = _open(name, O_RDONLY)) == -1) {
170				saved_errno = errno;
171				free(buf);
172				free(dirp);
173				errno = saved_errno;
174				return (NULL);
175			}
176		}
177
178		/*
179		 * There is now a buffer full of (possibly) duplicate
180		 * names.
181		 */
182		dirp->dd_buf = buf;
183
184		/*
185		 * Go round this loop twice...
186		 *
187		 * Scan through the buffer, counting entries.
188		 * On the second pass, save pointers to each one.
189		 * Then sort the pointers and remove duplicate names.
190		 */
191		for (dpv = 0;;) {
192			n = 0;
193			ddptr = buf;
194			while (ddptr < ddeptr) {
195				struct dirent *dp;
196
197				dp = (struct dirent *) ddptr;
198				if ((long)dp & 03L)
199					break;
200				if ((dp->d_reclen <= 0) ||
201				    (dp->d_reclen > (ddeptr + 1 - ddptr)))
202					break;
203				ddptr += dp->d_reclen;
204				if (dp->d_fileno) {
205					if (dpv)
206						dpv[n] = dp;
207					n++;
208				}
209			}
210
211			if (dpv) {
212				struct dirent *xp;
213
214				/*
215				 * This sort must be stable.
216				 */
217				mergesort(dpv, n, sizeof(*dpv), alphasort);
218
219				dpv[n] = NULL;
220				xp = NULL;
221
222				/*
223				 * Scan through the buffer in sort order,
224				 * zapping the inode number of any
225				 * duplicate names.
226				 */
227				for (n = 0; dpv[n]; n++) {
228					struct dirent *dp = dpv[n];
229
230					if ((xp == NULL) ||
231					    strcmp(dp->d_name, xp->d_name)) {
232						xp = dp;
233					} else {
234						dp->d_fileno = 0;
235					}
236					if (dp->d_type == DT_WHT &&
237					    (flags & DTF_HIDEW))
238						dp->d_fileno = 0;
239				}
240
241				free(dpv);
242				break;
243			} else {
244				dpv = malloc((n+1) * sizeof(struct dirent *));
245				if (dpv == NULL)
246					break;
247			}
248		}
249
250		dirp->dd_len = len;
251		dirp->dd_size = ddptr - dirp->dd_buf;
252	} else {
253		dirp->dd_len = incr;
254		dirp->dd_size = 0;
255		dirp->dd_buf = malloc(dirp->dd_len);
256		if (dirp->dd_buf == NULL)
257			goto fail;
258		dirp->dd_seek = 0;
259		flags &= ~DTF_REWIND;
260	}
261
262	dirp->dd_loc = 0;
263	dirp->dd_fd = fd;
264	dirp->dd_flags = flags;
265	dirp->dd_lock = NULL;
266
267	/*
268	 * Set up seek point for rewinddir.
269	 */
270	dirp->dd_rewind = telldir(dirp);
271
272	return (dirp);
273
274fail:
275	saved_errno = errno;
276	free(dirp);
277	(void)_close(fd);
278	errno = saved_errno;
279	return (NULL);
280}
281