[pacman-dev] [PATCH] fetch_url: look for files in cache before downloading

Dave Reisner d at falconindy.com
Sat Jan 14 15:46:40 EST 2012


We lost this logic somewhere between the libfetch and libcurl
transition, as it existed in the internal downloader, but was pulled
back only into the sync workflow. Add a helper function that will let us
check for existance in the filecache prior to calling the downloader.

Signed-off-by: Dave Reisner <dreisner at archlinux.org>
---
Ugly patch, for not a whole lot of noise. Pointed out by Ioni that we don't
look in the cachedirs anymore for files downloaded via -U.

 lib/libalpm/dload.c |   92 +++++++++++++++++++++++++++++++++------------------
 1 files changed, 60 insertions(+), 32 deletions(-)

diff --git a/lib/libalpm/dload.c b/lib/libalpm/dload.c
index a3980d6..0092838 100644
--- a/lib/libalpm/dload.c
+++ b/lib/libalpm/dload.c
@@ -558,6 +558,22 @@ int _alpm_download(struct dload_payload *payload, const char *localpath,
 	}
 }
 
+static char *filecache_find_url(alpm_handle_t *handle, const char *url)
+{
+	const char *basename = strrchr(url, '/');
+
+	if(basename == NULL) {
+		return 0;
+	}
+
+	basename++;
+	if(basename == '\0') {
+		return 0;
+	}
+
+	return _alpm_filecache_find(handle, basename);
+}
+
 /** Fetch a remote pkg. */
 char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url)
 {
@@ -565,7 +581,7 @@ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url)
 	const char *cachedir;
 	char *final_file = NULL;
 	struct dload_payload payload;
-	int ret;
+	int ret = 0;
 
 	CHECK_HANDLE(handle, return NULL);
 	ASSERT(url, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL));
@@ -574,51 +590,63 @@ char SYMEXPORT *alpm_fetch_pkgurl(alpm_handle_t *handle, const char *url)
 	cachedir = _alpm_filecache_setup(handle);
 
 	memset(&payload, 0, sizeof(struct dload_payload));
-	payload.handle = handle;
-	STRDUP(payload.fileurl, url, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
-	payload.allow_resume = 1;
-
-	/* download the file */
-	ret = _alpm_download(&payload, cachedir, &final_file);
-	_alpm_dload_payload_reset(&payload);
-	if(ret == -1) {
-		_alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), url);
-		free(final_file);
-		return NULL;
+
+	/* attempt to find the file in our pkgcache */
+	filepath = filecache_find_url(handle, url);
+	if(filepath == NULL) {
+		STRDUP(payload.fileurl, url, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
+		payload.allow_resume = 1;
+		payload.handle = handle;
+
+		/* download the file */
+		ret = _alpm_download(&payload, cachedir, &final_file);
+		_alpm_dload_payload_reset(&payload);
+		if(ret == -1) {
+			_alpm_log(handle, ALPM_LOG_WARNING, _("failed to download %s\n"), url);
+			free(final_file);
+			return NULL;
+		}
+		_alpm_log(handle, ALPM_LOG_DEBUG, "successfully downloaded %s\n", url);
 	}
-	_alpm_log(handle, ALPM_LOG_DEBUG, "successfully downloaded %s\n", url);
 
 	/* attempt to download the signature */
 	if(ret == 0 && (handle->siglevel & ALPM_SIG_PACKAGE)) {
-		char *sig_final_file = NULL;
+		char *sig_filepath, *sig_final_file = NULL;
 		size_t len;
 
 		len = strlen(url) + 5;
 		MALLOC(payload.fileurl, len, RET_ERR(handle, ALPM_ERR_MEMORY, NULL));
 		snprintf(payload.fileurl, len, "%s.sig", url);
-		payload.handle = handle;
-		payload.force = 1;
-		payload.errors_ok = (handle->siglevel & ALPM_SIG_PACKAGE_OPTIONAL);
-
-		/* set hard upper limit of 16KiB */
-		payload.max_size = 16 * 1024;
-
-		ret = _alpm_download(&payload, cachedir, &sig_final_file);
-		if(ret == -1 && !payload.errors_ok) {
-			_alpm_log(handle, ALPM_LOG_WARNING,
-					_("failed to download %s\n"), payload.fileurl);
-			/* Warn now, but don't return NULL. We will fail later during package
-			 * load time. */
-		} else if(ret == 0) {
-			_alpm_log(handle, ALPM_LOG_DEBUG,
-					"successfully downloaded %s\n", payload.fileurl);
+
+		sig_filepath = filecache_find_url(handle, payload.fileurl);
+		if(sig_filepath == NULL) {
+			payload.handle = handle;
+			payload.force = 1;
+			payload.errors_ok = (handle->siglevel & ALPM_SIG_PACKAGE_OPTIONAL);
+
+			/* set hard upper limit of 16KiB */
+			payload.max_size = 16 * 1024;
+
+			ret = _alpm_download(&payload, cachedir, &sig_final_file);
+			if(ret == -1 && !payload.errors_ok) {
+				_alpm_log(handle, ALPM_LOG_WARNING,
+						_("failed to download %s\n"), payload.fileurl);
+				/* Warn now, but don't return NULL. We will fail later during package
+				 * load time. */
+			} else if(ret == 0) {
+				_alpm_log(handle, ALPM_LOG_DEBUG,
+						"successfully downloaded %s\n", payload.fileurl);
+			}
+			FREE(sig_final_file);
 		}
-		FREE(sig_final_file);
+		free(sig_filepath);
 		_alpm_dload_payload_reset(&payload);
 	}
 
 	/* we should be able to find the file the second time around */
-	filepath = _alpm_filecache_find(handle, final_file);
+	if(filepath == NULL) {
+		filepath = _alpm_filecache_find(handle, final_file);
+	}
 	free(final_file);
 
 	return filepath;
-- 
1.7.8.3



More information about the pacman-dev mailing list