[pacman-dev] FS#33992: force download *.sig file if it does not exist in the cache

Message ID 20200629173557.2219-1-anatol.pomozov@gmail.com
State Accepted, archived
Headers show
Series [pacman-dev] FS#33992: force download *.sig file if it does not exist in the cache | expand

Commit Message

Anatol Pomozov June 29, 2020, 5:35 p.m. UTC
In case if *.pkg exists but *.sig file does not we still have to pass
the pkg to multi_download API.

To avoid redownloading *.pkg file we use CURLOPT_TIMECONDITION curl option.

Signed-off-by: Anatol Pomozov <anatol.pomozov@gmail.com>
---
 lib/libalpm/dload.c |  2 +-
 lib/libalpm/sync.c  | 31 +++++++++++++++++++++++--------
 2 files changed, 24 insertions(+), 9 deletions(-)

Patch

diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index f8ee6f85..78808eb3 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -282,7 +282,7 @@  static void curl_set_handle_opts(CURL *curl, struct dload_payload *payload)
 		curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent);
 	}
 
-	if(!payload->allow_resume && !payload->force && payload->destfile_name &&
+	if(!payload->force && payload->destfile_name &&
 			stat(payload->destfile_name, &st) == 0) {
 		/* start from scratch, but only download if our local is out of date. */
 		curl_easy_setopt(curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c
index ea45767a..8c01ad95 100644
--- a/lib/libalpm/sync.c
+++ b/lib/libalpm/sync.c
@@ -22,6 +22,7 @@ 
  */
 
 #include <sys/types.h> /* off_t */
+#include <stdbool.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -698,8 +699,9 @@  static int find_dl_candidates(alpm_handle_t *handle, alpm_list_t **files)
 		alpm_pkg_t *spkg = i->data;
 
 		if(spkg->origin != ALPM_PKG_FROM_FILE) {
-			char *fpath = NULL;
 			alpm_db_t *repo = spkg->origin_data.db;
+			bool need_download;
+			int siglevel = alpm_db_get_siglevel(alpm_pkg_get_db(spkg));
 
 			if(!repo->servers) {
 				handle->pm_errno = ALPM_ERR_SERVER_NONE;
@@ -710,16 +712,26 @@  static int find_dl_candidates(alpm_handle_t *handle, alpm_list_t **files)
 
 			ASSERT(spkg->filename != NULL, RET_ERR(handle, ALPM_ERR_PKG_INVALID_NAME, -1));
 
-			if(spkg->download_size == 0) {
-				/* check for file in cache - allows us to handle complete .part files */
-				fpath = _alpm_filecache_find(handle, spkg->filename);
+			need_download = spkg->download_size != 0 || !_alpm_filecache_exists(handle, spkg->filename);
+			/* even if the package file in the cache we need to check for
+			 * accompanion *.sig file as well.
+			 * If *.sig is not cached then force download the package + its signature file.
+			 */
+			if(!need_download && (siglevel & ALPM_SIG_PACKAGE)) {
+				char *sig_filename = NULL;
+				int len = strlen(spkg->filename) + 5;
+
+				MALLOC(sig_filename, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
+				snprintf(sig_filename, len, "%s.sig", spkg->filename);
+
+				need_download = !_alpm_filecache_exists(handle, sig_filename);
+
+				FREE(sig_filename);
 			}
 
-			if(spkg->download_size != 0 || !fpath) {
+			if(need_download) {
 				*files = alpm_list_add(*files, spkg);
 			}
-
-			FREE(fpath);
 		}
 	}
 
@@ -782,7 +794,8 @@  static int download_files(alpm_handle_t *handle)
 		event.type = ALPM_EVENT_PKG_RETRIEVE_START;
 		EVENT(handle, &event);
 		for(i = files; i; i = i->next) {
-			const alpm_pkg_t *pkg = i->data;
+			alpm_pkg_t *pkg = i->data;
+			int siglevel = alpm_db_get_siglevel(alpm_pkg_get_db(pkg));
 			struct dload_payload *payload = NULL;
 
 			CALLOC(payload, 1, sizeof(*payload), GOTO_ERR(handle, ALPM_ERR_MEMORY, finish));
@@ -794,6 +807,8 @@  static int download_files(alpm_handle_t *handle)
 			payload->servers = pkg->origin_data.db->servers;
 			payload->handle = handle;
 			payload->allow_resume = 1;
+			payload->download_signature = (siglevel & ALPM_SIG_PACKAGE);
+			payload->signature_optional = (siglevel & ALPM_SIG_PACKAGE_OPTIONAL);
 
 			payloads = alpm_list_add(payloads, payload);
 		}