Home | History | Annotate | Line # | Download | only in gen
opendir.c revision 1.25
      1 /*	$NetBSD: opendir.c,v 1.25 2004/04/21 01:05:32 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.25 2004/04/21 01:05:32 christos Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include "namespace.h"
     42 #include "reentrant.h"
     43 #include <sys/param.h>
     44 #include <sys/mount.h>
     45 #include <sys/stat.h>
     46 
     47 #include <assert.h>
     48 #include <dirent.h>
     49 #include <errno.h>
     50 #include <fcntl.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 
     55 #ifdef __weak_alias
     56 __weak_alias(opendir,_opendir)
     57 #endif
     58 
     59 /*
     60  * Open a directory.
     61  */
     62 DIR *
     63 opendir(name)
     64 	const char *name;
     65 {
     66 
     67 	_DIAGASSERT(name != NULL);
     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 = NULL;
     78 	int fd;
     79 	int serrno;
     80 	struct stat sb;
     81 	int pagesz;
     82 	int incr;
     83 	int unionstack, nfsdir;
     84 	struct statvfs sfb;
     85 
     86 	_DIAGASSERT(name != NULL);
     87 
     88 	if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1)
     89 		return (NULL);
     90 	if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
     91 		close(fd);
     92 		errno = ENOTDIR;
     93 		return (NULL);
     94 	}
     95 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
     96 	    (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
     97 		goto error;
     98 	}
     99 	dirp->dd_buf = NULL;
    100 
    101 	/*
    102 	 * If the machine's page size is an exact multiple of DIRBLKSIZ,
    103 	 * use a buffer that is cluster boundary aligned.
    104 	 * Hopefully this can be a big win someday by allowing page trades
    105 	 * to user space to be done by getdirentries()
    106 	 */
    107 	if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
    108 		incr = pagesz;
    109 	else
    110 		incr = DIRBLKSIZ;
    111 
    112 	/*
    113 	 * Determine whether this directory is the top of a union stack.
    114 	 */
    115 
    116 	if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
    117 		goto error;
    118 
    119 	if (flags & DTF_NODUP)
    120 		unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION,
    121 		    MFSNAMELEN)) || (sfb.f_flag & 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, *nbuf;
    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 				nbuf = realloc(buf, len);
    164 				if (nbuf == NULL) {
    165 					dirp->dd_buf = buf;
    166 					goto error;
    167 				}
    168 				buf = nbuf;
    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 				dirp->dd_buf = buf;
    205 				goto error;
    206 			}
    207 		}
    208 
    209 		/*
    210 		 * There is now a buffer full of (possibly) duplicate
    211 		 * names.
    212 		 */
    213 		dirp->dd_buf = buf;
    214 
    215 		/*
    216 		 * Go round this loop twice...
    217 		 *
    218 		 * Scan through the buffer, counting entries.
    219 		 * On the second pass, save pointers to each one.
    220 		 * Then sort the pointers and remove duplicate names.
    221 		 */
    222 		if (!nfsdir) {
    223 			for (dpv = 0;;) {
    224 				for (n = 0, ddptr = buf; ddptr < ddeptr;) {
    225 					struct dirent *dp;
    226 
    227 					dp = (struct dirent *)(void *)ddptr;
    228 					if ((long)dp & 03)
    229 						break;
    230 					/*
    231 					 * d_reclen is unsigned,
    232 					 * so no need to compare <= 0
    233 					 */
    234 					if (dp->d_reclen > (ddeptr + 1 - ddptr))
    235 						break;
    236 					ddptr += dp->d_reclen;
    237 					if (dp->d_fileno) {
    238 						if (dpv)
    239 							dpv[n] = dp;
    240 						n++;
    241 					}
    242 				}
    243 
    244 				if (dpv) {
    245 					struct dirent *xp;
    246 
    247 					/*
    248 					 * This sort must be stable.
    249 					 */
    250 					mergesort(dpv, (size_t)n, sizeof(*dpv),
    251 					    alphasort);
    252 
    253 					dpv[n] = NULL;
    254 					xp = NULL;
    255 
    256 					/*
    257 					 * Scan through the buffer in sort
    258 					 * order, zapping the inode number
    259 					 * of any duplicate names.
    260 					 */
    261 					for (n = 0; dpv[n]; n++) {
    262 						struct dirent *dp = dpv[n];
    263 
    264 						if ((xp == NULL) ||
    265 						    strcmp(dp->d_name,
    266 						      xp->d_name))
    267 							xp = dp;
    268 						else
    269 							dp->d_fileno = 0;
    270 						if (dp->d_type == DT_WHT &&
    271 						    (flags & DTF_HIDEW))
    272 							dp->d_fileno = 0;
    273 					}
    274 
    275 					free(dpv);
    276 					break;
    277 				} else {
    278 					dpv = malloc((n + 1) *
    279 					    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 			goto error;
    293 		dirp->dd_seek = 0;
    294 		flags &= ~DTF_REWIND;
    295 	}
    296 
    297 	dirp->dd_loc = 0;
    298 	dirp->dd_fd = fd;
    299 	dirp->dd_flags = flags;
    300 
    301 	/*
    302 	 * Set up seek point for rewinddir.
    303 	 */
    304 #ifdef _REENTRANT
    305 	if (__isthreaded) {
    306 		if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
    307 			goto error;
    308 		mutex_init((mutex_t *)dirp->dd_lock, NULL);
    309 	}
    310 #endif
    311 	dirp->dd_rewind = telldir(dirp);
    312 	return (dirp);
    313 error:
    314 	serrno = errno;
    315 	if (dirp && dirp->dd_buf)
    316 		free(dirp->dd_buf);
    317 	if (dirp)
    318 		free(dirp);
    319 	if (fd != -1)
    320 		(void)close(fd);
    321 	errno = serrno;
    322 	return NULL;
    323 }
    324