opendir.c revision 1.10 1 /* $NetBSD: opendir.c,v 1.10 1995/06/18 10:58:32 cgd 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 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94";
39 #else
40 static char rcsid[] = "$NetBSD: opendir.c,v 1.10 1995/06/18 10:58:32 cgd Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47
48 #include <dirent.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53
54 /*
55 * Open a directory.
56 */
57 DIR *
58 opendir(name)
59 const char *name;
60 {
61
62 return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
63 }
64
65 DIR *
66 __opendir2(name, flags)
67 const char *name;
68 int flags;
69 {
70 DIR *dirp;
71 int fd;
72 struct stat sb;
73 int pagesz;
74 int incr;
75 int unionstack;
76
77 if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1)
78 return (NULL);
79 if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
80 errno = ENOTDIR;
81 close(fd);
82 return (NULL);
83 }
84 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
85 (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
86 close(fd);
87 return (NULL);
88 }
89
90 /*
91 * If the machine's page size is an exact multiple of DIRBLKSIZ,
92 * use a buffer that is cluster boundary aligned.
93 * Hopefully this can be a big win someday by allowing page trades
94 * to user space to be done by getdirentries()
95 */
96 if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
97 incr = pagesz;
98 else
99 incr = DIRBLKSIZ;
100
101 /*
102 * Determine whether this directory is the top of a union stack.
103 */
104 if (flags & DTF_NODUP) {
105 struct statfs sfb;
106
107 if (fstatfs(fd, &sfb) < 0) {
108 free(dirp);
109 close(fd);
110 return (NULL);
111 }
112 unionstack = !strncmp(sfb.f_fstypename, MOUNT_UNION,
113 MFSNAMELEN);
114 } else {
115 unionstack = 0;
116 }
117
118 if (unionstack) {
119 int len = 0;
120 int space = 0;
121 char *buf = 0;
122 char *ddptr = 0;
123 char *ddeptr;
124 int n;
125 struct dirent **dpv;
126
127 /*
128 * The strategy here is to read all the directory
129 * entries into a buffer, sort the buffer, and
130 * remove duplicate entries by setting the inode
131 * number to zero.
132 */
133
134 do {
135 /*
136 * Always make at least DIRBLKSIZ bytes
137 * available to getdirentries
138 */
139 if (space < DIRBLKSIZ) {
140 space += incr;
141 len += incr;
142 buf = realloc(buf, len);
143 if (buf == NULL) {
144 free(dirp);
145 close(fd);
146 return (NULL);
147 }
148 ddptr = buf + (len - space);
149 }
150
151 n = getdirentries(fd, ddptr, space, &dirp->dd_seek);
152 if (n > 0) {
153 ddptr += n;
154 space -= n;
155 }
156 } while (n > 0);
157
158 ddeptr = ddptr;
159 flags |= __DTF_READALL;
160
161 /*
162 * Re-open the directory.
163 * This has the effect of rewinding back to the
164 * top of the union stack and is needed by
165 * programs which plan to fchdir to a descriptor
166 * which has also been read -- see fts.c.
167 */
168 if (flags & DTF_REWIND) {
169 (void) close(fd);
170 if ((fd = open(name, O_RDONLY)) == -1) {
171 free(buf);
172 free(dirp);
173 return (NULL);
174 }
175 }
176
177 /*
178 * There is now a buffer full of (possibly) duplicate
179 * names.
180 */
181 dirp->dd_buf = buf;
182
183 /*
184 * Go round this loop twice...
185 *
186 * Scan through the buffer, counting entries.
187 * On the second pass, save pointers to each one.
188 * Then sort the pointers and remove duplicate names.
189 */
190 for (dpv = 0;;) {
191 for (n = 0, ddptr = buf; ddptr < ddeptr;) {
192 struct dirent *dp;
193
194 dp = (struct dirent *) ddptr;
195 if ((long)dp & 03)
196 break;
197 if ((dp->d_reclen <= 0) ||
198 (dp->d_reclen > (ddeptr + 1 - ddptr)))
199 break;
200 ddptr += dp->d_reclen;
201 if (dp->d_fileno) {
202 if (dpv)
203 dpv[n] = dp;
204 n++;
205 }
206 }
207
208 if (dpv) {
209 struct dirent *xp;
210
211 /*
212 * This sort must be stable.
213 */
214 mergesort(dpv, n, sizeof(*dpv), alphasort);
215
216 dpv[n] = NULL;
217 xp = NULL;
218
219 /*
220 * Scan through the buffer in sort order,
221 * zapping the inode number of any
222 * duplicate names.
223 */
224 for (n = 0; dpv[n]; n++) {
225 struct dirent *dp = dpv[n];
226
227 if ((xp == NULL) ||
228 strcmp(dp->d_name, xp->d_name))
229 xp = dp;
230 else
231 dp->d_fileno = 0;
232 if (dp->d_type == DT_WHT &&
233 (flags & DTF_HIDEW))
234 dp->d_fileno = 0;
235 }
236
237 free(dpv);
238 break;
239 } else {
240 dpv = malloc((n+1) * sizeof(struct dirent *));
241 if (dpv == NULL)
242 break;
243 }
244 }
245
246 dirp->dd_len = len;
247 dirp->dd_size = ddptr - dirp->dd_buf;
248 } else {
249 dirp->dd_len = incr;
250 dirp->dd_buf = malloc(dirp->dd_len);
251 if (dirp->dd_buf == NULL) {
252 free(dirp);
253 close (fd);
254 return (NULL);
255 }
256 dirp->dd_seek = 0;
257 flags &= ~DTF_REWIND;
258 }
259
260 dirp->dd_loc = 0;
261 dirp->dd_fd = fd;
262 dirp->dd_flags = flags;
263
264 /*
265 * Set up seek point for rewinddir.
266 */
267 dirp->dd_rewind = telldir(dirp);
268
269 return (dirp);
270 }
271