[Midnightbsd-cvs] src [9073] trunk/lib/libc/gen/opendir.c: finally removed the stat and fstat calls from the opendir code.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sat Oct 1 06:17:36 EDT 2016


Revision: 9073
          http://svnweb.midnightbsd.org/src/?rev=9073
Author:   laffer1
Date:     2016-10-01 06:17:36 -0400 (Sat, 01 Oct 2016)
Log Message:
-----------
finally removed the stat and fstat calls from the opendir code.

Modified Paths:
--------------
    trunk/lib/libc/gen/opendir.c

Modified: trunk/lib/libc/gen/opendir.c
===================================================================
--- trunk/lib/libc/gen/opendir.c	2016-10-01 10:17:05 UTC (rev 9072)
+++ trunk/lib/libc/gen/opendir.c	2016-10-01 10:17:36 UTC (rev 9073)
@@ -66,7 +66,17 @@
 DIR *
 fdopendir(int fd)
 {
+	struct stat statb;
 
+	/* Check that fd is associated with a directory. */
+	if (_fstat(fd, &statb) != 0)
+		return (NULL);
+	if (!S_ISDIR(statb.st_mode)) {
+		errno = ENOTDIR;
+		return (NULL);
+	}
+	if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+		return (NULL);
 	return (__opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP));
 }
 
@@ -74,20 +84,10 @@
 __opendir2(const char *name, int flags)
 {
 	int fd;
-	struct stat statb;
 
-	/*
-	 * stat() before _open() because opening of special files may be
-	 * harmful.
-	 */
-	if (stat(name, &statb) != 0)
+	if ((fd = _open(name,
+	    O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC)) == -1)
 		return (NULL);
-	if (!S_ISDIR(statb.st_mode)) {
-		errno = ENOTDIR;
-		return (NULL);
-	}
-	if ((fd = _open(name, O_RDONLY | O_NONBLOCK | O_DIRECTORY)) == -1)
-		return (NULL);
 
 	return __opendir_common(fd, name, flags);
 }
@@ -110,19 +110,9 @@
 	int incr;
 	int saved_errno;
 	int unionstack;
-	struct stat statb;
 
-	dirp = NULL;
-	/* _fstat() the open handler because the file may have changed.  */
-	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;
+	if ((dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL)
+		return (NULL);
 
 	dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR));
 	LIST_INIT(&dirp->dd_td->td_locq);



More information about the Midnightbsd-cvs mailing list