From d734ebdde270c1e13e3b910e731bea88d874a51c Mon Sep 17 00:00:00 2001 From: Chantry Xavier Date: Wed, 27 Feb 2008 00:50:17 +0100 Subject: libalpm: clean up of md5sum functions. test_delta_md5sum and test_pkg_md5sum were simple wrappers to test_md5sum, and only used once, so not very useful. I removed them. Also, test_md5sum and alpm_pkg_checkmd5sum functions were a bit duplicated, so I refactored them with a new _alpm_test_md5sum function in libalpm/util.c Signed-off-by: Chantry Xavier --- lib/libalpm/alpm.h | 3 +- lib/libalpm/error.c | 6 +-- lib/libalpm/package.c | 27 +++-------- lib/libalpm/sync.c | 126 ++++++++++---------------------------------------- lib/libalpm/util.c | 27 ++++++++--- lib/libalpm/util.h | 1 + 6 files changed, 55 insertions(+), 135 deletions(-) (limited to 'lib/libalpm') diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h index 2f9eae77..4e2c2219 100644 --- a/lib/libalpm/alpm.h +++ b/lib/libalpm/alpm.h @@ -458,10 +458,9 @@ enum _pmerrno_t { PM_ERR_PKG_CANT_FRESH, PM_ERR_PKG_CANT_REMOVE, PM_ERR_PKG_INVALID_NAME, - PM_ERR_PKG_CORRUPTED, PM_ERR_PKG_REPO_NOT_FOUND, /* Deltas */ - PM_ERR_DLT_CORRUPTED, + PM_ERR_DLT_INVALID, PM_ERR_DLT_PATCHFAILED, /* Groups */ PM_ERR_GRP_NOT_FOUND, diff --git a/lib/libalpm/error.c b/lib/libalpm/error.c index a68340ad..2e015a68 100644 --- a/lib/libalpm/error.c +++ b/lib/libalpm/error.c @@ -117,13 +117,11 @@ const char SYMEXPORT *alpm_strerror(int err) return _("cannot remove all files for package"); case PM_ERR_PKG_INVALID_NAME: return _("package name is not valid"); - case PM_ERR_PKG_CORRUPTED: - return _("corrupted package"); case PM_ERR_PKG_REPO_NOT_FOUND: return _("no such repository"); /* Deltas */ - case PM_ERR_DLT_CORRUPTED: - return _("corrupted delta"); + case PM_ERR_DLT_INVALID: + return _("invalid or corrupted delta"); case PM_ERR_DLT_PATCHFAILED: return _("delta patch failed"); /* Groups */ diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c index 98417155..d4341819 100644 --- a/lib/libalpm/package.c +++ b/lib/libalpm/package.c @@ -105,8 +105,7 @@ int SYMEXPORT alpm_pkg_free(pmpkg_t *pkg) int SYMEXPORT alpm_pkg_checkmd5sum(pmpkg_t *pkg) { char *fpath; - char *md5sum = NULL; - int retval = 0; + int retval; ALPM_LOG_FUNC; @@ -116,28 +115,16 @@ int SYMEXPORT alpm_pkg_checkmd5sum(pmpkg_t *pkg) ASSERT(pkg->origin_data.db != handle->db_local, RET_ERR(PM_ERR_PKG_INVALID, -1)); fpath = _alpm_filecache_find(alpm_pkg_get_filename(pkg)); - md5sum = alpm_get_md5sum(fpath); - if(md5sum == NULL) { - _alpm_log(PM_LOG_ERROR, _("could not get md5sum for package %s-%s\n"), - alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); - pm_errno = PM_ERR_NOT_A_FILE; + retval = _alpm_test_md5sum(fpath, alpm_pkg_get_md5sum(pkg)); + + if(retval == 0) { + return(0); + } else if (retval == 1) { + pm_errno = PM_ERR_PKG_INVALID; retval = -1; - } else { - if(strcmp(md5sum, alpm_pkg_get_md5sum(pkg)) == 0) { - _alpm_log(PM_LOG_DEBUG, "md5sums for package %s-%s match\n", - alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); - } else { - _alpm_log(PM_LOG_ERROR, _("md5sums do not match for package %s-%s\n"), - alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); - pm_errno = PM_ERR_PKG_INVALID; - retval = -1; - } } - FREE(fpath); - FREE(md5sum); - return(retval); } diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index ff002caa..2bd9c529 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -798,107 +798,29 @@ static int apply_deltas(pmtrans_t *trans, alpm_list_t *patches) * @param trans the transaction * @param filename the filename of the file to test * @param md5sum the expected md5sum of the file - * @param data data to write the error messages to * - * @return 0 if the md5sum matched, 1 otherwise + * @return 0 if the md5sum matched, 1 if not, -1 in case of errors */ static int test_md5sum(pmtrans_t *trans, const char *filename, - const char *md5sum, alpm_list_t **data) + const char *md5sum) { char *filepath; - char *md5sum2; - char *errormsg = NULL; - int ret = 0; + int ret; filepath = _alpm_filecache_find(filename); - md5sum2 = alpm_get_md5sum(filepath); - if(md5sum == NULL) { - if(data) { - /* TODO wtf is this? malloc'd strings for error messages? */ - if((errormsg = calloc(512, sizeof(char))) == NULL) { - RET_ERR(PM_ERR_MEMORY, -1); - } - snprintf(errormsg, 512, _("can't get md5 checksum for file %s\n"), - filename); - *data = alpm_list_add(*data, errormsg); - } - ret = 1; - } else if(md5sum2 == NULL) { - if(data) { - if((errormsg = calloc(512, sizeof(char))) == NULL) { - RET_ERR(PM_ERR_MEMORY, -1); - } - snprintf(errormsg, 512, _("can't get md5 checksum for file %s\n"), - filename); - *data = alpm_list_add(*data, errormsg); - } - ret = 1; - } else if(strcmp(md5sum, md5sum2) != 0) { + ret = _alpm_test_md5sum(filepath, md5sum); + + if(ret == 1) { int doremove = 0; QUESTION(trans, PM_TRANS_CONV_CORRUPTED_PKG, (char *)filename, NULL, NULL, &doremove); if(doremove) { unlink(filepath); } - if(data) { - if((errormsg = calloc(512, sizeof(char))) == NULL) { - RET_ERR(PM_ERR_MEMORY, -1); - } - snprintf(errormsg, 512, _("file %s was corrupted (bad MD5 checksum)\n"), - filename); - *data = alpm_list_add(*data, errormsg); - } - ret = 1; } FREE(filepath); - FREE(md5sum2); - - return(ret); -} - -/** Compares the md5sum of a delta to the expected value. - * - * @param trans the transaction - * @param delta the delta to test - * @param data data to write the error messages to - * - * @return 0 if the md5sum matched, 1 otherwise - */ -static int test_delta_md5sum(pmtrans_t *trans, pmdelta_t *delta, - alpm_list_t **data) -{ - const char *filename; - const char *md5sum; - int ret = 0; - - filename = alpm_delta_get_filename(delta); - md5sum = alpm_delta_get_md5sum(delta); - - ret = test_md5sum(trans, filename, md5sum, data); - - return(ret); -} - -/** Compares the md5sum of a package to the expected value. - * - * @param trans the transaction - * @param pkg the package to test - * @param data data to write the error messages to - * - * @return 0 if the md5sum matched, 1 otherwise - */ -static int test_pkg_md5sum(pmtrans_t *trans, pmpkg_t *pkg, alpm_list_t **data) -{ - const char *filename; - const char *md5sum; - int ret = 0; - - filename = alpm_pkg_get_filename(pkg); - md5sum = alpm_pkg_get_md5sum(pkg); - - ret = test_md5sum(trans, filename, md5sum, data); return(ret); } @@ -908,7 +830,8 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data) alpm_list_t *i, *j, *files = NULL; alpm_list_t *patches = NULL, *deltas = NULL; pmtrans_t *tr = NULL; - int replaces = 0, retval = 0; + int replaces = 0; + int errors = 0; const char *cachedir = NULL; ALPM_LOG_FUNC; @@ -999,22 +922,22 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data) /* only output if there are deltas to work with */ if(deltas) { + errors = 0; /* Check integrity of deltas */ EVENT(trans, PM_TRANS_EVT_DELTA_INTEGRITY_START, NULL, NULL); for(i = deltas; i; i = i->next) { pmdelta_t *d = alpm_list_getdata(i); + const char *filename = alpm_delta_get_filename(d); + const char *md5sum = alpm_delta_get_md5sum(d); - ret = test_delta_md5sum(trans, d, data); - - if(ret == 1) { - retval = 1; - } else if(ret == -1) { /* -1 is for serious errors */ - RET_ERR(pm_errno, -1); + if(test_md5sum(trans, filename, md5sum) != 0) { + errors++; + *data = alpm_list_add(*data, strdup(filename)); } } - if(retval) { - pm_errno = PM_ERR_DLT_CORRUPTED; + if(errors) { + pm_errno = PM_ERR_DLT_INVALID; goto error; } EVENT(trans, PM_TRANS_EVT_DELTA_INTEGRITY_DONE, NULL, NULL); @@ -1038,21 +961,20 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data) /* Check integrity of packages */ EVENT(trans, PM_TRANS_EVT_INTEGRITY_START, NULL, NULL); + errors = 0; for(i = trans->packages; i; i = i->next) { pmsyncpkg_t *sync = i->data; pmpkg_t *spkg = sync->pkg; - int ret = 0; - - ret = test_pkg_md5sum(trans, spkg, data); + const char *filename = alpm_pkg_get_filename(spkg); + const char *md5sum = alpm_pkg_get_md5sum(spkg); - if(ret == 1) { - retval = 1; - } else if(ret == -1) { /* -1 is for serious errors */ - RET_ERR(pm_errno, -1); + if(test_md5sum(trans, filename, md5sum) != 0) { + errors++; + *data = alpm_list_add(*data, strdup(filename)); } } - if(retval) { - pm_errno = PM_ERR_PKG_CORRUPTED; + if(errors) { + pm_errno = PM_ERR_PKG_INVALID; goto error; } EVENT(trans, PM_TRANS_EVT_INTEGRITY_DONE, NULL, NULL); diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index e1413a25..806f601c 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -640,13 +640,7 @@ char SYMEXPORT *alpm_get_md5sum(const char *filename) ret = md5_file(filename, output); if (ret > 0) { - if (ret == 1) { - _alpm_log(PM_LOG_ERROR, _("md5: %s can't be opened\n"), filename); - } else if (ret == 2) { - _alpm_log(PM_LOG_ERROR, _("md5: %s can't be read\n"), filename); - } - - return(NULL); + RET_ERR(PM_ERR_NOT_A_FILE, NULL); } /* Convert the result to something readable */ @@ -660,4 +654,23 @@ char SYMEXPORT *alpm_get_md5sum(const char *filename) return(md5sum); } +int _alpm_test_md5sum(const char *filepath, const char *md5sum) +{ + char *md5sum2; + int ret; + + md5sum2 = alpm_get_md5sum(filepath); + + if(md5sum == NULL || md5sum2 == NULL) { + ret = -1; + } else if(strcmp(md5sum, md5sum2) != 0) { + ret = 1; + } else { + ret = 0; + } + + FREE(md5sum2); + return(ret); +} + /* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 4a06e3b0..6849fa19 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -65,6 +65,7 @@ int _alpm_str_cmp(const void *s1, const void *s2); char *_alpm_filecache_find(const char *filename); const char *_alpm_filecache_setup(void); int _alpm_lstat(const char *path, struct stat *buf); +int _alpm_test_md5sum(const char *filepath, const char *md5sum); #ifndef HAVE_STRVERSCMP int strverscmp(const char *, const char *); -- cgit v1.2.3-24-g4f1b