Cleanup parsing of packages.
[oweals/opkg-lede.git] / libopkg / pkg_hash.c
index 580fe6e7f0b1bf4bec92c7b3938c29252bce976f..a445839a9f391d7543919de690b572f8ad153575 100644 (file)
@@ -28,6 +28,7 @@
 #include "pkg_hash.h"
 #include "pkg_parse.h"
 #include "opkg_utils.h"
+#include "libbb/libbb.h"
 
 static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
 
@@ -47,7 +48,6 @@ static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const
  */
 
 
-\f
 int pkg_hash_init(const char *name, hash_table_t *hash, int len)
 {
   return hash_table_init(name, hash, len);
@@ -73,7 +73,9 @@ void free_pkgs (const char *key, void *entry, void *data)
   }
 
   abstract_pkg_vec_free (ab_pkg->provided_by);
+  abstract_pkg_vec_free (ab_pkg->replaced_by);
   pkg_vec_free (ab_pkg->pkgs);
+  free (ab_pkg->depended_upon_by);
   free (ab_pkg->name);
   free (ab_pkg);
 }
@@ -92,10 +94,8 @@ static char *pkg_get_default_arch(opkg_conf_t *conf)
      char *def_arch = HOST_CPU_STR;            /* Default arch */
      int def_prio = 0;                         /* Other archs override this */
 
-     l = conf->arch_list.head;
-
-     while (l) {
-         nv_pair_t *nv = l->data;
+     list_for_each_entry(l , &conf->arch_list.head, node) {
+         nv_pair_t *nv = (nv_pair_t *)l->data;
          int priority = strtol(nv->value, NULL, 0);
 
          /* Check if this arch has higher priority, and is valid */
@@ -105,51 +105,61 @@ static char *pkg_get_default_arch(opkg_conf_t *conf)
               def_prio = priority;
               def_arch = nv->name;
          }
-         l = l->next;
      }
 
-     return strdup(def_arch);
+     return xstrdup(def_arch);
 }
 
 int pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name,
                           pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
 {
-     hash_table_t *hash = &conf->pkg_hash;
-     char **raw;
-     char **raw_start;
-     pkg_t *pkg;
-    
-     raw = raw_start = read_raw_pkgs_from_file(file_name);
-     if (!raw)
-        return -ENOMEM;
-
-     while(*raw){         /* don't worry, we'll increment raw in the parsing function */
-         pkg = pkg_new();
-         if (!pkg)
-              return -ENOMEM;
-
-         if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
-              if (!pkg->architecture) {
-                   char *version_str = pkg_version_str_alloc(pkg);
-                   pkg->architecture = pkg_get_default_arch(conf);
-                   opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
-                                pkg->name, version_str, pkg->architecture);
-                   free(version_str);
-              }
-              hash_insert_pkg(hash, pkg, is_status_file,conf);
-         } else {
-              free(pkg);
-         }
-     }
+       hash_table_t *hash = &conf->pkg_hash;
+       pkg_t *pkg;
+       FILE *fp;
+       char *buf;
+       const size_t len = 4096;
+       int ret = 0;
+
+       fp = fopen(file_name, "r");
+       if (fp == NULL) {
+               fprintf(stderr, "%s: fopen(%s): %s\n",
+                            __FUNCTION__, file_name, strerror(errno));
+               return -1;
+       }
 
-     /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
-       memory after read_raw_pkgs_from_file */
-     raw = raw_start;
-     while (*raw) {
-         free(*raw++);
-     }
-     free(raw_start);
-     return 0;
+       buf = xmalloc(len);
+
+       do {
+               pkg = pkg_new();
+               pkg->src = src;
+               pkg->dest = dest;
+
+               ret = pkg_parse_from_stream_nomalloc(pkg, fp, PFM_ALL,
+                               &buf, len);
+               if (ret) {
+                       pkg_deinit (pkg);
+                       free(pkg);
+                       if (ret == -1)
+                               break;
+                       continue;
+               }
+
+               if (!pkg->architecture) {
+                       char *version_str = pkg_version_str_alloc(pkg);
+                       pkg->architecture = pkg_get_default_arch(conf);
+                       opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
+                       pkg->name, version_str, pkg->architecture);
+                       free(version_str);
+               }
+
+               hash_insert_pkg(hash, pkg, is_status_file, conf);
+
+       } while (!feof(fp));
+
+       free(buf);
+       fclose(fp);
+
+       return ret;
 }
 
 abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
