libopkg: check installed reverse dependencies upon install/upgrade
authorJo-Philipp Wich <jo@mein.io>
Tue, 15 Jan 2019 08:19:32 +0000 (09:19 +0100)
committerJo-Philipp Wich <jo@mein.io>
Tue, 15 Jan 2019 19:20:26 +0000 (20:20 +0100)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
libopkg/opkg_install.c
libopkg/pkg.h
libopkg/pkg_depends.c
libopkg/pkg_hash.c

index d2d919a85651b9af41ca12c4bf518b1f143a9886..cc8ba9491ecbcc0f38d18858e640a9be7dab8850 100644 (file)
 #include "xsystem.h"
 #include "libbb/libbb.h"
 
-static int satisfy_dependencies_for(pkg_t * pkg)
+static pkg_vec_t * check_dependencies_for(pkg_t *pkg, bool silent)
 {
-       int i, err;
-       pkg_vec_t *depends = pkg_vec_alloc();
-       pkg_t *dep;
        char **tmp, **unresolved = NULL, *prev = NULL;
-       int ndepends;
+       pkg_vec_t *depends = pkg_vec_alloc();
 
-       ndepends = pkg_hash_fetch_unsatisfied_dependencies(pkg, depends,
-                                                          &unresolved);
+       pkg_hash_fetch_unsatisfied_dependencies(pkg, depends, &unresolved);
 
        if (unresolved) {
-               opkg_msg(ERROR,
-                        "Cannot satisfy the following dependencies for %s:\n",
-                        pkg->name);
+               if (silent == false)
+                       opkg_msg(ERROR,
+                                "Cannot satisfy the following dependencies for %s:\n",
+                                pkg->name);
                tmp = unresolved;
                while (*unresolved) {
                        if (!prev || strcmp(*unresolved, prev))
-                               opkg_message(ERROR, "\t%s\n", *unresolved);
+                               if (silent == false)
+                                       opkg_message(ERROR, "\t%s\n", *unresolved);
                        prev = *unresolved;
                        unresolved++;
                }
@@ -68,19 +66,30 @@ static int satisfy_dependencies_for(pkg_t * pkg)
                        unresolved++;
                }
                free(tmp);
+               pkg_vec_free(depends);
+               return NULL;
+       }
+
+       return depends;
+}
+
+static int satisfy_dependencies_for(pkg_t * pkg)
+{
+       pkg_vec_t *depends;
+       int i, err;
+       pkg_t *dep;
+
+       depends = check_dependencies_for(pkg, false);
+
+       if (depends == NULL) {
                if (!conf->force_depends) {
                        opkg_msg(INFO,
                                 "This could mean that your package list is out of date or that the packages\n"
                                 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
                                 "of this problem try again with the '-force-depends' option.\n");
-                       pkg_vec_free(depends);
-                       return -1;
                }
-       }
 
-       if (ndepends <= 0) {
-               pkg_vec_free(depends);
-               return 0;
+               return -1;
        }
 
        /* Mark packages as to-be-installed */
@@ -144,6 +153,67 @@ static int check_conflicts_for(pkg_t * pkg)
        return 0;
 }
 
