From 7c7352ffe39e215fb4e8dfd83ecf2d1d1c1a6856 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 8 Apr 2017 18:54:04 +0200 Subject: [PATCH] http: Compare retrieved files against a checksum If the checksum does not match, the file is redownloaded from another mirror server until all mirrors have been tried. Signed-off-by: Michael Tremer --- src/pakfire/http.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pakfire/http.py b/src/pakfire/http.py index be4619320..7222cf5cb 100644 --- a/src/pakfire/http.py +++ b/src/pakfire/http.py @@ -20,6 +20,7 @@ ############################################################################### import base64 +import hashlib import json import logging import shutil @@ -296,7 +297,7 @@ class Client(object): raise MaxTriesExceededError - def retrieve(self, url, filename=None, message=None, **kwargs): + def retrieve(self, url, filename=None, message=None, checksum=None, checksum_algo=None, **kwargs): p = None skipped_mirrors = [] @@ -329,17 +330,25 @@ class Client(object): l = self._get_content_length(res) p.value_max = l + # Compute a checksum of each downloaded file + h = hashlib.new(checksum_algo or "sha512") + while True: buf = res.read(BUFFER_SIZE) if not buf: break # Write downloaded data to file + h.update(buf) f.write(buf) l = len(buf) p.increment(l) + # Check integrity of the downloaded file + if checksum and not checksum == h.hexdigest(): + raise DownloadError(_("Invalid checksum")) + # If the download succeeded, we will # break the loop break -- 2.39.5