opkg_verify_integrity: better logging and error conditions
[project/opkg-lede.git] / libopkg / opkg_install.c
index 976f446c8411be0684b28ff7f03902610e583eeb..68fb9ea246f3c9321e726a7f2a0493cf6d8048c0 100644 (file)
@@ -49,7 +49,7 @@ static int satisfy_dependencies_for(pkg_t * pkg)
        int ndepends;
 
        ndepends = pkg_hash_fetch_unsatisfied_dependencies(pkg, depends,
-                                                          &unresolved);
+                                                          &unresolved, 0);
 
        if (unresolved) {
                opkg_msg(ERROR,
@@ -244,8 +244,7 @@ static int unpack_pkg_control_files(pkg_t * pkg)
 
        tmp_unpack_dir = mkdtemp(tmp_unpack_dir);
        if (tmp_unpack_dir == NULL) {
-               opkg_perror(ERROR, "Failed to create temporary directory '%s'",
-                           tmp_unpack_dir);
+               opkg_perror(ERROR, "Failed to create temporary directory");
                return -1;
        }
 
@@ -1251,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)
@@ -1368,71 +1363,17 @@ 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));
+       /* Check integrity (size, checksums) */
+       err = opkg_verify_integrity(pkg, local_filename);
+       if (err && !conf->force_checksum) {
+               opkg_msg(ERROR, "Checksum or size mismatch for package %s. "
+                        "Either the opkg or the package index are corrupt. "
+                        "Try 'opkg update'.\n", pkg->name);
                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 (err && conf->force_checksum) {
+               opkg_msg(NOTICE, "Ignored %s checksum or size mismatch.\n",
+                       pkg->name);
        }
 
        if (conf->download_only) {