]> git.ipfire.org Git - pakfire.git/commitdiff
Check hashsum of download package database file.
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Feb 2011 23:34:07 +0000 (00:34 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Feb 2011 23:34:07 +0000 (00:34 +0100)
pakfire/index.py
pakfire/packages/util.py

index 4082fc669bfd2afee61149fa63e9c4f1ea85bdad..7b9efb1a9dde9583170b6fd3522a4b99726b3eda 100644 (file)
@@ -269,7 +269,11 @@ class DatabaseIndex(InstalledIndex):
 
                        data = grabber.urlread(filename)
 
-                       # XXX check the hashsum of the downloaded file
+                       # check the hashsum of the downloaded file
+                       if not util.calc_hash1(data=data) == self.metadata.database_hash1:
+                               # XXX an exception is not a very good idea because this file could
+                               # be downloaded from another mirror. need a better way to handle this.
+                               raise Exception, "Downloaded file did not match the hashsum. Need to re-download it."
 
                        with cache.open(filename, "w") as o:
                                o.write(data)
@@ -323,7 +327,6 @@ class DatabaseIndex(InstalledIndex):
                self._update_database(force)
 
                # XXX this code needs lots of work:
-               # XXX   * make checks for downloads (hashsums)
                # XXX   * check the metadata content
 
        def save(self, path=None, compress="xz"):
index 071dbdd7907566fc2e4f6b170d4a550eb5ef6031..3254b4c76c287335506bd4f3f722ce9be6e36c06 100644 (file)
@@ -68,16 +68,21 @@ def format_size(s):
 
        return "%d%s" % (int(s), units[unit])
 
-def calc_hash1(filename):
+def calc_hash1(filename=None, data=None):
        h = hashlib.sha1()
 
-       f = open(filename)
-       buf = f.read(BUFFER_SIZE)
-       while buf:
-               h.update(buf)
+       if filename:
+               f = open(filename)
                buf = f.read(BUFFER_SIZE)
+               while buf:
+                       h.update(buf)
+                       buf = f.read(BUFFER_SIZE)
+
+               f.close()
+
+       elif data:
+               h.update(data)
 
-       f.close()
        return h.hexdigest()
 
 def parse_pkg_expr(s):