Fix pkg_get_installed_files() to work with an offline_root + dest.
[project/opkg-lede.git] / libopkg / opkg_remove.c
index 3886ccc6eeb40918c0d9d15a59de2a150fae5e97..aadfa6aa6d4b9925775b978fba780fbf9c36d4f3 100644 (file)
 
 #include "opkg_remove.h"
 #include "opkg_error.h"
-#include "opkg_state.h"
+#include "opkg_cmd.h"
 
 #include "file_util.h"
 #include "sprintf_alloc.h"
 #include "str_util.h"
+#include "libbb/libbb.h"
 
 /*
  * Returns number of the number of packages depending on the packages provided by this package.
@@ -38,7 +39,7 @@ int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg,
      abstract_pkg_t **provides = pkg->provides;
      int n_installed_dependents = 0;
      int i;
-     for (i = 0; i <= nprovides; i++) {
+     for (i = 0; i < nprovides; i++) {
          abstract_pkg_t *providee = provides[i];
          abstract_pkg_t **dependers = providee->depended_upon_by;
          abstract_pkg_t *dep_ab_pkg;
@@ -54,15 +55,10 @@ int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg,
      /* if caller requested the set of installed dependents */
      if (pdependents) {
          int p = 0;
-         abstract_pkg_t **dependents = (abstract_pkg_t **)calloc((n_installed_dependents+1), sizeof(abstract_pkg_t *));
-
-          if ( dependents == NULL ){
-              fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
-              return -1;  
-          }
+         abstract_pkg_t **dependents = xcalloc((n_installed_dependents+1), sizeof(abstract_pkg_t *));
 
          *pdependents = dependents;
-         for (i = 0; i <= nprovides; i++) {
+         for (i = 0; i < nprovides; i++) {
               abstract_pkg_t *providee = provides[i];
               abstract_pkg_t **dependers = providee->depended_upon_by;
               abstract_pkg_t *dep_ab_pkg;
@@ -181,12 +177,7 @@ static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg)
     int x = 0;
     pkg_t *p;
     d_str = pkg->depends_str[i];
-    buffer = calloc (1, strlen (d_str) + 1);
-    if (!buffer)
-    {
-      fprintf(stderr,"%s Unable to allocate memory.\n", __FUNCTION__);
-      return -1;
-    }
+    buffer = xcalloc(1, strlen (d_str) + 1);
 
     while (d_str[x] != '\0' && d_str[x] != ' ')
     {
@@ -194,7 +185,7 @@ static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg)
       ++x;
     }
     buffer[x] = '\0';
-    buffer = realloc (buffer, strlen (buffer) + 1);
+    buffer = xrealloc (buffer, strlen (buffer) + 1);
     p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash, buffer);
 
     /* if the package is not installed, this could have been a circular
@@ -269,6 +260,7 @@ int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
 
               if (!conf->force_removal_of_dependent_packages
                   && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
+                   free(dependents);
                    return OPKG_PKG_HAS_DEPENDENTS;
               }
 
@@ -342,9 +334,14 @@ int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
      conffile_t *conffile;
      int removed_a_dir;
      pkg_t *owner;
+     int rootdirlen = 0;
 
      str_list_init(&installed_dirs);
-     installed_files = pkg_get_installed_files(pkg);
+     installed_files = pkg_get_installed_files(conf, pkg);
+
+     /* don't include trailing slash */
+     if (conf->offline_root)
+          rootdirlen = strlen(conf->offline_root);
 
      for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) {
          file_name = (char *)iter->data;
@@ -354,7 +351,7 @@ int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
               continue;
          }
 
-         conffile = pkg_get_conffile(pkg, file_name);
+         conffile = pkg_get_conffile(pkg, file_name+rootdirlen);
          if (conffile) {
               /* XXX: QUESTION: Is this right? I figure we only need to
                  save the conffile if it has been modified. Is that what