opendir.c revision 23668
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) 35static char sccsid[] = "@(#)opendir.c 8.8 (Berkeley) 5/1/95"; 36#endif /* LIBC_SCCS and not lint */ 37 38#include <sys/param.h> 39#include <sys/stat.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 */ 51DIR * 52opendir(name) 53 const char *name; 54{ 55 return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); 56} 57 58DIR * 59__opendir2(name, flags) 60 const char *name; 61 int flags; 62{ 63 DIR *dirp; 64 int fd; 65 int incr; 66 int unionstack; 67 struct stat statb; 68 69 /* 70 * stat() before open() because opening of special files may be 71 * harmful. fstat() after open because the file may have changed. 72 */ 73 if (stat(name, &statb) != 0) 74 return NULL; 75 if (!S_ISDIR(statb.st_mode)) { 76 errno = ENOTDIR; 77 return NULL; 78 } 79 if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1) 80 return (NULL); 81 if (fstat(fd, &statb) || !S_ISDIR(statb.st_mode)) { 82 errno = ENOTDIR; 83 close(fd); 84 return (NULL); 85 } 86 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || 87 (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) { 88 close(fd); 89 return (NULL); 90 } 91 92 /* 93 * Use the system page size if that is a multiple of DIRBLKSIZ 94 * this could speed things up in some cases. 95 * Hopefully this can be a big win someday by allowing page 96 * trades to user space to be done by getdirentries(). 97 */ 98 incr = getpagesize(); 99 if ((incr % DIRBLKSIZ) != 0) 100 incr = DIRBLKSIZ; 101 102 /* 103 * Determine whether this directory is the top of a union stack. 104 */ 105 if (flags & DTF_NODUP) { 106 struct statfs sfb; 107 108 if (fstatfs(fd, &sfb) < 0) { 109 free(dirp); 110 close(fd); 111 return (NULL); 112 } 113 unionstack = !strcmp(sfb.f_fstypename, "union"); 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 n = 0; 192 ddptr = buf; 193 while (ddptr < ddeptr) { 194 struct dirent *dp; 195 196 dp = (struct dirent *) ddptr; 197 if ((int)dp & 03) 198 break; 199 if ((dp->d_reclen <= 0) || 200 (dp->d_reclen > (ddeptr + 1 - ddptr))) 201 break; 202 ddptr += dp->d_reclen; 203 if (dp->d_fileno) { 204 if (dpv) 205 dpv[n] = dp; 206 n++; 207 } 208 } 209 210 if (dpv) { 211 struct dirent *xp; 212 213 /* 214 * This sort must be stable. 215 */ 216 mergesort(dpv, n, sizeof(*dpv), alphasort); 217 218 dpv[n] = NULL; 219 xp = NULL; 220 221 /* 222 * Scan through the buffer in sort order, 223 * zapping the inode number of any 224 * duplicate names. 225 */ 226 for (n = 0; dpv[n]; n++) { 227 struct dirent *dp = dpv[n]; 228 229 if ((xp == NULL) || 230 strcmp(dp->d_name, xp->d_name)) { 231 xp = dp; 232 } else { 233 dp->d_fileno = 0; 234 } 235 if (dp->d_type == DT_WHT && 236 (flags & DTF_HIDEW)) 237 dp->d_fileno = 0; 238 } 239 240 free(dpv); 241 break; 242 } else { 243 dpv = malloc((n+1) * sizeof(struct dirent *)); 244 if (dpv == NULL) 245 break; 246 } 247 } 248 249 dirp->dd_len = len; 250 dirp->dd_size = ddptr - dirp->dd_buf; 251 } else { 252 dirp->dd_len = incr; 253 dirp->dd_buf = malloc(dirp->dd_len); 254 if (dirp->dd_buf == NULL) { 255 free(dirp); 256 close (fd); 257 return (NULL); 258 } 259 dirp->dd_seek = 0; 260 flags &= ~DTF_REWIND; 261 } 262 263 dirp->dd_loc = 0; 264 dirp->dd_fd = fd; 265 dirp->dd_flags = flags; 266 267 /* 268 * Set up seek point for rewinddir. 269 */ 270 dirp->dd_rewind = telldir(dirp); 271 272 return (dirp); 273} 274