From 199b1993b2dfcecbd871e3e5524428bcea3e3d18 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 10 Dec 2022 14:08:38 +0200 Subject: [PATCH 12/12] Rulesave: Save description file when needed If description file lives under the ruleset directory itself, save it to the new ruleset directory being created. See osdn #46242 Signed-off-by: Marko Lindqvist --- tools/download.c | 10 ++------ tools/ruleutil/rulesave.c | 52 +++++++++++++++++++++++++++++---------- utility/shared.c | 23 +++++++++++++++++ utility/shared.h | 2 ++ 4 files changed, 66 insertions(+), 21 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/tools/ruleutil/rulesave.c b/tools/ruleutil/rulesave.c index a7a1c1628d..5550c4af3e 100644 --- a/tools/ruleutil/rulesave.c +++ b/tools/ruleutil/rulesave.c @@ -2750,23 +2750,33 @@ static bool save_units_ruleset(const char *filename, const char *name) /************************************************************************** Save script.lua **************************************************************************/ -static bool save_script_lua(const char *filename, const char *name) +static bool copy_ruleset_file(const char *filename, const char *name, + const char *buffer) { - char *buffer = get_script_buffer(); - if (buffer != NULL) { - FILE *ffile = fc_fopen(filename, "w"); - int full_len = strlen(buffer); - int len; + char *local_name = fc_strdup(filename); + bool success; + + success = make_dir_for_file(local_name); - if (ffile != NULL) { - len = fwrite(buffer, 1, full_len, ffile); + free(local_name); + + if (success) { + FILE *ffile = fc_fopen(filename, "w"); + int full_len = strlen(buffer); + int len; + + if (ffile != NULL) { + len = fwrite(buffer, 1, full_len, ffile); + + if (len != full_len) { + return FALSE; + } - if (len != full_len) { + fclose(ffile); + } else { return FALSE; } - - fclose(ffile); } else { return FALSE; } @@ -2813,7 +2823,7 @@ bool save_ruleset(const char *path, const char *name, struct rule_data *data) fc_snprintf(filename, sizeof(filename), "%s/governments.ruleset", path); success = save_governments_ruleset(filename, name); } - + if (success) { fc_snprintf(filename, sizeof(filename), "%s/nations.ruleset", path); success = save_nations_ruleset(filename, name, data); @@ -2836,7 +2846,23 @@ bool save_ruleset(const char *path, const char *name, struct rule_data *data) if (success) { fc_snprintf(filename, sizeof(filename), "%s/script.lua", path); - success = save_script_lua(filename, name); + success = copy_ruleset_file(filename, name, get_script_buffer()); + } + + if (success + && game.ruleset_description != NULL + && game.server.ruledit.description_file != NULL) { + size_t rsdir_name_len = strlen(game.server.rulesetdir); + + if (!strncmp(game.server.ruledit.description_file, game.server.rulesetdir, + rsdir_name_len) + && game.server.ruledit.description_file[rsdir_name_len] == '/') { + /* Description file lives under old rulesetdir. + * Write it also to the new rulesetdir. */ + fc_snprintf(filename, sizeof(filename), "%s/%s", path, + game.server.ruledit.description_file + rsdir_name_len + 1); + success = copy_ruleset_file(filename, name, game.ruleset_description); + } } return success; diff --git a/utility/shared.c b/utility/shared.c index b682f6ea4e..4922f7bae4 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.35.1