diff --git a/src/fullfile.c b/src/fullfile.c index 45701958..6a00ee1e 100644 --- a/src/fullfile.c +++ b/src/fullfile.c @@ -27,7 +27,6 @@ static struct list *download_loop(struct list *files, bool free_list) { - int ret; struct list *iter; unsigned int complete = 0; unsigned int list_length = list_len(files); @@ -55,13 +54,9 @@ static struct list *download_loop(struct list *files, bool free_list) string_or_die(&url, "%s/%i/files/%s.tar", MIX_STATE_DIR, file->last_change, file->hash); string_or_die(&file->staging, "%s/download/.%s.tar", state_dir, file->hash); - ret = link(url, file->staging); - /* Try doing a regular rename if hardlink fails */ - if (ret) { - if (rename(url, file->staging) != 0) { - fprintf(stderr, "Failed to copy local mix file: %s\n", file->staging); - continue; - } + if (link_or_rename(url, file->staging) != 0) { + fprintf(stderr, "Failed to copy local mix file: %s\n", file->staging); + continue; } untar_full_download(file); free_string(&url); diff --git a/src/helpers.c b/src/helpers.c index 5f551b33..442c00c6 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -941,3 +941,15 @@ bool on_new_format(void) * different from the one we started on. */ return !is_compatible_format(res); } + +/* Try to create a link to a file. If it fails, rename it. + * Return 0 on success or an error code on errors.*/ +int link_or_rename(const char *orig, const char *dest) +{ + /* Try doing a regular rename if hardlink fails */ + if (link(orig, dest) != 0) { + return rename(orig, dest); + } + + return 0; +} diff --git a/src/manifest.c b/src/manifest.c index ab83dc47..e0573bc3 100644 --- a/src/manifest.c +++ b/src/manifest.c @@ -489,11 +489,7 @@ static int retrieve_manifests(int current, int version, char *component, struct if (is_mix) { string_or_die(&filename, "%s/%i/Manifest.%s.tar", state_dir, version, component); string_or_die(&url, "%s/%i/Manifest.%s.tar", basedir, version, component); - ret = link(url, filename); - /* Try doing a regular rename if hardlink fails */ - if (ret) { - ret = rename(url, filename); - } + ret = link_or_rename(url, filename); /* If rename fails, we try again below with curl to the contenurl */ if (ret == 0) { goto untar; diff --git a/src/swupd.h b/src/swupd.h index b0614763..333c32b4 100644 --- a/src/swupd.h +++ b/src/swupd.h @@ -383,6 +383,7 @@ extern int list_installable_bundles(); extern int install_bundles_frontend(char **bundles); extern int add_subscriptions(struct list *bundles, struct list **subs, int current_version, struct manifest *mom, bool find_all, int recursion); int list_local_bundles(); +extern int link_or_rename(const char *orig, const char *dest); /* telemetry.c */ typedef enum telem_prio_t {