opendir.c revision 1.36 1 1.36 yamt /* $NetBSD: opendir.c,v 1.36 2010/09/16 02:38:50 yamt 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.36 yamt __RCSID("$NetBSD: opendir.c,v 1.36 2010/09/16 02:38:50 yamt 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.34 ad if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
108 1.28 christos goto error;
109 1.28 christos if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
110 1.28 christos errno = ENOTDIR;
111 1.28 christos goto error;
112 1.28 christos }
113 1.28 christos if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL)
114 1.28 christos goto error;
115 1.28 christos dirp->dd_buf = NULL;
116 1.28 christos
117 1.28 christos /*
118 1.28 christos * If the machine's page size is an exact multiple of DIRBLKSIZ,
119 1.28 christos * use a buffer that is cluster boundary aligned.
120 1.28 christos * Hopefully this can be a big win someday by allowing page trades
121 1.36 yamt * to user space to be done by getdents()
122 1.28 christos */
123 1.28 christos if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
124 1.28 christos incr = pagesz;
125 1.28 christos else
126 1.28 christos incr = DIRBLKSIZ;
127 1.28 christos
128 1.28 christos /*
129 1.28 christos * Determine whether this directory is the top of a union stack.
130 1.28 christos */
131 1.28 christos
132 1.28 christos if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
133 1.28 christos goto error;
134 1.28 christos
135 1.28 christos if (flags & DTF_NODUP)
136 1.28 christos unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION,
137 1.32 christos sizeof(sfb.f_fstypename))) || (sfb.f_flag & MNT_UNION);
138 1.28 christos else
139 1.28 christos unionstack = 0;
140 1.28 christos
141 1.36 yamt nfsdir = !(strncmp(sfb.f_fstypename, MOUNT_NFS,
142 1.36 yamt sizeof(sfb.f_fstypename)));
143 1.28 christos
144 1.28 christos if (unionstack || nfsdir) {
145 1.28 christos size_t len;
146 1.28 christos size_t space;
147 1.28 christos char *buf, *nbuf;
148 1.28 christos char *ddptr;
149 1.28 christos char *ddeptr;
150 1.28 christos int n;
151 1.28 christos struct dirent **dpv;
152 1.33 elad int i;
153 1.28 christos
154 1.28 christos /*
155 1.28 christos * The strategy here for directories on top of a union stack
156 1.28 christos * is to read all the directory entries into a buffer, sort
157 1.28 christos * the buffer, and remove duplicate entries by setting the
158 1.28 christos * inode number to zero.
159 1.28 christos *
160 1.28 christos * For directories on an NFS mounted filesystem, we try
161 1.28 christos * to get a consistent snapshot by trying until we have
162 1.28 christos * successfully read all of the directory without errors
163 1.28 christos * (i.e. 'bad cookie' errors from the server because
164 1.28 christos * the directory was modified). These errors should not
165 1.28 christos * happen often, but need to be dealt with.
166 1.28 christos */
167 1.33 elad i = 0;
168 1.28 christos retry:
169 1.28 christos len = 0;
170 1.28 christos space = 0;
171 1.28 christos buf = 0;
172 1.28 christos ddptr = 0;
173 1.28 christos
174 1.28 christos do {
175 1.28 christos /*
176 1.28 christos * Always make at least DIRBLKSIZ bytes
177 1.36 yamt * available to getdents
178 1.28 christos */
179 1.28 christos if (space < DIRBLKSIZ) {
180 1.28 christos space += incr;
181 1.28 christos len += incr;
182 1.28 christos nbuf = realloc(buf, len);
183 1.28 christos if (nbuf == NULL) {
184 1.28 christos dirp->dd_buf = buf;
185 1.28 christos goto error;
186 1.28 christos }
187 1.28 christos buf = nbuf;
188 1.28 christos ddptr = buf + (len - space);
189 1.28 christos }
190 1.28 christos
191 1.28 christos dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR);
192 1.28 christos n = getdents(fd, ddptr, space);
193 1.28 christos /*
194 1.28 christos * For NFS: EINVAL means a bad cookie error
195 1.28 christos * from the server. Keep trying to get a
196 1.28 christos * consistent view, in this case this means
197 1.28 christos * starting all over again.
198 1.28 christos */
199 1.28 christos if (n == -1 && errno == EINVAL && nfsdir) {
200 1.28 christos free(buf);
201 1.28 christos lseek(fd, (off_t)0, SEEK_SET);
202 1.33 elad if (++i > MAXITERATIONS)
203 1.33 elad goto error;
204 1.28 christos goto retry;
205 1.28 christos }
206 1.28 christos if (n > 0) {
207 1.28 christos ddptr += n;
208 1.28 christos space -= n;
209 1.28 christos }
210 1.28 christos } while (n > 0);
211 1.28 christos
212 1.28 christos ddeptr = ddptr;
213 1.28 christos flags |= __DTF_READALL;
214 1.28 christos
215 1.28 christos /*
216 1.28 christos * Re-open the directory.
217 1.28 christos * This has the effect of rewinding back to the
218 1.28 christos * top of the union stack and is needed by
219 1.28 christos * programs which plan to fchdir to a descriptor
220 1.28 christos * which has also been read -- see fts.c.
221 1.28 christos */
222 1.28 christos if (flags & DTF_REWIND) {
223 1.28 christos (void) close(fd);
224 1.28 christos if ((fd = open(name, O_RDONLY)) == -1 ||
225 1.28 christos fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
226 1.28 christos dirp->dd_buf = buf;
227 1.28 christos goto error;
228 1.28 christos }
229 1.28 christos }
230 1.28 christos
231 1.28 christos /*
232 1.28 christos * There is now a buffer full of (possibly) duplicate
233 1.28 christos * names.
234 1.28 christos */
235 1.28 christos dirp->dd_buf = buf;
236 1.28 christos
237 1.28 christos /*
238 1.28 christos * Go round this loop twice...
239 1.28 christos *
240 1.28 christos * Scan through the buffer, counting entries.
241 1.28 christos * On the second pass, save pointers to each one.
242 1.28 christos * Then sort the pointers and remove duplicate names.
243 1.28 christos */
244 1.28 christos if (!nfsdir) {
245 1.28 christos for (dpv = 0;;) {
246 1.28 christos for (n = 0, ddptr = buf; ddptr < ddeptr;) {
247 1.28 christos struct dirent *dp;
248 1.28 christos
249 1.28 christos dp = (struct dirent *)(void *)ddptr;
250 1.28 christos if ((long)dp & _DIRENT_ALIGN(dp))
251 1.28 christos break;
252 1.28 christos /*
253 1.28 christos * d_reclen is unsigned,
254 1.28 christos * so no need to compare <= 0
255 1.28 christos */
256 1.28 christos if (dp->d_reclen > (ddeptr + 1 - ddptr))
257 1.28 christos break;
258 1.28 christos ddptr += dp->d_reclen;
259 1.28 christos if (dp->d_fileno) {
260 1.28 christos if (dpv)
261 1.28 christos dpv[n] = dp;
262 1.28 christos n++;
263 1.28 christos }
264 1.28 christos }
265 1.28 christos
266 1.28 christos if (dpv) {
267 1.28 christos struct dirent *xp;
268 1.28 christos
269 1.28 christos /*
270 1.28 christos * This sort must be stable.
271 1.28 christos */
272 1.28 christos mergesort(dpv, (size_t)n, sizeof(*dpv),
273 1.28 christos alphasort);
274 1.28 christos
275 1.28 christos dpv[n] = NULL;
276 1.28 christos xp = NULL;
277 1.28 christos
278 1.28 christos /*
279 1.28 christos * Scan through the buffer in sort
280 1.28 christos * order, zapping the inode number
281 1.28 christos * of any duplicate names.
282 1.28 christos */
283 1.28 christos for (n = 0; dpv[n]; n++) {
284 1.28 christos struct dirent *dp = dpv[n];
285 1.28 christos
286 1.28 christos if ((xp == NULL) ||
287 1.28 christos strcmp(dp->d_name,
288 1.28 christos xp->d_name))
289 1.28 christos xp = dp;
290 1.28 christos else
291 1.28 christos dp->d_fileno = 0;
292 1.28 christos if (dp->d_type == DT_WHT &&
293 1.28 christos (flags & DTF_HIDEW))
294 1.28 christos dp->d_fileno = 0;
295 1.28 christos }
296 1.28 christos
297 1.28 christos free(dpv);
298 1.28 christos break;
299 1.28 christos } else {
300 1.28 christos dpv = malloc((n + 1) *
301 1.28 christos sizeof(struct dirent *));
302 1.28 christos if (dpv == NULL)
303 1.28 christos break;
304 1.28 christos }
305 1.28 christos }
306 1.28 christos }
307 1.28 christos
308 1.28 christos dirp->dd_len = len;
309 1.28 christos dirp->dd_size = ddptr - dirp->dd_buf;
310 1.28 christos } else {
311 1.28 christos dirp->dd_len = incr;
312 1.28 christos dirp->dd_buf = malloc((size_t)dirp->dd_len);
313 1.28 christos if (dirp->dd_buf == NULL)
314 1.28 christos goto error;
315 1.28 christos dirp->dd_seek = 0;
316 1.28 christos flags &= ~DTF_REWIND;
317 1.28 christos }
318 1.28 christos
319 1.28 christos dirp->dd_loc = 0;
320 1.28 christos dirp->dd_fd = fd;
321 1.28 christos dirp->dd_flags = flags;
322 1.5 mycroft
323 1.28 christos /*
324 1.28 christos * Set up seek point for rewinddir.
325 1.28 christos */
326 1.28 christos #ifdef _REENTRANT
327 1.28 christos if (__isthreaded) {
328 1.28 christos if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
329 1.28 christos goto error;
330 1.28 christos mutex_init((mutex_t *)dirp->dd_lock, NULL);
331 1.28 christos }
332 1.28 christos #endif
333 1.31 christos dirp->dd_internal = NULL;
334 1.31 christos (void)_telldir_unlocked(dirp);
335 1.28 christos return (dirp);
336 1.28 christos error:
337 1.28 christos serrno = errno;
338 1.28 christos if (dirp && dirp->dd_buf)
339 1.28 christos free(dirp->dd_buf);
340 1.28 christos if (dirp)
341 1.28 christos free(dirp);
342 1.28 christos if (fd != -1)
343 1.28 christos (void)close(fd);
344 1.28 christos errno = serrno;
345 1.28 christos return NULL;
346 1.28 christos }
347