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