diff --git a/sysinstall/hooks.c b/sysinstall/hooks.c index 4d74b316..3af2f307 100644 --- a/sysinstall/hooks.c +++ b/sysinstall/hooks.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Jonas 'Sortie' Termansen. + * Copyright (c) 2016, 2017 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -51,12 +51,8 @@ void upgrade_finalize(const struct release* old_release, (void) target_prefix; // TODO: After releasing Sortix 1.1, remove this compatibility. - if ( old_release->version_major < 1 || - (old_release->version_major == 1 && - old_release->version_minor < 1) || - (old_release->version_major == 1 && - old_release->version_minor == 1 && - old_release->version_dev) ) + if ( version_compare(old_release->version_major, old_release->version_minor, + old_release->version_dev, 1, 1, false) < 0 ) // < 1.1 { char* random_seed_path; if ( asprintf(&random_seed_path, "%sboot/random.seed", target_prefix) < 0 ) diff --git a/sysinstall/release.c b/sysinstall/release.c index 3582db39..0c476d9a 100644 --- a/sysinstall/release.c +++ b/sysinstall/release.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2016 Jonas 'Sortie' Termansen. + * Copyright (c) 2015, 2016, 2017 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -27,6 +27,44 @@ #include "release.h" +int abi_compare(unsigned long a_major, unsigned long a_minor, + unsigned long b_major, unsigned long b_minor) +{ + if ( a_major < b_major ) + return -1; + if ( a_major > b_major ) + return 1; + if ( a_minor < b_minor ) + return -1; + if ( a_minor > b_minor ) + return 1; + return 0; +} + +bool abi_compatible(unsigned long a_major, unsigned long a_minor, + unsigned long b_major, unsigned long b_minor) +{ + return a_major == b_major && a_minor <= b_minor; +} + +int version_compare(unsigned long a_major, unsigned long a_minor, bool a_dev, + unsigned long b_major, unsigned long b_minor, bool b_dev) +{ + if ( a_major < b_major ) + return -1; + if ( a_major > b_minor ) + return 1; + if ( a_minor < b_minor ) + return -1; + if ( a_minor > b_minor ) + return 1; + if ( a_dev && !b_dev ) + return -1; + if ( !a_dev && b_dev ) + return 1; + return 0; +} + void release_free(struct release* release) { free(release->pretty_name); diff --git a/sysinstall/release.h b/sysinstall/release.h index bc8f399c..54c62331 100644 --- a/sysinstall/release.h +++ b/sysinstall/release.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2016 Jonas 'Sortie' Termansen. + * Copyright (c) 2015, 2016, 2017 Jonas 'Sortie' Termansen. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -30,7 +30,15 @@ struct release unsigned long abi_minor; }; +int abi_compare(unsigned long a_major, unsigned long a_minor, + unsigned long b_major, unsigned long b_minor); +bool abi_compatible(unsigned long a_major, unsigned long a_minor, + unsigned long b_major, unsigned long b_minor); +int version_compare(unsigned long a_major, unsigned long a_minor, bool a_dev, + unsigned long b_major, unsigned long b_minor, bool b_dev); void release_free(struct release* release); +int release_compare_abi(const struct release* a, const struct release* b); +int release_compare_version(const struct release* a, const struct release* b); bool os_release_load(struct release* release, const char* path, const char* errpath); diff --git a/sysinstall/sysmerge.c b/sysinstall/sysmerge.c index 49db562e..1eff27be 100644 --- a/sysinstall/sysmerge.c +++ b/sysinstall/sysmerge.c @@ -213,8 +213,9 @@ int main(int argc, char* argv[]) struct conf conf; load_upgrade_conf(&conf, "/etc/upgrade.conf"); - bool can_run_new_abi = new_release.abi_major == old_release.abi_major && - new_release.abi_minor <= old_release.abi_minor; + bool can_run_new_abi = + abi_compatible(new_release.abi_major, new_release.abi_minor, + old_release.abi_major, old_release.abi_minor); bool header; bool copy_files; diff --git a/sysinstall/sysupgrade.c b/sysinstall/sysupgrade.c index 9e462f59..d75b8e92 100644 --- a/sysinstall/sysupgrade.c +++ b/sysinstall/sysupgrade.c @@ -626,9 +626,8 @@ int main(void) text("\n"); } - if ( new_release.abi_major < target_release->abi_major || - (target_release->abi_major == new_release.abi_major && - new_release.abi_minor < target_release->abi_minor) ) + if ( abi_compare(new_release.abi_major, new_release.abi_minor, + target_release->abi_major, target_release->abi_minor) < 0 ) { text("Warning: You are downgrading an existing installation to an " "release with an earlier ABI. This is not supported and there is " @@ -646,8 +645,9 @@ int main(void) text("\n"); } - bool can_run_old_abi = target_release->abi_major == new_release.abi_major && - target_release->abi_minor <= new_release.abi_minor; + bool can_run_old_abi = + abi_compatible(target_release->abi_major, target_release->abi_minor, + new_release.abi_major, new_release.abi_minor); mountpoints = target->mountpoints; mountpoints_used = target->mountpoints_used;