From 1d59162c13db8051badb1bb501faf8ccf42b10e9 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Mon, 14 Dec 2020 00:07:55 +0100 Subject: make directory creation create all parent directories too --- src/common.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/common.c b/src/common.c index 9b0ab51..7f91105 100644 --- a/src/common.c +++ b/src/common.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "common.h" @@ -54,15 +55,29 @@ bool isDir(const char* path) int makeDir(const char* path) { - // we do not care about the contents but what stat returns - struct stat sb; + char pathcpy[PATH_MAX]; + char *index; - if (stat(path, &sb) < 0) + strncpy(pathcpy, path, PATH_MAX); // make a mutable copy of the path + + for(index = pathcpy+1; *index; ++index) { - return mkdir(path, 0755); + + if (*index == '/') + { + *index = '\0'; + + if (mkdir(pathcpy, 0755) != 0) + { + if (errno != EEXIST) + return -1; + } + + *index = '/'; + } } - return 0; // directory exists, pretend we made it + return mkdir(path, 0755); } int removeDir(const char *path) { -- cgit v1.2.3