]> git.sur5r.net Git - i3/i3/commitdiff
Merge pull request #1606 from Deiz/libi3-mkdirp
authorMichael Stapelberg <stapelberg@users.noreply.github.com>
Sun, 29 Mar 2015 21:36:28 +0000 (23:36 +0200)
committerMichael Stapelberg <stapelberg@users.noreply.github.com>
Sun, 29 Mar 2015 21:36:28 +0000 (23:36 +0200)
Move mkdirp into libi3

include/ipc.h
include/libi3.h
libi3/mkdirp.c [new file with mode: 0644]
src/ipc.c

index 96a60a1f29d27ea7f3b9ea719269ea269fef35f5..4eed319a21e86c7a95765c96c03974e9d6c6b7c5 100644 (file)
@@ -50,12 +50,6 @@ typedef void (*handler_t)(int, uint8_t *, int, uint32_t, uint32_t);
                               int size, uint32_t message_size, \
                               uint32_t message_type)
 
-/**
- * Emulates mkdir -p (creates any missing folders)
- *
- */
-bool mkdirp(const char *path);
-
 /**
  * Handler for activity on the listening socket, meaning that a new client
  * has just connected and we should accept() him. Sets up the event handler
index 82c46fcea645a59ac44cad99bdac70ffb62f0e48..da8c8a42377d7ec09748c648cd0f88030dcf146b 100644 (file)
@@ -452,3 +452,9 @@ char *resolve_tilde(const char *path);
  *
  */
 char *get_config_path(const char *override_configpath, bool use_system_paths);
+
+/**
+ * Emulates mkdir -p (creates any missing folders)
+ *
+ */
+bool mkdirp(const char *path);
diff --git a/libi3/mkdirp.c b/libi3/mkdirp.c
new file mode 100644 (file)
index 0000000..a0d35f9
--- /dev/null
@@ -0,0 +1,38 @@
+#include "libi3.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.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) {
+        ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
+        return false;
+    }
+    char *copy = sstrdup(path);
+    /* strip trailing slashes, if any */
+    while (copy[strlen(copy) - 1] == '/')
+        copy[strlen(copy) - 1] = '\0';
+
+    char *sep = strrchr(copy, '/');
+    if (sep == NULL) {
+        if (copy != NULL) {
+            free(copy);
+            copy = NULL;
+        }
+        return false;
+    }
+    *sep = '\0';
+    bool result = false;
+    if (mkdirp(copy))
+        result = mkdirp(path);
+    free(copy);
+
+    return result;
+}
index 52f7db2efe432c50fedecefb0940b8404b8ef2b2..8fed75f1b13dbf3993d7ac4c7dbbfa80bf4fbd58 100644 (file)
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -37,36 +37,6 @@ static void set_nonblock(int sockfd) {
         err(-1, "Could not set O_NONBLOCK");
 }
 
-/*
- * 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) {
-        ELOG("mkdir(%s) failed: %s\n", path, strerror(errno));
-        return false;
-    }
-    char *copy = sstrdup(path);
-    /* strip trailing slashes, if any */
-    while (copy[strlen(copy) - 1] == '/')
-        copy[strlen(copy) - 1] = '\0';
-
-    char *sep = strrchr(copy, '/');
-    if (sep == NULL) {
-        FREE(copy);
-        return false;
-    }
-    *sep = '\0';
-    bool result = false;
-    if (mkdirp(copy))
-        result = mkdirp(path);
-    free(copy);
-
-    return result;
-}
-
 /*
  * Sends the specified event to all IPC clients which are currently connected
  * and subscribed to this kind of event.