From cf263bbd07e392974033db97ea118c3f287fd201 Mon Sep 17 00:00:00 2001 From: Castulo Martinez Date: Mon, 11 May 2020 11:49:53 -0700 Subject: [PATCH] Move function to set the path to the statedir Moving the function to the appropriate statedir module. Signed-off-by: Castulo Martinez --- src/3rd_party/3rd_party_repos.c | 6 ++-- src/cmds/verify.c | 2 +- src/swupd_lib/globals.c | 61 +-------------------------------- src/swupd_lib/globals.h | 2 -- src/swupd_lib/statedir.c | 36 +++++++++++++++++++ src/swupd_lib/statedir.h | 14 ++++++++ 6 files changed, 55 insertions(+), 66 deletions(-) diff --git a/src/3rd_party/3rd_party_repos.c b/src/3rd_party/3rd_party_repos.c index b2bdf069..0149370f 100644 --- a/src/3rd_party/3rd_party_repos.c +++ b/src/3rd_party/3rd_party_repos.c @@ -314,7 +314,7 @@ enum swupd_code third_party_set_repo(struct repo *repo, bool sigcheck) error("Unable to create the state directories for repository %s\n\n", repo->name); return SWUPD_COULDNT_CREATE_DIR; } - set_state_dir(repo_state_dir); + statedir_set_path(repo_state_dir); FREE(repo_state_dir); return SWUPD_OK; @@ -379,7 +379,7 @@ static enum swupd_code third_party_find_bundle(const char *bundle, struct list * clean_and_exit: set_path_prefix(globals_bkp.path_prefix); - set_state_dir(globals_bkp.state_dir); + statedir_set_path(globals_bkp.state_dir); set_content_url(globals_bkp.content_url); set_version_url(globals_bkp.version_url); @@ -444,7 +444,7 @@ enum swupd_code third_party_run_operation(struct list *bundles, const char *repo /* return the global variables to the original values */ next: set_path_prefix(globals_bkp.path_prefix); - set_state_dir(globals_bkp.state_dir); + statedir_set_path(globals_bkp.state_dir); set_content_url(globals_bkp.content_url); set_version_url(globals_bkp.version_url); } diff --git a/src/cmds/verify.c b/src/cmds/verify.c index 3fac900f..cb346c67 100644 --- a/src/cmds/verify.c +++ b/src/cmds/verify.c @@ -929,7 +929,7 @@ enum swupd_code execute_verify_extra(extra_proc_fn_t post_verify_fn) bool invalid_bundle = false; if (cmdline_option_statedir_cache != NULL) { - ret = set_state_dir_cache(cmdline_option_statedir_cache); + ret = statedir_dup_set_path(cmdline_option_statedir_cache); if (ret != true) { error("Failed to set statedir-cache\n"); goto clean_args_and_exit; diff --git a/src/swupd_lib/globals.c b/src/swupd_lib/globals.c index da7824e7..e9535283 100644 --- a/src/swupd_lib/globals.c +++ b/src/swupd_lib/globals.c @@ -193,65 +193,6 @@ static bool is_valid_integer_format(char *str) return true; } -/* Initializes the state_dir global variable. If the path parameter is not - * NULL, state_dir will be set to its value. Otherwise, the value is the - * build-time default (STATE_DIR). - */ -bool set_state_dir(char *path) -{ - if (!path) { - error("Statedir shouldn't be NULL\n"); - return false; - } - - if (path[0] != '/') { - error("State dir must be a full path starting with '/', not '%c'\n", path[0]); - return false; - } - - /* Prevent some disasters: since the state dir can be destroyed and - * reconstructed, make sure we never set those by accident and nuke the - * system. */ - if (!str_cmp(path, "/") || !str_cmp(path, "/var") || !str_cmp(path, "/usr")) { - error("Refusing to use '%s' as a state dir because it might be erased first\n", path); - return false; - } - - FREE(globals.state_dir); - string_or_die(&globals.state_dir, "%s", path); - - return true; -} - -/* Sets the state_dir_cache global variable. If the path parameter is not - * NULL, state_dir_cache will be set to its value. - */ -bool set_state_dir_cache(char *path) -{ - if (!path) { - error("Statedir-cache shouldn't be set to NULL\n"); - return false; - } - - if (path[0] != '/') { - error("Statedir-cache must be a full path starting with '/', not '%c'\n", path[0]); - return false; - } - - /* Prevent some disasters: since the statedir-cache can be destroyed and - * reconstructed, make sure we never set those by accident and nuke the - * system. */ - if (!str_cmp(path, "/") || !str_cmp(path, "/var") || !str_cmp(path, "/usr")) { - error("Refusing to use '%s' as a statedir-cache because it might be erased first\n", path); - return false; - } - - FREE(globals.state_dir_cache); - string_or_die(&globals.state_dir_cache, "%s", path); - - return true; -} - static void set_default_state_dir(void) { string_or_die(&globals.state_dir, "%s", STATE_DIR); @@ -595,7 +536,7 @@ static bool global_parse_opt(int opt, char *optarg) } return true; case 'S': - if (!set_state_dir(optarg)) { + if (!statedir_set_path(optarg)) { error("Invalid --statedir argument\n\n"); return false; } diff --git a/src/swupd_lib/globals.h b/src/swupd_lib/globals.h index a27bf7e8..834dee00 100644 --- a/src/swupd_lib/globals.h +++ b/src/swupd_lib/globals.h @@ -95,10 +95,8 @@ void save_cmd(char **argv); bool set_path_prefix(char *path); bool set_default_urls(void); -bool set_state_dir_cache(char *path); void set_default_path_prefix(void); void set_content_url(char *url); -bool set_state_dir(char *path); void set_version_url(char *url); void set_cert_path(char *path); diff --git a/src/swupd_lib/statedir.c b/src/swupd_lib/statedir.c index 46464f62..b5afec66 100644 --- a/src/swupd_lib/statedir.c +++ b/src/swupd_lib/statedir.c @@ -172,3 +172,39 @@ int statedir_create_dirs(const char *path) return ret; } + +static bool set_state_path(char** state, char *path) +{ + if (!path) { + error("Statedir shouldn't be NULL\n"); + return false; + } + + if (path[0] != '/') { + error("State dir must be a full path starting with '/', not '%c'\n", path[0]); + return false; + } + + /* Prevent some disasters: since the state dir can be destroyed and + * reconstructed, make sure we never set those by accident and nuke the + * system. */ + if (!str_cmp(path, "/") || !str_cmp(path, "/var") || !str_cmp(path, "/usr")) { + error("Refusing to use '%s' as a state dir because it might be erased first\n", path); + return false; + } + + FREE(*state); + *state = sys_path_join("%s", path); + + return true; +} + +bool statedir_set_path(char *path) +{ + return set_state_path(&globals.state_dir, path); +} + +bool statedir_dup_set_path(char *path) +{ + return set_state_path(&globals.state_dir_cache, path); +} diff --git a/src/swupd_lib/statedir.h b/src/swupd_lib/statedir.h index 38d71a2d..2f67e0fd 100644 --- a/src/swupd_lib/statedir.h +++ b/src/swupd_lib/statedir.h @@ -137,6 +137,20 @@ char *statedir_get_version(void); */ int statedir_create_dirs(const char *path); +/** + * @brief Sets the path to the statedir. + * + * @param path The path of the statedir + */ +bool statedir_set_path(char *path); + +/** + * @brief Sets the path to the duplicate (cache) of the statedir. + * + * @param path The path of the statedir duplicate + */ +bool statedir_dup_set_path(char *path); + #ifdef __cplusplus } #endif