+ int i, j;
+ int nprovides = 0;
+ int nmatching = 0;
+ int wrong_arch_found = 0;
+ int arch_priority;
+ pkg_vec_t *matching_pkgs;
+ abstract_pkg_vec_t *matching_apkgs;
+ abstract_pkg_vec_t *provided_apkg_vec;
+ abstract_pkg_t **provided_apkgs;
+ abstract_pkg_vec_t *providers;
+ pkg_t *latest_installed_parent = NULL;
+ pkg_t *latest_matching = NULL;
+ pkg_t *priorized_matching = NULL;
+ pkg_t *held_pkg = NULL;
+ pkg_t *good_pkg_by_name = NULL;
+
+ if (apkg == NULL || apkg->provided_by == NULL
+ || (apkg->provided_by->len == 0))
+ return NULL;
+
+ matching_pkgs = pkg_vec_alloc();
+ matching_apkgs = abstract_pkg_vec_alloc();
+ providers = abstract_pkg_vec_alloc();
+
+ opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
+
+ provided_apkg_vec = apkg->provided_by;
+ nprovides = provided_apkg_vec->len;
+ provided_apkgs = provided_apkg_vec->pkgs;
+ if (nprovides > 1)
+ opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name,
+ nprovides);
+
+ /* accumulate all the providers */
+ for (i = 0; i < nprovides; i++) {
+ abstract_pkg_t *provider_apkg = provided_apkgs[i];
+ opkg_msg(DEBUG, "Adding %s to providers.\n",
+ provider_apkg->name);
+ abstract_pkg_vec_insert(providers, provider_apkg);
+ }
+ nprovides = providers->len;
+
+ for (i = 0; i < nprovides; i++) {
+ abstract_pkg_t *provider_apkg =
+ abstract_pkg_vec_get(providers, i);
+ abstract_pkg_t *replacement_apkg = NULL;
+ pkg_vec_t *vec;
+
+ if (provider_apkg->replaced_by
+ && provider_apkg->replaced_by->len) {
+ replacement_apkg = provider_apkg->replaced_by->pkgs[0];
+ if (provider_apkg->replaced_by->len > 1) {
+ opkg_msg(NOTICE, "Multiple replacers for %s, "
+ "using first one (%s).\n",
+ provider_apkg->name,
+ replacement_apkg->name);
+ }
+ }
+
+ if (replacement_apkg)
+ opkg_msg(DEBUG,
+ "replacement_apkg=%s for provider_apkg=%s.\n",
+ replacement_apkg->name, provider_apkg->name);
+
+ if (replacement_apkg && (replacement_apkg != provider_apkg)) {
+ if (abstract_pkg_vec_contains
+ (providers, replacement_apkg))
+ continue;
+ else
+ provider_apkg = replacement_apkg;
+ }
+
+ if (!(vec = provider_apkg->pkgs)) {
+ opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
+ provider_apkg->name);
+ continue;
+ }
+
+ /* now check for supported architecture */
+ {
+ int max_count = 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);
+
+ opkg_msg(DEBUG,
+ "%s arch=%s arch_priority=%d version=%s.\n",
+ maybe->name, pkg_get_architecture(maybe),
+ arch_priority, pkg_get_string(maybe, PKG_VERSION));
+ /* We make sure not to add the same package twice. Need to search for the reason why
+ they show up twice sometimes. */
+ if ((arch_priority > 0)
+ &&
+ (!pkg_vec_contains(matching_pkgs, maybe))) {
+ max_count++;
+ abstract_pkg_vec_insert(matching_apkgs,
+ maybe->parent);
+ pkg_vec_insert(matching_pkgs, maybe);
+ }
+ }
+
+ if (vec->len > 0 && matching_pkgs->len < 1)
+ wrong_arch_found = 1;
+ }
+ }
+
+ if (matching_pkgs->len < 1) {
+ if (wrong_arch_found)
+ opkg_msg(ERROR, "Packages for %s found, but"
+ " incompatible with the architectures configured\n",
+ apkg->name);
+ pkg_vec_free(matching_pkgs);
+ abstract_pkg_vec_free(matching_apkgs);
+ abstract_pkg_vec_free(providers);
+ return NULL;
+ }
+
+ if (matching_pkgs->len > 1)
+ pkg_vec_sort(matching_pkgs,
+ pkg_name_version_and_architecture_compare);
+ if (matching_apkgs->len > 1)
+ abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
+
+ for (i = 0; i < matching_pkgs->len; i++) {
+ pkg_t *matching = matching_pkgs->pkgs[i];
+ if (constraint_fcn(matching, cdata)) {
+ opkg_msg(DEBUG, "Candidate: %s %s.\n",
+ matching->name, pkg_get_string(matching, PKG_VERSION));
+ good_pkg_by_name = matching;
+ /* It has been provided by hand, so it is what user want */
+ if (matching->provided_by_hand == 1)
+ break;
+ }
+ }
+
+ for (i = 0; i < matching_pkgs->len; i++) {
+ pkg_t *matching = matching_pkgs->pkgs[i];
+ latest_matching = matching;
+ if (matching->parent->state_status == SS_INSTALLED
+ || matching->parent->state_status == SS_UNPACKED)
+ latest_installed_parent = matching;
+ if (matching->state_flag & (SF_HOLD | SF_PREFER)) {
+ if (held_pkg)
+ opkg_msg(NOTICE,
+ "Multiple packages (%s and %s) providing"
+ " same name marked HOLD or PREFER. "
+ "Using latest.\n", held_pkg->name,
+ matching->name);
+ held_pkg = matching;
+ }
+ }
+
+ if (!good_pkg_by_name && !held_pkg && !latest_installed_parent
+ && matching_apkgs->len > 1 && !quiet) {
+ int prio = 0;
+ for (i = 0; i < matching_pkgs->len; i++) {
+ pkg_t *matching = matching_pkgs->pkgs[i];
+ arch_priority = pkg_get_arch_priority(matching);
+ if (arch_priority > prio) {
+ priorized_matching = matching;
+ prio = arch_priority;
+ opkg_msg(DEBUG, "Match %s with priority %i.\n",
+ matching->name, prio);
+ }
+ }
+
+ }
+
+ if (conf->verbosity >= INFO && matching_apkgs->len > 1) {
+ opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n",
+ matching_pkgs->len, apkg->name);
+ for (i = 0; i < matching_pkgs->len; i++) {
+ pkg_t *matching = matching_pkgs->pkgs[i];
+ opkg_msg(INFO, "%s %s %s\n",
+ matching->name, pkg_get_string(matching, PKG_VERSION),
+ pkg_get_architecture(matching));
+ }
+ }
+
+ nmatching = matching_apkgs->len;
+
+ pkg_vec_free(matching_pkgs);
+ abstract_pkg_vec_free(matching_apkgs);
+ abstract_pkg_vec_free(providers);
+
+ if (good_pkg_by_name) { /* We found a good candidate, we will install it */
+ return good_pkg_by_name;
+ }
+ if (held_pkg) {
+ opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
+ return held_pkg;
+ }
+ if (latest_installed_parent) {
+ opkg_msg(INFO,
+ "Using latest version of installed package %s.\n",
+ latest_installed_parent->name);
+ return latest_installed_parent;
+ }
+ if (priorized_matching) {
+ opkg_msg(INFO, "Using priorized matching %s %s %s.\n",
+ priorized_matching->name, pkg_get_string(priorized_matching, PKG_VERSION),
+ pkg_get_architecture(priorized_matching));
+ return priorized_matching;
+ }
+ if (nmatching > 1) {
+ opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
+ nmatching);
+ return NULL;
+ }
+ if (latest_matching) {
+ opkg_msg(INFO, "Using latest matching %s %s %s.\n",
+ latest_matching->name, pkg_get_string(latest_matching, PKG_VERSION),
+ pkg_get_architecture(latest_matching));
+ return latest_matching;