diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2020-12-14 00:07:55 +0100 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2020-12-14 00:07:55 +0100 |
commit | 1d59162c13db8051badb1bb501faf8ccf42b10e9 (patch) | |
tree | e565efb96434d4c55687163b20cc6a16a959e5c8 | |
parent | 4cd335b3c10eed9ff859ec6f826874b81ae2e15b (diff) | |
download | polecat-1d59162c13db8051badb1bb501faf8ccf42b10e9.tar.gz polecat-1d59162c13db8051badb1bb501faf8ccf42b10e9.zip |
make directory creation create all parent directories too
-rw-r--r-- | src/common.c | 25 |
1 files 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 <unistd.h> #include <sys/types.h> #include <dirent.h> +#include <errno.h> #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) { |