1 /* $NetBSD: opendir.c,v 1.17 1998/11/13 12:31:50 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #if defined(LIBC_SCCS) && !defined(lint) 38 #if 0 39 static char sccsid[] = "@(#)opendir.c 8.7 (Berkeley) 12/10/94"; 40 #else 41 __RCSID("$NetBSD: opendir.c,v 1.17 1998/11/13 12:31:50 christos Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 #include "namespace.h" 46 #include <sys/param.h> 47 #include <sys/mount.h> 48 #include <sys/stat.h> 49 50 #include <dirent.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #ifdef __weak_alias 58 __weak_alias(opendir,_opendir); 59 #endif 60 61 /* 62 * Open a directory. 63 */ 64 DIR * 65 opendir(name) 66 const char *name; 67 { 68 69 return (__opendir2(name, DTF_HIDEW|DTF_NODUP)); 70 } 71 72 DIR * 73 __opendir2(name, flags) 74 const char *name; 75 int flags; 76 { 77 DIR *dirp; 78 int fd; 79 struct stat sb; 80 int pagesz; 81 int incr; 82 int unionstack, nfsdir; 83 struct statfs sfb; 84 85 if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1) 86 return (NULL); 87 if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) { 88 errno = ENOTDIR; 89 close(fd); 90 return (NULL); 91 } 92 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || 93 (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) { 94 close(fd); 95 return (NULL); 96 } 97 98 /* 99 * If the machine's page size is an exact multiple of DIRBLKSIZ, 100 * use a buffer that is cluster boundary aligned. 101 * Hopefully this can be a big win someday by allowing page trades 102 * to user space to be done by getdirentries() 103 */ 104 if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0) 105 incr = pagesz; 106 else 107 incr = DIRBLKSIZ; 108 109 /* 110 * Determine whether this directory is the top of a union stack. 111 */ 112 113 if (fstatfs(fd, &sfb) < 0) { 114 free(dirp); 115 close(fd); 116 return (NULL); 117 } 118 119 if (flags & DTF_NODUP) 120 unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION, 121 MFSNAMELEN)) || (sfb.f_flags & MNT_UNION); 122 else 123 unionstack = 0; 124 125 nfsdir = !(strncmp(sfb.f_fstypename, MOUNT_NFS, MFSNAMELEN)); 126 127 if (unionstack || nfsdir) { 128 size_t len; 129 size_t space; 130 char *buf; 131 char *ddptr; 132 char *ddeptr; 133 int n; 134 struct dirent **dpv; 135 136 /* 137 * The strategy here for directories on top of a union stack 138 * is to read all the directory entries into a buffer, sort 139 * the buffer, and remove duplicate entries by setting the 140 * inode number to zero. 141 * 142 * For directories on an NFS mounted filesystem, we try 143 * to get a consistent snapshot by trying until we have 144 * successfully read all of the directory without errors 145 * (i.e. 'bad cookie' errors from the server because 146 * the directory was modified). These errors should not 147 * happen often, but need to be dealt with. 148 */ 149 retry: 150 len = 0; 151 space = 0; 152 buf = 0; 153 ddptr = 0; 154 155 do { 156 /* 157 * Always make at least DIRBLKSIZ bytes 158 * available to getdirentries 159 */ 160 if (space < DIRBLKSIZ) { 161 space += incr; 162 len += incr; 163 buf = realloc(buf, len); 164 if (buf == NULL) { 165 free(dirp); 166 close(fd); 167 return (NULL); 168 } 169 ddptr = buf + (len - space); 170 } 171 172 dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR); 173 n = getdents(fd, ddptr, space); 174 /* 175 * For NFS: EINVAL means a bad cookie error 176 * from the server. Keep trying to get a 177 * consistent view, in this case this means 178 * starting all over again. 179 */ 180 if (n == -1 && errno == EINVAL && nfsdir) { 181 free(buf); 182 lseek(fd, (off_t)0, SEEK_SET); 183 goto retry; 184 } 185 if (n > 0) { 186 ddptr += n; 187 space -= n; 188 } 189 } while (n > 0); 190 191 ddeptr = ddptr; 192 flags |= __DTF_READALL; 193 194 /* 195 * Re-open the directory. 196 * This has the effect of rewinding back to the 197 * top of the union stack and is needed by 198 * programs which plan to fchdir to a descriptor 199 * which has also been read -- see fts.c. 200 */ 201 if (flags & DTF_REWIND) { 202 (void) close(fd); 203 if ((fd = open(name, O_RDONLY)) == -1) { 204 free(buf); 205 free(dirp); 206 return (NULL); 207 } 208 } 209 210 /* 211 * There is now a buffer full of (possibly) duplicate 212 * names. 213 */ 214 dirp->dd_buf = buf; 215 216 /* 217 * Go round this loop twice... 218 * 219 * Scan through the buffer, counting entries. 220 * On the second pass, save pointers to each one. 221 * Then sort the pointers and remove duplicate names. 222 */ 223 if (!nfsdir) { 224 for (dpv = 0;;) { 225 for (n = 0, ddptr = buf; ddptr < ddeptr;) { 226 struct dirent *dp; 227 228 dp = (struct dirent *)(void *)ddptr; 229 if ((long)dp & 03) 230 break; 231 /* 232 * d_reclen is unsigned, 233 * so no need to compare <= 0 234 */ 235 if (dp->d_reclen > (ddeptr + 1 - ddptr)) 236 break; 237 ddptr += dp->d_reclen; 238 if (dp->d_fileno) { 239 if (dpv) 240 dpv[n] = dp; 241 n++; 242 } 243 } 244 245 if (dpv) { 246 struct dirent *xp; 247 248 /* 249 * This sort must be stable. 250 */ 251 mergesort(dpv, (size_t)n, sizeof(*dpv), 252 alphasort); 253 254 dpv[n] = NULL; 255 xp = NULL; 256 257 /* 258 * Scan through the buffer in sort 259 * order, zapping the inode number 260 * of any duplicate names. 261 */ 262 for (n = 0; dpv[n]; n++) { 263 struct dirent *dp = dpv[n]; 264 265 if ((xp == NULL) || 266 strcmp(dp->d_name, 267 xp->d_name)) 268 xp = dp; 269 else 270 dp->d_fileno = 0; 271 if (dp->d_type == DT_WHT && 272 (flags & DTF_HIDEW)) 273 dp->d_fileno = 0; 274 } 275 276 free(dpv); 277 break; 278 } else { 279 dpv = malloc((n+1) * sizeof(struct dirent *)); 280 if (dpv == NULL) 281 break; 282 } 283 } 284 } 285 286 dirp->dd_len = len; 287 dirp->dd_size = ddptr - dirp->dd_buf; 288 } else { 289 dirp->dd_len = incr; 290 dirp->dd_buf = malloc((size_t)dirp->dd_len); 291 if (dirp->dd_buf == NULL) { 292 free(dirp); 293 close (fd); 294 return (NULL); 295 } 296 dirp->dd_seek = 0; 297 flags &= ~DTF_REWIND; 298 } 299 300 dirp->dd_loc = 0; 301 dirp->dd_fd = fd; 302 dirp->dd_flags = flags; 303 304 /* 305 * Set up seek point for rewinddir. 306 */ 307 dirp->dd_rewind = telldir(dirp); 308 309 return (dirp); 310 } 311