]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/gen/opendir.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libc / gen / opendir.c
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)opendir.c   8.8 (Berkeley) 5/1/95";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40
41 #include <dirent.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include "un-namespace.h"
48
49 #include "telldir.h"
50
51 static DIR * __opendir_common(int, const char *, int);
52
53 /*
54  * Open a directory.
55  */
56 DIR *
57 opendir(const char *name)
58 {
59
60         return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
61 }
62
63 /*
64  * Open a directory with existing file descriptor.
65  */
66 DIR *
67 fdopendir(int fd)
68 {
69         struct stat statb;
70
71         /* Check that fd is associated with a directory. */
72         if (_fstat(fd, &statb) != 0)
73                 return (NULL);
74         if (!S_ISDIR(statb.st_mode)) {
75                 errno = ENOTDIR;
76                 return (NULL);
77         }
78         if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
79                 return (NULL);
80         return (__opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP));
81 }
82
83 DIR *
84 __opendir2(const char *name, int flags)
85 {
86         int fd;
87
88         if ((fd = _open(name,
89             O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC)) == -1)
90                 return (NULL);
91
92         return __opendir_common(fd, name, flags);
93 }
94
95 static int
96 opendir_compar(const void *p1, const void *p2)
97 {
98
99         return (strcmp((*(const struct dirent **)p1)->d_name,
100             (*(const struct dirent **)p2)->d_name));
101 }
102
103 /*
104  * Common routine for opendir(3), __opendir2(3) and fdopendir(3).
105  */
106 static DIR *
107 __opendir_common(int fd, const char *name, int flags)
108 {
109         DIR *dirp;
110         int incr;
111         int saved_errno;
112         int unionstack;
113
114         if ((dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
115                 return (NULL);
116
117         dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
118         LIST_INIT(&dirp->dd_td->td_locq);
119         dirp->dd_td->td_loccnt = 0;
120
121         /*
122          * Use the system page size if that is a multiple of DIRBLKSIZ.
123          * Hopefully this can be a big win someday by allowing page
124          * trades to user space to be done by _getdirentries().
125          */
126         incr = getpagesize();
127         if ((incr % DIRBLKSIZ) != 0) 
128                 incr = DIRBLKSIZ;
129
130         /*
131          * Determine whether this directory is the top of a union stack.
132          */
133         if (flags & DTF_NODUP) {
134                 struct statfs sfb;
135
136                 if (_fstatfs(fd, &sfb) < 0)
137                         goto fail;
138                 unionstack = !strcmp(sfb.f_fstypename, "unionfs")
139                     || (sfb.f_flags & MNT_UNION);
140         } else {
141                 unionstack = 0;
142         }
143
144         if (unionstack) {
145                 int len = 0;
146                 int space = 0;
147                 char *buf = 0;
148                 char *ddptr = 0;
149                 char *ddeptr;
150                 int n;
151                 struct dirent **dpv;
152
153                 /*
154                  * The strategy here is to read all the directory
155                  * entries into a buffer, sort the buffer, and
156                  * remove duplicate entries by setting the inode
157                  * number to zero.
158                  */
159
160                 do {
161                         /*
162                          * Always make at least DIRBLKSIZ bytes
163                          * available to _getdirentries
164                          */
165                         if (space < DIRBLKSIZ) {
166                                 space += incr;
167                                 len += incr;
168                                 buf = reallocf(buf, len);
169                                 if (buf == NULL)
170                                         goto fail;
171                                 ddptr = buf + (len - space);
172                         }
173
174                         n = _getdirentries(fd, ddptr, space, &dirp->dd_seek);
175                         if (n > 0) {
176                                 ddptr += n;
177                                 space -= n;
178                         }
179                 } while (n > 0);
180
181                 ddeptr = ddptr;
182                 flags |= __DTF_READALL;
183
184                 /*
185                  * Re-open the directory.
186                  * This has the effect of rewinding back to the
187                  * top of the union stack and is needed by
188                  * programs which plan to fchdir to a descriptor
189                  * which has also been read -- see fts.c.
190                  */
191                 if (flags & DTF_REWIND) {
192                         (void)_close(fd);
193                         if ((fd = _open(name, O_RDONLY | O_DIRECTORY)) == -1) {
194                                 saved_errno = errno;
195                                 free(buf);
196                                 free(dirp);
197                                 errno = saved_errno;
198                                 return (NULL);
199                         }
200                 }
201
202                 /*
203                  * There is now a buffer full of (possibly) duplicate
204                  * names.
205                  */
206                 dirp->dd_buf = buf;
207
208                 /*
209                  * Go round this loop twice...
210                  *
211                  * Scan through the buffer, counting entries.
212                  * On the second pass, save pointers to each one.
213                  * Then sort the pointers and remove duplicate names.
214                  */
215                 for (dpv = 0;;) {
216                         n = 0;
217                         ddptr = buf;
218                         while (ddptr < ddeptr) {
219                                 struct dirent *dp;
220
221                                 dp = (struct dirent *) ddptr;
222                                 if ((long)dp & 03L)
223                                         break;
224                                 if ((dp->d_reclen <= 0) ||
225                                     (dp->d_reclen > (ddeptr + 1 - ddptr)))
226                                         break;
227                                 ddptr += dp->d_reclen;
228                                 if (dp->d_fileno) {
229                                         if (dpv)
230                                                 dpv[n] = dp;
231                                         n++;
232                                 }
233                         }
234
235                         if (dpv) {
236                                 struct dirent *xp;
237
238                                 /*
239                                  * This sort must be stable.
240                                  */
241                                 mergesort(dpv, n, sizeof(*dpv),
242                                     opendir_compar);
243
244                                 dpv[n] = NULL;
245                                 xp = NULL;
246
247                                 /*
248                                  * Scan through the buffer in sort order,
249                                  * zapping the inode number of any
250                                  * duplicate names.
251                                  */
252                                 for (n = 0; dpv[n]; n++) {
253                                         struct dirent *dp = dpv[n];
254
255                                         if ((xp == NULL) ||
256                                             strcmp(dp->d_name, xp->d_name)) {
257                                                 xp = dp;
258                                         } else {
259                                                 dp->d_fileno = 0;
260                                         }
261                                         if (dp->d_type == DT_WHT &&
262                                             (flags & DTF_HIDEW))
263                                                 dp->d_fileno = 0;
264                                 }
265
266                                 free(dpv);
267                                 break;
268                         } else {
269                                 dpv = malloc((n+1) * sizeof(struct dirent *));
270                                 if (dpv == NULL)
271                                         break;
272                         }
273                 }
274
275                 dirp->dd_len = len;
276                 dirp->dd_size = ddptr - dirp->dd_buf;
277         } else {
278                 dirp->dd_len = incr;
279                 dirp->dd_size = 0;
280                 dirp->dd_buf = malloc(dirp->dd_len);
281                 if (dirp->dd_buf == NULL)
282                         goto fail;
283                 dirp->dd_seek = 0;
284                 flags &= ~DTF_REWIND;
285         }
286
287         dirp->dd_loc = 0;
288         dirp->dd_fd = fd;
289         dirp->dd_flags = flags;
290         dirp->dd_lock = NULL;
291
292         /*
293          * Set up seek point for rewinddir.
294          */
295         dirp->dd_rewind = telldir(dirp);
296
297         return (dirp);
298
299 fail:
300         saved_errno = errno;
301         free(dirp);
302         (void)_close(fd);
303         errno = saved_errno;
304         return (NULL);
305 }