Add sha256 ckecksums to okpg
[oweals/opkg-lede.git] / libopkg / opkg_remove.c
index eb7825a860395e800fcde32794dd42f8a31c1a6f..3886ccc6eeb40918c0d9d15a59de2a150fae5e97 100644 (file)
@@ -1,4 +1,4 @@
-/* opkg_remove.c - the itsy package management system
+/* opkg_remove.c - the opkg package management system
 
    Carl D. Worth
 
    General Public License for more details.
 */
 
-#include "opkg.h"
+#include "includes.h"
 #include "opkg_message.h"
 
 #include <glob.h>
 
 #include "opkg_remove.h"
+#include "opkg_error.h"
+#include "opkg_state.h"
 
 #include "file_util.h"
 #include "sprintf_alloc.h"
 #include "str_util.h"
 
-#include "opkg_cmd.h"
-
 /*
  * Returns number of the number of packages depending on the packages provided by this package.
  * Every package implicitly provides itself.
@@ -54,7 +54,7 @@ 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 **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
+         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__);
@@ -90,7 +90,7 @@ int opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **
     int i;
     int a;
     int count;
-    pkg_vec_t *dependent_pkgs = pkg_vec_alloc();
+    pkg_vec_t *dependent_pkgs;
     abstract_pkg_t * ab_pkg;
 
     if((ab_pkg = pkg->parent) == NULL){
@@ -110,6 +110,8 @@ int opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **
 
     i = 0;
     count = 1;
+    dependent_pkgs = pkg_vec_alloc();
+
     while (dependents [i] != NULL) {
         abstract_pkg_t *dep_ab_pkg = dependents[i];
        
@@ -131,16 +133,20 @@ int opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **
         * 2 - to keep track of pkgs whose deps have been checked alrdy  - Karthik */   
     }
     
-    if (count == 1)
-           return 0;
+    if (count == 1) {
+        free(dependent_pkgs);  
+       return 0;
+    }
     
     
+    int err=0;
     for (i = 0; i < dependent_pkgs->len; i++) {
-        int err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
+        err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
         if (err)
-            return err;
+            break;
     }
-    return 0;
+    free(dependent_pkgs);
+    return err;
 }
 
 static int user_prefers_removing_dependents(opkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
@@ -161,6 +167,63 @@ static int user_prefers_removing_dependents(opkg_conf_t *conf, abstract_pkg_t *a
     return 0;
 }
 
+static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg)
+{
+  /*
+   * find and remove packages that were autoinstalled and are orphaned by the removal of pkg
+   */
+
+  char *buffer, *d_str;
+  int i;
+
+  for (i = 0; i < pkg->depends_count; ++i)
+  {
+    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;
+    }
+
+    while (d_str[x] != '\0' && d_str[x] != ' ')
+    {
+      buffer[x] = d_str[x];
+      ++x;
+    }
+    buffer[x] = '\0';
+    buffer = realloc (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
+     * depenancy and the package has already been removed */
+    if (!p)
+      return -1;
+
+    if (p->auto_installed)
+    {
+      int deps;
+      abstract_pkg_t **dependents;
+
+      deps = pkg_has_installed_dependents(conf, NULL, p, &dependents);
+      if (deps == 0)
+      {
+        opkg_message (conf, OPKG_INFO,
+                      "%s was autoinstalled but is now orphaned\n", buffer);
+         opkg_remove_pkg(conf, p,0);
+      }
+       else
+          opkg_message (conf, OPKG_INFO, "%s was autoinstalled and is still required by "
+                        "%d installed packages\n", buffer, deps);
+    }
+    free (buffer);
+  }
+
+  return 0;
+}
+
 int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
 {
 /* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
@@ -169,7 +232,7 @@ int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
 */
      int err;
      abstract_pkg_t *parent_pkg = NULL;
-       
+
      if (pkg->essential && !message) {
          if (conf->force_removal_of_essential_packages) {
               fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
@@ -211,13 +274,18 @@ int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
 
               /* remove packages depending on this package - Karthik */
               err = opkg_remove_dependent_pkgs (conf, pkg, dependents);
-              free(dependents);
-              if (err) return err;
+              if (err) {
+                free(dependents);
+                 return err;
+               }
          }
+          if (dependents)
+              free(dependents);
      }
 
      if ( message==0 ){
-         printf("Removing package %s from %s...\n", pkg->name, pkg->dest->name);
+         opkg_message (conf, OPKG_NOTICE,
+                      "Removing package %s from %s...\n", pkg->name, pkg->dest->name);
          fflush(stdout);
      }
      pkg->state_flag |= SF_FILELIST_CHANGED;
@@ -249,6 +317,13 @@ int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
      if (parent_pkg) 
          parent_pkg->state_status = SS_NOT_INSTALLED;
 
+
+     /* remove autoinstalled packages that are orphaned by the removal of this one */
+     if (conf->autoremove)
+       remove_autoinstalled (conf, pkg);
+
+
+
      return 0;
 }
 
@@ -271,11 +346,11 @@ int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
      str_list_init(&installed_dirs);
      installed_files = pkg_get_installed_files(pkg);
 
-     for (iter = installed_files->head; iter; iter = iter->next) {
-         file_name = iter->data;
+     for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) {
+         file_name = (char *)iter->data;
 
          if (file_is_dir(file_name)) {
-              str_list_append(&installed_dirs, strdup(file_name));
+              str_list_append(&installed_dirs, file_name);
               continue;
          }
 
@@ -287,7 +362,8 @@ int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
                  this seems like a better thing to do to conserve
                  space. */
               if (conffile_has_been_modified(conf, conffile)) {
-                   printf("  not deleting modified conffile %s\n", file_name);
+                   opkg_message (conf, OPKG_NOTICE,
+                                 "  not deleting modified conffile %s\n", file_name);
                    fflush(stdout);
                    continue;
               }
@@ -301,8 +377,8 @@ int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
      if (!conf->noaction) {
          do {
               removed_a_dir = 0;
-              for (iter = installed_dirs.head; iter; iter = iter->next) {
-                   file_name = iter->data;
+              for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
+                   file_name = (char *)iter->data;
            
                    if (rmdir(file_name) == 0) {
                         opkg_message(conf, OPKG_INFO, "  deleting %s\n", file_name);
@@ -319,8 +395,8 @@ int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
      pkg_remove_installed_files_list(conf, pkg);
 
      /* Don't print warning for dirs that are provided by other packages */
-     for (iter = installed_dirs.head; iter; iter = iter->next) {
-         file_name = iter->data;
+     for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
+         file_name = (char *)iter->data;
 
          owner = file_hash_get_file_owner(conf, file_name);
          if (owner) {
@@ -331,9 +407,10 @@ int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
      }
 
      /* cleanup */
-     for (iter = installed_dirs.head; iter; iter = iter->next) {
-         free(iter->data);
-         iter->data = NULL;
+     while (!void_list_empty(&installed_dirs)) {
+        iter = str_list_pop(&installed_dirs);
+        free(iter->data);
+        free(iter);
      }
      str_list_deinit(&installed_dirs);