diff --git a/src/3rd_party_repos.c b/src/3rd_party_repos.c index 88855d8d..a89234a6 100644 --- a/src/3rd_party_repos.c +++ b/src/3rd_party_repos.c @@ -227,4 +227,29 @@ exit: list_free_list_and_data(repos, repo_free_data); return ret; } + +int third_party_set_repo(const char *state_dir, const char *path_prefix, struct repo *repo) +{ + char *repo_state_dir; + char *repo_path_prefix; + + set_content_url(repo->url); + + string_or_die(&repo_path_prefix, "%s/opt/3rd_party/%s", path_prefix, repo->name); + set_path_prefix(repo_path_prefix); + free_string(&repo_path_prefix); + + /* make sure there are state directories for the 3rd-party + * repo if not there already */ + string_or_die(&repo_state_dir, "%s/3rd_party/%s", state_dir, repo->name); + if (create_state_dirs(repo_state_dir)) { + free_string(&repo_state_dir); + return -1; + } + set_state_dir(repo_state_dir); + free_string(&repo_state_dir); + + return 0; +} + #endif diff --git a/src/3rd_party_repos.h b/src/3rd_party_repos.h index 0a63f9b8..702cf80a 100644 --- a/src/3rd_party_repos.h +++ b/src/3rd_party_repos.h @@ -63,6 +63,16 @@ int third_party_add_repo(const char *repo_name, const char *repo_url); */ int third_party_remove_repo(char *repo_name); +/** + * @brief This function sets up swupd to use a specific 3rd-party repo. + * + * @param state_dir the original state directory of the system + * @param path_prefix the original path prefix of the system + * + * @returns 0 on success, -1 on any failure + */ +int third_party_set_repo(const char *state_dir, const char *path_prefix, struct repo *repo); + #endif #ifdef __cplusplus diff --git a/src/globals.c b/src/globals.c index 31c5011d..9beb77e8 100644 --- a/src/globals.c +++ b/src/globals.c @@ -76,7 +76,7 @@ static void set_json_format(bool on) } /* Sets the content_url global variable */ -static void set_content_url(char *url) +void set_content_url(char *url) { if (globals.content_url) { free_string(&globals.content_url); @@ -193,7 +193,7 @@ static bool is_valid_integer_format(char *str) * NULL, state_dir will be set to its value. Otherwise, the value is the * build-time default (STATE_DIR). */ -static bool set_state_dir(char *path) +bool set_state_dir(char *path) { if (!path) { error("Statedir shouldn't be NULL\n"); diff --git a/src/globals.h b/src/globals.h index 730ba5d1..be404322 100644 --- a/src/globals.h +++ b/src/globals.h @@ -81,6 +81,8 @@ 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); #ifdef __cplusplus } diff --git a/src/helpers.c b/src/helpers.c index 10708050..ab573120 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -188,12 +188,20 @@ static bool validate_tracking_dir(const char *state_dir) * user installed themselves just copy the entire system tracking directory * into the state tracking directory. */ if (!is_populated_dir(tracking_dir)) { + src = sys_path_join(globals.path_prefix, "/usr/share/clear/bundles"); + + if (!sys_file_exists(src)) { + /* there is no bundles directory to copy from */ + free_string(&src); + free_string(&tracking_dir); + return false; + } + ret = rm_rf(tracking_dir); if (ret) { goto out; } - src = sys_path_join(globals.path_prefix, "/usr/share/clear/bundles"); /* at the point this function is called is already * installed on the system and therefore has a tracking file under * /usr/share/clear/bundles. A simple cp -a of that directory will @@ -233,7 +241,7 @@ int create_state_dirs(const char *state_dir_path) // check for existence if (ensure_root_owned_dir(state_dir_path)) { - //state dir doesn't exist + // state dir doesn't exist if (mkdir_p(state_dir_path) != 0 || chmod(state_dir_path, S_IRWXU) != 0) { error("failed to create %s\n", state_dir_path); return -1; @@ -247,6 +255,7 @@ int create_state_dirs(const char *state_dir_path) ret = mkdir(dir, S_IRWXU); if (ret) { error("failed to create %s\n", dir); + free_string(&dir); return -1; } }