Home | History | Annotate | Line # | Download | only in gen
opendir.c revision 1.8
      1 /*	$NetBSD: opendir.c,v 1.8 1995/02/25 08:51:28 cgd 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 #if defined(LIBC_SCCS) && !defined(lint)
     37 #if 0
     38 static char sccsid[] = "@(#)opendir.c	8.7 (Berkeley) 12/10/94";
     39 #else
     40 static char rcsid[] = "$NetBSD: opendir.c,v 1.8 1995/02/25 08:51:28 cgd Exp $";
     41 #endif
     42 #endif /* LIBC_SCCS and not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/mount.h>
     46 #include <sys/stat.h>
     47 
     48 #include <dirent.h>
     49 #include <errno.h>
     50 #include <fcntl.h>
     51 #include <stdlib.h>
     52 #include <unistd.h>
     53 
     54 /*
     55  * Open a directory.
     56  */
     57 DIR *
     58 opendir(name)
     59 	const char *name;
     60 {
     61 
     62 	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
     63 }
     64 
     65 DIR *
     66 __opendir2(name, flags)
     67 	const char *name;
     68 	int flags;
     69 {
     70 	DIR *dirp;
     71 	int fd;
     72 	struct stat sb;
     73 	int pagesz;
     74 	int incr;
     75 	int unionstack;
     76 
     77 	if ((fd = open(name, O_RDONLY)) == -1)
     78 		return (NULL);
     79 	if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
     80 		errno = ENOTDIR;
     81 		close(fd);
     82 		return (NULL);
     83 	}
     84 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
     85 	    (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
     86 		close(fd);
     87 		return (NULL);
     88 	}
     89 
     90 	/*
     91 	 * If the machine's page size is an exact multiple of DIRBLKSIZ,
     92 	 * use a buffer that is cluster boundary aligned.
     93 	 * Hopefully this can be a big win someday by allowing page trades
     94 	 * to user space to be done by getdirentries()
     95 	 */
     96 	if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
     97 		incr = pagesz;
     98 	else
     99 		incr = DIRBLKSIZ;
    100 
    101 	/*
    102 	 * Determine whether this directory is the top of a union stack.
    103 	 */
    104 	if (flags & DTF_NODUP) {
    105 		struct statfs sfb;
    106 
    107 		if (fstatfs(fd, &sfb) < 0) {
    108 			free(dirp);
    109 			close(fd);
    110 			return (NULL);
    111 		}
    112 		unionstack = !strcmp(sfb.f_fstypename, MOUNT_UNION);
    113 	} else {
    114 		unionstack = 0;
    115 	}
    116 
    117 	if (unionstack) {
    118 		int len = 0;
    119 		int space = 0;
    120 		char *buf = 0;
    121 		char *ddptr = 0;
    122 		char *ddeptr;
    123 		int n;
    124 		struct dirent **dpv;
    125 
    126 		/*
    127 		 * The strategy here is to read all the directory
    128 		 * entries into a buffer, sort the buffer, and
    129 		 * remove duplicate entries by setting the inode
    130 		 * number to zero.
    131 		 */
    132 
    133 		do {
    134 			/*
    135 			 * Always make at least DIRBLKSIZ bytes
    136 			 * available to getdirentries
    137 			 */
    138 			if (space < DIRBLKSIZ) {
    139 				space += incr;
    140 				len += incr;
    141 				buf = realloc(buf, len);
    142 				if (buf == NULL) {
    143 					free(dirp);
    144 					close(fd);
    145 					return (NULL);
    146 				}
    147 				ddptr = buf + (len - space);
    148 			}
    149 
    150 			n = getdirentries(fd, ddptr, space, &dirp->dd_seek);
    151 			if (n > 0) {
    152 				ddptr += n;
    153 				space -= n;
    154 			}
    155 		} while (n > 0);
    156 
    157 		ddeptr = ddptr;
    158 		flags |= __DTF_READALL;
    159 
    160 		/*
    161 		 * Re-open the directory.
    162 		 * This has the effect of rewinding back to the
    163 		 * top of the union stack and is needed by
    164 		 * programs which plan to fchdir to a descriptor
    165 		 * which has also been read -- see fts.c.
    166 		 */
    167 		if (flags & DTF_REWIND) {
    168 			(void) close(fd);
    169 			if ((fd = open(name, O_RDONLY)) == -1) {
    170 				free(buf);
    171 				free(dirp);
    172 				return (NULL);
    173 			}
    174 		}
    175 
    176 		/*
    177 		 * There is now a buffer full of (possibly) duplicate
    178 		 * names.
    179 		 */
    180 		dirp->dd_buf = buf;
    181 
    182 		/*
    183 		 * Go round this loop twice...
    184 		 *
    185 		 * Scan through the buffer, counting entries.
    186 		 * On the second pass, save pointers to each one.
    187 		 * Then sort the pointers and remove duplicate names.
    188 		 */
    189 		for (dpv = 0;;) {
    190 			for (n = 0, ddptr = buf; ddptr < ddeptr;) {
    191 				struct dirent *dp;
    192 
    193 				dp = (struct dirent *) ddptr;
    194 				if ((long)dp & 03)
    195 					break;
    196 				if ((dp->d_reclen <= 0) ||
    197 				    (dp->d_reclen > (ddeptr + 1 - ddptr)))
    198 					break;
    199 				ddptr += dp->d_reclen;
    200 				if (dp->d_fileno) {
    201 					if (dpv)
    202 						dpv[n] = dp;
    203 					n++;
    204 				}
    205 			}
    206 
    207 			if (dpv) {
    208 				struct dirent *xp;
    209 
    210 				/*
    211 				 * This sort must be stable.
    212 				 */
    213 				mergesort(dpv, n, sizeof(*dpv), alphasort);
    214 
    215 				dpv[n] = NULL;
    216 				xp = NULL;
    217 
    218 				/*
    219 				 * Scan through the buffer in sort order,
    220 				 * zapping the inode number of any
    221 				 * duplicate names.
    222 				 */
    223 				for (n = 0; dpv[n]; n++) {
    224 					struct dirent *dp = dpv[n];
    225 
    226 					if ((xp == NULL) ||
    227 					    strcmp(dp->d_name, xp->d_name))
    228 						xp = dp;
    229 					else
    230 						dp->d_fileno = 0;
    231 					if (dp->d_type == DT_WHT &&
    232 					    (flags & DTF_HIDEW))
    233 						dp->d_fileno = 0;
    234 				}
    235 
    236 				free(dpv);
    237 				break;
    238 			} else {
    239 				dpv = malloc((n+1) * sizeof(struct dirent *));
    240 				if (dpv == NULL)
    241 					break;
    242 			}
    243 		}
    244 
    245 		dirp->dd_len = len;
    246 		dirp->dd_size = ddptr - dirp->dd_buf;
    247 	} else {
    248 		dirp->dd_len = incr;
    249 		dirp->dd_buf = malloc(dirp->dd_len);
    250 		if (dirp->dd_buf == NULL) {
    251 			free(dirp);
    252 			close (fd);
    253 			return (NULL);
    254 		}
    255 		dirp->dd_seek = 0;
    256 		flags &= ~DTF_REWIND;
    257 	}
    258 
    259 	dirp->dd_loc = 0;
    260 	dirp->dd_fd = fd;
    261 	dirp->dd_flags = flags;
    262 
    263 	/*
    264 	 * Set up seek point for rewinddir.
    265 	 */
    266 	dirp->dd_rewind = telldir(dirp);
    267 
    268 	return (dirp);
    269 }
    270