xref: /dragonfly/lib/libc/gen/opendir.c (revision 333227be)
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: src/lib/libc/gen/opendir.c,v 1.10.2.1 2001/06/04 20:59:48 joerg Exp $
34  * $DragonFly: src/lib/libc/gen/opendir.c,v 1.2 2003/06/17 04:26:42 dillon Exp $
35  *
36  * @(#)opendir.c	8.8 (Berkeley) 5/1/95
37  */
38 
39 #include <sys/param.h>
40 #include <sys/mount.h>
41 #include <sys/stat.h>
42 
43 #include <dirent.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 
49 /*
50  * Open a directory.
51  */
52 DIR *
53 opendir(name)
54 	const char *name;
55 {
56 
57 	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
58 }
59 
60 DIR *
61 __opendir2(name, flags)
62 	const char *name;
63 	int flags;
64 {
65 	DIR *dirp;
66 	int fd;
67 	int incr;
68 	int saved_errno;
69 	int unionstack;
70 	struct stat statb;
71 
72 	/*
73 	 * stat() before open() because opening of special files may be
74 	 * harmful.  fstat() after open because the file may have changed.
75 	 */
76 	if (stat(name, &statb) != 0)
77 		return (NULL);
78 	if (!S_ISDIR(statb.st_mode)) {
79 		errno = ENOTDIR;
80 		return (NULL);
81 	}
82 	if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
83 		return (NULL);
84 	dirp = NULL;
85 	if (fstat(fd, &statb) != 0)
86 		goto fail;
87 	if (!S_ISDIR(statb.st_mode)) {
88 		errno = ENOTDIR;
89 		goto fail;
90 	}
91 	if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
92 	    (dirp = malloc(sizeof(DIR))) == NULL)
93 		goto fail;
94 
95 	/*
96 	 * Use the system page size if that is a multiple of DIRBLKSIZ.
97 	 * Hopefully this can be a big win someday by allowing page
98 	 * trades to user space to be done by getdirentries().
99 	 */
100 	incr = getpagesize();
101 	if ((incr % DIRBLKSIZ) != 0)
102 		incr = DIRBLKSIZ;
103 
104 	/*
105 	 * Determine whether this directory is the top of a union stack.
106 	 */
107 	if (flags & DTF_NODUP) {
108 		struct statfs sfb;
109 
110 		if (fstatfs(fd, &sfb) < 0)
111 			goto fail;
112 		unionstack = !strcmp(sfb.f_fstypename, "union")
113 		    || (sfb.f_flags & MNT_UNION);
114 	} else {
115 		unionstack = 0;
116 	}
117 
118 	if (unionstack) {
119 		int len = 0;
120 		int space = 0;
121 		char *buf = 0;
122 		char *ddptr = 0;
123 		char *ddeptr;
124 		int n;
125 		struct dirent **dpv;
126 
127 		/*
128 		 * The strategy here is to read all the directory
129 		 * entries into a buffer, sort the buffer, and
130 		 * remove duplicate entries by setting the inode
131 		 * number to zero.
132 		 */
133 
134 		do {
135 			/*
136 			 * Always make at least DIRBLKSIZ bytes
137 			 * available to getdirentries
138 			 */
139 			if (space < DIRBLKSIZ) {
140 				space += incr;
141 				len += incr;
142 				buf = reallocf(buf, len);
143 				if (buf == NULL)
144 					goto fail;
145 				ddptr = buf + (len - space);
146 			}
147 
148 			n = getdirentries(fd, ddptr, space, &dirp->dd_seek);
149 			if (n > 0) {
150 				ddptr += n;
151 				space -= n;
152 			}
153 		} while (n > 0);
154 
155 		ddeptr = ddptr;
156 		flags |= __DTF_READALL;
157 
158 		/*
159 		 * Re-open the directory.
160 		 * This has the effect of rewinding back to the
161 		 * top of the union stack and is needed by
162 		 * programs which plan to fchdir to a descriptor
163 		 * which has also been read -- see fts.c.
164 		 */
165 		if (flags & DTF_REWIND) {
166 			(void)_close(fd);
167 			if ((fd = _open(name, O_RDONLY)) == -1) {
168 				saved_errno = errno;
169 				free(buf);
170 				free(dirp);
171 				errno = saved_errno;
172 				return (NULL);
173 			}
174 		}
175 
176 		/*
177 		 * There is now a buffer full of (possibly) duplicate
178 		 * names.
179 		 */
180 		dirp->dd_buf = buf;
181 
182 		/*
183 		 * Go round this loop twice...
184 		 *
185 		 * Scan through the buffer, counting entries.
186 		 * On the second pass, save pointers to each one.
187 		 * Then sort the pointers and remove duplicate names.
188 		 */
189 		for (dpv = 0;;) {
190 			n = 0;
191 			ddptr = buf;
192 			while (ddptr < ddeptr) {
193 				struct dirent *dp;
194 
195 				dp = (struct dirent *) ddptr;
196 				if ((long)dp & 03L)
197 					break;
198 				if ((dp->d_reclen <= 0) ||
199 				    (dp->d_reclen > (ddeptr + 1 - ddptr)))
200 					break;
201 				ddptr += dp->d_reclen;
202 				if (dp->d_fileno) {
203 					if (dpv)
204 						dpv[n] = dp;
205 					n++;
206 				}
207 			}
208 
209 			if (dpv) {
210 				struct dirent *xp;
211 
212 				/*
213 				 * This sort must be stable.
214 				 */
215 				mergesort(dpv, n, sizeof(*dpv), alphasort);
216 
217 				dpv[n] = NULL;
218 				xp = NULL;
219 
220 				/*
221 				 * Scan through the buffer in sort order,
222 				 * zapping the inode number of any
223 				 * duplicate names.
224 				 */
225 				for (n = 0; dpv[n]; n++) {
226 					struct dirent *dp = dpv[n];
227 
228 					if ((xp == NULL) ||
229 					    strcmp(dp->d_name, xp->d_name)) {
230 						xp = dp;
231 					} else {
232 						dp->d_fileno = 0;
233 					}
234 					if (dp->d_type == DT_WHT &&
235 					    (flags & DTF_HIDEW))
236 						dp->d_fileno = 0;
237 				}
238 
239 				free(dpv);
240 				break;
241 			} else {
242 				dpv = malloc((n+1) * sizeof(struct dirent *));
243 				if (dpv == NULL)
244 					break;
245 			}
246 		}
247 
248 		dirp->dd_len = len;
249 		dirp->dd_size = ddptr - dirp->dd_buf;
250 	} else {
251 		dirp->dd_len = incr;
252 		dirp->dd_buf = malloc(dirp->dd_len);
253 		if (dirp->dd_buf == NULL)
254 			goto fail;
255 		dirp->dd_seek = 0;
256 		flags &= ~DTF_REWIND;
257 	}
258 
259 	dirp->dd_loc = 0;
260 	dirp->dd_fd = fd;
261 	dirp->dd_flags = flags;
262 
263 	/*
264 	 * Set up seek point for rewinddir.
265 	 */
266 	dirp->dd_rewind = telldir(dirp);
267 
268 	return (dirp);
269 
270 fail:
271 	saved_errno = errno;
272 	free(dirp);
273 	(void)_close(fd);
274 	errno = saved_errno;
275 	return (NULL);
276 }
277