diff --git a/Makefile.am b/Makefile.am index 576124d7..5be2caa8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -115,6 +115,7 @@ swupd_SOURCES = \ src/swupd_lib/hashdump.c \ src/swupd_lib/helpers.c \ src/swupd_lib/heuristics.c \ + src/swupd_lib/heuristics.h \ src/swupd_lib/lock.c \ src/swupd_lib/manifest.c \ src/swupd_lib/manifest.h \ diff --git a/src/cmds/bundle_add.c b/src/cmds/bundle_add.c index 6515d486..f726fff1 100644 --- a/src/cmds/bundle_add.c +++ b/src/cmds/bundle_add.c @@ -33,6 +33,7 @@ #include "swupd_lib/alias.h" #include "swupd.h" #include "swupd_lib/target_root.h" +#include "swupd_lib/heuristics.h" #define MODE_RW_O (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) #define VERIFY_NOPICKY 0 @@ -217,15 +218,8 @@ static int compute_bundle_dependecies(struct manifest *mom, struct list *bundles static enum swupd_code apply_heuristics_for_new_files(struct list *files) { - struct list *iter; - struct file *file; - timelist_timer_start(globals.global_times, "Applying heuristics"); - for (iter = files; iter; iter = iter->next) { - file = iter->data; - (void)ignore(file); - apply_heuristics(file); - } + heuristics_apply(files); timelist_timer_stop(globals.global_times); return SWUPD_OK; diff --git a/src/cmds/update.c b/src/cmds/update.c index a0b4d4a8..654ae813 100644 --- a/src/cmds/update.c +++ b/src/cmds/update.c @@ -34,6 +34,7 @@ #include "swupd.h" #include "swupd_lib/signature.h" #include "swupd_lib/target_root.h" +#include "swupd_lib/heuristics.h" #define FLAG_DOWNLOAD_ONLY 2000 #define FLAG_UPDATE_SEARCH_FILE_INDEX 2001 @@ -243,11 +244,6 @@ static struct list *create_update_list(struct manifest *server) * files, so they will not have a peer. */ if (!file->peer || (file->peer && file->last_change > file->peer->last_change)) { - /* check and if needed mark as do_not_update */ - (void)ignore(file); - /* check if we need to run scripts/update the bootloader/etc */ - apply_heuristics(file); - output = list_prepend_data(output, file); continue; } @@ -415,6 +411,9 @@ enum swupd_code execute_update_extra(extra_proc_fn_t post_update_fn, extra_proc_ print_statistics(current_version, server_version); timelist_timer_stop(globals.global_times); // closing: Create update list + timelist_timer_start(globals.global_times, "Applying heuristics"); + heuristics_apply(updates); + timelist_timer_stop(globals.global_times); // closing: Applying heuristics /* downloading and applying updates */ /* need update list in filename order to insure directories are * created before their contents */ diff --git a/src/cmds/verify.c b/src/cmds/verify.c index 0ab0994e..1f3cb4bb 100644 --- a/src/cmds/verify.c +++ b/src/cmds/verify.c @@ -35,6 +35,7 @@ #include "swupd.h" #include "swupd_lib/signature.h" #include "swupd_lib/target_root.h" +#include "swupd_lib/heuristics.h" #define FLAG_EXTRA_FILES_ONLY 2000 #define FLAG_FILE 2001 @@ -369,8 +370,7 @@ static void add_missing_files(struct manifest *official_manifest, struct list *f iter = iter->next; complete++; - if ((file->is_deleted) || - (file->do_not_update)) { + if (file->is_deleted || file->do_not_update) { goto progress; } @@ -439,7 +439,7 @@ static void check_and_fix_one(struct file *file, struct manifest *official_manif char *fullname; // Note: boot files not marked as deleted are candidates for verify/fix - if (file->is_deleted || ignore(file) || file->do_not_update) { + if (file->is_deleted || file->do_not_update) { return; } @@ -563,17 +563,14 @@ static void remove_orphaned_files(struct list *files_to_verify, bool repair) iter = iter->next; complete++; - /* Do not remove files that have not been deleted, are config files, or - * are marked as ghosted */ - if (!file->is_deleted || file->is_config || file->is_ghosted) { - goto progress; - } - - /* Note: boot files marked as deleted should not be deleted by - * verify/fix; this task is delegated to an external program - * (currently /usr/bin/clr-boot-manager). + /* + * Don't remove files set as do_not_update, like: + * - Config files + * - ghosted files + * - boot files, that should be managed by the external tool + * clr-boot-manager */ - if (ignore(file)) { + if (!file->is_deleted || file->do_not_update) { goto progress; } @@ -899,6 +896,15 @@ static struct list *keep_matching_path(struct list *all_files) return list_head(matching_files); } +static enum swupd_code apply_heuristics_for_files(struct list *files) +{ + timelist_timer_start(globals.global_times, "Applying heuristics"); + heuristics_apply(files); + timelist_timer_stop(globals.global_times); + + return SWUPD_OK; +} + /* This function does a simple verification of files listed in the * subscribed bundle manifests. If the optional "fix" or "install" parameter * is specified, the disk will be modified at each point during the @@ -1176,6 +1182,7 @@ enum swupd_code execute_verify_extra(extra_proc_fn_t post_verify_fn) goto extra_files; } } + apply_heuristics_for_files(files_to_verify); if (cmdline_option_extra_files_only) { /* user wants to deal only with the extra files, so skip everything else */ diff --git a/src/swupd.h b/src/swupd.h index 20b17917..02609e84 100644 --- a/src/swupd.h +++ b/src/swupd.h @@ -169,9 +169,6 @@ extern bool get_distribution_string(char *path_prefix, char *dist); extern int get_current_format(void); extern int get_server_format(int server_version); -extern bool ignore(struct file *file); -extern void apply_heuristics(struct file *file); - extern struct manifest *load_mom(int version, int *err); extern struct manifest *load_manifest(int version, struct file *file, struct manifest *mom, bool header_only, int *err); extern void link_manifests(struct manifest *m1, struct manifest *m2); diff --git a/src/swupd_lib/heuristics.c b/src/swupd_lib/heuristics.c index 03b9e640..005a643e 100644 --- a/src/swupd_lib/heuristics.c +++ b/src/swupd_lib/heuristics.c @@ -27,6 +27,7 @@ #include #include "swupd.h" +#include "heuristics.h" /* trailing slash is to indicate dir itself is expected to exist, but * contents are ignored */ @@ -124,21 +125,13 @@ static void boot_manager_heuristics(struct file *file) } } -void apply_heuristics(struct file *file) -{ - runtime_state_heuristics(file); - boot_file_heuristics(file); - config_file_heuristics(file); - boot_manager_heuristics(file); -} - /* Determines whether or not FILE should be ignored for this swupd action. Note * that boot files are ignored only if they are marked as deleted; this does * not happen in current manifests produced by swupd-server, but this check is * enabled in case swupd-server ever allows for deleted boot files in manifests * in the future. */ -bool ignore(struct file *file) +static void check_ignore_file(struct file *file) { if ((OS_IS_STATELESS && file->is_config) || (OS_IS_STATELESS && is_config(file->filename)) || // ideally we trust the manifest but short term reapply check here @@ -148,8 +141,25 @@ bool ignore(struct file *file) (file->is_orphan) || (file->is_ghosted)) { file->do_not_update = 1; - return true; } - - return false; +} + +static void apply_heuristics_for_file(struct file *file) +{ + runtime_state_heuristics(file); + boot_file_heuristics(file); + config_file_heuristics(file); + boot_manager_heuristics(file); + check_ignore_file(file); +} + +void heuristics_apply(struct list *files) +{ + struct list *iter; + struct file *file; + + for (iter = files; iter; iter = iter->next) { + file = iter->data; + apply_heuristics_for_file(file); + } } diff --git a/src/swupd_lib/heuristics.h b/src/swupd_lib/heuristics.h new file mode 100644 index 00000000..70931e2b --- /dev/null +++ b/src/swupd_lib/heuristics.h @@ -0,0 +1,35 @@ +#ifndef __HEURISTICS__ +#define __HEURISTICS__ + +/** + * @file + * @brief Heuristics system to define which scripts to run and which files to + * ignore on installation time. + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Use heuristics to define which files should be updated and which + * scripts should be executed. + * + * Use the path and properties of the file to define it's type and by that + * define if the file should be set with a do_not_update flag or is_boot flag. + * Also use that information to check if the bootloader or systemd update + * scripts should be executed. + * + * @param files The list of struct file objects where swupd heuristics will be + * applied. + */ +void heuristics_apply(struct list *files); + +#ifdef __cplusplus +} +#endif + +#endif