xref: /minix3/lib/libc/gen/initdir.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1f14fb602SLionel Sambuc /*	$NetBSD: initdir.c,v 1.3 2012/03/13 21:13:36 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1983, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras  *    without specific prior written permission.
182fe8fb19SBen Gras  *
192fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras  * SUCH DAMAGE.
302fe8fb19SBen Gras  */
312fe8fb19SBen Gras 
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34f14fb602SLionel Sambuc __RCSID("$NetBSD: initdir.c,v 1.3 2012/03/13 21:13:36 christos Exp $");
352fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras #include "namespace.h"
382fe8fb19SBen Gras #include "reentrant.h"
392fe8fb19SBen Gras #include "extern.h"
402fe8fb19SBen Gras 
412fe8fb19SBen Gras #include <sys/param.h>
422fe8fb19SBen Gras 
432fe8fb19SBen Gras #include <assert.h>
442fe8fb19SBen Gras #include <dirent.h>
452fe8fb19SBen Gras #include <errno.h>
462fe8fb19SBen Gras #include <fcntl.h>
472fe8fb19SBen Gras #include <stdlib.h>
482fe8fb19SBen Gras #include <string.h>
492fe8fb19SBen Gras #include <unistd.h>
502fe8fb19SBen Gras 
512fe8fb19SBen Gras #include "dirent_private.h"
522fe8fb19SBen Gras 
532fe8fb19SBen Gras #define	MAXITERATIONS	100
542fe8fb19SBen Gras 
552fe8fb19SBen Gras int
_initdir(DIR * dirp,int fd,const char * name)562fe8fb19SBen Gras _initdir(DIR *dirp, int fd, const char *name)
572fe8fb19SBen Gras {
582fe8fb19SBen Gras 	int flags = dirp->dd_flags;
592fe8fb19SBen Gras 	int pagesz;
602fe8fb19SBen Gras 	int incr;
612fe8fb19SBen Gras 
622fe8fb19SBen Gras 	/*
632fe8fb19SBen Gras 	 * If the machine's page size is an exact multiple of DIRBLKSIZ,
642fe8fb19SBen Gras 	 * use a buffer that is cluster boundary aligned.
652fe8fb19SBen Gras 	 * Hopefully this can be a big win someday by allowing page trades
662fe8fb19SBen Gras 	 * to user space to be done by getdents()
672fe8fb19SBen Gras 	 */
682fe8fb19SBen Gras 	if (((pagesz = getpagesize()) % DIRBLKSIZ) == 0)
692fe8fb19SBen Gras 		incr = pagesz;
702fe8fb19SBen Gras 	else
712fe8fb19SBen Gras 		incr = DIRBLKSIZ;
722fe8fb19SBen Gras 
732fe8fb19SBen Gras 	if ((flags & DTF_REWIND) && name == NULL) {
742fe8fb19SBen Gras 		return EINVAL;
752fe8fb19SBen Gras 	}
762fe8fb19SBen Gras 	if ((flags & __DTF_READALL) != 0) {
772fe8fb19SBen Gras 		size_t len;
782fe8fb19SBen Gras 		size_t space;
792fe8fb19SBen Gras 		char *buf, *nbuf;
802fe8fb19SBen Gras 		char *ddptr;
812fe8fb19SBen Gras 		char *ddeptr;
822fe8fb19SBen Gras 		int n;
832fe8fb19SBen Gras 		struct dirent **dpv;
842fe8fb19SBen Gras 		int i;
852fe8fb19SBen Gras 
862fe8fb19SBen Gras 		/*
872fe8fb19SBen Gras 		 * The strategy here for directories on top of a union stack
882fe8fb19SBen Gras 		 * is to read all the directory entries into a buffer, sort
892fe8fb19SBen Gras 		 * the buffer, and remove duplicate entries by setting the
902fe8fb19SBen Gras 		 * inode number to zero.
912fe8fb19SBen Gras 		 *
922fe8fb19SBen Gras 		 * For directories on an NFS mounted filesystem, we try
932fe8fb19SBen Gras 	 	 * to get a consistent snapshot by trying until we have
942fe8fb19SBen Gras 		 * successfully read all of the directory without errors
952fe8fb19SBen Gras 		 * (i.e. 'bad cookie' errors from the server because
962fe8fb19SBen Gras 		 * the directory was modified). These errors should not
972fe8fb19SBen Gras 		 * happen often, but need to be dealt with.
982fe8fb19SBen Gras 		 */
992fe8fb19SBen Gras 		i = 0;
1002fe8fb19SBen Gras retry:
1012fe8fb19SBen Gras 		len = 0;
1022fe8fb19SBen Gras 		space = 0;
1032fe8fb19SBen Gras 		buf = 0;
1042fe8fb19SBen Gras 		ddptr = 0;
1052fe8fb19SBen Gras 
1062fe8fb19SBen Gras 		do {
1072fe8fb19SBen Gras 			/*
1082fe8fb19SBen Gras 			 * Always make at least DIRBLKSIZ bytes
1092fe8fb19SBen Gras 			 * available to getdents
1102fe8fb19SBen Gras 			 */
1112fe8fb19SBen Gras 			if (space < DIRBLKSIZ) {
1122fe8fb19SBen Gras 				space += incr;
1132fe8fb19SBen Gras 				len += incr;
1142fe8fb19SBen Gras 				nbuf = realloc(buf, len);
1152fe8fb19SBen Gras 				if (nbuf == NULL) {
1162fe8fb19SBen Gras 					dirp->dd_buf = buf;
1172fe8fb19SBen Gras 					return errno;
1182fe8fb19SBen Gras 				}
1192fe8fb19SBen Gras 				buf = nbuf;
1202fe8fb19SBen Gras 				ddptr = buf + (len - space);
1212fe8fb19SBen Gras 			}
1222fe8fb19SBen Gras 
1232fe8fb19SBen Gras 			dirp->dd_seek = lseek(fd, (off_t)0, SEEK_CUR);
1242fe8fb19SBen Gras 			n = getdents(fd, ddptr, space);
1252fe8fb19SBen Gras 			/*
1262fe8fb19SBen Gras 			 * For NFS: EINVAL means a bad cookie error
1272fe8fb19SBen Gras 			 * from the server. Keep trying to get a
1282fe8fb19SBen Gras 			 * consistent view, in this case this means
1292fe8fb19SBen Gras 			 * starting all over again.
1302fe8fb19SBen Gras 			 */
1312fe8fb19SBen Gras 			if (n == -1 && errno == EINVAL &&
1322fe8fb19SBen Gras 			    (flags & __DTF_RETRY_ON_BADCOOKIE) != 0) {
1332fe8fb19SBen Gras 				free(buf);
1342fe8fb19SBen Gras 				lseek(fd, (off_t)0, SEEK_SET);
1352fe8fb19SBen Gras 				if (++i > MAXITERATIONS)
1362fe8fb19SBen Gras 					return EINVAL;
1372fe8fb19SBen Gras 				goto retry;
1382fe8fb19SBen Gras 			}
1392fe8fb19SBen Gras 			if (n > 0) {
1402fe8fb19SBen Gras 				ddptr += n;
1412fe8fb19SBen Gras 				space -= n;
1422fe8fb19SBen Gras 			}
1432fe8fb19SBen Gras 		} while (n > 0);
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras 		ddeptr = ddptr;
1462fe8fb19SBen Gras 
1472fe8fb19SBen Gras 		/*
1482fe8fb19SBen Gras 		 * Re-open the directory.
1492fe8fb19SBen Gras 		 * This has the effect of rewinding back to the
1502fe8fb19SBen Gras 		 * top of the union stack and is needed by
1512fe8fb19SBen Gras 		 * programs which plan to fchdir to a descriptor
1522fe8fb19SBen Gras 		 * which has also been read -- see fts.c.
1532fe8fb19SBen Gras 		 */
1542fe8fb19SBen Gras 		if (flags & DTF_REWIND) {
1552fe8fb19SBen Gras 			(void) close(fd);
156f14fb602SLionel Sambuc 			if ((fd = open(name, O_RDONLY | O_CLOEXEC)) == -1) {
1572fe8fb19SBen Gras 				dirp->dd_buf = buf;
1582fe8fb19SBen Gras 				return errno;
1592fe8fb19SBen Gras 			}
1602fe8fb19SBen Gras 		}
1612fe8fb19SBen Gras 
1622fe8fb19SBen Gras 		/*
1632fe8fb19SBen Gras 		 * There is now a buffer full of (possibly) duplicate
1642fe8fb19SBen Gras 		 * names.
1652fe8fb19SBen Gras 		 */
1662fe8fb19SBen Gras 		dirp->dd_buf = buf;
1672fe8fb19SBen Gras 
1682fe8fb19SBen Gras 		/*
1692fe8fb19SBen Gras 		 * Go round this loop twice...
1702fe8fb19SBen Gras 		 *
1712fe8fb19SBen Gras 		 * Scan through the buffer, counting entries.
1722fe8fb19SBen Gras 		 * On the second pass, save pointers to each one.
1732fe8fb19SBen Gras 		 * Then sort the pointers and remove duplicate names.
1742fe8fb19SBen Gras 		 */
1752fe8fb19SBen Gras 		if ((flags & DTF_NODUP) != 0) {
1762fe8fb19SBen Gras 			for (dpv = 0;;) {
1772fe8fb19SBen Gras 				for (n = 0, ddptr = buf; ddptr < ddeptr;) {
1782fe8fb19SBen Gras 					struct dirent *dp;
1792fe8fb19SBen Gras 
1802fe8fb19SBen Gras 					dp = (struct dirent *)(void *)ddptr;
1812fe8fb19SBen Gras 					if ((long)dp & _DIRENT_ALIGN(dp))
1822fe8fb19SBen Gras 						break;
1832fe8fb19SBen Gras 					/*
1842fe8fb19SBen Gras 					 * d_reclen is unsigned,
1852fe8fb19SBen Gras 					 * so no need to compare <= 0
1862fe8fb19SBen Gras 					 */
1872fe8fb19SBen Gras 					if (dp->d_reclen > (ddeptr + 1 - ddptr))
1882fe8fb19SBen Gras 						break;
1892fe8fb19SBen Gras 					ddptr += dp->d_reclen;
1902fe8fb19SBen Gras 					if (dp->d_fileno) {
1912fe8fb19SBen Gras 						if (dpv)
1922fe8fb19SBen Gras 							dpv[n] = dp;
1932fe8fb19SBen Gras 						n++;
1942fe8fb19SBen Gras 					}
1952fe8fb19SBen Gras 				}
1962fe8fb19SBen Gras 
1972fe8fb19SBen Gras 				if (dpv) {
1982fe8fb19SBen Gras 					struct dirent *xp;
1992fe8fb19SBen Gras 
2002fe8fb19SBen Gras 					/*
2012fe8fb19SBen Gras 					 * This sort must be stable.
2022fe8fb19SBen Gras 					 */
2032fe8fb19SBen Gras 					mergesort(dpv, (size_t)n, sizeof(*dpv),
2042fe8fb19SBen Gras 					    alphasort);
2052fe8fb19SBen Gras 
2062fe8fb19SBen Gras 					dpv[n] = NULL;
2072fe8fb19SBen Gras 					xp = NULL;
2082fe8fb19SBen Gras 
2092fe8fb19SBen Gras 					/*
2102fe8fb19SBen Gras 					 * Scan through the buffer in sort
2112fe8fb19SBen Gras 					 * order, zapping the inode number
2122fe8fb19SBen Gras 					 * of any duplicate names.
2132fe8fb19SBen Gras 					 */
2142fe8fb19SBen Gras 					for (n = 0; dpv[n]; n++) {
2152fe8fb19SBen Gras 						struct dirent *dp = dpv[n];
2162fe8fb19SBen Gras 
2172fe8fb19SBen Gras 						if ((xp == NULL) ||
2182fe8fb19SBen Gras 						    strcmp(dp->d_name,
2192fe8fb19SBen Gras 						      xp->d_name))
2202fe8fb19SBen Gras 							xp = dp;
2212fe8fb19SBen Gras 						else
2222fe8fb19SBen Gras 							dp->d_fileno = 0;
223*84d9c625SLionel Sambuc #if !defined(__minix)
2242fe8fb19SBen Gras 						if (dp->d_type == DT_WHT &&
2252fe8fb19SBen Gras 						    (flags & DTF_HIDEW))
2262fe8fb19SBen Gras 							dp->d_fileno = 0;
227*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
2282fe8fb19SBen Gras 					}
2292fe8fb19SBen Gras 
2302fe8fb19SBen Gras 					free(dpv);
2312fe8fb19SBen Gras 					break;
2322fe8fb19SBen Gras 				} else {
2332fe8fb19SBen Gras 					dpv = malloc((n + 1) *
2342fe8fb19SBen Gras 					    sizeof(struct dirent *));
2352fe8fb19SBen Gras 					if (dpv == NULL)
2362fe8fb19SBen Gras 						break;
2372fe8fb19SBen Gras 				}
2382fe8fb19SBen Gras 			}
2392fe8fb19SBen Gras 		}
2402fe8fb19SBen Gras 
241f14fb602SLionel Sambuc 		_DIAGASSERT(__type_fit(int, len));
242f14fb602SLionel Sambuc 		dirp->dd_len = (int)len;
2432fe8fb19SBen Gras 		dirp->dd_size = ddptr - dirp->dd_buf;
2442fe8fb19SBen Gras 	} else {
2452fe8fb19SBen Gras 		dirp->dd_len = incr;
2462fe8fb19SBen Gras 		dirp->dd_buf = malloc((size_t)dirp->dd_len);
2472fe8fb19SBen Gras 		if (dirp->dd_buf == NULL)
2482fe8fb19SBen Gras 			return errno;
2492fe8fb19SBen Gras 		dirp->dd_seek = 0;
2502fe8fb19SBen Gras 		flags &= ~DTF_REWIND;
2512fe8fb19SBen Gras 	}
2522fe8fb19SBen Gras 	dirp->dd_loc = 0;
2532fe8fb19SBen Gras 	dirp->dd_fd = fd;
2542fe8fb19SBen Gras 	dirp->dd_flags = flags;
2552fe8fb19SBen Gras 	/*
2562fe8fb19SBen Gras 	 * Set up seek point for rewinddir.
2572fe8fb19SBen Gras 	 */
2582fe8fb19SBen Gras 	(void)_telldir_unlocked(dirp);
2592fe8fb19SBen Gras 	return 0;
2602fe8fb19SBen Gras }
2612fe8fb19SBen Gras 
2622fe8fb19SBen Gras void
_finidir(DIR * dirp)2632fe8fb19SBen Gras _finidir(DIR *dirp)
2642fe8fb19SBen Gras {
2652fe8fb19SBen Gras 	struct dirpos *poslist;
2662fe8fb19SBen Gras 
2672fe8fb19SBen Gras 	free(dirp->dd_buf);
2682fe8fb19SBen Gras 
2692fe8fb19SBen Gras 	/* free seekdir/telldir storage */
2702fe8fb19SBen Gras 	for (poslist = dirp->dd_internal; poslist; ) {
2712fe8fb19SBen Gras 		struct dirpos *nextpos = poslist->dp_next;
2722fe8fb19SBen Gras 		free(poslist);
2732fe8fb19SBen Gras 		poslist = nextpos;
2742fe8fb19SBen Gras 	}
2752fe8fb19SBen Gras 	dirp->dd_internal = NULL;
2762fe8fb19SBen Gras }
277