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