Home | History | Annotate | Line # | Download | only in gen
opendir.c revision 1.33
      1 /*	$NetBSD: opendir.c,v 1.33 2008/01/10 09:49:04 elad 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 2008/01/10 09:49:04 elad Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include "namespace.h"
     42 #include "reentrant.h"
     43 #include "extern.h"
     44 #include <sys/param.h>
     45 #include <sys/mount.h>
     46 #include <sys/stat.h>
     47 
     48 #include <assert.h>
     49 #include <dirent.h>
     50 #include <errno.h>
     51 #include <fcntl.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "dirent_private.h"
     57 
     58 #define	MAXITERATIONS	100
     59 
     60 /*
     61  * Open a directory.
     62  */
     63 DIR *
     64 opendir(const char *name)
     65 {
     66 
     67 	_DIAGASSERT(name != NULL);
     68 
     69 	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
     70 }
     71 
     72 DIR *
     73 __opendir2(const char *name, int flags)
     74 {
     75 	DIR *dirp = NULL;
     76 	int fd;
     77 	int serrno;
     78 	struct stat sb;
     79 	int pagesz;
     80 	int incr;
     81 	int unionstack, nfsdir;
     82 	struct statvfs sfb;
     83 
     84 	_DIAGASSERT(name != NULL);
     85 
     86 	if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1 ||
     87 	    fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
     88 		goto error;
     89 	if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
     90 		errno = ENOTDIR;
     91 		goto error;
     92 	}
     93 	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL)
     94 		goto error;
     95 	dirp->dd_buf = NULL;
     96 
     97 	/*
     98 	 * If the machine's page size is an exact multiple of DIRBLKSIZ,
     99 	 * use a buffer that is cluster boundary aligned.
    100 	 * Hopefully this can be a big win someday by allowing page trades
    101 	 * to user space to be done by getdirentries()
    102 	 */
    103 	if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
    104 		incr = pagesz;
    105 	else
    106 		incr = DIRBLKSIZ;
    107 
    108 	/*
    109 	 * Determine whether this directory is the top of a union stack.
    110 	 */
    111 
    112 	if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
    113 		goto error;
    114 
    115 	if (flags & DTF_NODUP)
    116 		unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION,
    117 		    sizeof(sfb.f_fstypename))) || (sfb.f_flag & MNT_UNION);
    118 	else
    119 		unionstack = 0;
    120 
    121 	nfsdir = !(strncmp(sfb.f_fstypename, MOUNT_NFS, sizeof(sfb.f_fstypename)));
    122 
    123 	if (unionstack || nfsdir) {
    124 		size_t len;
    125 		size_t space;
    126 		char *buf, *nbuf;
    127 		char *ddptr;
    128 		char *ddeptr;
    129 		int n;
    130 		struct dirent **dpv;
    131 		int i;
    132 
    133 		/*
    134 		 * The strategy here for directories on top of a union stack
    135 		 * is to read all the directory entries into a buffer, sort
    136 		 * the buffer, and remove duplicate entries by setting the
    137 		 * inode number to zero.
    138 		 *
    139 		 * For directories on an NFS mounted filesystem, we try
    140 	 	 * to get a consistent snapshot by trying until we have
    141 		 * successfully read all of the directory without errors
    142 		 * (i.e. 'bad cookie' errors from the server because
    143 		 * the directory was modified). These errors should not
    144 		 * happen often, but need to be dealt with.
    145 		 */
    146 		i = 0;
    147 retry:
    148 		len = 0;
    149 		space = 0;
    150 		buf = 0;
    151 		ddptr = 0;
    152 
    153 		do {
    154 			/*
    155 			 * Always make at least DIRBLKSIZ bytes
    156 			 * available to getdirentries
    157 			 */
    158 			if (space < DIRBLKSIZ) {
    159 				space += incr;
    160 				len += incr;
    161 				nbuf = realloc(buf, len);
    162 				if (nbuf == NULL) {
    163 					dirp->dd_buf = buf;
    164 					goto error;
    165 				}
    166 				buf = nbuf;
    167 				ddptr = buf + (len - space);
    168 			}
    169 
    170 			dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR);
    171 			n = getdents(fd, ddptr, space);
    172 			/*
    173 			 * For NFS: EINVAL means a bad cookie error
    174 			 * from the server. Keep trying to get a
    175 			 * consistent view, in this case this means
    176 			 * starting all over again.
    177 			 */
    178 			if (n == -1 && errno == EINVAL && nfsdir) {
    179 				free(buf);
    180 				lseek(fd, (off_t)0, SEEK_SET);
    181 				if (++i > MAXITERATIONS)
    182 					goto error;
    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 			    fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
    205 				dirp->dd_buf = buf;
    206 				goto error;
    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 & _DIRENT_ALIGN(dp))
    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) *
    280 					    sizeof(struct dirent *));
    281 					if (dpv == NULL)
    282 						break;
    283 				}
    284 			}
    285 		}
    286 
    287 		dirp->dd_len = len;
    288 		dirp->dd_size = ddptr - dirp->dd_buf;
    289 	} else {
    290 		dirp->dd_len = incr;
    291 		dirp->dd_buf = malloc((size_t)dirp->dd_len);
    292 		if (dirp->dd_buf == NULL)
    293 			goto error;
    294 		dirp->dd_seek = 0;
    295 		flags &= ~DTF_REWIND;
    296 	}
    297 
    298 	dirp->dd_loc = 0;
    299 	dirp->dd_fd = fd;
    300 	dirp->dd_flags = flags;
    301 
    302 	/*
    303 	 * Set up seek point for rewinddir.
    304 	 */
    305 #ifdef _REENTRANT
    306 	if (__isthreaded) {
    307 		if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
    308 			goto error;
    309 		mutex_init((mutex_t *)dirp->dd_lock, NULL);
    310 	}
    311 #endif
    312 	dirp->dd_internal = NULL;
    313 	(void)_telldir_unlocked(dirp);
    314 	return (dirp);
    315 error:
    316 	serrno = errno;
    317 	if (dirp && dirp->dd_buf)
    318 		free(dirp->dd_buf);
    319 	if (dirp)
    320 		free(dirp);
    321 	if (fd != -1)
    322 		(void)close(fd);
    323 	errno = serrno;
    324 	return NULL;
    325 }
    326