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