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