Home | History | Annotate | Line # | Download | only in gen
opendir.c revision 1.14
      1 /*	$NetBSD: opendir.c,v 1.14 1997/07/21 14:07:22 jtc 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.14 1997/07/21 14:07:22 jtc 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;
     83 
     84 	if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1)
     85 		return (NULL);
     86 	if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
     87 		errno = ENOTDIR;
     88 		close(fd);
     89 		return (NULL);
     90 	}
     91 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
     92 	    (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
     93 		close(fd);
     94 		return (NULL);
     95 	}
     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 	if (flags & DTF_NODUP) {
    112 		struct statfs sfb;
    113 
    114 		if (fstatfs(fd, &sfb) < 0) {
    115 			free(dirp);
    116 			close(fd);
    117 			return (NULL);
    118 		}
    119 		unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION,
    120 		    MFSNAMELEN)) || (sfb.f_flags & MNT_UNION);
    121 	} else {
    122 		unionstack = 0;
    123 	}
    124 
    125 	if (unionstack) {
    126 		int len = 0;
    127 		int space = 0;
    128 		char *buf = 0;
    129 		char *ddptr = 0;
    130 		char *ddeptr;
    131 		int n;
    132 		struct dirent **dpv;
    133 
    134 		/*
    135 		 * The strategy here is to read all the directory
    136 		 * entries into a buffer, sort the buffer, and
    137 		 * remove duplicate entries by setting the inode
    138 		 * number to zero.
    139 		 */
    140 
    141 		do {
    142 			/*
    143 			 * Always make at least DIRBLKSIZ bytes
    144 			 * available to getdirentries
    145 			 */
    146 			if (space < DIRBLKSIZ) {
    147 				space += incr;
    148 				len += incr;
    149 				buf = realloc(buf, len);
    150 				if (buf == NULL) {
    151 					free(dirp);
    152 					close(fd);
    153 					return (NULL);
    154 				}
    155 				ddptr = buf + (len - space);
    156 			}
    157 
    158 			n = getdirentries(fd, ddptr, space, &dirp->dd_seek);
    159 			if (n > 0) {
    160 				ddptr += n;
    161 				space -= n;
    162 			}
    163 		} while (n > 0);
    164 
    165 		ddeptr = ddptr;
    166 		flags |= __DTF_READALL;
    167 
    168 		/*
    169 		 * Re-open the directory.
    170 		 * This has the effect of rewinding back to the
    171 		 * top of the union stack and is needed by
    172 		 * programs which plan to fchdir to a descriptor
    173 		 * which has also been read -- see fts.c.
    174 		 */
    175 		if (flags & DTF_REWIND) {
    176 			(void) close(fd);
    177 			if ((fd = open(name, O_RDONLY)) == -1) {
    178 				free(buf);
    179 				free(dirp);
    180 				return (NULL);
    181 			}
    182 		}
    183 
    184 		/*
    185 		 * There is now a buffer full of (possibly) duplicate
    186 		 * names.
    187 		 */
    188 		dirp->dd_buf = buf;
    189 
    190 		/*
    191 		 * Go round this loop twice...
    192 		 *
    193 		 * Scan through the buffer, counting entries.
    194 		 * On the second pass, save pointers to each one.
    195 		 * Then sort the pointers and remove duplicate names.
    196 		 */
    197 		for (dpv = 0;;) {
    198 			for (n = 0, ddptr = buf; ddptr < ddeptr;) {
    199 				struct dirent *dp;
    200 
    201 				dp = (struct dirent *) ddptr;
    202 				if ((long)dp & 03)
    203 					break;
    204 				if ((dp->d_reclen <= 0) ||
    205 				    (dp->d_reclen > (ddeptr + 1 - ddptr)))
    206 					break;
    207 				ddptr += dp->d_reclen;
    208 				if (dp->d_fileno) {
    209 					if (dpv)
    210 						dpv[n] = dp;
    211 					n++;
    212 				}
    213 			}
    214 
    215 			if (dpv) {
    216 				struct dirent *xp;
    217 
    218 				/*
    219 				 * This sort must be stable.
    220 				 */
    221 				mergesort(dpv, n, sizeof(*dpv), alphasort);
    222 
    223 				dpv[n] = NULL;
    224 				xp = NULL;
    225 
    226 				/*
    227 				 * Scan through the buffer in sort order,
    228 				 * zapping the inode number of any
    229 				 * duplicate names.
    230 				 */
    231 				for (n = 0; dpv[n]; n++) {
    232 					struct dirent *dp = dpv[n];
    233 
    234 					if ((xp == NULL) ||
    235 					    strcmp(dp->d_name, xp->d_name))
    236 						xp = dp;
    237 					else
    238 						dp->d_fileno = 0;
    239 					if (dp->d_type == DT_WHT &&
    240 					    (flags & DTF_HIDEW))
    241 						dp->d_fileno = 0;
    242 				}
    243 
    244 				free(dpv);
    245 				break;
    246 			} else {
    247 				dpv = malloc((n+1) * sizeof(struct dirent *));
    248 				if (dpv == NULL)
    249 					break;
    250 			}
    251 		}
    252 
    253 		dirp->dd_len = len;
    254 		dirp->dd_size = ddptr - dirp->dd_buf;
    255 	} else {
    256 		dirp->dd_len = incr;
    257 		dirp->dd_buf = malloc(dirp->dd_len);
    258 		if (dirp->dd_buf == NULL) {
    259 			free(dirp);
    260 			close (fd);
    261 			return (NULL);
    262 		}
    263 		dirp->dd_seek = 0;
    264 		flags &= ~DTF_REWIND;
    265 	}
    266 
    267 	dirp->dd_loc = 0;
    268 	dirp->dd_fd = fd;
    269 	dirp->dd_flags = flags;
    270 
    271 	/*
    272 	 * Set up seek point for rewinddir.
    273 	 */
    274 	dirp->dd_rewind = telldir(dirp);
    275 
    276 	return (dirp);
    277 }
    278