@@ -167,7 +177,7 @@ abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *has
 
 
 pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pkg_t *apkg, 
-                                                 int (*constraint_fcn)(pkg_t *pkg, void *cdata), void *cdata, int quiet)
+                                                 int (*constraint_fcn)(pkg_t *pkg, void *cdata), void *cdata, int quiet, int *err)
 {
      int i; 
      int nprovides = 0;
@@ -179,11 +189,14 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pk
      abstract_pkg_vec_t *providers = abstract_pkg_vec_alloc();
      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 (matching_apkgs == NULL || providers == NULL || 
-         apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
+     if (err)
+       *err = 0;
+
+     if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
          return NULL;
 
      opkg_message(conf, OPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
@@ -242,12 +255,21 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pk
                    pkg_t *maybe = vec->pkgs[i];
                    opkg_message(conf, OPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
                                 maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
-                   if (maybe->arch_priority > 0)  {
+                    /* We make sure not to add the same package twice. Need to search for the reason why 
+                       they show up twice sometimes. */
+                   if ((maybe->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)
+               {
+                 opkg_message (conf, OPKG_ERROR, "Packages were found, but none compatible with the architectures configured\n");
+                 if (err)
+                   *err = OPKG_PKG_HAS_NO_AVAILABLE_ARCH;
+               }
          }
      }
 
@@ -284,21 +306,24 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pk
               latest_installed_parent = matching;
          if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
               if (held_pkg)
-                   opkg_message(conf, OPKG_ERROR, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER.  Using latest.\n",
+                   opkg_message(conf, OPKG_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) {
-         opkg_message(conf, OPKG_ERROR, "Package=%s, %d matching providers\n",
-                      apkg->name, matching_apkgs->len);
-         for (i = 0; i < matching_apkgs->len; i++) {
-              abstract_pkg_t *matching = matching_apkgs->pkgs[i];
-              opkg_message(conf, OPKG_ERROR, "    %s\n", matching->name);
-         }
-         opkg_message(conf, OPKG_ERROR, "Please select one with opkg install or opkg flag prefer\n");
-     }
+          int prio = 0;
+          for (i = 0; i < matching_pkgs->len; i++) {
+              pkg_t *matching = matching_pkgs->pkgs[i];
+                  if (matching->arch_priority > prio) {
+                      priorized_matching = matching;
+                      prio = matching->arch_priority;
+                      opkg_message(conf, OPKG_DEBUG, "Match with priority %i    %s\n", prio, matching->name);
+                  }
+              }
+          
+          }
 
      if (matching_apkgs->len > 1 && conf->verbosity > 1) {
          opkg_message(conf, OPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
@@ -327,6 +352,11 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pk
          opkg_message(conf, OPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
          return latest_installed_parent;
      }
+     if (priorized_matching) {
+         opkg_message(conf, OPKG_INFO, "  using priorized matching %s %s %s\n",
+                      priorized_matching->name, priorized_matching->version, priorized_matching->architecture);
+         return priorized_matching;
+     }
      if (nmatching > 1) {
          opkg_message(conf, OPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
          return NULL;
@@ -348,15 +378,18 @@ static int pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
          return 0;   
 }
 
-pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(opkg_conf_t *conf, const char *name)
+pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(opkg_conf_t *conf, const char *name, int *err)
 {
      hash_table_t *hash = &conf->pkg_hash;
      abstract_pkg_t *apkg = NULL;
+     pkg_t *ret;
 
      if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
          return NULL;
-     
-     return pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0);
+
+     ret = pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0, err);
+
+     return ret;
 }
 
 
@@ -365,7 +398,7 @@ pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash,
                                       const char * version)
 {
     pkg_vec_t * vec;
-    register int i;
+    int i;
     char *version_str = NULL;
     
     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
@@ -391,7 +424,7 @@ pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
                                             pkg_dest_t *dest)
 {
     pkg_vec_t * vec;
-    register int i;
+    int i;
 
     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
        return NULL;
@@ -408,7 +441,7 @@ pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
                                        const char *pkg_name)
 {
     pkg_vec_t * vec;
-    register int i;
+    int i;
 
     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))){
        return NULL;
@@ -444,17 +477,6 @@ pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
     }
 }
 
-static int pkg_compare_names(const void *p1, const void *p2)
-{
-  const pkg_t *pkg1 = *(const pkg_t **)p1;
-  const pkg_t *pkg2 = *(const pkg_t **)p2;
-  if (pkg1->name == NULL)
-    return 1;
-  if (pkg2->name == NULL)
-    return -1;
-  return(strcmp(pkg1->name, pkg2->name));
-}
-
 
 static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
 {
@@ -473,7 +495,6 @@ static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, v
 void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
 {
     hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
-    qsort(all->pkgs, all->len, sizeof(pkg_t *), pkg_compare_names);
 }
 
 static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
@@ -494,7 +515,6 @@ static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entr
 void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
 {
     hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
-    qsort(all->pkgs, all->len, sizeof(void*), pkg_compare_names);
 }
 
 static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
@@ -514,7 +534,7 @@ static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
   if (dependents != NULL)
     while (dependents [i] != NULL && i < ab_pkg->provided_by->len)
       printf ("\tprovided by - %s\n", dependents [i ++]->name);
-  pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name);
+  pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name, NULL);
   if (pkg) {
     i = 0;
     while (i < pkg->depends_count)
@@ -549,10 +569,8 @@ pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,opkg_conf_
 
      arch_priority = pkg->arch_priority;
 
-     if (buildDepends(hash, pkg)<0){
-        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
-        return NULL;
-     }
+     buildDepends(hash, pkg);
+
      ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
 
      if (set_status) {
@@ -566,25 +584,19 @@ pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,opkg_conf_
      if(!ab_pkg->pkgs)
          ab_pkg->pkgs = pkg_vec_alloc();
     
+     buildProvides(hash, ab_pkg, pkg);
+
+     /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
+     buildConflicts(hash, ab_pkg, pkg);
+
+     buildReplaces(hash, ab_pkg, pkg);
+
+     buildDependedUponBy(pkg, ab_pkg);
+
      /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
      pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
      pkg->parent = ab_pkg;
 
-     if (buildProvides(hash, ab_pkg, pkg)<0){
-        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
-        return NULL;
-     }
-     /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
-     if (buildConflicts(hash, ab_pkg, pkg)<0){
-        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
-        return NULL;
-     }
-     if (buildReplaces(hash, ab_pkg, pkg)<0) {
-        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
-        return NULL;
-     }
-    
-     buildDependedUponBy(pkg, ab_pkg);
      return pkg;
 }
 
@@ -599,7 +611,7 @@ static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const
   ab_pkg = abstract_pkg_new();
   if (ab_pkg == NULL) { return NULL; }
 
-  ab_pkg->name = strdup(pkg_name);
+  ab_pkg->name = xstrdup(pkg_name);
   hash_table_insert(hash, pkg_name, ab_pkg);
 
   return ab_pkg;
@@ -632,10 +644,13 @@ int file_hash_set_file_owner(opkg_conf_t *conf, const char *file_name, pkg_t *ow
      // opkg_message(conf, OPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
      hash_table_insert(file_hash, file_name, owning_pkg); 
      if (old_owning_pkg) {
+         pkg_get_installed_files(old_owning_pkg);
          str_list_remove_elt(old_owning_pkg->installed_files, file_name);
+         pkg_free_installed_files(old_owning_pkg);
          /* mark this package to have its filelist written */
          old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
          owning_pkg->state_flag |= SF_FILELIST_CHANGED;
+         
      }
      return 0;
 }