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