Home | History | Annotate | Line # | Download | only in gen
opendir.c revision 1.7
      1 /*
      2  * Copyright (c) 1983, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #if defined(LIBC_SCCS) && !defined(lint)
     35 /*static char sccsid[] = "from: @(#)opendir.c	8.7 (Berkeley) 12/10/94";*/
     36 static char rcsid[] = "$Id: opendir.c,v 1.7 1994/12/28 03:34:37 mycroft Exp $";
     37 #endif /* LIBC_SCCS and not lint */
     38 
     39 #include <sys/param.h>
     40 #include <sys/mount.h>
     41 #include <sys/stat.h>
     42 
     43 #include <dirent.h>
     44 #include <errno.h>
     45 #include <fcntl.h>
     46 #include <stdlib.h>
     47 #include <unistd.h>
     48 
     49 /*
     50  * Open a directory.
     51  */
     52 DIR *
     53 opendir(name)
     54 	const char *name;
     55 {
     56 
     57 	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
     58 }
     59 
     60 DIR *
     61 __opendir2(name, flags)
     62 	const char *name;
     63 	int flags;
     64 {
     65 	DIR *dirp;
     66 	int fd;
     67 	struct stat sb;
     68 	int pagesz;
     69 	int incr;
     70 	int unionstack;
     71 
     72 	if ((fd = open(name, O_RDONLY)) == -1)
     73 		return (NULL);
     74 	if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
     75 		errno = ENOTDIR;
     76 		close(fd);
     77 		return (NULL);
     78 	}
     79 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
     80 	    (dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
     81 		close(fd);
     82 		return (NULL);
     83 	}
     84 
     85 	/*
     86 	 * If the machine's page size is an exact multiple of DIRBLKSIZ,
     87 	 * use a buffer that is cluster boundary aligned.
     88 	 * Hopefully this can be a big win someday by allowing page trades
     89 	 * to user space to be done by getdirentries()
     90 	 */
     91 	if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
     92 		incr = pagesz;
     93 	else
     94 		incr = DIRBLKSIZ;
     95 
     96 	/*
     97 	 * Determine whether this directory is the top of a union stack.
     98 	 */
     99 	if (flags & DTF_NODUP) {
    100 		struct statfs sfb;
    101 
    102 		if (fstatfs(fd, &sfb) < 0) {
    103 			free(dirp);
    104 			close(fd);
    105 			return (NULL);
    106 		}
    107 		unionstack = !strcmp(sfb.f_fstypename, MOUNT_UNION);
    108 	} else {
    109 		unionstack = 0;
    110 	}
    111 
    112 	if (unionstack) {
    113 		int len = 0;
    114 		int space = 0;
    115 		char *buf = 0;
    116 		char *ddptr = 0;
    117 		char *ddeptr;
    118 		int n;
    119 		struct dirent **dpv;
    120 
    121 		/*
    122 		 * The strategy here is to read all the directory
    123 		 * entries into a buffer, sort the buffer, and
    124 		 * remove duplicate entries by setting the inode
    125 		 * number to zero.
    126 		 */
    127 
    128 		do {
    129 			/*
    130 			 * Always make at least DIRBLKSIZ bytes
    131 			 * available to getdirentries
    132 			 */
    133 			if (space < DIRBLKSIZ) {
    134 				space += incr;
    135 				len += incr;
    136 				buf = realloc(buf, len);
    137 				if (buf == NULL) {
    138 					free(dirp);
    139 					close(fd);
    140 					return (NULL);
    141 				}
    142 				ddptr = buf + (len - space);
    143 			}
    144 
    145 			n = getdirentries(fd, ddptr, space, &dirp->dd_seek);
    146 			if (n > 0) {
    147 				ddptr += n;
    148 				space -= n;
    149 			}
    150 		} while (n > 0);
    151 
    152 		ddeptr = ddptr;
    153 		flags |= __DTF_READALL;
    154 
    155 		/*
    156 		 * Re-open the directory.
    157 		 * This has the effect of rewinding back to the
    158 		 * top of the union stack and is needed by
    159 		 * programs which plan to fchdir to a descriptor
    160 		 * which has also been read -- see fts.c.
    161 		 */
    162 		if (flags & DTF_REWIND) {
    163 			(void) close(fd);
    164 			if ((fd = open(name, O_RDONLY)) == -1) {
    165 				free(buf);
    166 				free(dirp);
    167 				return (NULL);
    168 			}
    169 		}
    170 
    171 		/*
    172 		 * There is now a buffer full of (possibly) duplicate
    173 		 * names.
    174 		 */
    175 		dirp->dd_buf = buf;
    176 
    177 		/*
    178 		 * Go round this loop twice...
    179 		 *
    180 		 * Scan through the buffer, counting entries.
    181 		 * On the second pass, save pointers to each one.
    182 		 * Then sort the pointers and remove duplicate names.
    183 		 */
    184 		for (dpv = 0;;) {
    185 			for (n = 0, ddptr = buf; ddptr < ddeptr;) {
    186 				struct dirent *dp;
    187 
    188 				dp = (struct dirent *) ddptr;
    189 				if ((long)dp & 03)
    190 					break;
    191 				if ((dp->d_reclen <= 0) ||
    192 				    (dp->d_reclen > (ddeptr + 1 - ddptr)))
    193 					break;
    194 				ddptr += dp->d_reclen;
    195 				if (dp->d_fileno) {
    196 					if (dpv)
    197 						dpv[n] = dp;
    198 					n++;
    199 				}
    200 			}
    201 
    202 			if (dpv) {
    203 				struct dirent *xp;
    204 
    205 				/*
    206 				 * This sort must be stable.
    207 				 */
    208 				mergesort(dpv, n, sizeof(*dpv), alphasort);
    209 
    210 				dpv[n] = NULL;
    211 				xp = NULL;
    212 
    213 				/*
    214 				 * Scan through the buffer in sort order,
    215 				 * zapping the inode number of any
    216 				 * duplicate names.
    217 				 */
    218 				for (n = 0; dpv[n]; n++) {
    219 					struct dirent *dp = dpv[n];
    220 
    221 					if ((xp == NULL) ||
    222 					    strcmp(dp->d_name, xp->d_name))
    223 						xp = dp;
    224 					else
    225 						dp->d_fileno = 0;
    226 					if (dp->d_type == DT_WHT &&
    227 					    (flags & DTF_HIDEW))
    228 						dp->d_fileno = 0;
    229 				}
    230 
    231 				free(dpv);
    232 				break;
    233 			} else {
    234 				dpv = malloc((n+1) * sizeof(struct dirent *));
    235 				if (dpv == NULL)
    236 					break;
    237 			}
    238 		}
    239 
    240 		dirp->dd_len = len;
    241 		dirp->dd_size = ddptr - dirp->dd_buf;
    242 	} else {
    243 		dirp->dd_len = incr;
    244 		dirp->dd_buf = malloc(dirp->dd_len);
    245 		if (dirp->dd_buf == NULL) {
    246 			free(dirp);
    247 			close (fd);
    248 			return (NULL);
    249 		}
    250 		dirp->dd_seek = 0;
    251 		flags &= ~DTF_REWIND;
    252 	}
    253 
    254 	dirp->dd_loc = 0;
    255 	dirp->dd_fd = fd;
    256 	dirp->dd_flags = flags;
    257 
    258 	/*
    259 	 * Set up seek point for rewinddir.
    260 	 */
    261 	dirp->dd_rewind = telldir(dirp);
    262 
    263 	return (dirp);
    264 }
    265