Remove unused parameter from pkg_hash_fetch_best_installation_candidate().
[project/opkg-lede.git] / libopkg / opkg_install.c
index 881759179b8043ce0a18877c7e4d090ba14623e5..44aad0097b6413413a53dafffeebef3c23b72a15 100644 (file)
@@ -70,7 +70,7 @@ satisfy_dependencies_for(opkg_conf_t *conf, pkg_t *pkg)
                            "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
                            "of this problem try again with the '-force-depends' option.\n");
               pkg_vec_free(depends);
-              return OPKG_PKG_DEPS_UNSATISFIED;
+              return -1;
          }
      }
 
@@ -138,7 +138,7 @@ check_conflicts_for(opkg_conf_t *conf, pkg_t *pkg)
               opkg_message(conf, level, " %s", conflicts->pkgs[i++]->name);
          opkg_message(conf, level, "\n");
          pkg_vec_free(conflicts);
-         return OPKG_PKG_DEPS_UNSATISFIED;
+         return -1;
      }
      return 0;
 }
@@ -146,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)) {
@@ -159,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)) {
@@ -226,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);
@@ -255,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);
 
@@ -756,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)) {
@@ -829,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)
 {
@@ -845,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)) {
@@ -888,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
@@ -931,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);
@@ -1166,42 +1186,25 @@ resolve_conffiles(opkg_conf_t *conf, pkg_t *pkg)
 }
 
 
-opkg_error_t
+int
 opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
 {
-     int cmp, err = 0;
+     int cmp;
      pkg_t *old, *new;
      char *old_version, *new_version;
 
-     opkg_message(conf, OPKG_DEBUG2, " Getting old  from pkg_hash_fetch \n" );
      old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
-     if ( old ) 
-        opkg_message(conf, OPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n",  old->version );
+     if (old)
+        opkg_message(conf, OPKG_DEBUG2, "Old versions from pkg_hash_fetch %s \n",  old->version);
     
-     opkg_message(conf, OPKG_DEBUG2, " Getting new  from pkg_hash_fetch \n" );
-     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name, &err);
-     if ( new ) 
-        opkg_message(conf, OPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n",  new->version );
-
-/* Pigi Basically here is broken the version stuff.
-   What's happening is that nothing provide the version to differents 
-   functions, so the returned struct is always the latest.
-   That's why the install by name don't work.
-*/
-     opkg_message(conf, OPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
+     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
+     if (new == NULL)
+       return -1;
 
+     opkg_message(conf, OPKG_DEBUG2, "Versions from pkg_hash_fetch in %s ", __FUNCTION__);
      if ( old ) 
-        opkg_message(conf, OPKG_DEBUG2, " old %s ", old->version );
-     if ( new ) 
-        opkg_message(conf, OPKG_DEBUG2, " new %s ", new->version );
-     opkg_message(conf, OPKG_DEBUG2, " \n");
-
-     if (new == NULL) {
-         if (err)
-           return err;
-         else
-           return OPKG_PKG_HAS_NO_CANDIDATE;
-     }
+        opkg_message(conf, OPKG_DEBUG2, " old %s ", old->version);
+     opkg_message(conf, OPKG_DEBUG2, " new %s\n", new->version);
 
      new->state_flag |= SF_USER;
      if (old) {
@@ -1243,13 +1246,7 @@ opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
          free(new_version);
      }
 
-     /* XXX: CLEANUP: The error code of opkg_install_by_name is really
-       supposed to be an opkg_error_t, but opkg_install_pkg could
-       return any kind of integer, (might be errno from a syscall,
-       etc.). This is a real mess and will need to be cleaned up if
-       anyone ever wants to make a nice libopkg. */
-
-     opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
+     opkg_message(conf, OPKG_DEBUG2,"%s: calling opkg_install_pkg \n",__FUNCTION__);
      return opkg_install_pkg(conf, new,0);
 }
 
@@ -1269,6 +1266,7 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
 #ifdef HAVE_SHA256
      char* file_sha256;
 #endif
+     sigset_t newset, oldset;
 
      if ( from_upgrade ) 
         message = 1;            /* Coming from an upgrade, and should change the output message */
@@ -1276,7 +1274,7 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
      if (!pkg) {
          opkg_message(conf, OPKG_ERROR,
                       "INTERNAL ERROR: null pkg passed to opkg_install_pkg\n");
-         return OPKG_INSTALL_ERR_INTERNAL;
+         return -1;
      }
 
      opkg_message(conf, OPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
@@ -1284,11 +1282,12 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
      if (!pkg_arch_supported(conf, pkg)) {
          opkg_message(conf, OPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
                       pkg->architecture, pkg->name);
-         return OPKG_INSTALL_ERR_INTERNAL;
+         return -1;
      }
      if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
          err = satisfy_dependencies_for(conf, pkg);
-         if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
+         if (err)
+                 return -1;
 
          opkg_message(conf, OPKG_NOTICE,
                       "Package %s is already installed in %s.\n", 
@@ -1303,27 +1302,28 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
      old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
 
      err = opkg_install_check_downgrade(conf, pkg, old_pkg, message);
-     if (err) { return OPKG_INSTALL_ERR_NO_DOWNGRADE; }
+     if (err)
+            return -1;
 
      pkg->state_want = SW_INSTALL;
      if (old_pkg){                          
          old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
      }
 
-
-     /* Abhaya: conflicts check */
      err = check_conflicts_for(conf, pkg);
-     if (err) { return OPKG_INSTALL_ERR_CONFLICTS; }
+     if (err)
+            return -1;
     
      /* this setup is to remove the upgrade scenario in the end when
        installing pkg A, A deps B & B deps on A. So both B and A are
        installed. Then A's installation is started resulting in an
        uncecessary upgrade */ 
-     if (pkg->state_status == SS_INSTALLED
-        && conf->force_reinstall == 0) return 0;
+     if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0)
+            return 0;
     
      err = verify_pkg_installable(conf, pkg);
-     if (err) { return OPKG_INSTALL_ERR_NO_SPACE; }
+     if (err)
+            return -1;
 
      if (pkg->local_filename == NULL) {
          err = opkg_download_pkg(conf, pkg, conf->tmp_dir);
@@ -1331,7 +1331,7 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
               opkg_message(conf, OPKG_ERROR,
                            "Failed to download %s. Perhaps you need to run 'opkg update'?\n",
                            pkg->name);
-              return OPKG_INSTALL_ERR_DOWNLOAD;
+              return -1;
          }
      }
 
@@ -1354,12 +1354,12 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
          if (opkg_verify_file (conf, list_file_name, sig_file_name)){
            opkg_message(conf, OPKG_ERROR, "Failed to verify the signature of: %s\n",
                            list_file_name);
-           return OPKG_INSTALL_ERR_SIGNATURE;
+           return -1;
          }
        }else{
          opkg_message(conf, OPKG_ERROR, "Signature file is missing. "
                          "Perhaps you need to run 'opkg update'?\n");
-         return OPKG_INSTALL_ERR_SIGNATURE;
+         return -1;
        }
 
        free (lists_dir);
@@ -1378,7 +1378,7 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
                            "Package %s md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
                            pkg->name);
               free(file_md5);
