Add sha256 ckecksums to okpg
[oweals/opkg-lede.git] / libopkg / pkg.c
index 5096ba022c82cc22a01fb264500b47ce8bdd004b..0c7bb5ae5eaf68074213037fab4787ba492fc0dd 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "includes.h"
 #include <ctype.h>
+#include <alloca.h>
 #include <string.h>
 #include <stdbool.h>
 #include <errno.h>
@@ -130,6 +131,9 @@ int pkg_init(pkg_t *pkg)
      pkg->local_filename = NULL;
      pkg->tmp_unpack_dir = NULL;
      pkg->md5sum = NULL;
+#if defined HAVE_SHA256
+     pkg->sha256sum = NULL;
+#endif
      pkg->size = NULL;
      pkg->installed_size = NULL;
      pkg->priority = NULL;
@@ -254,6 +258,10 @@ void pkg_deinit(pkg_t *pkg)
      pkg->tmp_unpack_dir = NULL;
      free(pkg->md5sum);
      pkg->md5sum = NULL;
+#if defined HAVE_SHA256
+     free(pkg->sha256sum);
+     pkg->sha256sum = NULL;
+#endif
      free(pkg->size);
      pkg->size = NULL;
      free(pkg->installed_size);
@@ -404,6 +412,10 @@ int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status)
          oldpkg->tmp_unpack_dir = str_dup_safe(newpkg->tmp_unpack_dir);
      if (!oldpkg->md5sum)
          oldpkg->md5sum = str_dup_safe(newpkg->md5sum);
+#if defined HAVE_SHA256
+     if (!oldpkg->sha256sum)
+         oldpkg->sha256sum = str_dup_safe(newpkg->sha256sum);
+#endif
      if (!oldpkg->size)
          oldpkg->size = str_dup_safe(newpkg->size);
      if (!oldpkg->installed_size)
@@ -461,7 +473,10 @@ void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg){
      char **raw =NULL;
      char **raw_start=NULL; 
 
-     temp_str = (char *) calloc (1, strlen(pkg->dest->info_dir)+strlen(pkg->name)+12);
+     size_t str_size = strlen(pkg->dest->info_dir)+strlen(pkg->name)+12;
+     temp_str = (char *) alloca (str_size);
+     memset(temp_str, 0 , str_size);
+     
      if (temp_str == NULL ){
         opkg_message(conf, OPKG_INFO, "Out of memory in  %s\n", __FUNCTION__);
         return;
@@ -486,7 +501,6 @@ void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg){
      }
 
      free(raw_start); 
-     free(temp_str);
 
      return ;
 
@@ -952,7 +966,7 @@ char * pkg_formatted_field(pkg_t *pkg, const char *field )
          break;
      case 's':
      case 'S': {
-         /* Section | Size | Source | Status | Suggests */
+         /* Section | SHA256sum | Size | Source | Status | Suggests */
          if (strcasecmp(field, "Section") == 0) {
               /* Section */
               if (pkg->section) {
@@ -964,6 +978,19 @@ char * pkg_formatted_field(pkg_t *pkg, const char *field )
                    temp[0]='\0';
                    snprintf(temp, (strlen(pkg->section)+11), "Section: %s\n", pkg->section);
               }
+#if defined HAVE_SHA256
+         } else if (strcasecmp(field, "SHA256sum") == 0) {
+              /* SHA256sum */
+              if (pkg->sha256sum) {
+                   temp = (char *)realloc(temp,strlen(pkg->sha256sum)+13);
+                   if ( temp == NULL ){
+                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
+                     return NULL;
+                   }
+                   temp[0]='\0';
+                   snprintf(temp, (strlen(pkg->sha256sum)+13), "SHA256sum: %s\n", pkg->sha256sum);
+              }
+#endif
          } else if (strcasecmp(field, "Size") == 0) {
               /* Size */
               if (pkg->size) {
@@ -1409,6 +1436,7 @@ str_list_t *pkg_get_installed_files(pkg_t *pkg)
               sprintf_alloc(&installed_file_name, "%s", file_name);
          }
          str_list_append(pkg->installed_files, installed_file_name);
+          free(installed_file_name);
          free(line);
      }
 
@@ -1430,7 +1458,7 @@ int pkg_free_installed_files(pkg_t *pkg)
          return 0;
 
      if (pkg->installed_files) {
-         str_list_deinit(pkg->installed_files);
+         str_list_purge(pkg->installed_files);
      }
 
      pkg->installed_files = NULL;
@@ -1758,12 +1786,14 @@ int pkg_info_preinstall_check(opkg_conf_t *conf)
      for (i = 0; i < installed_pkgs->len; i++) {
          pkg_t *pkg = installed_pkgs->pkgs[i];
          str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
-         str_list_elt_t *iter;
+         str_list_elt_t *iter, *niter;
          if (installed_files == NULL) {
               opkg_message(conf, OPKG_ERROR, "No installed files for pkg %s\n", pkg->name);
               break;
          }
-         for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) {
+         for (iter = str_list_first(installed_files), niter = str_list_next(installed_files, iter); 
+                  iter; 
+                  iter = niter, niter = str_list_next(installed_files, iter)) {
               char *installed_file = (char *) iter->data;
               // opkg_message(conf, OPKG_DEBUG2, "pkg %s: file=%s\n", pkg->name, installed_file);
               file_hash_set_file_owner(conf, installed_file, pkg);