From fbcb227537a15d1ef5873500203ae7394a3b3d8f Mon Sep 17 00:00:00 2001 From: Deiz Date: Sun, 29 Mar 2015 17:18:00 -0400 Subject: [PATCH] Move mkdirp into libi3 --- include/ipc.h | 6 ------ include/libi3.h | 6 ++++++ libi3/mkdirp.c | 38 ++++++++++++++++++++++++++++++++++++++ src/ipc.c | 30 ------------------------------ 4 files changed, 44 insertions(+), 36 deletions(-) create mode 100644 libi3/mkdirp.c diff --git a/include/ipc.h b/include/ipc.h index 96a60a1f..4eed319a 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -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 diff --git a/include/libi3.h b/include/libi3.h index 3a125827..d6a2e980 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -434,3 +434,9 @@ char *get_exe_path(const char *argv0); * */ int logical_px(const int logical); + +/** + * 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 index 00000000..a0d35f96 --- /dev/null +++ b/libi3/mkdirp.c @@ -0,0 +1,38 @@ +#include "libi3.h" +#include +#include +#include +#include + +/* + * 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; +} diff --git a/src/ipc.c b/src/ipc.c index 52f7db2e..8fed75f1 100644 --- 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. -- 2.39.5