introduce the active_list for searching.
[project/opkg-lede.git] / libopkg / pkg.c
index 10427ed3bad4d0f7e66a2825f7fc4b582fa59f2e..dde3a19f22ad5ea83ec98eaed425af2ecf3df647 100644 (file)
@@ -18,6 +18,7 @@
 #include "includes.h"
 #include <ctype.h>
 #include <string.h>
+#include <stdbool.h>
 #include <errno.h>
 
 #include "pkg.h"
@@ -76,7 +77,7 @@ pkg_t *pkg_new(void)
 {
      pkg_t *pkg;
 
-     pkg = malloc(sizeof(pkg_t));
+     pkg = calloc(1, sizeof(pkg_t));
      if (pkg == NULL) {
          fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
          return NULL;
@@ -89,7 +90,6 @@ pkg_t *pkg_new(void)
 
 int pkg_init(pkg_t *pkg)
 {
-     memset(pkg, 0, sizeof(pkg_t));
      pkg->name = NULL;
      pkg->epoch = 0;
      pkg->version = NULL;
@@ -111,6 +111,9 @@ int pkg_init(pkg_t *pkg)
      pkg->recommends_str = NULL;
      pkg->suggests_count = 0;
      pkg->recommends_count = 0;
+     
+     active_list_init(&pkg->list);
+     active_list_init(&pkg->searched_node);
 
      /* Abhaya: added init for conflicts fields */
      pkg->conflicts = NULL;
@@ -182,7 +185,8 @@ void pkg_deinit(pkg_t *pkg)
      pkg->state_flag = SF_OK;
      pkg->state_status = SS_NOT_INSTALLED;
 
-     //for (i = 0; i < pkg->replaces_count; i++)
+     active_list_clear(&pkg->list);
+
      free (pkg->replaces);
      pkg->replaces = NULL;
 
@@ -428,7 +432,7 @@ abstract_pkg_t *abstract_pkg_new(void)
 {
      abstract_pkg_t * ab_pkg;
 
-     ab_pkg = malloc(sizeof(abstract_pkg_t));
+     ab_pkg = calloc(1, sizeof(abstract_pkg_t));
 
      if (ab_pkg == NULL) {
          fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
@@ -443,14 +447,13 @@ abstract_pkg_t *abstract_pkg_new(void)
 
 int abstract_pkg_init(abstract_pkg_t *ab_pkg)
 {
-     memset(ab_pkg, 0, sizeof(abstract_pkg_t));
-
      ab_pkg->provided_by = abstract_pkg_vec_alloc();
      if (ab_pkg->provided_by==NULL){
         return -1;
      }
      ab_pkg->dependencies_checked = 0;
      ab_pkg->state_status = SS_NOT_INSTALLED;
+     active_list_init(&ab_pkg->searched_node);
 
      return 0;
 }
@@ -460,7 +463,7 @@ void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg){
      char **raw =NULL;
      char **raw_start=NULL; 
 
-     temp_str = (char *) malloc (strlen(pkg->dest->info_dir)+strlen(pkg->name)+12);
+     temp_str = (char *) calloc (1, strlen(pkg->dest->info_dir)+strlen(pkg->name)+12);
      if (temp_str == NULL ){
         opkg_message(conf, OPKG_INFO, "Out of memory in  %s\n", __FUNCTION__);
         return;
@@ -496,7 +499,7 @@ char * pkg_formatted_info(pkg_t *pkg )
      char *line;
      char * buff;
 
-     buff = malloc(8192);
+     buff = calloc(1, 8192);
      if (buff == NULL) {
          fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
          return NULL;
@@ -1052,6 +1055,7 @@ char * pkg_formatted_field(pkg_t *pkg, const char *field )
 
 void pkg_print_info(pkg_t *pkg, FILE *file)
 {
+  int t=0;
      char * buff;
      if (pkg == NULL) {
        return;
@@ -1061,7 +1065,7 @@ void pkg_print_info(pkg_t *pkg, FILE *file)
      if ( buff == NULL ) 
          return;
      if (strlen(buff)>2){
-         fwrite(buff, 1, strlen(buff), file);
+       t = fwrite(buff, 1, strlen(buff), file); //#~rzr:TODO
      } 
      free(buff);
 }
@@ -1311,7 +1315,7 @@ str_list_t *pkg_get_installed_files(pkg_t *pkg)
          return NULL;
      }
 
-     /* For uninstalled packages, get the file list firectly from the package.
+     /* For uninstalled packages, get the file list directly from the package.
        For installed packages, look at the package.list file in the database.
      */
      if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
@@ -1396,9 +1400,9 @@ int pkg_free_installed_files(pkg_t *pkg)
      str_list_elt_t *iter;
 
      pkg->installed_files_ref_cnt--;
-     if (pkg->installed_files_ref_cnt > 0) {
+
+     if (pkg->installed_files_ref_cnt > 0)
          return 0;
-     }
 
      if (pkg->installed_files) {
 
@@ -1493,8 +1497,24 @@ int pkg_run_script(opkg_conf_t *conf, pkg_t *pkg,
        scripts when running with offline_root mode and/or a dest other
        than '/'. I've been playing around with some clever chroot
        tricks and I might come up with something workable. */
+     /*
+      * Attempt to provide a restricted environment for offline operation
+      * Need the following set as a minimum:
+      * OPKG_OFFLINE_ROOT = absolute path to root dir
+      * D                 = absolute path to root dir (for OE generated postinst)
+      * PATH              = something safe (a restricted set of utilities)
+      */
+
+     bool AllowOfflineMode = false;
      if (conf->offline_root) {
          setenv("OPKG_OFFLINE_ROOT", conf->offline_root, 1);
+         setenv("D", conf->offline_root, 1);
+          if (NULL == conf->offline_root_path || '\0' == conf->offline_root_path[0]) {
+            setenv("PATH", "/dev/null", 1);
+          } else {
+            setenv("PATH", conf->offline_root_path, 1);
+            AllowOfflineMode = true;
+          }
      }
 
      setenv("PKG_ROOT",
@@ -1505,7 +1525,7 @@ int pkg_run_script(opkg_conf_t *conf, pkg_t *pkg,
          return 0;
      }
 
-     if (conf->offline_root) {
+     if (conf->offline_root && !AllowOfflineMode) {
          fprintf(stderr, "(offline root mode: not running %s.%s)\n", pkg->name, script);
          free(path);
          return 0;
@@ -1737,8 +1757,7 @@ 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_free_installed_files(pkg);
      }
      pkg_vec_free(installed_pkgs);
 
@@ -1791,6 +1810,8 @@ int pkg_write_filelist(opkg_conf_t *conf, pkg_t *pkg)
      fclose(data.stream);
      free(list_file_name);
 
+     pkg->state_flag &= ~SF_FILELIST_CHANGED;
+
      return err;
 }