libopkg: pkg_hash: prefer to-be-installed packages
[project/opkg-lede.git] / libopkg / pkg_hash.c
index 879c0ec0e693a62ce507c303f6c1fa2249895a23..11b1a06bc83c759efe37c500c0212a3fd20ad8c8 100644 (file)
@@ -26,6 +26,7 @@
 #include "parse_util.h"
 #include "pkg_parse.h"
 #include "opkg_utils.h"
+#include "opkg_cmd.h"
 #include "sprintf_alloc.h"
 #include "file_util.h"
 #include "libbb/libbb.h"
@@ -263,8 +264,10 @@ pkg_hash_check_unresolved(pkg_t *maybe)
        if (unresolved) {
                res = 1;
                tmp = unresolved;
-               while (tmp)
+               while (*tmp) {
+                       opkg_msg(ERROR, "cannot find dependency %s for %s\n", *tmp, maybe->name);
                        free(*(tmp++));
+               }
                free(unresolved);
        }
        pkg_vec_free(depends);
@@ -284,6 +287,7 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg,
        int nmatching = 0;
        int wrong_arch_found = 0;
        int arch_priority;
+       int good_pkg_score = 0;
        pkg_vec_t *matching_pkgs;
        abstract_pkg_vec_t *matching_apkgs;
        abstract_pkg_vec_t *provided_apkg_vec;
@@ -409,9 +413,25 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg,
        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));
+                       int score = 1;
+                       if (strcmp(matching->name, apkg->name) == 0)
+                               score++;
+
+                       for (j = 0; j < opkg_cli_argc; ++j) {
+                               if (!strcmp(matching->name, opkg_cli_argv[j])) {
+                                       score++;
+                                       break;
+                               }
+                       }
+
+                       opkg_msg(DEBUG, "Candidate: %s %s (score %d).\n",
+                                matching->name, pkg_get_string(matching, PKG_VERSION),
+                                score);
+                       if (score < good_pkg_score)
+                               continue;
+
                        good_pkg_by_name = matching;
+                       good_pkg_score = score;
                        /* It has been provided by hand, so it is what user want */
                        if (matching->provided_by_hand == 1)
                                break;