xref: /netbsd-src/lib/libc/gen/opendir.c (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1 /*	$NetBSD: opendir.c,v 1.26 2005/01/19 00:53:33 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)opendir.c	8.7 (Berkeley) 12/10/94";
36 #else
37 __RCSID("$NetBSD: opendir.c,v 1.26 2005/01/19 00:53:33 mycroft Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include "namespace.h"
42 #include "reentrant.h"
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <sys/stat.h>
46 
47 #include <assert.h>
48 #include <dirent.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #ifdef __weak_alias
56 __weak_alias(opendir,_opendir)
57 #endif
58 
59 /*
60  * Open a directory.
61  */
62 DIR *
63 opendir(name)
64 	const char *name;
65 {
66 
67 	_DIAGASSERT(name != NULL);
68 
69 	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
70 }
71 
72 DIR *
73 __opendir2(name, flags)
74 	const char *name;
75 	int flags;
76 {
77 	DIR *dirp = NULL;
78 	int fd;
79 	int serrno;
80 	struct stat sb;
81 	int pagesz;
82 	int incr;
83 	int unionstack, nfsdir;
84 	struct statvfs sfb;
85 
86 	_DIAGASSERT(name != NULL);
87 
88 	if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1 ||
89 	    fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
90 		goto error;
91 	if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
92 		errno = ENOTDIR;
93 		goto error;
94 	}
95 	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL)
96 		goto error;
97 	dirp->dd_buf = NULL;
98 
99 	/*
100 	 * If the machine's page size is an exact multiple of DIRBLKSIZ,
101 	 * use a buffer that is cluster boundary aligned.
102 	 * Hopefully this can be a big win someday by allowing page trades
103 	 * to user space to be done by getdirentries()
104 	 */
105 	if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
106 		incr = pagesz;
107 	else
108 		incr = DIRBLKSIZ;
109 
110 	/*
111 	 * Determine whether this directory is the top of a union stack.
112 	 */
113 
114 	if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
115 		goto error;
116 
117 	if (flags & DTF_NODUP)
118 		unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION,
119 		    MFSNAMELEN)) || (sfb.f_flag & MNT_UNION);
120 	else
121 		unionstack = 0;
122 
123 	nfsdir = !(strncmp(sfb.f_fstypename, MOUNT_NFS, MFSNAMELEN));
124 
125 	if (unionstack || nfsdir) {
126 		size_t len;
127 		size_t space;
128 		char *buf, *nbuf;
129 		char *ddptr;
130 		char *ddeptr;
131 		int n;
132 		struct dirent **dpv;
133 
134 		/*
135 		 * The strategy here for directories on top of a union stack
136 		 * is to read all the directory entries into a buffer, sort
137 		 * the buffer, and remove duplicate entries by setting the
138 		 * inode number to zero.
139 		 *
140 		 * For directories on an NFS mounted filesystem, we try
141 	 	 * to get a consistent snapshot by trying until we have
142 		 * successfully read all of the directory without errors
143 		 * (i.e. 'bad cookie' errors from the server because
144 		 * the directory was modified). These errors should not
145 		 * happen often, but need to be dealt with.
146 		 */
147 retry:
148 		len = 0;
149 		space = 0;
150 		buf = 0;
151 		ddptr = 0;
152 
153 		do {
154 			/*
155 			 * Always make at least DIRBLKSIZ bytes
156 			 * available to getdirentries
157 			 */
158 			if (space < DIRBLKSIZ) {
159 				space += incr;
160 				len += incr;
161 				nbuf = realloc(buf, len);
162 				if (nbuf == NULL) {
163 					dirp->dd_buf = buf;
164 					goto error;
165 				}
166 				buf = nbuf;
167 				ddptr = buf + (len - space);
168 			}
169 
170 			dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR);
171 			n = getdents(fd, ddptr, space);
172 			/*
173 			 * For NFS: EINVAL means a bad cookie error
174 			 * from the server. Keep trying to get a
175 			 * consistent view, in this case this means
176 			 * starting all over again.
177 			 */
178 			if (n == -1 && errno == EINVAL && nfsdir) {
179 				free(buf);
180 				lseek(fd, (off_t)0, SEEK_SET);
181 				goto retry;
182 			}
183 			if (n > 0) {
184 				ddptr += n;
185 				space -= n;
186 			}
187 		} while (n > 0);
188 
189 		ddeptr = ddptr;
190 		flags |= __DTF_READALL;
191 
192 		/*
193 		 * Re-open the directory.
194 		 * This has the effect of rewinding back to the
195 		 * top of the union stack and is needed by
196 		 * programs which plan to fchdir to a descriptor
197 		 * which has also been read -- see fts.c.
198 		 */
199 		if (flags & DTF_REWIND) {
200 			(void) close(fd);
201 			if ((fd = open(name, O_RDONLY)) == -1 ||
202 			    fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
203 				dirp->dd_buf = buf;
204 				goto error;
205 			}
206 		}
207 
208 		/*
209 		 * There is now a buffer full of (possibly) duplicate
210 		 * names.
211 		 */
212 		dirp->dd_buf = buf;
213 
214 		/*
215 		 * Go round this loop twice...
216 		 *
217 		 * Scan through the buffer, counting entries.
218 		 * On the second pass, save pointers to each one.
219 		 * Then sort the pointers and remove duplicate names.
220 		 */
221 		if (!nfsdir) {
222 			for (dpv = 0;;) {
223 				for (n = 0, ddptr = buf; ddptr < ddeptr;) {
224 					struct dirent *dp;
225 
226 					dp = (struct dirent *)(void *)ddptr;
227 					if ((long)dp & 03)
228 						break;
229 					/*
230 					 * d_reclen is unsigned,
231 					 * so no need to compare <= 0
232 					 */
233 					if (dp->d_reclen > (ddeptr + 1 - ddptr))
234 						break;
235 					ddptr += dp->d_reclen;
236 					if (dp->d_fileno) {
237 						if (dpv)
238 							dpv[n] = dp;
239 						n++;
240 					}
241 				}
242 
243 				if (dpv) {
244 					struct dirent *xp;
245 
246 					/*
247 					 * This sort must be stable.
248 					 */
249 					mergesort(dpv, (size_t)n, sizeof(*dpv),
250 					    alphasort);
251 
252 					dpv[n] = NULL;
253 					xp = NULL;
254 
255 					/*
256 					 * Scan through the buffer in sort
257 					 * order, zapping the inode number
258 					 * of any duplicate names.
259 					 */
260 					for (n = 0; dpv[n]; n++) {
261 						struct dirent *dp = dpv[n];
262 
263 						if ((xp == NULL) ||
264 						    strcmp(dp->d_name,
265 						      xp->d_name))
266 							xp = dp;
267 						else
268 							dp->d_fileno = 0;
269 						if (dp->d_type == DT_WHT &&
270 						    (flags & DTF_HIDEW))
271 							dp->d_fileno = 0;
272 					}
273 
274 					free(dpv);
275 					break;
276 				} else {
277 					dpv = malloc((n + 1) *
278 					    sizeof(struct dirent *));
279 					if (dpv == NULL)
280 						break;
281 				}
282 			}
283 		}
284 
285 		dirp->dd_len = len;
286 		dirp->dd_size = ddptr - dirp->dd_buf;
287 	} else {
288 		dirp->dd_len = incr;
289 		dirp->dd_buf = malloc((size_t)dirp->dd_len);
290 		if (dirp->dd_buf == NULL)
291 			goto error;
292 		dirp->dd_seek = 0;
293 		flags &= ~DTF_REWIND;
294 	}
295 
296 	dirp->dd_loc = 0;
297 	dirp->dd_fd = fd;
298 	dirp->dd_flags = flags;
299 
300 	/*
301 	 * Set up seek point for rewinddir.
302 	 */
303 #ifdef _REENTRANT
304 	if (__isthreaded) {
305 		if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
306 			goto error;
307 		mutex_init((mutex_t *)dirp->dd_lock, NULL);
308 	}
309 #endif
310 	dirp->dd_rewind = telldir(dirp);
311 	return (dirp);
312 error:
313 	serrno = errno;
314 	if (dirp && dirp->dd_buf)
315 		free(dirp->dd_buf);
316 	if (dirp)
317 		free(dirp);
318 	if (fd != -1)
319 		(void)close(fd);
320 	errno = serrno;
321 	return NULL;
322 }
323