From 4143790d59ea640b98a18306661fe8ac0ffb4cba Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 22 Oct 2022 12:02:14 +0300 Subject: [PATCH 46/46] Don't try to create current directory for savegames Game saving was trying to create the directory for the savegames even when they are saved to the current directory, i.e., when the directory name is empty. See osdn #45841 Signed-off-by: Marko Lindqvist --- server/savegame/savemain.c | 42 ++++++++++++++++++++++++++++++++++---- utility/shared.c | 4 ++++ utility/shared.h | 6 ++++-- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/server/savegame/savemain.c b/server/savegame/savemain.c index 8eda2ed9b6..ed21e91a84 100644 --- a/server/savegame/savemain.c +++ b/server/savegame/savemain.c @@ -107,6 +107,15 @@ struct save_thread_data enum fz_method save_compress_type; }; +/************************************************************************//** + Free resources of save_thread_data, including itself +****************************************************************************/ +static void save_thread_data_free(struct save_thread_data *stdata) +{ + secfile_destroy(stdata->sfile); + free(stdata); +} + /************************************************************************//** Run game saving thread. ****************************************************************************/ @@ -123,8 +132,7 @@ static void save_thread_run(void *arg) con_write(C_OK, _("Game saved as %s"), stdata->filepath); } - secfile_destroy(stdata->sfile); - free(arg); + save_thread_data_free(stdata); } /************************************************************************//** @@ -244,12 +252,38 @@ void save_game(const char *orig_filename, const char *save_reason, if (!scenario) { /* Ensure the saves directory exists. */ - make_dir(srvarg.saves_pathname); + if (srvarg.saves_pathname[0] != '\0' + && !make_dir(srvarg.saves_pathname)) { + log_error(_("Can't create saves directory %s!"), + srvarg.saves_pathname); + /* Don't tell server paths to clients */ + notify_conn(NULL, NULL, E_SETTING, ftc_warning, + _("Can't create saves directory!")); + + save_thread_data_free(stdata); + timer_destroy(timer_cpu); + timer_destroy(timer_user); + + return; + } sz_strlcpy(tmpname, srvarg.saves_pathname); } else { /* Make sure scenario directory exist */ - make_dir(srvarg.scenarios_pathname); + if (srvarg.scenarios_pathname[0] != '\0' + && !make_dir(srvarg.scenarios_pathname)) { + log_error(_("Can't create scenario saves directory %s!"), + srvarg.scenarios_pathname); + /* Don't tell server paths to clients */ + notify_conn(NULL, NULL, E_SETTING, ftc_warning, + _("Can't create scenario saves directory!")); + + save_thread_data_free(stdata); + timer_destroy(timer_cpu); + timer_destroy(timer_user); + + return; + } sz_strlcpy(tmpname, srvarg.scenarios_pathname); } diff --git a/utility/shared.c b/utility/shared.c index 4fbe79a8d5..d9fcd4d2fc 100644 --- a/utility/shared.c +++ b/utility/shared.c @@ -1741,6 +1741,10 @@ bool make_dir(const char *pathname) char *dir; char *path = NULL; + if (pathname[0] == '\0') { + return FALSE; + } + path = interpret_tilde_alloc(pathname); dir = path; diff --git a/utility/shared.h b/utility/shared.h index 3d323bcd7d..a1a2a0ee3a 100644 --- a/utility/shared.h +++ b/utility/shared.h @@ -154,7 +154,7 @@ void remove_leading_trailing_spaces(char *s); bool check_strlen(const char *str, size_t len, const char *errmsg); size_t loud_strlcpy(char *buffer, const char *str, size_t len, - const char *errmsg); + const char *errmsg); /* Convenience macro. */ #define sz_loud_strlcpy(buffer, str, errmsg) \ loud_strlcpy(buffer, str, sizeof(buffer), errmsg) @@ -253,7 +253,9 @@ void interpret_tilde(char *buf, size_t buf_size, const char *filename); char *interpret_tilde_alloc(const char *filename); char *skip_to_basename(char *filepath); -bool make_dir(const char *pathname); +bool make_dir(const char *pathname) + fc__attribute((nonnull (1))); + bool path_is_absolute(const char *filename); char scanin(const char **buf, char *delimiters, char *dest, int size); -- 2.35.1