[sheepdog] [PATCH 1/2] util: enhance xmkdir

Liu Yuan namei.unix at gmail.com
Fri Apr 19 07:55:54 CEST 2013


From: Liu Yuan <tailai.ly at taobao.com>

When the path exists but is a rgular file, we should also return -1

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 lib/util.c |   17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/lib/util.c b/lib/util.c
index db1d481..61a1cb5 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -232,10 +232,23 @@ ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
 	return total;
 }
 
+/* Return EEXIST when path exists but not a directory */
 int xmkdir(const char *pathname, mode_t mode)
 {
-	if (mkdir(pathname, mode) < 0 && errno != EEXIST)
-		return -1;
+	if (mkdir(pathname, mode) < 0) {
+		struct stat st;
+
+		if (errno != EEXIST)
+			return -1;
+
+		if (stat(pathname, &st) < 0)
+			return -1;
+
+		if (!S_ISDIR(st.st_mode)) {
+			errno = EEXIST;
+			return -1;
+		}
+	}
 	return 0;
 }
 
-- 
1.7.9.5




More information about the sheepdog mailing list