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