From 61d75021c8a1180d2b591e70c783bd2eb21188bd Mon Sep 17 00:00:00 2001 From: Erik K Date: Thu, 26 May 2022 16:31:27 +0000 Subject: mkdir parent directories when needed, also fixed bug in list(). --- util.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index 3f4f51d..50a7116 100644 --- a/util.c +++ b/util.c @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -65,6 +67,28 @@ xrealloc(void *ptr, size_t size) return n; } +int +mkparentdirs(const char *path) +{ + char part[512]; + int i; + + if (!path[0]) + return 0; + my_strlcpy(part, path, sizeof(part)); + for (i = 1; part[i]; i++) { + if (part[i] == '/' && part[i + 1] != '/' && part[i + 1]) { + part[i] = '\0'; + if (mkdir(part, 0755) < 0 && errno != EEXIST) { + fprintf(stderr, "mkdir %s: %s\n", part, strerror(errno)); + return 0; + } + part[i] = '/'; + } + } + return 1; +} + /* strlcpy is unportable, and strncpy is a mess. So we define our own strlcpy * instead. */ size_t -- cgit v1.2.3