opendir.c revision 1.34 1 1.34 ad /* $NetBSD: opendir.c,v 1.34 2008/12/05 13:08:53 ad Exp $ */
2 1.28 christos
3 1.28 christos /*
4 1.28 christos * Copyright (c) 1983, 1993
5 1.28 christos * The Regents of the University of California. All rights reserved.
6 1.28 christos *
7 1.28 christos * Redistribution and use in source and binary forms, with or without
8 1.28 christos * modification, are permitted provided that the following conditions
9 1.28 christos * are met:
10 1.28 christos * 1. Redistributions of source code must retain the above copyright
11 1.28 christos * notice, this list of conditions and the following disclaimer.
12 1.28 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.28 christos * notice, this list of conditions and the following disclaimer in the
14 1.28 christos * documentation and/or other materials provided with the distribution.
15 1.28 christos * 3. Neither the name of the University nor the names of its contributors
16 1.28 christos * may be used to endorse or promote products derived from this software
17 1.28 christos * without specific prior written permission.
18 1.28 christos *
19 1.28 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.28 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.28 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.28 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.28 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.28 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.28 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.28 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.28 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.28 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.28 christos * SUCH DAMAGE.
30 1.28 christos */
31 1.28 christos
32 1.28 christos #include <sys/cdefs.h>
33 1.28 christos #if defined(LIBC_SCCS) && !defined(lint)
34 1.28 christos #if 0
35 1.28 christos static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94";
36 1.28 christos #else
37 1.34 ad __RCSID("$NetBSD: opendir.c,v 1.34 2008/12/05 13:08:53 ad Exp $");
38 1.28 christos #endif
39 1.28 christos #endif /* LIBC_SCCS and not lint */
40 1.1 cgd
41 1.14 jtc #include "namespace.h"
42 1.28 christos #include "reentrant.h"
43 1.30 christos #include "extern.h"
44 1.34 ad
45 1.28 christos #include <sys/param.h>
46 1.28 christos #include <sys/mount.h>
47 1.6 mycroft #include <sys/stat.h>
48 1.5 mycroft
49 1.28 christos #include <assert.h>
50 1.28 christos #include <dirent.h>
51 1.28 christos #include <errno.h>
52 1.28 christos #include <fcntl.h>
53 1.28 christos #include <stdlib.h>
54 1.28 christos #include <string.h>
55 1.28 christos #include <unistd.h>
56 1.28 christos
57 1.31 christos #include "dirent_private.h"
58 1.31 christos
59 1.33 elad #define MAXITERATIONS 100
60 1.33 elad
61 1.34 ad static DIR *__opendir_common(int, const char *, int);
62 1.34 ad
63 1.34 ad __weak_alias(fdopendir,_fdopendir)
64 1.34 ad
65 1.28 christos /*
66 1.28 christos * Open a directory.
67 1.28 christos */
68 1.28 christos DIR *
69 1.31 christos opendir(const char *name)
70 1.28 christos {
71 1.28 christos
72 1.28 christos _DIAGASSERT(name != NULL);
73 1.28 christos
74 1.28 christos return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
75 1.28 christos }
76 1.28 christos
77 1.28 christos DIR *
78 1.31 christos __opendir2(const char *name, int flags)
79 1.28 christos {
80 1.34 ad int fd;
81 1.34 ad
82 1.34 ad if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1)
83 1.34 ad return NULL;
84 1.34 ad return __opendir_common(fd, name, flags);
85 1.34 ad }
86 1.34 ad
87 1.34 ad #ifndef __LIBC12_SOURCE__
88 1.34 ad DIR *
89 1.34 ad _fdopendir(int fd)
90 1.34 ad {
91 1.34 ad
92 1.34 ad return __opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP);
93 1.34 ad }
94 1.34 ad #endif
95 1.34 ad
96 1.34 ad static DIR *
97 1.34 ad __opendir_common(int fd, const char *name, int flags)
98 1.34 ad {
99 1.28 christos DIR *dirp = NULL;
100 1.28 christos int serrno;
101 1.28 christos struct stat sb;
102 1.28 christos int pagesz;
103 1.28 christos int incr;
104 1.28 christos int unionstack, nfsdir;
105 1.28 christos struct statvfs sfb;
106 1.28 christos
107 1.28 christos _DIAGASSERT(name != NULL);
108 1.28 christos
109 1.34 ad if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
110 1.28 christos goto error;
111 1.28 christos if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
112 1.28 christos errno = ENOTDIR;
113 1.28 christos goto error;
114 1.28 christos }
115 1.28 christos if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL)
116 1.28 christos goto error;
117 1.28 christos dirp->dd_buf = NULL;
118 1.28 christos
119 1.28 christos /*
120 1.28 christos * If the machine's page size is an exact multiple of DIRBLKSIZ,
121 1.28 christos * use a buffer that is cluster boundary aligned.
122 1.28 christos * Hopefully this can be a big win someday by allowing page trades
123 1.28 christos * to user space to be done by getdirentries()
124 1.28 christos */
125 1.28 christos if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
126 1.28 christos incr = pagesz;
127 1.28 christos else
128 1.28 christos incr = DIRBLKSIZ;
129 1.28 christos
130 1.28 christos /*
131 1.28 christos * Determine whether this directory is the top of a union stack.
132 1.28 christos */
133 1.28 christos
134 1.28 christos if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
135 1.28 christos goto error;
136 1.28 christos
137 1.28 christos if (flags & DTF_NODUP)
138 1.28 christos unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION,
139 1.32 christos sizeof(sfb.f_fstypename))) || (sfb.f_flag & MNT_UNION);
140 1.28 christos else
141 1.28 christos unionstack = 0;
142 1.28 christos
143 1.32 christos nfsdir = !(strncmp(sfb.f_fstypename, MOUNT_NFS, sizeof(sfb.f_fstypename)));
144 1.28 christos
145 1.28 christos if (unionstack || nfsdir) {
146 1.28 christos size_t len;
147 1.28 christos size_t space;
148 1.28 christos char *buf, *nbuf;
149 1.28 christos char *ddptr;
150 1.28 christos char *ddeptr;
151 1.28 christos int n;
152 1.28 christos struct dirent **dpv;
153 1.33 elad int i;
154 1.28 christos
155 1.28 christos /*
156 1.28 christos * The strategy here for directories on top of a union stack
157 1.28 christos * is to read all the directory entries into a buffer, sort
158 1.28 christos * the buffer, and remove duplicate entries by setting the
159 1.28 christos * inode number to zero.
160 1.28 christos *
161 1.28 christos * For directories on an NFS mounted filesystem, we try
162 1.28 christos * to get a consistent snapshot by trying until we have
163 1.28 christos * successfully read all of the directory without errors
164 1.28 christos * (i.e. 'bad cookie' errors from the server because
165 1.28 christos * the directory was modified). These errors should not
166 1.28 christos * happen often, but need to be dealt with.
167 1.28 christos */
168 1.33 elad i = 0;
169 1.28 christos retry:
170 1.28 christos len = 0;
171 1.28 christos space = 0;
172 1.28 christos buf = 0;
173 1.28 christos ddptr = 0;
174 1.28 christos
175 1.28 christos do {
176 1.28 christos /*
177 1.28 christos * Always make at least DIRBLKSIZ bytes
178 1.28 christos * available to getdirentries
179 1.28 christos */
180 1.28 christos if (space < DIRBLKSIZ) {
181 1.28 christos space += incr;
182 1.28 christos len += incr;
183 1.28 christos nbuf = realloc(buf, len);
184 1.28 christos if (nbuf == NULL) {
185 1.28 christos dirp->dd_buf = buf;
186 1.28 christos goto error;
187 1.28 christos }
188 1.28 christos buf = nbuf;
189 1.28 christos ddptr = buf + (len - space);
190 1.28 christos }
191 1.28 christos
192 1.28 christos dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR);
193 1.28 christos n = getdents(fd, ddptr, space);
194 1.28 christos /*
195 1.28 christos * For NFS: EINVAL means a bad cookie error
196 1.28 christos * from the server. Keep trying to get a
197 1.28 christos * consistent view, in this case this means
198 1.28 christos * starting all over again.
199 1.28 christos */
200 1.28 christos if (n == -1 && errno == EINVAL && nfsdir) {
201 1.28 christos free(buf);
202 1.28 christos lseek(fd, (off_t)0, SEEK_SET);
203 1.33 elad if (++i > MAXITERATIONS)
204 1.33 elad goto error;
205 1.28 christos goto retry;
206 1.28 christos }
207 1.28 christos if (n > 0) {
208 1.28 christos ddptr += n;
209 1.28 christos space -= n;
210 1.28 christos }
211 1.28 christos } while (n > 0);
212 1.28 christos
213 1.28 christos ddeptr = ddptr;
214 1.28 christos flags |= __DTF_READALL;
215 1.28 christos
216 1.28 christos /*
217 1.28 christos * Re-open the directory.
218 1.28 christos * This has the effect of rewinding back to the
219 1.28 christos * top of the union stack and is needed by
220 1.28 christos * programs which plan to fchdir to a descriptor
221 1.28 christos * which has also been read -- see fts.c.
222 1.28 christos */
223 1.28 christos if (flags & DTF_REWIND) {
224 1.28 christos (void) close(fd);
225 1.28 christos if ((fd = open(name, O_RDONLY)) == -1 ||
226 1.28 christos fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
227 1.28 christos dirp->dd_buf = buf;
228 1.28 christos goto error;
229 1.28 christos }
230 1.28 christos }
231 1.28 christos
232 1.28 christos /*
233 1.28 christos * There is now a buffer full of (possibly) duplicate
234 1.28 christos * names.
235 1.28 christos */
236 1.28 christos dirp->dd_buf = buf;
237 1.28 christos
238 1.28 christos /*
239 1.28 christos * Go round this loop twice...
240 1.28 christos *
241 1.28 christos * Scan through the buffer, counting entries.
242 1.28 christos * On the second pass, save pointers to each one.
243 1.28 christos * Then sort the pointers and remove duplicate names.
244 1.28 christos */
245 1.28 christos if (!nfsdir) {
246 1.28 christos for (dpv = 0;;) {
247 1.28 christos for (n = 0, ddptr = buf; ddptr < ddeptr;) {
248 1.28 christos struct dirent *dp;
249 1.28 christos
250 1.28 christos dp = (struct dirent *)(void *)ddptr;
251 1.28 christos if ((long)dp & _DIRENT_ALIGN(dp))
252 1.28 christos break;
253 1.28 christos /*
254 1.28 christos * d_reclen is unsigned,
255 1.28 christos * so no need to compare <= 0
256 1.28 christos */
257 1.28 christos if (dp->d_reclen > (ddeptr + 1 - ddptr))
258 1.28 christos break;
259 1.28 christos ddptr += dp->d_reclen;
260 1.28 christos if (dp->d_fileno) {
261 1.28 christos if (dpv)
262 1.28 christos dpv[n] = dp;
263 1.28 christos n++;
264 1.28 christos }
265 1.28 christos }
266 1.28 christos
267 1.28 christos if (dpv) {
268 1.28 christos struct dirent *xp;
269 1.28 christos
270 1.28 christos /*
271 1.28 christos * This sort must be stable.
272 1.28 christos */
273 1.28 christos mergesort(dpv, (size_t)n, sizeof(*dpv),
274 1.28 christos alphasort);
275 1.28 christos
276 1.28 christos dpv[n] = NULL;
277 1.28 christos xp = NULL;
278 1.28 christos
279 1.28 christos /*
280 1.28 christos * Scan through the buffer in sort
281 1.28 christos * order, zapping the inode number
282 1.28 christos * of any duplicate names.
283 1.28 christos */
284 1.28 christos for (n = 0; dpv[n]; n++) {
285 1.28 christos struct dirent *dp = dpv[n];
286 1.28 christos
287 1.28 christos if ((xp == NULL) ||
288 1.28 christos strcmp(dp->d_name,
289 1.28 christos xp->d_name))
290 1.28 christos xp = dp;
291 1.28 christos else
292 1.28 christos dp->d_fileno = 0;
293 1.28 christos if (dp->d_type == DT_WHT &&
294 1.28 christos (flags & DTF_HIDEW))
295 1.28 christos dp->d_fileno = 0;
296 1.28 christos }
297 1.28 christos
298 1.28 christos free(dpv);
299 1.28 christos break;
300 1.28 christos } else {
301 1.28 christos dpv = malloc((n + 1) *
302 1.28 christos sizeof(struct dirent *));
303 1.28 christos if (dpv == NULL)
304 1.28 christos break;
305 1.28 christos }
306 1.28 christos }
307 1.28 christos }
308 1.28 christos
309 1.28 christos dirp->dd_len = len;
310 1.28 christos dirp->dd_size = ddptr - dirp->dd_buf;
311 1.28 christos } else {
312 1.28 christos dirp->dd_len = incr;
313 1.28 christos dirp->dd_buf = malloc((size_t)dirp->dd_len);
314 1.28 christos if (dirp->dd_buf == NULL)
315 1.28 christos goto error;
316 1.28 christos dirp->dd_seek = 0;
317 1.28 christos flags &= ~DTF_REWIND;
318 1.28 christos }
319 1.28 christos
320 1.28 christos dirp->dd_loc = 0;
321 1.28 christos dirp->dd_fd = fd;
322 1.28 christos dirp->dd_flags = flags;
323 1.5 mycroft
324 1.28 christos /*
325 1.28 christos * Set up seek point for rewinddir.
326 1.28 christos */
327 1.28 christos #ifdef _REENTRANT
328 1.28 christos if (__isthreaded) {
329 1.28 christos if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
330 1.28 christos goto error;
331 1.28 christos mutex_init((mutex_t *)dirp->dd_lock, NULL);
332 1.28 christos }
333 1.28 christos #endif
334 1.31 christos dirp->dd_internal = NULL;
335 1.31 christos (void)_telldir_unlocked(dirp);
336 1.28 christos return (dirp);
337 1.28 christos error:
338 1.28 christos serrno = errno;
339 1.28 christos if (dirp && dirp->dd_buf)
340 1.28 christos free(dirp->dd_buf);
341 1.28 christos if (dirp)
342 1.28 christos free(dirp);
343 1.28 christos if (fd != -1)
344 1.28 christos (void)close(fd);
345 1.28 christos errno = serrno;
346 1.28 christos return NULL;
347 1.28 christos }
348