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