Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Golle <daniel@makrotopia.org>2020-12-13 01:45:53 +0300
committerDaniel Golle <daniel@makrotopia.org>2020-12-13 01:50:50 +0300
commit357877693ca363b12e6e7e14d345639b2440cd07 (patch)
tree01c5400d66e3a9761df89600acfa490bad68fa29 /utils.c
parent9e52171d70def760a6949676800d0b73f85ee22d (diff)
utils: introduce mkdir_p
Add new utility function mkdir_p(char *path, mode_t mode) to replace the partially buggy implementations found accross fstools and procd. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index c22250d..db63238 100644
--- a/utils.c
+++ b/utils.c
@@ -17,9 +17,11 @@
*/
#include <sys/mman.h>
+#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include "utils.h"
#define foreach_arg(_arg, _addr, _len, _first_addr, _first_len) \
@@ -155,3 +157,33 @@ void cbuf_free(void *ptr, unsigned int order)
{
munmap(ptr, cbuf_size(order) * 2);
}
+
+int mkdir_p(char *dir, mode_t mask)
+{
+ char *l;
+ int ret;
+
+ ret = mkdir(dir, mask);
+ if (!ret || (ret && errno == EEXIST))
+ return 0;
+
+ if (ret && (errno != ENOENT))
+ return -1;
+
+ l = strrchr(dir, '/');
+ if (!l || l == dir)
+ return -1;
+
+ *l = '\0';
+
+ if (mkdir_p(dir, mask))
+ return -1;
+
+ *l = '/';
+
+ ret = mkdir(dir, mask);
+ if (!ret || (ret && errno == EEXIST))
+ return 0;
+ else
+ return -1;
+}