summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNitroretro <nitroretro@protonmail.com>2019-12-17 00:58:02 +0200
committerLukas Fleischer <lfleischer@calcurse.org>2019-12-23 13:16:39 -0500
commit250a233ff7aea152284e62e99b8ca09a0e032699 (patch)
treede954c3af556848efde35f29e9f2066f00dd3866
parenta0129d67510f60fadbd272252624a352f5c9f8b6 (diff)
downloadcalcurse-250a233ff7aea152284e62e99b8ca09a0e032699.tar.gz
calcurse-250a233ff7aea152284e62e99b8ca09a0e032699.zip
Make io_check_dir() create parent directories
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
-rw-r--r--src/io.c32
1 files changed, 27 insertions, 5 deletions
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)