From 63e2cd63b23fba71cea38ad4c8b6139bddeb181d Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 29 Jan 2023 01:37:30 +0200 Subject: [PATCH 33/33] Introduce make_dir_for_file() See osdn #46317 Signed-off-by: Marko Lindqvist --- tools/fcmp/download.c | 10 ++-------- utility/shared.c | 23 +++++++++++++++++++++++ utility/shared.h | 2 ++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/tools/fcmp/download.c b/tools/fcmp/download.c index 0d2baa307c..965f4305e4 100644 --- a/tools/fcmp/download.c +++ b/tools/fcmp/download.c @@ -315,7 +315,7 @@ static const char *download_modpack_recursive(const char *URL, #endif /* DIR_SEPARATOR_IS_DEFAULT */ for (i = 0; dest_name[i] != '\0'; i++) { - if (dest_name[i] == '.' && dest_name[i+1] == '.') { + if (dest_name[i] == '.' && dest_name[i + 1] == '.') { if (mcb != NULL) { char buf[2048]; @@ -348,16 +348,10 @@ static const char *download_modpack_recursive(const char *URL, free(dest_name_copy); #endif /* DIR_SEPARATOR_IS_DEFAULT */ - for (i = strlen(local_name) - 1 ; local_name[i] != DIR_SEPARATOR_CHAR ; i--) { - /* Nothing */ - } - local_name[i] = '\0'; - log_debug("Create directory \"%s\"", local_name); - if (!make_dir(local_name)) { + if (!make_dir_for_file(local_name)) { secfile_destroy(control); return _("Cannot create required directories"); } - local_name[i] = DIR_SEPARATOR_CHAR; if (mcb != NULL) { char buf[2048]; diff --git a/utility/shared.c b/utility/shared.c index ce19fe66cf..c502f0e7c7 100644 --- a/utility/shared.c +++ b/utility/shared.c @@ -1825,6 +1825,29 @@ bool make_dir(const char *pathname) return TRUE; } +/************************************************************************//** + If the directory part of the "filename" does not exist, recursively create + all directories until it does. +****************************************************************************/ +bool make_dir_for_file(char *filename) +{ + int i; + + for (i = strlen(filename) - 1 ; filename[i] != DIR_SEPARATOR_CHAR ; i--) { + /* Nothing */ + } + + filename[i] = '\0'; + log_debug("Create directory \"%s\"", filename); + + if (!make_dir(filename)) { + return FALSE; + } + filename[i] = DIR_SEPARATOR_CHAR; + + return TRUE; +} + /************************************************************************//** Returns TRUE if the filename's path is absolute. ****************************************************************************/ diff --git a/utility/shared.h b/utility/shared.h index 94302db8c5..fbe6f1084b 100644 --- a/utility/shared.h +++ b/utility/shared.h @@ -255,6 +255,8 @@ char *skip_to_basename(char *filepath); bool make_dir(const char *pathname) fc__attribute((nonnull (1))); +bool make_dir_for_file(char *filename) + fc__attribute((nonnull (1))); bool path_is_absolute(const char *filename); -- 2.39.0