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