opendir.c revision 201604
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) 31static 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: head/lib/libc/gen/opendir.c 201604 2010-01-05 20:20:31Z kib $"); 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 51static DIR * __opendir_common(int, const char *, int); 52 53/* 54 * Open a directory. 55 */ 56DIR * 57opendir(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 */ 66DIR * 67fdopendir(int fd) 68{ 69 70 return (__opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP)); 71} 72 73DIR * 74__opendir2(const char *name, int flags) 75{ 76 int fd; 77 struct stat statb; 78 79 /* 80 * stat() before _open() because opening of special files may be 81 * harmful. 82 */ 83 if (stat(name, &statb) != 0) 84 return (NULL); 85 if (!S_ISDIR(statb.st_mode)) { 86 errno = ENOTDIR; 87 return (NULL); 88 } 89 if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1) 90 return (NULL); 91 92 return __opendir_common(fd, name, flags); 93} 94 95/* 96 * POSIX 2008 and XSI 7 require alphasort() to call strcoll() for 97 * directory entries ordering. Use local copy that uses strcmp(). 98 */ 99static int 100opendir_alphasort(const void *p1, const void *p2) 101{ 102 103 return (strcmp((*(const struct dirent **)p1)->d_name, 104 (*(const struct dirent **)p2)->d_name)); 105} 106 107/* 108 * Common routine for opendir(3), __opendir2(3) and fdopendir(3). 109 */ 110static DIR * 111__opendir_common(int fd, const char *name, int flags) 112{ 113 DIR *dirp; 114 int incr; 115 int saved_errno; 116 int unionstack; 117 struct stat statb; 118 119 dirp = NULL; 120 /* _fstat() the open handler because the file may have changed. */ 121 if (_fstat(fd, &statb) != 0) 122 goto fail; 123 if (!S_ISDIR(statb.st_mode)) { 124 errno = ENOTDIR; 125 goto fail; 126 } 127 if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || 128 (dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL) 129 goto fail; 130 131 dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR)); 132 LIST_INIT(&dirp->dd_td->td_locq); 133 dirp->dd_td->td_loccnt = 0; 134 135 /* 136 * Use the system page size if that is a multiple of DIRBLKSIZ. 137 * Hopefully this can be a big win someday by allowing page 138 * trades to user space to be done by _getdirentries(). 139 */ 140 incr = getpagesize(); 141 if ((incr % DIRBLKSIZ) != 0) 142 incr = DIRBLKSIZ; 143 144 /* 145 * Determine whether this directory is the top of a union stack. 146 */ 147 if (flags & DTF_NODUP) { 148 struct statfs sfb; 149 150 if (_fstatfs(fd, &sfb) < 0) 151 goto fail; 152 unionstack = !strcmp(sfb.f_fstypename, "unionfs") 153 || (sfb.f_flags & MNT_UNION); 154 } else { 155 unionstack = 0; 156 } 157 158 if (unionstack) { 159 int len = 0; 160 int space = 0; 161 char *buf = 0; 162 char *ddptr = 0; 163 char *ddeptr; 164 int n; 165 struct dirent **dpv; 166 167 /* 168 * The strategy here is to read all the directory 169 * entries into a buffer, sort the buffer, and 170 * remove duplicate entries by setting the inode 171 * number to zero. 172 */ 173 174 do { 175 /* 176 * Always make at least DIRBLKSIZ bytes 177 * available to _getdirentries 178 */ 179 if (space < DIRBLKSIZ) { 180 space += incr; 181 len += incr; 182 buf = reallocf(buf, len); 183 if (buf == NULL) 184 goto fail; 185 ddptr = buf + (len - space); 186 } 187 188 n = _getdirentries(fd, ddptr, space, &dirp->dd_seek); 189 if (n > 0) { 190 ddptr += n; 191 space -= n; 192 } 193 } while (n > 0); 194 195 ddeptr = ddptr; 196 flags |= __DTF_READALL; 197 198 /* 199 * Re-open the directory. 200 * This has the effect of rewinding back to the 201 * top of the union stack and is needed by 202 * programs which plan to fchdir to a descriptor 203 * which has also been read -- see fts.c. 204 */ 205 if (flags & DTF_REWIND) { 206 (void)_close(fd); 207 if ((fd = _open(name, O_RDONLY)) == -1) { 208 saved_errno = errno; 209 free(buf); 210 free(dirp); 211 errno = saved_errno; 212 return (NULL); 213 } 214 } 215 216 /* 217 * There is now a buffer full of (possibly) duplicate 218 * names. 219 */ 220 dirp->dd_buf = buf; 221 222 /* 223 * Go round this loop twice... 224 * 225 * Scan through the buffer, counting entries. 226 * On the second pass, save pointers to each one. 227 * Then sort the pointers and remove duplicate names. 228 */ 229 for (dpv = 0;;) { 230 n = 0; 231 ddptr = buf; 232 while (ddptr < ddeptr) { 233 struct dirent *dp; 234 235 dp = (struct dirent *) ddptr; 236 if ((long)dp & 03L) 237 break; 238 if ((dp->d_reclen <= 0) || 239 (dp->d_reclen > (ddeptr + 1 - ddptr))) 240 break; 241 ddptr += dp->d_reclen; 242 if (dp->d_fileno) { 243 if (dpv) 244 dpv[n] = dp; 245 n++; 246 } 247 } 248 249 if (dpv) { 250 struct dirent *xp; 251 252 /* 253 * This sort must be stable. 254 */ 255 mergesort(dpv, n, sizeof(*dpv), 256 opendir_alphasort); 257 258 dpv[n] = NULL; 259 xp = NULL; 260 261 /* 262 * Scan through the buffer in sort order, 263 * zapping the inode number of any 264 * duplicate names. 265 */ 266 for (n = 0; dpv[n]; n++) { 267 struct dirent *dp = dpv[n]; 268 269 if ((xp == NULL) || 270 strcmp(dp->d_name, xp->d_name)) { 271 xp = dp; 272 } else { 273 dp->d_fileno = 0; 274 } 275 if (dp->d_type == DT_WHT && 276 (flags & DTF_HIDEW)) 277 dp->d_fileno = 0; 278 } 279 280 free(dpv); 281 break; 282 } else { 283 dpv = malloc((n+1) * sizeof(struct dirent *)); 284 if (dpv == NULL) 285 break; 286 } 287 } 288 289 dirp->dd_len = len; 290 dirp->dd_size = ddptr - dirp->dd_buf; 291 } else { 292 dirp->dd_len = incr; 293 dirp->dd_size = 0; 294 dirp->dd_buf = malloc(dirp->dd_len); 295 if (dirp->dd_buf == NULL) 296 goto fail; 297 dirp->dd_seek = 0; 298 flags &= ~DTF_REWIND; 299 } 300 301 dirp->dd_loc = 0; 302 dirp->dd_fd = fd; 303 dirp->dd_flags = flags; 304 dirp->dd_lock = NULL; 305 306 /* 307 * Set up seek point for rewinddir. 308 */ 309 dirp->dd_rewind = telldir(dirp); 310 311 return (dirp); 312 313fail: 314 saved_errno = errno; 315 free(dirp); 316 (void)_close(fd); 317 errno = saved_errno; 318 return (NULL); 319} 320