From c7c88f880ad11a4afe2fab9bfb4f9d505f25b492 Mon Sep 17 00:00:00 2001 From: morganamilo Date: Mon, 27 Sep 2021 18:30:17 +0100 Subject: [PATCH] libalpm: check filecache_find return and log errors Some user had erros while updating their system. :: Proceed with installation? [Y/n] :: Retrieving packages... checking keyring... checking package integrity... error: failed to commit transaction (invalid or corrupted package) Errors occurred, no packages were upgraded. The issue was filecache_find returning null and alpm passing that null path to check validity. How this happened I have no idea. It may be something to do with the user's cachedir being a network drive. Also warn when the file exists but it is not a regular file or can not be opened. --- lib/libalpm/sync.c | 13 +++++++++++++ lib/libalpm/util.c | 15 +++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index 36ad6242..8deb9b3a 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -991,6 +991,13 @@ static int check_validity(alpm_handle_t *handle, current_bytes += v.pkg->size; v.path = _alpm_filecache_find(handle, v.pkg->filename); + + if(!v.path) { + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: could not find package in cache\n"), v.pkg->name); + RET_ERR(handle, ALPM_ERR_PKG_NOT_FOUND, -1); + } + v.siglevel = alpm_db_get_siglevel(alpm_pkg_get_db(v.pkg)); if(_alpm_pkg_validate_internal(handle, v.path, v.pkg, @@ -1082,6 +1089,12 @@ static int load_packages(alpm_handle_t *handle, alpm_list_t **data, current_bytes += spkg->size; filepath = _alpm_filecache_find(handle, spkg->filename); + if(!filepath) { + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: could not find package in cache\n"), spkg->name); + RET_ERR(handle, ALPM_ERR_PKG_NOT_FOUND, -1); + } + /* load the package file and replace pkgcache entry with it in the target list */ /* TODO: alpm_pkg_get_db() will not work on this target anymore */ _alpm_log(handle, ALPM_LOG_DEBUG, diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 299d287e..f73556a8 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -831,10 +831,17 @@ char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename) for(i = handle->cachedirs; i; i = i->next) { snprintf(path, PATH_MAX, "%s%s", (char *)i->data, filename); - if(stat(path, &buf) == 0 && S_ISREG(buf.st_mode)) { - retpath = strdup(path); - _alpm_log(handle, ALPM_LOG_DEBUG, "found cached pkg: %s\n", retpath); - return retpath; + if(stat(path, &buf) == 0) { + if(S_ISREG(buf.st_mode)) { + retpath = strdup(path); + _alpm_log(handle, ALPM_LOG_DEBUG, "found cached pkg: %s\n", retpath); + return retpath; + } else { + _alpm_log(handle, ALPM_LOG_WARNING, + "cached pkg '%s' is not a regular file: mode=%i\n", path, buf.st_mode); + } + } else if(errno != ENOENT) { + _alpm_log(handle, ALPM_LOG_WARNING, "could not open '%s'\n: %s", path, strerror(errno)); } } /* package wasn't found in any cachedir */