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