From 5163485e81e1423e54a44e3d345b7aa2121f104a Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 29 Jan 2023 01:40:50 +0200 Subject: [PATCH 7/7] Introduce make_dir_for_file() See osdn #46317 Signed-off-by: Marko Lindqvist --- tools/download.c | 10 ++-------- utility/shared.c | 23 +++++++++++++++++++++++ utility/shared.h | 2 ++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/tools/download.c b/tools/download.c index 0fea2d176b..b9a7860b0c 100644 --- a/tools/download.c +++ b/tools/download.c @@ -306,7 +306,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]; @@ -339,16 +339,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 bf53e53253..a4ade4ca54 100644 --- a/utility/shared.c +++ b/utility/shared.c @@ -134,6 +134,29 @@ enum fc_tristate fc_tristate_and(enum fc_tristate one, enum fc_tristate two) return TRI_YES; } +/************************************************************************//** + 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; +} + /************************************************************************//** An OR function for fc_tristate. ****************************************************************************/ diff --git a/utility/shared.h b/utility/shared.h index e164b691c0..d90ee9216f 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