diff --git a/include/swupd.h b/include/swupd.h index 4cbee3e..1134fb3 100644 --- a/include/swupd.h +++ b/include/swupd.h @@ -198,6 +198,7 @@ extern void sort_manifest_by_version(struct manifest *manifest); extern bool manifest_includes(struct manifest *manifest, char *component); extern bool changed_includes(struct manifest *old, struct manifest *new); extern int prune_manifest(struct manifest *manifest); +extern void clean_renames(struct manifest *manifest); extern int remove_deprecated_files(struct manifest *m1, struct manifest *m2, bool (*compfunc)(struct file *, struct file *)); extern void create_manifest_delta(int oldversion, int newversion, char *module); extern void create_manifest_deltas(struct manifest *manifest, GList *last_versions_list); @@ -257,6 +258,7 @@ extern void type_change_detection(struct manifest *manifest); extern void rename_detection(struct manifest *manifest); extern void link_renames(GList *newfiles, int to_version); +extern void final_link(GList *files); extern void __create_delta(struct file *file, int from_version, char *from_hash); extern void account_delta_hit(void); diff --git a/src/create_update.c b/src/create_update.c index 17e2c25..f514d43 100644 --- a/src/create_update.c +++ b/src/create_update.c @@ -405,6 +405,8 @@ int main(int argc, char **argv) old_deleted = remove_deprecated_files(old_core, new_core, both_deleted); old_ghosted = remove_deprecated_files(old_core, new_core, both_ghosted); sort_manifest_by_version(new_core); /* sorts by filename */ + /* clean up orphaned renames by marking them as deleted */ + clean_renames(new_core); newfiles = prune_manifest(new_core); if (newfiles <= 0) { LOG(NULL, "", "Core component has not changed (after pruning), exiting"); @@ -528,6 +530,8 @@ int main(int argc, char **argv) old_ghosted = remove_deprecated_files(oldm, newm, both_ghosted); sort_manifest_by_version(newm); type_change_detection(newm); + /* clean up orphaned renames by marking them as deleted */ + clean_renames(newm); newfiles = prune_manifest(newm); if (newfiles > 0 || old_deleted > 0 || old_ghosted > 0 || changed_includes(oldm, newm)) { LOG(NULL, "", "%s component has changes (%d new, %d deleted, %d ghosted), writing out new manifest", group, newfiles, old_deleted, old_ghosted); @@ -568,6 +572,8 @@ int main(int argc, char **argv) maximize_to_full(new_MoM, new_full); sort_manifest_by_version(new_full); + /* clean up orphaned renames by marking them as deleted */ + clean_renames(new_full); prune_manifest(new_full); if (write_manifest(new_full) != 0) { goto exit; diff --git a/src/manifest.c b/src/manifest.c index 6368c4c..ea3fea5 100644 --- a/src/manifest.c +++ b/src/manifest.c @@ -1050,6 +1050,32 @@ int remove_deprecated_files(struct manifest *m1, struct manifest *m2, bool (*com return count; } +/* Performs a final link of renames in the manifest to catch any renames from + * old versions and then removes any orphaned renames */ +void clean_renames(struct manifest *manifest) +{ + GList *list; + struct file *file; + + /* make sure all renames are linked, this is necessary for renames + * from old manifests that carry over to the current one */ + final_link(manifest->files); + + list = g_list_first(manifest->files); + while (list) { + file = list->data; + /* if the file is marked as deleted and renamed it is a + * renamed-from file. If the rename_peer field is still NULL + * then it has been orphaned. Mark these as deleted */ + if (file->is_deleted && file->is_rename && !file->rename_peer) { + hash_set_zeros(file->hash); + file->is_rename = 0; + } + + list = g_list_next(list); + } +} + /* Conditionally remove some things from a manifest. * Returns > 0 when the pruned manifest has new files. * Returns 0 when the pruned manifest no longer has new files. @@ -1068,14 +1094,6 @@ int prune_manifest(struct manifest *manifest) next = g_list_next(list); file = list->data; - /* if the file is marked as deleted and renamed it is a renamed_from - * file. If the rename_peer field is still NULL then it has been - * orphaned. Prune these files */ - if (file->is_deleted && file->is_rename && !file->rename_peer) { - manifest->files = g_list_delete_link(manifest->files, list); - manifest->count--; - } - if (OS_IS_STATELESS && (!file->is_deleted) && (file->is_config)) { // toward being a stateless OS LOG(file, "Skipping config file in manifest write", "component %s", manifest->component); diff --git a/src/rename.c b/src/rename.c index f6138ec..581fb9a 100644 --- a/src/rename.c +++ b/src/rename.c @@ -446,6 +446,48 @@ redo: g_list_free(deleted_files); } +void final_link(GList *files) +{ + GList *list1, *list2; + struct file *file1, *file2; + + list1 = new_list_renamed_files(files); + list1 = list2 = g_list_sort(list1, file_sort_version); + + for (; list1; list1 = g_list_next(list1)) { + file1 = list1->data; + /* do not re-link already-linked renames */ + if (file1->rename_peer != NULL) { + continue; + } + + /* check the rest of the list */ + list2 = g_list_next(list1); + for (; list2; list2 = g_list_next(list2)) { + file2 = list2->data; + /* must be the same version to link */ + if (file1->last_change != file2->last_change) { + continue; + } + + /* need one and only one to be deleted to link as this + * indicates a renamed-from/renamed-to relationship + * (.d.r -> F..r) */ + if (file1->is_deleted == file2->is_deleted) { + continue; + } + + /* must have same hash to link */ + if (!hash_compare(file2->hash, file1->hash)) { + continue; + } + + file1->rename_peer = file2; + file2->rename_peer = file1; + } + } +} + /* What do we need this for? * * rename_detection has already set up the links in the manifest it