Home | History | Annotate | Line # | Download | only in gen
opendir.c revision 1.31
      1  1.31  christos /*	$NetBSD: opendir.c,v 1.31 2006/05/17 20:36:50 christos Exp $	*/
      2  1.28  christos 
      3  1.28  christos /*
      4  1.28  christos  * Copyright (c) 1983, 1993
      5  1.28  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.28  christos  *
      7  1.28  christos  * Redistribution and use in source and binary forms, with or without
      8  1.28  christos  * modification, are permitted provided that the following conditions
      9  1.28  christos  * are met:
     10  1.28  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.28  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.28  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.28  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.28  christos  *    documentation and/or other materials provided with the distribution.
     15  1.28  christos  * 3. Neither the name of the University nor the names of its contributors
     16  1.28  christos  *    may be used to endorse or promote products derived from this software
     17  1.28  christos  *    without specific prior written permission.
     18  1.28  christos  *
     19  1.28  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.28  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.28  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.28  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.28  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.28  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.28  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.28  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.28  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.28  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.28  christos  * SUCH DAMAGE.
     30  1.28  christos  */
     31  1.28  christos 
     32  1.28  christos #include <sys/cdefs.h>
     33  1.28  christos #if defined(LIBC_SCCS) && !defined(lint)
     34  1.28  christos #if 0
     35  1.28  christos static char sccsid[] = "@(#)opendir.c	8.7 (Berkeley) 12/10/94";
     36  1.28  christos #else
     37  1.31  christos __RCSID("$NetBSD: opendir.c,v 1.31 2006/05/17 20:36:50 christos Exp $");
     38  1.28  christos #endif
     39  1.28  christos #endif /* LIBC_SCCS and not lint */
     40   1.1       cgd 
     41  1.14       jtc #include "namespace.h"
     42  1.28  christos #include "reentrant.h"
     43  1.30  christos #include "extern.h"
     44  1.28  christos #include <sys/param.h>
     45  1.28  christos #include <sys/mount.h>
     46   1.6   mycroft #include <sys/stat.h>
     47   1.5   mycroft 
     48  1.28  christos #include <assert.h>
     49  1.28  christos #include <dirent.h>
     50  1.28  christos #include <errno.h>
     51  1.28  christos #include <fcntl.h>
     52  1.28  christos #include <stdlib.h>
     53  1.28  christos #include <string.h>
     54  1.28  christos #include <unistd.h>
     55  1.28  christos 
     56  1.31  christos #include "dirent_private.h"
     57  1.31  christos 
     58  1.28  christos /*
     59  1.28  christos  * Open a directory.
     60  1.28  christos  */
     61  1.28  christos DIR *
     62  1.31  christos opendir(const char *name)
     63  1.28  christos {
     64  1.28  christos 
     65  1.28  christos 	_DIAGASSERT(name != NULL);
     66  1.28  christos 
     67  1.28  christos 	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
     68  1.28  christos }
     69  1.28  christos 
     70  1.28  christos DIR *
     71  1.31  christos __opendir2(const char *name, int flags)
     72  1.28  christos {
     73  1.28  christos 	DIR *dirp = NULL;
     74  1.28  christos 	int fd;
     75  1.28  christos 	int serrno;
     76  1.28  christos 	struct stat sb;
     77  1.28  christos 	int pagesz;
     78  1.28  christos 	int incr;
     79  1.28  christos 	int unionstack, nfsdir;
     80  1.28  christos 	struct statvfs sfb;
     81  1.28  christos 
     82  1.28  christos 	_DIAGASSERT(name != NULL);
     83  1.28  christos 
     84  1.28  christos 	if ((fd = open(name, O_RDONLY | O_NONBLOCK)) == -1 ||
     85  1.28  christos 	    fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
     86  1.28  christos 		goto error;
     87  1.28  christos 	if (fstat(fd, &sb) || !S_ISDIR(sb.st_mode)) {
     88  1.28  christos 		errno = ENOTDIR;
     89  1.28  christos 		goto error;
     90  1.28  christos 	}
     91  1.28  christos 	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL)
     92  1.28  christos 		goto error;
     93  1.28  christos 	dirp->dd_buf = NULL;
     94  1.28  christos 
     95  1.28  christos 	/*
     96  1.28  christos 	 * If the machine's page size is an exact multiple of DIRBLKSIZ,
     97  1.28  christos 	 * use a buffer that is cluster boundary aligned.
     98  1.28  christos 	 * Hopefully this can be a big win someday by allowing page trades
     99  1.28  christos 	 * to user space to be done by getdirentries()
    100  1.28  christos 	 */
    101  1.28  christos 	if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
    102  1.28  christos 		incr = pagesz;
    103  1.28  christos 	else
    104  1.28  christos 		incr = DIRBLKSIZ;
    105  1.28  christos 
    106  1.28  christos 	/*
    107  1.28  christos 	 * Determine whether this directory is the top of a union stack.
    108  1.28  christos 	 */
    109  1.28  christos 
    110  1.28  christos 	if (fstatvfs1(fd, &sfb, ST_NOWAIT) < 0)
    111  1.28  christos 		goto error;
    112  1.28  christos 
    113  1.28  christos 	if (flags & DTF_NODUP)
    114  1.28  christos 		unionstack = !(strncmp(sfb.f_fstypename, MOUNT_UNION,
    115  1.28  christos 		    MFSNAMELEN)) || (sfb.f_flag & MNT_UNION);
    116  1.28  christos 	else
    117  1.28  christos 		unionstack = 0;
    118  1.28  christos 
    119  1.28  christos 	nfsdir = !(strncmp(sfb.f_fstypename, MOUNT_NFS, MFSNAMELEN));
    120  1.28  christos 
    121  1.28  christos 	if (unionstack || nfsdir) {
    122  1.28  christos 		size_t len;
    123  1.28  christos 		size_t space;
    124  1.28  christos 		char *buf, *nbuf;
    125  1.28  christos 		char *ddptr;
    126  1.28  christos 		char *ddeptr;
    127  1.28  christos 		int n;
    128  1.28  christos 		struct dirent **dpv;
    129  1.28  christos 
    130  1.28  christos 		/*
    131  1.28  christos 		 * The strategy here for directories on top of a union stack
    132  1.28  christos 		 * is to read all the directory entries into a buffer, sort
    133  1.28  christos 		 * the buffer, and remove duplicate entries by setting the
    134  1.28  christos 		 * inode number to zero.
    135  1.28  christos 		 *
    136  1.28  christos 		 * For directories on an NFS mounted filesystem, we try
    137  1.28  christos 	 	 * to get a consistent snapshot by trying until we have
    138  1.28  christos 		 * successfully read all of the directory without errors
    139  1.28  christos 		 * (i.e. 'bad cookie' errors from the server because
    140  1.28  christos 		 * the directory was modified). These errors should not
    141  1.28  christos 		 * happen often, but need to be dealt with.
    142  1.28  christos 		 */
    143  1.28  christos retry:
    144  1.28  christos 		len = 0;
    145  1.28  christos 		space = 0;
    146  1.28  christos 		buf = 0;
    147  1.28  christos 		ddptr = 0;
    148  1.28  christos 
    149  1.28  christos 		do {
    150  1.28  christos 			/*
    151  1.28  christos 			 * Always make at least DIRBLKSIZ bytes
    152  1.28  christos 			 * available to getdirentries
    153  1.28  christos 			 */
    154  1.28  christos 			if (space < DIRBLKSIZ) {
    155  1.28  christos 				space += incr;
    156  1.28  christos 				len += incr;
    157  1.28  christos 				nbuf = realloc(buf, len);
    158  1.28  christos 				if (nbuf == NULL) {
    159  1.28  christos 					dirp->dd_buf = buf;
    160  1.28  christos 					goto error;
    161  1.28  christos 				}
    162  1.28  christos 				buf = nbuf;
    163  1.28  christos 				ddptr = buf + (len - space);
    164  1.28  christos 			}
    165  1.28  christos 
    166  1.28  christos 			dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR);
    167  1.28  christos 			n = getdents(fd, ddptr, space);
    168  1.28  christos 			/*
    169  1.28  christos 			 * For NFS: EINVAL means a bad cookie error
    170  1.28  christos 			 * from the server. Keep trying to get a
    171  1.28  christos 			 * consistent view, in this case this means
    172  1.28  christos 			 * starting all over again.
    173  1.28  christos 			 */
    174  1.28  christos 			if (n == -1 && errno == EINVAL && nfsdir) {
    175  1.28  christos 				free(buf);
    176  1.28  christos 				lseek(fd, (off_t)0, SEEK_SET);
    177  1.28  christos 				goto retry;
    178  1.28  christos 			}
    179  1.28  christos 			if (n > 0) {
    180  1.28  christos 				ddptr += n;
    181  1.28  christos 				space -= n;
    182  1.28  christos 			}
    183  1.28  christos 		} while (n > 0);
    184  1.28  christos 
    185  1.28  christos 		ddeptr = ddptr;
    186  1.28  christos 		flags |= __DTF_READALL;
    187  1.28  christos 
    188  1.28  christos 		/*
    189  1.28  christos 		 * Re-open the directory.
    190  1.28  christos 		 * This has the effect of rewinding back to the
    191  1.28  christos 		 * top of the union stack and is needed by
    192  1.28  christos 		 * programs which plan to fchdir to a descriptor
    193  1.28  christos 		 * which has also been read -- see fts.c.
    194  1.28  christos 		 */
    195  1.28  christos 		if (flags & DTF_REWIND) {
    196  1.28  christos 			(void) close(fd);
    197  1.28  christos 			if ((fd = open(name, O_RDONLY)) == -1 ||
    198  1.28  christos 			    fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
    199  1.28  christos 				dirp->dd_buf = buf;
    200  1.28  christos 				goto error;
    201  1.28  christos 			}
    202  1.28  christos 		}
    203  1.28  christos 
    204  1.28  christos 		/*
    205  1.28  christos 		 * There is now a buffer full of (possibly) duplicate
    206  1.28  christos 		 * names.
    207  1.28  christos 		 */
    208  1.28  christos 		dirp->dd_buf = buf;
    209  1.28  christos 
    210  1.28  christos 		/*
    211  1.28  christos 		 * Go round this loop twice...
    212  1.28  christos 		 *
    213  1.28  christos 		 * Scan through the buffer, counting entries.
    214  1.28  christos 		 * On the second pass, save pointers to each one.
    215  1.28  christos 		 * Then sort the pointers and remove duplicate names.
    216  1.28  christos 		 */
    217  1.28  christos 		if (!nfsdir) {
    218  1.28  christos 			for (dpv = 0;;) {
    219  1.28  christos 				for (n = 0, ddptr = buf; ddptr < ddeptr;) {
    220  1.28  christos 					struct dirent *dp;
    221  1.28  christos 
    222  1.28  christos 					dp = (struct dirent *)(void *)ddptr;
    223  1.28  christos 					if ((long)dp & _DIRENT_ALIGN(dp))
    224  1.28  christos 						break;
    225  1.28  christos 					/*
    226  1.28  christos 					 * d_reclen is unsigned,
    227  1.28  christos 					 * so no need to compare <= 0
    228  1.28  christos 					 */
    229  1.28  christos 					if (dp->d_reclen > (ddeptr + 1 - ddptr))
    230  1.28  christos 						break;
    231  1.28  christos 					ddptr += dp->d_reclen;
    232  1.28  christos 					if (dp->d_fileno) {
    233  1.28  christos 						if (dpv)
    234  1.28  christos 							dpv[n] = dp;
    235  1.28  christos 						n++;
    236  1.28  christos 					}
    237  1.28  christos 				}
    238  1.28  christos 
    239  1.28  christos 				if (dpv) {
    240  1.28  christos 					struct dirent *xp;
    241  1.28  christos 
    242  1.28  christos 					/*
    243  1.28  christos 					 * This sort must be stable.
    244  1.28  christos 					 */
    245  1.28  christos 					mergesort(dpv, (size_t)n, sizeof(*dpv),
    246  1.28  christos 					    alphasort);
    247  1.28  christos 
    248  1.28  christos 					dpv[n] = NULL;
    249  1.28  christos 					xp = NULL;
    250  1.28  christos 
    251  1.28  christos 					/*
    252  1.28  christos 					 * Scan through the buffer in sort
    253  1.28  christos 					 * order, zapping the inode number
    254  1.28  christos 					 * of any duplicate names.
    255  1.28  christos 					 */
    256  1.28  christos 					for (n = 0; dpv[n]; n++) {
    257  1.28  christos 						struct dirent *dp = dpv[n];
    258  1.28  christos 
    259  1.28  christos 						if ((xp == NULL) ||
    260  1.28  christos 						    strcmp(dp->d_name,
    261  1.28  christos 						      xp->d_name))
    262  1.28  christos 							xp = dp;
    263  1.28  christos 						else
    264  1.28  christos 							dp->d_fileno = 0;
    265  1.28  christos 						if (dp->d_type == DT_WHT &&
    266  1.28  christos 						    (flags & DTF_HIDEW))
    267  1.28  christos 							dp->d_fileno = 0;
    268  1.28  christos 					}
    269  1.28  christos 
    270  1.28  christos 					free(dpv);
    271  1.28  christos 					break;
    272  1.28  christos 				} else {
    273  1.28  christos 					dpv = malloc((n + 1) *
    274  1.28  christos 					    sizeof(struct dirent *));
    275  1.28  christos 					if (dpv == NULL)
    276  1.28  christos 						break;
    277  1.28  christos 				}
    278  1.28  christos 			}
    279  1.28  christos 		}
    280  1.28  christos 
    281  1.28  christos 		dirp->dd_len = len;
    282  1.28  christos 		dirp->dd_size = ddptr - dirp->dd_buf;
    283  1.28  christos 	} else {
    284  1.28  christos 		dirp->dd_len = incr;
    285  1.28  christos 		dirp->dd_buf = malloc((size_t)dirp->dd_len);
    286  1.28  christos 		if (dirp->dd_buf == NULL)
    287  1.28  christos 			goto error;
    288  1.28  christos 		dirp->dd_seek = 0;
    289  1.28  christos 		flags &= ~DTF_REWIND;
    290  1.28  christos 	}
    291  1.28  christos 
    292  1.28  christos 	dirp->dd_loc = 0;
    293  1.28  christos 	dirp->dd_fd = fd;
    294  1.28  christos 	dirp->dd_flags = flags;
    295   1.5   mycroft 
    296  1.28  christos 	/*
    297  1.28  christos 	 * Set up seek point for rewinddir.
    298  1.28  christos 	 */
    299  1.28  christos #ifdef _REENTRANT
    300  1.28  christos 	if (__isthreaded) {
    301  1.28  christos 		if ((dirp->dd_lock = malloc(sizeof(mutex_t))) == NULL)
    302  1.28  christos 			goto error;
    303  1.28  christos 		mutex_init((mutex_t *)dirp->dd_lock, NULL);
    304  1.28  christos 	}
    305  1.28  christos #endif
    306  1.31  christos 	dirp->dd_internal = NULL;
    307  1.31  christos 	(void)_telldir_unlocked(dirp);
    308  1.28  christos 	return (dirp);
    309  1.28  christos error:
    310  1.28  christos 	serrno = errno;
    311  1.28  christos 	if (dirp && dirp->dd_buf)
    312  1.28  christos 		free(dirp->dd_buf);
    313  1.28  christos 	if (dirp)
    314  1.28  christos 		free(dirp);
    315  1.28  christos 	if (fd != -1)
    316  1.28  christos 		(void)close(fd);
    317  1.28  christos 	errno = serrno;
    318  1.28  christos 	return NULL;
    319  1.28  christos }
    320