libopkg: make MD5 support optional
[oweals/opkg-lede.git] / libopkg / opkg_install.c
index ec4afba3702f3d19a46502f10ba3028162a67b28..0db0acdf84e7d6f297da9f690e82aa8d4a41e15e 100644 (file)
@@ -21,6 +21,7 @@
 #include <time.h>
 #include <signal.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include "pkg.h"
 #include "pkg_hash.h"
@@ -192,13 +193,24 @@ static int
 verify_pkg_installable(pkg_t *pkg)
 {
        unsigned long kbs_available, pkg_size_kbs;
-       char *root_dir;
+       char *root_dir = NULL;
+       struct stat s;
 
        if (conf->force_space || pkg->installed_size == 0)
                return 0;
 
-       root_dir = pkg->dest ? pkg->dest->root_dir :
-                                               conf->default_dest->root_dir;
+       if (pkg->dest)
+       {
+               if (!strcmp(pkg->dest->name, "root") && conf->overlay_root
+                   && !stat(conf->overlay_root, &s) && (s.st_mode & S_IFDIR))
+                       root_dir = conf->overlay_root;
+               else
+                       root_dir = pkg->dest->root_dir;
+       }
+
+       if (!root_dir)
+               root_dir = conf->default_dest->root_dir;
+
        kbs_available = get_available_kbytes(root_dir);
 
        pkg_size_kbs = (pkg->installed_size + 1023)/1024;
@@ -262,6 +274,7 @@ unpack_pkg_control_files(pkg_t *pkg)
      while (1) {
          char *cf_name;
          char *cf_name_in_dest;
+         int i;
 
          cf_name = file_read_line_alloc(conffiles_file);
          if (cf_name == NULL) {
@@ -270,6 +283,12 @@ unpack_pkg_control_files(pkg_t *pkg)
          if (cf_name[0] == '\0') {
               continue;
          }
+         for (i = strlen(cf_name) - 1;
+              (i >= 0) && (cf_name[i] == ' ' || cf_name[i] == '\t');
+              i--
+         ) {
+              cf_name[i] = '\0';
+         }
 
          /* Prepend dest->root_dir to conffile name.
             Take pains to avoid multiple slashes. */
@@ -324,7 +343,7 @@ pkg_remove_orphan_dependent(pkg_t *pkg, pkg_t *old_pkg)
                        found = 0;
 
                        for (k=0; k<count1; k++) {
-                               cd1 = &pkg->depends[i];
+                               cd1 = &pkg->depends[k];
                                if (cd1->type != DEPEND)
                                        continue;
                                for (l=0; l<cd1->possibility_count; l++) {
@@ -1063,7 +1082,7 @@ resolve_conffiles(pkg_t *pkg)
      conffile_list_elt_t *iter;
      conffile_t *cf;
      char *cf_backup;
-     char *md5sum;
+     char *chksum;
 
      if (conf->noaction) return 0;
 
@@ -1074,7 +1093,7 @@ resolve_conffiles(pkg_t *pkg)
 
          /* Might need to initialize the md5sum for each conffile */
          if (cf->value == NULL) {
-              cf->value = file_md5sum_alloc(root_filename);
+              cf->value = file_sha256sum_alloc(root_filename);
          }
 
          if (!file_exists(root_filename)) {
@@ -1086,8 +1105,16 @@ resolve_conffiles(pkg_t *pkg)
 
           if (file_exists(cf_backup)) {
               /* Let's compute md5 to test if files are changed */
-              md5sum = file_md5sum_alloc(cf_backup);
-              if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) {
+#ifdef HAVE_MD5
+              if(cf->value && strlen(cf->value) > 33) {
+                  chksum = file_sha256sum_alloc(cf_backup);
+              } else {
+                  chksum = file_md5sum_alloc(cf_backup);
+              }
+#else
+              chksum = file_sha256sum_alloc(cf_backup);
+#endif
+              if (chksum && cf->value && strcmp(cf->value,chksum) != 0 ) {
                   if (conf->force_maintainer) {
                       opkg_msg(NOTICE, "Conffile %s using maintainer's setting.\n",
                                      cf_backup);
@@ -1104,8 +1131,8 @@ resolve_conffiles(pkg_t *pkg)
                  }
               }
               unlink(cf_backup);
-             if (md5sum)
-                  free(md5sum);
+             if (chksum)
+                  free(chksum);
           }
 
          free(cf_backup);
@@ -1269,7 +1296,7 @@ opkg_install_pkg(pkg_t *pkg, int from_upgrade)
      }
 
      /* check that the repository is valid */
-     #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
+     #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
      char *list_file_name, *sig_file_name, *lists_dir;
 
      /* check to ensure the package has come from a repository */
@@ -1287,13 +1314,15 @@ opkg_install_pkg(pkg_t *pkg, int from_upgrade)
          if (opkg_verify_file (list_file_name, sig_file_name)){
            opkg_msg(ERROR, "Failed to verify the signature of %s.\n",
                            list_file_name);
-           return -1;
+           if (!conf->force_signature)
+             return -1;
          }
        }else{
          opkg_msg(ERROR, "Signature file is missing for %s. "
                          "Perhaps you need to run 'opkg update'?\n",
                         pkg->name);
-         return -1;
+         if (!conf->force_signature)
+           return -1;
        }
 
        free (lists_dir);
@@ -1302,22 +1331,31 @@ opkg_install_pkg(pkg_t *pkg, int from_upgrade)
      }
      #endif
 
+#ifdef HAVE_MD5
      /* Check for md5 values */
      if (pkg->md5sum)
      {
          file_md5 = file_md5sum_alloc(pkg->local_filename);
          if (file_md5 && strcmp(file_md5, pkg->md5sum))
          {
-              opkg_msg(ERROR, "Package %s md5sum mismatch. "
-                       "Either the opkg or the package index are corrupt. "
-                       "Try 'opkg update'.\n",
-                       pkg->name);
-              free(file_md5);
-              return -1;
+              if (!conf->force_checksum)
+              {
+                  opkg_msg(ERROR, "Package %s md5sum mismatch. "
+                           "Either the opkg or the package index are corrupt. "
+                           "Try 'opkg update'.\n",
+                           pkg->name);
+                  free(file_md5);
+                  return -1;
+              }
+              else
+              {
+                  opkg_msg(NOTICE, "Ignored %s md5sum mismatch.\n", pkg->name);
+              }
          }
         if (file_md5)
               free(file_md5);
      }
+#endif
 
 #ifdef HAVE_SHA256
      /* Check for sha256 value */
@@ -1379,9 +1417,11 @@ opkg_install_pkg(pkg_t *pkg, int from_upgrade)
          opkg_state_changed++;
          pkg->state_flag |= SF_FILELIST_CHANGED;
 
-         if (old_pkg)
+         if (old_pkg) {
                pkg_remove_orphan_dependent(pkg, old_pkg);
-
+              old_pkg->is_upgrade = 1;
+              pkg->is_upgrade = 1;
+         }
          /* XXX: BUG: we really should treat replacement more like an upgrade
           *      Instead, we're going to remove the replacees
           */
@@ -1440,7 +1480,7 @@ opkg_install_pkg(pkg_t *pkg, int from_upgrade)
          }
 
 
-         opkg_msg(INFO, "Installing maintainer scripts.\n");
+         opkg_msg(INFO, "%s maintainer scripts.\n", (pkg->is_upgrade) ? ("Upgrading") : ("Installing"));
          if (install_maintainer_scripts(pkg, old_pkg)) {
                opkg_msg(ERROR, "Failed to extract maintainer scripts for %s."
                               " Package debris may remain!\n",