introduce the active_list for searching.
[oweals/opkg-lede.git] / libopkg / opkg_upgrade.c
index c8b3f1925d7ea89034de4bb0b6260686966e1d38..0861dfeb418a5cd2e8b4e6844c8e409ae7553cb8 100644 (file)
@@ -1,4 +1,4 @@
-/* opkg_upgrade.c - the itsy package management system
+/* opkg_upgrade.c - the opkg package management system
 
    Carl D. Worth
    Copyright (C) 2001 University of Southern California
@@ -33,7 +33,7 @@ int opkg_upgrade_pkg(opkg_conf_t *conf, pkg_t *old)
           return 0;
      }
 
-     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name);
+     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name, NULL);
      if (new == NULL) {
           old_version = pkg_version_str_alloc(old);
           opkg_message(conf, OPKG_NOTICE,
@@ -72,6 +72,53 @@ int opkg_upgrade_pkg(opkg_conf_t *conf, pkg_t *old)
           old->state_want = SW_DEINSTALL;
      }
 
-     new->state_flag |= SF_USER;
-     return opkg_install_pkg(conf, new,1);
+    free(old_version);
+    free(new_version);
+    new->state_flag |= SF_USER;
+    return opkg_install_pkg(conf, new,1);
+}
+
+
+static void pkg_hash_check_installed_pkg_helper(const char *pkg_name, void *entry, void *data) {
+    struct active_list * head = (struct active_list *) data;
+    abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
+    pkg_vec_t *pkg_vec = ab_pkg->pkgs;
+    int j;
+    if (pkg_vec) {
+        for (j = 0; j < pkg_vec->len; j++) {
+            pkg_t *pkg = pkg_vec->pkgs[j];
+            if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
+                active_list_add(head, &pkg->list);
+            }
+        }
+    }
+}
+
+struct active_list * prepare_upgrade_list (opkg_conf_t *conf) {
+    struct active_list * head = active_list_head_new();
+    struct active_list * all = active_list_head_new();
+    struct active_list *node=NULL;
+
+    /* ensure all data is valid */
+    pkg_info_preinstall_check (conf);
+
+    hash_table_foreach(&conf->pkg_hash, pkg_hash_check_installed_pkg_helper, all);
+    for (node=active_list_next(all,all); node; node = active_list_next(all, node)) {
+        pkg_t *old, *new;
+        int cmp;
+
+        old = list_entry(node, pkg_t, list);
+        new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name, NULL);
+
+        if (new == NULL)
+            continue;
+
+        cmp = pkg_compare_versions(old, new);
+
+        if ( cmp < 0 ) {
+           node = active_list_move_node(all, head, &old->list); 
+        }
+    }
+    active_list_head_delete(all);
+    return head;
 }