Make io_check_dir() create parent directories

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Nitroretro 2019-12-17 00:58:02 +02:00 committed by Lukas Fleischer
parent a0129d6751
commit 250a233ff7

View File

@ -1053,19 +1053,41 @@ int io_check_dir(const char *dir)
if (read_only) if (read_only)
return -1; return -1;
char *path = mem_strdup(dir);
char *index;
int existed = 1;
errno = 0; 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) { if (errno != EEXIST) {
fprintf(stderr, fprintf(stderr,
_("FATAL ERROR: could not create %s: %s\n"), _("FATAL ERROR: could not create %s: %s\n"),
dir, strerror(errno)); path, strerror(errno));
exit_calcurse(EXIT_FAILURE); exit_calcurse(EXIT_FAILURE);
} else {
return 1;
} }
} else { } else {
return 0; existed = 0;
} }
mem_free(path);
return existed;
} }
unsigned io_dir_exists(const char *path) unsigned io_dir_exists(const char *path)