opkg: balance the installed_files reference count
[oweals/opkg-lede.git] / libopkg / opkg_install.c
index 1c73b5c0a2544475a253ec93edf461686fb3bb2f..a3893c7131f54e89cc2fc926d15d57f00af2e2cf 100644 (file)
@@ -137,7 +137,7 @@ int opkg_install_from_file(opkg_conf_t *conf, const char *filename)
 
 opkg_error_t opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
 {
-     int cmp;
+     int cmp, err;
      pkg_t *old, *new;
      char *old_version, *new_version;
 
@@ -147,7 +147,7 @@ opkg_error_t opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
         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);
+     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 );
 
@@ -165,7 +165,10 @@ opkg_error_t opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
      opkg_message(conf, OPKG_DEBUG2, " \n");
 
      if (new == NULL) {
-         return OPKG_PKG_HAS_NO_CANDIDATE;
+         if (err)
+           return err;
+         else
+           return OPKG_PKG_HAS_NO_CANDIDATE;
      }
 
      new->state_flag |= SF_USER;
@@ -307,7 +310,7 @@ int pkg_mark_dependencies_for_installation(opkg_conf_t *conf, pkg_t *pkg, pkg_ve
 
      return 0;
 }
-
+#if 0
 int name_mark_dependencies_for_installation(opkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed)
 {
      int cmp;
@@ -360,7 +363,7 @@ int name_mark_dependencies_for_installation(opkg_conf_t *conf, const char *pkg_n
      return pkg_mark_dependencies_for_installation(conf, new, pkgs_needed);
 }
 
-\f
+#endif
 
 int satisfy_dependencies_for(opkg_conf_t *conf, pkg_t *pkg)
 {
@@ -503,7 +506,9 @@ static int update_file_ownership(opkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_p
                    hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
               }
          }
+          pkg_free_installed_files(old_pkg);
      }
+     pkg_free_installed_files(new_pkg);
      return 0;
 }
 
@@ -762,7 +767,7 @@ int 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 PKG_INSTALL_ERR_INTERNAL;
+         return OPKG_INSTALL_ERR_INTERNAL;
      }
 
      opkg_message(conf, OPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
@@ -770,11 +775,11 @@ int 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 PKG_INSTALL_ERR_INTERNAL;
+         return OPKG_INSTALL_ERR_INTERNAL;
      }
      if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
          err = satisfy_dependencies_for(conf, pkg);
-         if (err) { return PKG_INSTALL_ERR_DEPENDENCIES; }
+         if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
 
          opkg_message(conf, OPKG_NOTICE,
                       "Package %s is already installed in %s.\n", 
@@ -789,7 +794,7 @@ int 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 PKG_INSTALL_ERR_NO_DOWNGRADE; }
+     if (err) { return OPKG_INSTALL_ERR_NO_DOWNGRADE; }
 
      pkg->state_want = SW_INSTALL;
      if (old_pkg){                          
@@ -799,7 +804,7 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
 
      /* Abhaya: conflicts check */
      err = check_conflicts_for(conf, pkg);
-     if (err) { return PKG_INSTALL_ERR_CONFLICTS; }
+     if (err) { return OPKG_INSTALL_ERR_CONFLICTS; }
     
      /* 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
@@ -809,7 +814,7 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
         && conf->force_reinstall == 0) return 0;
     
      err = verify_pkg_installable(conf, pkg);
-     if (err) { return PKG_INSTALL_ERR_NO_SPACE; }
+     if (err) { return OPKG_INSTALL_ERR_NO_SPACE; }
 
      if (pkg->local_filename == NULL) {
          err = opkg_download_pkg(conf, pkg, conf->tmp_dir);
@@ -817,7 +822,7 @@ int 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 PKG_INSTALL_ERR_DOWNLOAD;
+              return OPKG_INSTALL_ERR_DOWNLOAD;
          }
      }
 
@@ -825,22 +830,26 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
      #if HAVE_GPGME
      char *list_file_name, *sig_file_name, *lists_dir;
 
-     sprintf_alloc (&lists_dir, "%s",
-                   (conf->restrict_to_default_dest)
-                    ? conf->default_dest->lists_dir
-                    : conf->lists_dir);
-     sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name);
-     sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name);
-
-     if (file_exists (sig_file_name))
+     /* check to ensure the package has come from a repository */
+     if (pkg->src)
      {
-       if (opkg_verify_file (conf, list_file_name, sig_file_name))
-         return PKG_INSTALL_ERR_SIGNATURE;
+       sprintf_alloc (&lists_dir, "%s",
+                     (conf->restrict_to_default_dest)
+                      ? conf->default_dest->lists_dir
+                      : conf->lists_dir);
+       sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name);
+       sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name);
+
+       if (file_exists (sig_file_name))
+       {
+         if (opkg_verify_file (conf, list_file_name, sig_file_name))
+           return OPKG_INSTALL_ERR_SIGNATURE;
+       }
+
+       free (lists_dir);
+       free (list_file_name);
+       free (sig_file_name);
      }
-
-     free (lists_dir);
-     free (list_file_name);
-     free (sig_file_name);
      #endif
 
      /* Check for md5 values */
@@ -853,7 +862,7 @@ int 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 PKG_INSTALL_ERR_MD5;
+              return OPKG_INSTALL_ERR_MD5;
          }
          free(file_md5);
      }
@@ -866,11 +875,11 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
 /* 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 PKG_INSTALL_ERR_UNKNOWN; }
+     if (err) { return OPKG_ERR_UNKNOWN; }
 
      if (conf->nodeps == 0) {
          err = satisfy_dependencies_for(conf, pkg);
-         if (err) { return PKG_INSTALL_ERR_DEPENDENCIES; }
+         if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
      }
 
      replacees = pkg_vec_alloc();
@@ -1019,7 +1028,7 @@ int opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
               sigprocmask(SIG_UNBLOCK, &newset, &oldset);
 
           pkg_vec_free (replacees);
-         return PKG_INSTALL_ERR_UNKNOWN;
+         return OPKG_ERR_UNKNOWN;
      }
      opkg_set_current_state (conf, OPKG_STATE_NONE, NULL);
 }