summaryrefslogtreecommitdiffstats
path: root/src/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c
index e20bd78..904687e 100644
--- a/src/common.c
+++ b/src/common.c
@@ -21,6 +21,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
#include "common.h"
@@ -132,3 +134,26 @@ char *generate_guid()
guid[36] = '\0';
return guid;
}
+
+int mkdir_with_parents(const char *dir, int mode)
+{
+ if (!dir) return -1;
+ if (__mkdir(dir, mode) == 0) {
+ return 0;
+ } else {
+ if (errno == EEXIST) return 0;
+ }
+ int res;
+ char *parent = strdup(dir);
+ parent = dirname(parent);
+ if (parent && (strcmp(parent, ".") != 0)) {
+ res = mkdir_with_parents(parent, mode);
+ } else {
+ res = -1;
+ }
+ free(parent);
+ if (res == 0) {
+ mkdir_with_parents(dir, mode);
+ }
+ return res;
+}