+static int check_reverse_depends_for(pkg_t *pkg, pkg_t *old_pkg)
+{
+       int err = 0;
+       int i, n_dependents;
+       abstract_pkg_t **dependents, *dependent;
+       pkg_vec_t *depends;
+       pkg_t *updated;
+
+       opkg_msg(DEBUG, "Checking reverse depends for %s\n", old_pkg->name);
+
+       n_dependents = pkg_has_installed_dependents(old_pkg, &dependents);
+
+       /* pretend that old package does not exist */
+       old_pkg->state_flag |= SF_IGNORE;
+
+       if (n_dependents == 0)
+               goto out;
+
+       while ((dependent = *dependents++) != NULL) {
+               if (dependent->state_status != SS_INSTALLED)
+                       continue;
+
+               if (dependent->pkgs == NULL)
+                       continue;
+
+               for (i = 0; i < dependent->pkgs->len; i++) {
+                       if (dependent->pkgs->pkgs[i]->state_status != SS_INSTALLED)
+                               continue;
+
+                       depends = check_dependencies_for(dependent->pkgs->pkgs[i], true);
+                       if (depends == NULL) {
+#if 0
+                               updated = pkg_hash_fetch_best_installation_candidate_by_name(dependent->pkgs->pkgs[i]->name);
+                               depends = updated ? check_dependencies_for(updated, false) : NULL;
+                               if (depends == NULL) {
+                                       opkg_msg(ERROR, "Package %s reverse dependency check failed\n",
+                                                pkg->name);
+                                       err = -1;
+                                       goto out;
+                               }
+                               else {
+                                       err = opkg_install_pkg(updated, 1);
+                                       if (err)
+                                               goto out;
+                               }
+#else
+                               opkg_msg(ERROR, "Package %s reverse dependency check failed\n", pkg->name);
+                               err = -1;
+                               goto out;
+#endif
+                       }
+
+                       pkg_vec_free(depends);
+               }
+       }
+
+out:
+       old_pkg->state_flag &= ~SF_IGNORE;
+       return err;
+}
+
 static int update_file_ownership(pkg_t * new_pkg, pkg_t * old_pkg)
 {
        str_list_t *new_list, *old_list;
@@ -1291,6 +1361,10 @@ int opkg_install_pkg(pkg_t * pkg, int from_upgrade)
        pkg->state_want = SW_INSTALL;
        if (old_pkg) {
                old_pkg->state_want = SW_DEINSTALL;     /* needed for check_data_file_clashes of dependencies */
+
+               err = check_reverse_depends_for(pkg, old_pkg);
+               if (err)
+                       return -1;
        }
 
        err = check_conflicts_for(pkg);
index 600fc9eaa25ca362d67c0ac108686b624de9f953..f03bbb4c9a296b1f38fc239b9cffb2ade02fe5d6 100644 (file)
@@ -59,6 +59,7 @@ enum pkg_state_flag {
        SF_FILELIST_CHANGED = 128,      /* needs filelist written */
        SF_USER = 256,
        SF_NEED_DETAIL = 512,
+       SF_IGNORE = 1024,               /* ignore this package in dependency checks */
        SF_LAST_STATE_FLAG
 };
 typedef enum pkg_state_flag pkg_state_flag_t;
index 711283ae874872ba16a1e206c88905d504e6499b..9fc7251989890e76d447e5430134e0574da056cb 100644 (file)
@@ -37,7 +37,8 @@ static int pkg_installed_and_constraint_satisfied(pkg_t * pkg, void *cdata)
        depend_t *depend = (depend_t *) cdata;
        if ((pkg->state_status == SS_INSTALLED
             || pkg->state_status == SS_UNPACKED)
-           && version_constraints_satisfied(depend, pkg))
+           && version_constraints_satisfied(depend, pkg)
+           && !(pkg->state_flag & SF_IGNORE))
                return 1;
        else
                return 0;
@@ -46,7 +47,8 @@ static int pkg_installed_and_constraint_satisfied(pkg_t * pkg, void *cdata)
 static int pkg_constraint_satisfied(pkg_t * pkg, void *cdata)
 {
        depend_t *depend = (depend_t *) cdata;
-       if (version_constraints_satisfied(depend, pkg))
+       if (version_constraints_satisfied(depend, pkg)
+           && !(pkg->state_flag & SF_IGNORE))
                return 1;
        else
                return 0;
index 611f3b901f5e6bd03a9cef4e7ac376904d1dfcb2..a474a186687f3f61ea18ee024dc24ca171a01d51 100644 (file)
@@ -357,12 +357,33 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg,
                /* now check for supported architecture */
                {
                        int max_count = 0;
+                       int found_apkg = 0;
 
                        /* count packages matching max arch priority and keep track of last one */
                        for (j = 0; j < vec->len; j++) {
                                pkg_t *maybe = vec->pkgs[j];
                                arch_priority = pkg_get_arch_priority(maybe);
 
+                               /* check if this package actually provides the requested
+                                  abstract package */
+                               found_apkg = 0;
+                               provided_apkgs = pkg_get_ptr(maybe, PKG_PROVIDES);
+
+                               while (provided_apkgs && *provided_apkgs) {
+                                       if (!strcmp((*provided_apkgs)->name, apkg->name)) {
+                                               found_apkg = 1;
+                                               break;
+                                       }
+                                       provided_apkgs++;
+                               }
+
+                               if (found_apkg == 0) {
+                                       opkg_msg(DEBUG,
+                                               "%s version=%s skipped since it does not provide %s\n",
+                                               maybe->name, pkg_get_string(maybe, PKG_VERSION), apkg->name);
+                                       continue;
+                               }
+
                                opkg_msg(DEBUG,
                                         "%s arch=%s arch_priority=%d version=%s.\n",
                                         maybe->name, pkg_get_architecture(maybe),