opendir.c.patch   [plain text]


--- opendir.c.orig	2004-11-25 11:38:01.000000000 -0800
+++ opendir.c	2006-05-03 23:21:10.000000000 -0700
@@ -48,6 +48,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <pthread.h>
 #include "un-namespace.h"
 
 #include "telldir.h"
@@ -72,27 +73,15 @@
 	int incr;
 	int saved_errno;
 	int unionstack;
-	struct stat statb;
 
 	/*
-	 * stat() before _open() because opening of special files may be
-	 * harmful.  _fstat() after open because the file may have changed.
+	 * Use O_DIRECTORY to only open directories (because opening of
+	 * special files may be harmful).  errno is set to ENOTDIR if
+	 * not a directory.
 	 */
-	if (stat(name, &statb) != 0)
-		return (NULL);
-	if (!S_ISDIR(statb.st_mode)) {
-		errno = ENOTDIR;
-		return (NULL);
-	}
-	if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
+	if ((fd = _open(name, O_RDONLY | O_NONBLOCK | O_DIRECTORY)) == -1)
 		return (NULL);
 	dirp = NULL;
-	if (_fstat(fd, &statb) != 0)
-		goto fail;
-	if (!S_ISDIR(statb.st_mode)) {
-		errno = ENOTDIR;
-		goto fail;
-	}
 	if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 ||
 	    (dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
 		goto fail;
@@ -269,7 +258,7 @@
 	dirp->dd_loc = 0;
 	dirp->dd_fd = fd;
 	dirp->dd_flags = flags;
-	dirp->dd_lock = NULL;
+	dirp->dd_lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
 
 	/*
 	 * Set up seek point for rewinddir.