[hackers] [sbase] [PATCH 4/5] mkdir -p: Fail if argument exists, but is not a directory

From: Michael Forney <mforney_AT_mforney.org>
Date: Wed, 14 Dec 2016 19:40:05 -0800

If it is a directory, we can just return straightaway.
---
 libutil/mkdirp.c | 9 +++++++++
 1 file changed, 9 insertions(+)
diff --git a/libutil/mkdirp.c b/libutil/mkdirp.c
index 7796e24..2ef94a3 100644
--- a/libutil/mkdirp.c
+++ b/libutil/mkdirp.c
_AT_@ -10,6 +10,15 @@ int
 mkdirp(const char *path)
 {
 	char tmp[PATH_MAX], *p;
+	struct stat st;
+
+	if (stat(path, &st) == 0) {
+		if (S_ISDIR(st.st_mode))
+			return 0;
+		errno = ENOTDIR;
+		weprintf("%s:", path);
+		return -1;
+	}
 
 	estrlcpy(tmp, path, sizeof(tmp));
 	for (p = tmp + (tmp[0] == '/'); *p; p++) {
-- 
2.11.0
Received on Thu Dec 15 2016 - 04:40:05 CET

This archive was generated by hypermail 2.3.0 : Thu Dec 15 2016 - 04:48:30 CET