Fix pkg_get_installed_files() to work with an offline_root + dest.
[oweals/opkg-lede.git] / libopkg / pkg_hash.c
index d15d441f54df793599179254b3d9e6f23152d369..ace26bf0cbcc90eceafe2effcc72952c6b228899 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);
 
@@ -106,49 +107,59 @@ static char *pkg_get_default_arch(opkg_conf_t *conf)
          }
      }
 
-     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 {
-              pkg_deinit (pkg);
-              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)
@@ -185,8 +196,7 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pk
      if (err)
        *err = 0;
 
-     if (matching_apkgs == NULL || providers == NULL || 
-         apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 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);
@@ -315,8 +325,8 @@ pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pk
           
           }
 
-     if (matching_apkgs->len > 1 && conf->verbosity > 1) {
-         opkg_message(conf, OPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
+     if (conf->verbosity >= OPKG_INFO && matching_apkgs->len > 1) {
+         opkg_message(conf, OPKG_INFO, "%s: for apkg=%s, %d matching pkgs\n",
                       __FUNCTION__, apkg->name, matching_pkgs->len);
          for (i = 0; i < matching_pkgs->len; i++) {
               pkg_t *matching = matching_pkgs->pkgs[i];
@@ -388,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)))
@@ -414,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;
@@ -431,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;
@@ -559,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) {
@@ -576,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;
 }
 
@@ -609,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;
@@ -642,7 +644,7 @@ 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);
+         pkg_get_installed_files(conf, 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 */