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