autofs-5.1.9 - dont call mkdir if dir exists

From: Ian Kent <raven@themaw.net>

In mkdir_path(), if the directory already exists don't call do_mkdir().

Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG          |    1 +
 daemon/automount.c |   35 ++++++++++++++++++-----------------
 2 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 7a5dcee1e..04aeda7fe 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -34,6 +34,7 @@
 - fix lock ordering deadlock in expire_cleanup().
 - fix handling of ignored offsets.
 - fix invalidated map entry handling in hosts module.
+- dont call mkdir if dir exists.
 
 02/11/2023 autofs-5.1.9
 - fix kernel mount status notification.
diff --git a/daemon/automount.c b/daemon/automount.c
index 22994defe..8d7ad0c05 100644
--- a/daemon/automount.c
+++ b/daemon/automount.c
@@ -173,15 +173,6 @@ static int do_mkdir(const char *parent, const char *path, mode_t mode)
 	struct stat st, root;
 	struct statfs fs;
 
-	/* If path exists we're done */
-	status = stat(path, &st);
-	if (status == 0) {
-		errno = EEXIST;
-		if (!S_ISDIR(st.st_mode))
-			errno = ENOTDIR;
-		return 0;
-	}
-
 	/*
 	 * We don't want to create the path on a remote file system
 	 * unless it's the root file system.
@@ -229,19 +220,29 @@ int mkdir_path(const char *path, mode_t mode)
 
 	do {
 		if (cp != path && (*cp == '/' || *cp == '\0')) {
+			struct stat st;
+
 			memcpy(bp, lcp, cp - lcp);
 			bp += cp - lcp;
 			*bp = '\0';
-			if (!do_mkdir(parent, buf, mode)) {
-				if (*cp != '\0') {
-					memcpy(pp, lcp, cp - lcp);
-					pp += cp - lcp;
-					*pp = '\0';
-					lcp = cp;
-					continue;
+
+			/* If path exists don't call do_mkdir() */
+			if (!stat(buf, &st)) {
+				if (*cp == '\0') {
+					errno = EEXIST;
+					if (!S_ISDIR(st.st_mode))
+						errno = ENOTDIR;
+					return -1;
 				}
-				return -1;
+				goto next;
+			}
+
+			if (!do_mkdir(parent, buf, mode)) {
+				/* errno will be set in do_mkdir() */
+				if (*cp == '\0')
+					return -1;
 			}
+next:
 			memcpy(pp, lcp, cp - lcp);
 			pp += cp - lcp;
 			*pp = '\0';
