From 250a233ff7aea152284e62e99b8ca09a0e032699 Mon Sep 17 00:00:00 2001
From: Nitroretro <nitroretro@protonmail.com>
Date: Tue, 17 Dec 2019 00:58:02 +0200
Subject: Make io_check_dir() create parent directories

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
---
 src/io.c | 32 +++++++++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 5 deletions(-)

(limited to 'src')

diff --git a/src/io.c b/src/io.c
index 5c6d254..668afc6 100644
--- a/src/io.c
+++ b/src/io.c
@@ -1053,19 +1053,41 @@ int io_check_dir(const char *dir)
 	if (read_only)
 		return -1;
 
+	char *path = mem_strdup(dir);
+	char *index;
+
+	int existed = 1;
 	errno = 0;
-	if (mkdir(dir, 0700) != 0) {
+	for (index = path + 1; *index; index++) {
+		if (*index == '/') {
+			*index = '\0';
+			if (mkdir(path, 0700) != 0) {
+				if (errno != EEXIST) {
+					fprintf(stderr,
+						_("FATAL ERROR: could not create %s: %s\n"),
+						path, strerror(errno));
+					exit_calcurse(EXIT_FAILURE);
+				}
+			} else {
+				existed = 0;
+			}
+			*index = '/';
+		}
+	}
+
+	if (mkdir(path, 0700) != 0) {
 		if (errno != EEXIST) {
 			fprintf(stderr,
 				_("FATAL ERROR: could not create %s: %s\n"),
-				dir, strerror(errno));
+				path, strerror(errno));
 			exit_calcurse(EXIT_FAILURE);
-		} else {
-			return 1;
 		}
 	} else {
-		return 0;
+		existed = 0;
 	}
+
+	mem_free(path);
+	return existed;
 }
 
 unsigned io_dir_exists(const char *path)
-- 
cgit v1.2.3-70-g09d2