]> git.ipfire.org Git - pakfire.git/commitdiff
Better handling for broken downloads.
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 11 Apr 2012 22:21:02 +0000 (00:21 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 11 Apr 2012 22:21:02 +0000 (00:21 +0200)
i.e. files with wrong checksums.

python/pakfire/keyring.py
python/pakfire/repository/cache.py
python/pakfire/repository/remote.py

index af2ad7c3d596562e13356530cd12aee324625c71..c7da4b3508ce1f17886848641ace9035dd3591c9 100644 (file)
@@ -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)
 
index ef9597d4180cd73f0d4b6935a133dd96520870a7..23641fe65ee8daade6bcbce8ba77e34d222085a3 100644 (file)
@@ -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):
                """
index 305d45e031a395f6c3042d3d53a802917c805705..dade232cdefa9b47e571395858e7fcdf249a7ac5 100644 (file)
@@ -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)