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