chiark / gitweb /
hashmap.h: fix coding style issue
[elogind.git] / src / shared / mkdir.c
index 3d9822129618e28bd4ad50727536f0c1fa5a41c0..0e51b64f69331d21228b2dcc1621fb29df18f274 100644 (file)
 #include "mkdir.h"
 #include "label.h"
 #include "util.h"
-#include "log.h"
 
-int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
+int mkdir_label(const char *path, mode_t mode) {
+        return label_mkdir(path, mode, true);
+}
+
+static int makedir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, bool apply) {
         struct stat st;
 
-        if (label_mkdir(path, mode) >= 0)
+        if (label_mkdir(path, mode, apply) >= 0)
                 if (chmod_and_chown(path, mode, uid, gid) < 0)
                         return -errno;
 
@@ -52,14 +55,33 @@ int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid) {
         return 0;
 }
 
-int mkdir_parents(const char *path, mode_t mode) {
+int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
+        return makedir_safe(path, mode, uid, gid, false);
+}
+
+int mkdir_safe_label(const char *path, mode_t mode, uid_t uid, gid_t gid) {
+        return makedir_safe(path, mode, uid, gid, true);
+}
+
+static int makedir_parents(const char *path, mode_t mode, bool apply) {
+        struct stat st;
         const char *p, *e;
 
         assert(path);
 
-        /* Creates every parent directory in the path except the last
-         * component. */
+        /* return immediately if directory exists */
+        e = strrchr(path, '/');
+        if (!e)
+                return -EINVAL;
+        p = strndupa(path, e - path);
+        if (stat(p, &st) >= 0) {
+                if ((st.st_mode & S_IFMT) == S_IFDIR)
+                        return 0;
+                else
+                        return -ENOTDIR;
+        }
 
+        /* create every parent directory in the path, except the last component */
         p = path + strspn(path, "/");
         for (;;) {
                 int r;
@@ -73,10 +95,11 @@ int mkdir_parents(const char *path, mode_t mode) {
                 if (*p == 0)
                         return 0;
 
-                if (!(t = strndup(path, e - path)))
+                t = strndup(path, e - path);
+                if (!t)
                         return -ENOMEM;
 
-                r = label_mkdir(t, mode);
+                r = label_mkdir(t, mode, apply);
                 free(t);
 
                 if (r < 0 && errno != EEXIST)
@@ -84,16 +107,41 @@ int mkdir_parents(const char *path, mode_t mode) {
         }
 }
 
-int mkdir_p(const char *path, mode_t mode) {
+int mkdir_parents(const char *path, mode_t mode) {
+        return makedir_parents(path, mode, false);
+}
+
+int mkdir_parents_label(const char *path, mode_t mode) {
+        return makedir_parents(path, mode, true);
+}
+
+static int is_dir(const char* path) {
+        struct stat st;
+        if (stat(path, &st) < 0)
+                return -errno;
+        return S_ISDIR(st.st_mode);
+}
+
+static int makedir_p(const char *path, mode_t mode, bool apply) {
         int r;
 
         /* Like mkdir -p */
 
-        if ((r = mkdir_parents(path, mode)) < 0)
+        r = makedir_parents(path, mode, apply);
+        if (r < 0)
                 return r;
 
-        if (label_mkdir(path, mode) < 0 && errno != EEXIST)
+        r = label_mkdir(path, mode, apply);
+        if (r < 0 && (errno != EEXIST || is_dir(path) <= 0))
                 return -errno;
 
         return 0;
 }
+
+int mkdir_p(const char *path, mode_t mode) {
+        return makedir_p(path, mode, false);
+}
+
+int mkdir_p_label(const char *path, mode_t mode) {
+        return makedir_p(path, mode, true);
+}