Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libc / gen / opendir.c
blobe1ac971d1fa59bc81da18d5a786dbc8477cc9504
1 /* $NetBSD: opendir.c,v 1.33.12.1 2009/01/04 17:02:19 christos Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.33.12.1 2009/01/04 17:02:19 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include "reentrant.h"
43 #include "extern.h"
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #include <sys/stat.h>
49 #include <assert.h>
50 #include <dirent.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
57 #include "dirent_private.h"
59 #define MAXITERATIONS 100
61 static DIR *__opendir_common(int, const char *, int);
63 __weak_alias(fdopendir,_fdopendir)
66 * Open a directory.
68 DIR *
69 opendir(const char *name)
72 _DIAGASSERT(name != NULL);
74 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
77 DIR *
78 __opendir2(const char *name, int flags)
80 int fd;
82 if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1)
83 return NULL;
84 return __opendir_common(fd, name, flags);
87 #ifndef __LIBC12_SOURCE__
88 DIR *
89 _fdopendir(int fd)
92 return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP);
94 #endif
96 static DIR *
97 __opendir_common(int fd, const char *name, int flags)
99 DIR *dirp = NULL;
100 int serrno;
101 struct stat sb;
102 int pagesz;
103 int incr;
104 int unionstack, nfsdir;
105 struct statvfs sfb;
107 _DIAGASSERT(name != NULL);
109 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
110 goto error;
111 if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
112 errno = ENOTDIR;
113 goto error;
115 if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL)
116 goto error;
117 dirp->dd_buf = NULL;
120 * If the machine's page size is an exact multiple of DIRBLKSIZ,
121 * use a buffer that is cluster boundary aligned.
122 * Hopefully this can be a big win someday by allowing page trades
123 * to user space to be done by getdirentries()
125 if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
126 incr = pagesz;
127 else
128 incr = DIRBLKSIZ;
131 * Determine whether this directory is the top of a union stack.
134 if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
135 goto error;
137 if (flags & DTF_NODUP)
138 unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION,
139 sizeof(sfb.f_fstypename))) || (sfb.f_flag & MNT_UNION);
140 else
141 unionstack = 0;
143 nfsdir = !(strncmp(sfb.f_fstypename, MOUNT_NFS, sizeof(sfb.f_fstypename)));
145 if (unionstack || nfsdir) {
146 size_t len;
147 size_t space;
148 char *buf, *nbuf;
149 char *ddptr;
150 char *ddeptr;
151 int n;
152 struct dirent **dpv;
153 int i;
156 * The strategy here for directories on top of a union stack
157 * is to read all the directory entries into a buffer, sort
158 * the buffer, and remove duplicate entries by setting the
159 * inode number to zero.
161 * For directories on an NFS mounted filesystem, we try
162 * to get a consistent snapshot by trying until we have
163 * successfully read all of the directory without errors
164 * (i.e. 'bad cookie' errors from the server because
165 * the directory was modified). These errors should not
166 * happen often, but need to be dealt with.
168 i = 0;
169 retry:
170 len = 0;
171 space = 0;
172 buf = 0;
173 ddptr = 0;
175 do {
177 * Always make at least DIRBLKSIZ bytes
178 * available to getdirentries
180 if (space < DIRBLKSIZ) {
181 space += incr;
182 len += incr;
183 nbuf = realloc(buf, len);
184 if (nbuf == NULL) {
185 dirp->dd_buf = buf;
186 goto error;
188 buf = nbuf;
189 ddptr = buf + (len - space);
192 dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR);
193 n = getdents(fd, ddptr, space);
195 * For NFS: EINVAL means a bad cookie error
196 * from the server. Keep trying to get a
197 * consistent view, in this case this means
198 * starting all over again.
200 if (n == -1 && errno == EINVAL && nfsdir) {
201 free(buf);
202 lseek(fd, (off_t)0, SEEK_SET);
203 if (++i > MAXITERATIONS)
204 goto error;
205 goto retry;
207 if (n > 0) {
208 ddptr += n;
209 space -= n;
211 } while (n > 0);
213 ddeptr = ddptr;
214 flags |= __DTF_READALL;
217 * Re-open the directory.
218 * This has the effect of rewinding back to the
219 * top of the union stack and is needed by
220 * programs which plan to fchdir to a descriptor
221 * which has also been read -- see fts.c.
223 if (flags & DTF_REWIND) {
224 (void) close(fd);
225 if ((fd = open(name, O_RDONLY)) == -1 ||
226 fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
227 dirp->dd_buf = buf;
228 goto error;
233 * There is now a buffer full of (possibly) duplicate
234 * names.
236 dirp->dd_buf = buf;
239 * Go round this loop twice...
241 * Scan through the buffer, counting entries.
242 * On the second pass, save pointers to each one.
243 * Then sort the pointers and remove duplicate names.
245 if (!nfsdir) {
246 for (dpv = 0;;) {
247 for (n = 0, ddptr = buf; ddptr < ddeptr;) {
248 struct dirent *dp;
250 dp = (struct dirent *)(void *)ddptr;
251 if ((long)dp & _DIRENT_ALIGN(dp))
252 break;
254 * d_reclen is unsigned,
255 * so no need to compare <= 0
257 if (dp->d_reclen > (ddeptr + 1 - ddptr))
258 break;
259 ddptr += dp->d_reclen;
260 if (dp->d_fileno) {
261 if (dpv)
262 dpv[n] = dp;
263 n++;
267 if (dpv) {
268 struct dirent *xp;
271 * This sort must be stable.
273 mergesort(dpv, (size_t)n, sizeof(*dpv),
274 alphasort);
276 dpv[n] = NULL;
277 xp = NULL;
280 * Scan through the buffer in sort
281 * order, zapping the inode number
282 * of any duplicate names.
284 for (n = 0; dpv[n]; n++) {
285 struct dirent *dp = dpv[n];
287 if ((xp == NULL) ||
288 strcmp(dp->d_name,
289 xp->d_name))
290 xp = dp;
291 else
292 dp->d_fileno = 0;
293 if (dp->d_type == DT_WHT &&
294 (flags & DTF_HIDEW))
295 dp->d_fileno = 0;
298 free(dpv);
299 break;
300 } else {
301 dpv = malloc((n + 1) *
302 sizeof(struct dirent *));
303 if (dpv == NULL)
304 break;
309 dirp->dd_len = len;
310 dirp->dd_size = ddptr - dirp->dd_buf;
311 } else {
312 dirp->dd_len = incr;
313 dirp->dd_buf = malloc((size_t)dirp->dd_len);
314 if (dirp->dd_buf == NULL)
315 goto error;
316 dirp->dd_seek = 0;
317 flags &= ~DTF_REWIND;
320 dirp->dd_loc = 0;
321 dirp->dd_fd = fd;
322 dirp->dd_flags = flags;
325 * Set up seek point for rewinddir.
327 #ifdef _REENTRANT
328 if (__isthreaded) {
329 if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
330 goto error;
331 mutex_init((mutex_t *)dirp->dd_lock, NULL);
333 #endif
334 dirp->dd_internal = NULL;
335 (void)_telldir_unlocked(dirp);
336 return (dirp);
337 error:
338 serrno = errno;
339 if (dirp && dirp->dd_buf)
340 free(dirp->dd_buf);
341 if (dirp)
342 free(dirp);
343 if (fd != -1)
344 (void)close(fd);
345 errno = serrno;
346 return NULL;