Fix some errno abuse.
[oweals/opkg-lede.git] / libopkg / opkg_install.c
index c75a8d41d3542a99ee86f21a6ec70d2dce2ce2ff..819410fbf51d12cee5d91ec564441ecfadf35b0b 100644 (file)
@@ -38,7 +38,6 @@
 
 #include "sprintf_alloc.h"
 #include "file_util.h"
-#include "str_util.h"
 #include "xsystem.h"
 #include "user.h"
 #include "libbb/libbb.h"
@@ -147,9 +146,13 @@ check_conflicts_for(opkg_conf_t *conf, pkg_t *pkg)
 static int
 update_file_ownership(opkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
 {
-     str_list_t *new_list = pkg_get_installed_files(conf, new_pkg);
+     str_list_t *new_list, *old_list;
      str_list_elt_t *iter, *niter;
 
+     new_list = pkg_get_installed_files(conf, new_pkg);
+     if (new_list == NULL)
+            return -1;
+
      for (iter = str_list_first(new_list), niter = str_list_next(new_list, iter); 
              iter; 
              iter = niter, niter = str_list_next(new_list, niter)) {
@@ -160,8 +163,14 @@ update_file_ownership(opkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
          if (!owner || (owner == old_pkg))
               file_hash_set_file_owner(conf, new_file, new_pkg);
      }
+
      if (old_pkg) {
-         str_list_t *old_list = pkg_get_installed_files(conf, old_pkg);
+         old_list = pkg_get_installed_files(conf, old_pkg);
+         if (old_list == NULL) {
+                 pkg_free_installed_files(new_pkg);
+                 return -1;
+         }
+
          for (iter = str_list_first(old_list), niter = str_list_next(old_list, iter); 
                   iter; 
                   iter = niter, niter = str_list_next(old_list, niter)) {
@@ -227,7 +236,7 @@ unpack_pkg_control_files(opkg_conf_t *conf, pkg_t *pkg)
          opkg_message(conf, OPKG_ERROR,
                       "%s: Failed to create temporary directory '%s': %s\n",
                       __FUNCTION__, pkg->tmp_unpack_dir, strerror(errno));
-         return errno;
+         return -1;
      }
 
      err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
@@ -256,7 +265,7 @@ unpack_pkg_control_files(opkg_conf_t *conf, pkg_t *pkg)
          fprintf(stderr, "%s: failed to open %s: %s\n",
                  __FUNCTION__, conffiles_file_name, strerror(errno));
          free(conffiles_file_name);
-         return errno;
+         return -1;
      }
      free(conffiles_file_name);
 
@@ -268,7 +277,6 @@ unpack_pkg_control_files(opkg_conf_t *conf, pkg_t *pkg)
          if (cf_name == NULL) {
               break;
          }
-         str_chomp(cf_name);
          if (cf_name[0] == '\0') {
               continue;
          }
@@ -758,6 +766,9 @@ check_data_file_clashes(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
      int clashes = 0;
 
      files_list = pkg_get_installed_files(conf, pkg);
+     if (files_list == NULL)
+            return -1;
+
      for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); 
              iter; 
              iter = niter, niter = str_list_next(files_list, iter)) {
@@ -831,6 +842,9 @@ check_data_file_clashes(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
      return clashes;
 }
 
+/*
+ * XXX: This function sucks, as does the below comment.
+ */
 static int
 check_data_file_clashes_change(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
 {
@@ -847,9 +861,10 @@ check_data_file_clashes_change(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
 
      char *root_filename = NULL;
 
-     int clashes = 0;
-
      files_list = pkg_get_installed_files(conf, pkg);
+     if (files_list == NULL)
+            return -1;
+
      for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); 
              iter; 
              iter = niter, niter = str_list_next(files_list, niter)) {
@@ -890,7 +905,7 @@ check_data_file_clashes_change(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
      }
      pkg_free_installed_files(pkg);
 
-     return clashes;
+     return 0;
 }
 
 static int
@@ -933,12 +948,15 @@ remove_obsolesced_files(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
      str_list_elt_t *nf;
      hash_table_t new_files_table;
 
-     if (old_pkg == NULL) {
-         return 0;
-     }
-
      old_files = pkg_get_installed_files(conf, old_pkg);
+     if (old_files == NULL)
+         return -1;
+
      new_files = pkg_get_installed_files(conf, pkg);
+     if (new_files == NULL) {
+          pkg_free_installed_files(old_pkg);
+         return -1;
+     }
 
      new_files_table.entries = NULL;
      hash_table_init("new_files" , &new_files_table, 20);
@@ -1405,7 +1423,11 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
 #endif
 
      if (pkg->tmp_unpack_dir == NULL) {
-         unpack_pkg_control_files(conf, pkg);
+         if (unpack_pkg_control_files(conf, pkg) == -1) {
+              opkg_message(conf, OPKG_ERROR, "Failed to unpack control"
+                             " files from %s.\n", pkg->local_filename);
+              return -1;
+         }
      }
 
      /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
@@ -1475,31 +1497,48 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
               } else {
                    opkg_message(conf, OPKG_INFO,
                                 "  removing obsolesced files\n");
-                   remove_obsolesced_files(conf, pkg, old_pkg);
+                   if (remove_obsolesced_files(conf, pkg, old_pkg)) {
+                       opkg_message(conf, OPKG_ERROR, "Failed to determine "
+                                       "obsolete files from previously "
+                                       "installed %s\n", old_pkg->name);
+                   }
               }
+
                /* removing files from old package, to avoid ghost files */ 
                remove_data_files_and_list(conf, old_pkg);
-/* Pigi : It should be better to remove also maintainer and postrem scripts here, just in case*/
-               remove_maintainer_scripts_except_postrm(conf, old_pkg);
-               remove_postrm(conf, old_pkg);
-/* Pigi */
-
+               remove_maintainer_scripts(conf, old_pkg);
          }
 
 
          opkg_message(conf, OPKG_INFO,
                       "  installing maintainer scripts\n");
-         install_maintainer_scripts(conf, pkg, old_pkg);
+         if (install_maintainer_scripts(conf, pkg, old_pkg)) {
+               opkg_message(conf, OPKG_ERROR, "Failed to extract maintainer"
+                              " scripts for %s. Package debris may remain!\n",
+                              pkg->name);
+               goto pkg_is_hosed;
+         }
 
          /* the following just returns 0 */
          remove_disappeared(conf, pkg);
 
          opkg_message(conf, OPKG_INFO,
                       "  installing data files\n");
-         install_data_files(conf, pkg);
 
-/* read comments from function for detail but I will execute this here as all other tests are ok.*/
+         if (install_data_files(conf, pkg)) {
+               opkg_message(conf, OPKG_ERROR, "Failed to extract data files "
+                              "for %s. Package debris may remain!\n",
+                              pkg->name);
+               goto pkg_is_hosed;
+         }
+
          err = check_data_file_clashes_change(conf, pkg, old_pkg);
+         if (err) {
+               opkg_message(conf, OPKG_ERROR,
+                               "check_data_file_clashes_change() failed for "
+                              "for files belonging to %s.\n",
+                              pkg->name);
+         }
 
          opkg_message(conf, OPKG_INFO,
                       "  resolving conf files\n");
@@ -1542,6 +1581,7 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
      UNWIND_REMOVE_INSTALLED_REPLACEES:
          pkg_remove_installed_replacees_unwind(conf, replacees);
 
+pkg_is_hosed:
          opkg_message(conf, OPKG_INFO,
                       "Failed.\n");