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 rcsid[] = "$OpenBSD: opendir.c,v 1.6 1998/08/15 08:10:14 deraadt Exp $"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/param.h> 39 #include <sys/mount.h> 40 #include <sys/stat.h> 41 42 #include <dirent.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 /* 50 * Open a directory. 51 */ 52 DIR * 53 opendir(name) 54 const char *name; 55 { 56 57 return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); 58 } 59 60 DIR * 61 __opendir2(name, flags) 62 const char *name; 63 int flags; 64 { 65 DIR *dirp; 66 int fd; 67 struct stat sb; 68 int pagesz; 69 int incr; 70 int unionstack; 71 72 if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1) 73 return (NULL); 74 if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) { 75 errno = ENOTDIR; 76 close(fd); 77 return (NULL); 78 } 79 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || 80 (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) { 81 close(fd); 82 return (NULL); 83 } 84 85 /* 86 * If the machine's page size is an exact multiple of DIRBLKSIZ, 87 * use a buffer that is cluster boundary aligned. 88 * Hopefully this can be a big win someday by allowing page trades 89 * to user space to be done by getdirentries() 90 */ 91 if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0) 92 incr = pagesz; 93 else 94 incr = DIRBLKSIZ; 95 96 /* 97 * Determine whether this directory is the top of a union stack. 98 */ 99 if (flags & DTF_NODUP) { 100 struct statfs sfb; 101 102 if (fstatfs(fd, &sfb) < 0) { 103 free(dirp); 104 close(fd); 105 return (NULL); 106 } 107 unionstack = strncmp(sfb.f_fstypename, MOUNT_UNION, MFSNAMELEN) == 0 || 108 (sfb.f_flags & MNT_UNION); 109 } else { 110 unionstack = 0; 111 } 112 113 if (unionstack) { 114 int len = 0; 115 int space = 0; 116 char *buf = 0; 117 char *ddptr = 0; 118 char *ddeptr; 119 int n; 120 struct dirent **dpv; 121 122 /* 123 * The strategy here is to read all the directory 124 * entries into a buffer, sort the buffer, and 125 * remove duplicate entries by setting the inode 126 * number to zero. 127 */ 128 129 do { 130 /* 131 * Always make at least DIRBLKSIZ bytes 132 * available to getdirentries 133 */ 134 if (space < DIRBLKSIZ) { 135 char *nbuf; 136 137 space += incr; 138 len += incr; 139 nbuf = realloc(buf, len); 140 if (nbuf == NULL) { 141 if (buf) 142 free(buf); 143 free(dirp); 144 close(fd); 145 return (NULL); 146 } 147 buf = nbuf; 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