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