/** Calculates a file's MD5 or SHA2 digest and compares it to an expected value. * @param filepath path of the file to check * @param expected hash value to compare against * @param type digest type to use * @return 0 if file matches the expected hash, 1 if they do not match, -1 on * error */ int _alpm_test_checksum(const char *filepath, const char *expected, alpm_pkgvalidation_t type) { char *computed; int ret; if(type == ALPM_PKG_VALIDATION_MD5SUM) { computed = alpm_compute_md5sum(filepath); } else if(type == ALPM_PKG_VALIDATION_SHA256SUM) { computed = alpm_compute_sha256sum(filepath); } else { return -1; } if(expected == NULL || computed == NULL) { ret = -1; } else if(strcmp(expected, computed) != 0) { ret = 1; } else { ret = 0; } FREE(computed); return ret; }
static int check_sha256sum(alpm_pkg_t *pkg) { int ret = 0; char path[PATH_MAX], *rel; pu_mtree_reader_t *reader; pu_mtree_t *m; if((reader = pu_mtree_reader_open_package(handle, pkg)) == NULL) { pu_ui_warn("%s: mtree data not available (%s)", alpm_pkg_get_name(pkg), strerror(errno)); return require_mtree; } strcpy(path, alpm_option_get_root(handle)); rel = path + strlen(alpm_option_get_root(handle)); if((m = pu_mtree_new()) == NULL) { pu_ui_warn("%s: error reading mtree data (%s)", alpm_pkg_get_name(pkg), strerror(errno)); pu_mtree_reader_free(reader); return require_mtree; } while(pu_mtree_reader_next(reader, m)) { char *sha; if(m->sha256digest[0] == '\0') { continue; } if(m->path[0] == '.') { continue; } if(skip_backups && match_backup(pkg, m->path)) { continue; } if(skip_noextract && match_noextract(handle, m->path)) { continue; } if(skip_noupgrade && match_noupgrade(handle, m->path)) { continue; } strcpy(rel, m->path); if((sha = alpm_compute_sha256sum(path)) == NULL) { pu_ui_warn("%s: '%s' read error (%s)", alpm_pkg_get_name(pkg), path, strerror(errno)); } else if(memcmp(m->sha256digest, sha, 32) != 0) { eprintf("%s: '%s' sha256sum mismatch (expected %s)\n", alpm_pkg_get_name(pkg), path, m->sha256digest); ret = 1; } free(sha); } pu_mtree_free(m); if(!reader->eof) { pu_ui_warn("%s: error reading mtree data (%s)", alpm_pkg_get_name(pkg), strerror(errno)); pu_mtree_reader_free(reader); return ret || require_mtree; } pu_mtree_reader_free(reader); if(!quiet && !ret) { eprintf("%s: all files match mtree sha256sums\n", alpm_pkg_get_name(pkg)); } return ret; }