opkg: mark one line that cause crash.
[project/opkg-lede.git] / libopkg / pkg.c
index af2dd834c22d404191a8b0b09dd7c1c1ca581fc2..10427ed3bad4d0f7e66a2825f7fc4b582fa59f2e 100644 (file)
@@ -1,4 +1,4 @@
-/* pkg.c - the itsy package management system
+/* pkg.c - the opkg package management system
 
    Carl D. Worth
 
@@ -15,7 +15,7 @@
    General Public License for more details.
 */
 
-#include "opkg.h"
+#include "includes.h"
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
@@ -94,7 +94,6 @@ int pkg_init(pkg_t *pkg)
      pkg->epoch = 0;
      pkg->version = NULL;
      pkg->revision = NULL;
-     pkg->familiar_revision = NULL;
      pkg->dest = NULL;
      pkg->src = NULL;
      pkg->architecture = NULL;
@@ -142,17 +141,31 @@ int pkg_init(pkg_t *pkg)
      return 0;
 }
 
+void compound_depend_deinit (compound_depend_t *depends)
+{
+    int i;
+    for (i = 0; i < depends->possibility_count; i++)
+    {
+        depend_t *d;
+        d = depends->possibilities[i];
+        free (d->version);
+        free (d);
+    }
+    free (depends->possibilities);
+}
+
 void pkg_deinit(pkg_t *pkg)
 {
+     int i;
+
      free(pkg->name);
      pkg->name = NULL;
      pkg->epoch = 0;
      free(pkg->version);
      pkg->version = NULL;
-     /* revision and familiar_revision share storage with version, so
+     /* revision shares storage with version, so
        don't free */
      pkg->revision = NULL;
-     pkg->familiar_revision = NULL;
      /* owned by opkg_conf_t */
      pkg->dest = NULL;
      /* owned by opkg_conf_t */
@@ -168,18 +181,65 @@ void pkg_deinit(pkg_t *pkg)
      pkg->state_want = SW_UNKNOWN;
      pkg->state_flag = SF_OK;
      pkg->state_status = SS_NOT_INSTALLED;
+
+     //for (i = 0; i < pkg->replaces_count; i++)
+     free (pkg->replaces);
+     pkg->replaces = NULL;
+
+     for (i = 0; i < pkg->depends_count; i++)
+       free (pkg->depends_str[i]);
      free(pkg->depends_str);
      pkg->depends_str = NULL;
+
+     for (i = 0; i < pkg->provides_count; i++)
+       free (pkg->provides_str[i]);
      free(pkg->provides_str);
      pkg->provides_str = NULL;
-     pkg->depends_count = 0;
-     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->depends ? */
+
+     for (i = 0; i < pkg->conflicts_count; i++)
+       free (pkg->conflicts_str[i]);
+     free(pkg->conflicts_str);
+     pkg->conflicts_str = NULL;
+
+     for (i = 0; i < pkg->replaces_count; i++)
+       free (pkg->replaces_str[i]);
+     free(pkg->replaces_str);
+     pkg->replaces_str = NULL;
+
+     for (i = 0; i < pkg->recommends_count; i++)
+       free (pkg->recommends_str[i]);
+     free(pkg->recommends_str);
+     pkg->recommends_str = NULL;
+
+     for (i = 0; i < pkg->suggests_count; i++)
+       free (pkg->suggests_str[i]);
+     free(pkg->suggests_str);
+     pkg->suggests_str = NULL;
+
+     if (pkg->depends)
+     {
+       int count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
+       int x;
+
+       for (x = 0; x < count; x++)
+        compound_depend_deinit (&pkg->depends[x]);
+       free (pkg->depends);
+     }
+
+     if (pkg->conflicts)
+     {
+       int x;
+       for (x = 0; x < pkg->conflicts_count; x++)
+         compound_depend_deinit (&pkg->conflicts[x]);
+       free (pkg->conflicts);
+     }
+
+     free (pkg->provides);
+
      pkg->pre_depends_count = 0;
      free(pkg->pre_depends_str);
      pkg->pre_depends_str = NULL;
      pkg->provides_count = 0;
-     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->provides ? */
-     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->suggests ? */
      free(pkg->filename);
      pkg->filename = NULL;
      free(pkg->local_filename);
@@ -1019,7 +1079,7 @@ void pkg_print_status(pkg_t * pkg, FILE * file)
        can be found in th available file.
 
        But, someone proposed the idea to make it possible to
-       reconstruct a .ipk from an installed package, (ie. for beaming
+       reconstruct a .opk from an installed package, (ie. for beaming
        from one handheld to another). So, maybe we actually want a few
        more fields here, (depends, suggests, etc.), so that that would
        be guaranteed to work even in the absence of more information
@@ -1159,10 +1219,10 @@ int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op)
      return 0;
 }
 
-int pkg_name_version_and_architecture_compare(void *p1, void *p2)
+int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
 {
-     const pkg_t *a = *(const pkg_t **)p1;
-     const pkg_t *b = *(const pkg_t **)p2;
+     const pkg_t *a = *(const pkg_t**) p1;
+     const pkg_t *b = *(const pkg_t**) p2;
      int namecmp;
      int vercmp;
      if (!a->name || !b->name) {
@@ -1189,7 +1249,7 @@ int pkg_name_version_and_architecture_compare(void *p1, void *p2)
      return 0;
 }
 
-int abstract_pkg_name_compare(void *p1, void *p2)
+int abstract_pkg_name_compare(const void *p1, const void *p2)
 {
      const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
      const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
@@ -1349,6 +1409,7 @@ int pkg_free_installed_files(pkg_t *pkg)
          }
 
          str_list_deinit(pkg->installed_files);
+         free (pkg->installed_files);
      }
 
      pkg->installed_files = NULL;
@@ -1652,7 +1713,7 @@ int pkg_info_preinstall_check(opkg_conf_t *conf)
          pkg_t *pkg = available_pkgs->pkgs[i];
          if (!pkg->arch_priority && (pkg->state_flag || (pkg->state_want != SW_UNKNOWN))) {
               /* clear flags and want for any uninstallable package */
-              opkg_message(conf, OPKG_NOTICE, "Clearing state_want and state_flag for pkg=%s (arch_priority=%d flag=%d want=%d)\n", 
+              opkg_message(conf, OPKG_DEBUG, "Clearing state_want and state_flag for pkg=%s (arch_priority=%d flag=%d want=%d)\n", 
                            pkg->name, pkg->arch_priority, pkg->state_flag, pkg->state_want);
               pkg->state_want = SW_UNKNOWN;
               pkg->state_flag = 0;
@@ -1676,6 +1737,8 @@ int pkg_info_preinstall_check(opkg_conf_t *conf)
               // opkg_message(conf, OPKG_DEBUG2, "pkg %s: file=%s\n", pkg->name, installed_file);
               file_hash_set_file_owner(conf, installed_file, pkg);
          }
+         //FIXME: mark this line. Thid avoid crash, But the reference count shall be balanced. (If there are some delay after file_hash_set_file_owner, it wont crash (Pondering why?))
+         //pkg_free_installed_files(pkg);
      }
      pkg_vec_free(installed_pkgs);
 
@@ -1751,5 +1814,6 @@ int pkg_write_changed_filelists(opkg_conf_t *conf)
                    opkg_message(conf, OPKG_NOTICE, "pkg_write_filelist pkg=%s returned %d\n", pkg->name, err);
          }
      }
+     pkg_vec_free (installed_pkgs);
      return 0;
 }