From: Michael Tremer Date: Wed, 11 Apr 2012 22:21:02 +0000 (+0200) Subject: Better handling for broken downloads. X-Git-Tag: 0.9.22~9^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=023fece012f425e349a57ab90cc9e95d7eff4153;p=pakfire.git Better handling for broken downloads. i.e. files with wrong checksums. --- diff --git a/python/pakfire/keyring.py b/python/pakfire/keyring.py index af2ad7c3d..c7da4b350 100644 --- a/python/pakfire/keyring.py +++ b/python/pakfire/keyring.py @@ -242,6 +242,8 @@ class Keyring(object): def verify(self, signature, cleartext): assert signature, "Empty signature?" + assert signature, "Signature is empty?" + signature = io.BytesIO(signature) cleartext = io.BytesIO(cleartext) diff --git a/python/pakfire/repository/cache.py b/python/pakfire/repository/cache.py index ef9597d41..23641fe65 100644 --- a/python/pakfire/repository/cache.py +++ b/python/pakfire/repository/cache.py @@ -99,11 +99,17 @@ class RepositoryCache(object): return open(filename, *args, **kwargs) + def hash1(self, filename): + """ + Return hash of the file in the cache. + """ + return util.calc_hash1(self.abspath(filename)) + def verify(self, filename, hash1): """ Return a bool that indicates if a file matches the given hash. """ - return util.calc_hash1(self.abspath(filename)) == hash1 + return self.hash1(filename) == hash1 def remove(self, filename): """ diff --git a/python/pakfire/repository/remote.py b/python/pakfire/repository/remote.py index 305d45e03..dade232cd 100644 --- a/python/pakfire/repository/remote.py +++ b/python/pakfire/repository/remote.py @@ -285,7 +285,7 @@ class RepositoryRemote(base.RepositoryFactory): download = False else: # The file in cache has a wrong hash. Remove it and repeat download. - cache.remove(cache_filename) + self.cache.remove(cache_filename) # Get a package grabber and add mirror download capabilities to it. grabber = downloader.PackageDownloader( @@ -305,7 +305,10 @@ class RepositoryRemote(base.RepositoryFactory): raise OfflineModeError, _("Cannot download this file in offline mode: %s") \ % filename - i = grabber.urlopen(filename) + try: + i = grabber.urlopen(filename) + except urlgrabber.grabber.URLGrabError, e: + raise DownloadError, _("Could not download %s: %s") % (filename, e) # Open input and output files and download the file. o = self.cache.open(cache_filename, "w") @@ -318,15 +321,27 @@ class RepositoryRemote(base.RepositoryFactory): i.close() o.close() - if self.cache.verify(cache_filename, hash1): + # Calc the hash1 of the downloaded file. + calc_hash1 = self.cache.hash1(cache_filename) + + if calc_hash1 == hash1: logger.debug("Successfully downloaded %s (%s)." % (filename, hash1)) break + sums = { + "good" : hash1, + "bad" : calc_hash1, + } + logger.warning(_("The checksum of the downloaded file did not match.")) + logger.warning(_("Expected %(good)s but got %(bad)s.") % sums) logger.warning(_("Trying an other mirror.")) + # Remove the bad file. + self.cache.remove(cache_filename) + # Go to the next mirror. - grabber.increment_mirror() + grabber.increment_mirror(grabber) return os.path.join(self.cache.path, cache_filename)