-              return OPKG_INSTALL_ERR_MD5;
+              return -1;
          }
         if (file_md5)
               free(file_md5);
@@ -1395,7 +1395,7 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
                            "Package %s sha256sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
                            pkg->name);
               free(file_sha256);
-              return OPKG_INSTALL_ERR_SHA256;
+              return -1;
          }
         if (file_sha256)
               free(file_sha256);
@@ -1403,29 +1403,33 @@ 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 */
 /* Pigi: check if it will pass from here when replacing. It seems to fail */
 /* That's rather strange that files don't change owner. Investigate !!!!!!*/
      err = update_file_ownership(conf, pkg, old_pkg);
-     if (err) { return OPKG_ERR_UNKNOWN; }
+     if (err)
+            return -1;
 
      if (conf->nodeps == 0) {
          err = satisfy_dependencies_for(conf, pkg);
-         if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
+         if (err)
+               return -1;
           if (pkg->state_status == SS_UNPACKED)
                /* Circular dependency has installed it for us. */
-               return 0;
+               return 0;
      }
 
      replacees = pkg_vec_alloc();
      pkg_get_installed_replacees(conf, pkg, replacees);
 
      /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
-     {
-         sigset_t newset, oldset;
 
          sigemptyset(&newset);
          sigaddset(&newset, SIGINT);
@@ -1441,27 +1445,35 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
           *      Instead, we're going to remove the replacees 
           */
          err = pkg_remove_installed_replacees(conf, replacees);
-         if (err) goto UNWIND_REMOVE_INSTALLED_REPLACEES;
+         if (err)
+                 goto UNWIND_REMOVE_INSTALLED_REPLACEES;
 
          err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
-         if (err) goto UNWIND_PRERM_UPGRADE_OLD_PKG;
+         if (err)
+                 goto UNWIND_PRERM_UPGRADE_OLD_PKG;
 
          err = prerm_deconfigure_conflictors(conf, pkg, replacees);
-         if (err) goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
+         if (err)
+                 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
 
          err = preinst_configure(conf, pkg, old_pkg);
-         if (err) goto UNWIND_PREINST_CONFIGURE;
+         if (err)
+                 goto UNWIND_PREINST_CONFIGURE;
 
          err = backup_modified_conffiles(conf, pkg, old_pkg);
-         if (err) goto UNWIND_BACKUP_MODIFIED_CONFFILES;
+         if (err)
+                 goto UNWIND_BACKUP_MODIFIED_CONFFILES;
 
          err = check_data_file_clashes(conf, pkg, old_pkg);
-         if (err) goto UNWIND_CHECK_DATA_FILE_CLASHES;
+         if (err)
+                 goto UNWIND_CHECK_DATA_FILE_CLASHES;
 
          err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
-         if (err) goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
+         if (err)
+                 goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
 
-         if (conf->noaction) return 0;
+         if (conf->noaction)
+                 return 0;
 
          /* point of no return: no unwinding after this */
          if (old_pkg && !conf->force_reinstall) {
@@ -1473,7 +1485,11 @@ 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 */ 
@@ -1484,17 +1500,33 @@ opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
 
          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");
@@ -1537,12 +1569,12 @@ 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");
 
          sigprocmask(SIG_UNBLOCK, &newset, &oldset);
 
           pkg_vec_free (replacees);
-         return OPKG_ERR_UNKNOWN;
-     }
+         return -1;
 }