]> git.sur5r.net Git - i3/i3/blobdiff - libi3/mkdirp.c
Merge pull request #3444 from orestisf1993/move
[i3/i3] / libi3 / mkdirp.c
index a0d35f961dbfcbd35687f051bcdd010ffc812282..f5281bd74164c5e1268a2cd3f3925ff1a9d28063 100644 (file)
@@ -1,4 +1,12 @@
+/*
+ * vim:ts=4:sw=4:expandtab
+ *
+ * i3 - an improved dynamic tiling window manager
+ * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
+ *
+ */
 #include "libi3.h"
+
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
  * Emulates mkdir -p (creates any missing folders)
  *
  */
-bool mkdirp(const char *path) {
-    if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0)
-        return true;
-    if (errno != ENOENT) {
+
+#if !defined(__sun)
+int mkdirp(const char *path, mode_t mode) {
+    if (mkdir(path, mode) == 0)
+        return 0;
+    if (errno == EEXIST) {
+        struct stat st;
+        /* Check that the named file actually is a directory. */
+        if (stat(path, &st)) {
+            ELOG("stat(%s) failed: %s\n", path, strerror(errno));
+            return -1;
+        }
+        if (!S_ISDIR(st.st_mode)) {
+            ELOG("mkdir(%s) failed: %s\n", path, strerror(ENOTDIR));
+            return -1;
+        }
+        return 0;
+    } else if (errno != ENOENT) {
         ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
-        return false;
+        return -1;
     }
     char *copy = sstrdup(path);
     /* strip trailing slashes, if any */
@@ -22,17 +44,15 @@ bool mkdirp(const char *path) {
 
     char *sep = strrchr(copy, '/');
     if (sep == NULL) {
-        if (copy != NULL) {
-            free(copy);
-            copy = NULL;
-        }
-        return false;
+        free(copy);
+        return -1;
     }
     *sep = '\0';
-    bool result = false;
-    if (mkdirp(copy))
-        result = mkdirp(path);
+    int result = -1;
+    if (mkdirp(copy, mode) == 0)
+        result = mkdirp(path, mode);
     free(copy);
 
     return result;
 }
+#endif