libopkg: factor out checksum and size verification
authorBaptiste Jonglez <git@bitsofnetworks.org>
Mon, 24 Aug 2020 23:00:29 +0000 (01:00 +0200)
committerPaul Spooren <mail@aparcar.org>
Tue, 24 Nov 2020 22:07:51 +0000 (12:07 -1000)
This is a sizeable chunk of code that be can pretty well isolated in its
own function.

This refactoring will be necessary for an upcoming feature in which
opkg_download_pkg() will need to verify the checksum of packages in the
cache.  This is the reason why the new function is located in
`opkg_download.c`.

Signed-off-by: Baptiste Jonglez <git@bitsofnetworks.org>
libopkg/opkg_download.c
libopkg/opkg_download.h
libopkg/opkg_install.c

index d5bc92c2e9b7827697e794e9d350d39fddab1706..373f2319e0b32c3ab5b06ad4c98e36438f9f6d27 100644 (file)
@@ -35,6 +35,84 @@ static int str_starts_with(const char *str, const char *prefix)
        return (strncmp(str, prefix, strlen(prefix)) == 0);
 }
 
+int opkg_verify_integrity(pkg_t *pkg, const char *filename)
+{
+       int err = 0;
+       char *file_md5, *pkg_md5;
+       char *file_sha256, *pkg_sha256;
+       struct stat pkg_stat;
+       long long int pkg_expected_size;
+
+       /* Check file size */
+       err = lstat(filename, &pkg_stat);
+
+       if (err) {
+               opkg_msg(ERROR, "Failed to stat %s: %s\n",
+                        filename, strerror(errno));
+               return err;
+       }
+
+       pkg_expected_size = pkg_get_int(pkg, PKG_SIZE);
+
+       if (pkg_expected_size > 0 && pkg_stat.st_size != pkg_expected_size) {
+               if (!conf->force_checksum) {
+                       opkg_msg(ERROR,
+                                "Package size mismatch: %s is %lld bytes, expecting %lld bytes\n",
+                                pkg->name, (long long int)pkg_stat.st_size, pkg_expected_size);
+                       return -1;
+               } else {
+                       opkg_msg(NOTICE,
+                                "Ignored %s size mismatch.\n",
+                                pkg->name);
+               }
+       }
+
+       /* Check for md5 values */
+       pkg_md5 = pkg_get_md5(pkg);
+       if (pkg_md5) {
+               file_md5 = file_md5sum_alloc(filename);
+               if (file_md5 && strcmp(file_md5, pkg_md5)) {
+                       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);
+       }
+
+       /* Check for sha256 value */
+       pkg_sha256 = pkg_get_sha256(pkg);
+       if (pkg_sha256) {
+               file_sha256 = file_sha256sum_alloc(filename);
+               if (file_sha256 && strcmp(file_sha256, pkg_sha256)) {
+                       if (!conf->force_checksum) {
+                               opkg_msg(ERROR,
+                                        "Package %s sha256sum mismatch. "
+                                        "Either the opkg or the package index are corrupt. "
+                                        "Try 'opkg update'.\n", pkg->name);
+                               free(file_sha256);
+                               return -1;
+                       } else {
+                               opkg_msg(NOTICE,
+                                        "Ignored %s sha256sum mismatch.\n",
+                                        pkg->name);
+                       }
+               }
+               if (file_sha256)
+                       free(file_sha256);
+       }
+
+       return err;
+}
+
 int
 opkg_download(const char *src, const char *dest_file_name,
               const short hide_error)
index fd260bc35aa97a2bde34c58eef2e349b22f568ea..863bcf25a80f137129312476a956624a9176c74d 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "pkg.h"
 
+int opkg_verify_integrity(pkg_t *pkg, const char *filename);
 int opkg_download(const char *src, const char *dest_file_name,
                   const short hide_error);
 int opkg_download_pkg(pkg_t * pkg, const char *dir);
index 62db9650f411845c9fa05aa92e5297b6e20bf80d..c6dda4876dc276d322d0fbe68b7d0c5330ad15a8 100644 (file)
@@ -1250,12 +1250,8 @@ int opkg_install_pkg(pkg_t * pkg, int from_upgrade)
        pkg_vec_t *replacees;
        abstract_pkg_t *ab_pkg = NULL;
        int old_state_flag;
-       char *file_md5, *pkg_md5;
-       char *file_sha256, *pkg_sha256;
        sigset_t newset, oldset;
        const char *local_filename;
-       long long int pkg_expected_size;
-       struct stat pkg_stat;
        time_t now;
 
        if (from_upgrade)
@@ -1367,72 +1363,9 @@ int opkg_install_pkg(pkg_t * pkg, int from_upgrade)
        }
 #endif
 
-       /* Check file size */
-       err = lstat(local_filename, &pkg_stat);
-
-       if (err) {
-               opkg_msg(ERROR, "Failed to stat %s: %s\n",
-                        local_filename, strerror(errno));
+       err = opkg_verify_integrity(pkg, local_filename);
+       if (err)
                return -1;
-       }
-
-       pkg_expected_size = pkg_get_int(pkg, PKG_SIZE);
-
-       if (pkg_expected_size > 0 && pkg_stat.st_size != pkg_expected_size) {
-               if (!conf->force_checksum) {
-                       opkg_msg(ERROR,
-                                "Package size mismatch: %s is %lld bytes, expecting %lld bytes\n",
-                                pkg->name, (long long int)pkg_stat.st_size, pkg_expected_size);
-                       return -1;
-               } else {
-                       opkg_msg(NOTICE,
-                                "Ignored %s size mismatch.\n",
-                                pkg->name);
-               }
-       }
-
-       /* Check for md5 values */
-       pkg_md5 = pkg_get_md5(pkg);
-       if (pkg_md5) {
-               file_md5 = file_md5sum_alloc(local_filename);
-               if (file_md5 && strcmp(file_md5, pkg_md5)) {
-                       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);
-       }
-
-       /* Check for sha256 value */
-       pkg_sha256 = pkg_get_sha256(pkg);
-       if (pkg_sha256) {
-               file_sha256 = file_sha256sum_alloc(local_filename);
-               if (file_sha256 && strcmp(file_sha256, pkg_sha256)) {
-                       if (!conf->force_checksum) {
-                               opkg_msg(ERROR,
-                                        "Package %s sha256sum mismatch. "
-                                        "Either the opkg or the package index are corrupt. "
-                                        "Try 'opkg update'.\n", pkg->name);
-                               free(file_sha256);
-                               return -1;
-                       } else {
-                               opkg_msg(NOTICE,
-                                        "Ignored %s sha256sum mismatch.\n",
-                                        pkg->name);
-                       }
-               }
-               if (file_sha256)
-                       free(file_sha256);
-       }
 
        if (conf->download_only) {
                if (conf->nodeps == 0) {