tools/userland-fetch
changeset 135 5c6feb0253fa
parent 58 d8024c042a00
child 136 6a7dd3a5aaf9
--- a/tools/userland-fetch	Tue Mar 15 09:38:10 2011 -0700
+++ b/tools/userland-fetch	Wed Mar 16 07:42:49 2011 -0700
@@ -32,15 +32,9 @@
 import sys
 import shutil
 from urllib import splittype, urlopen
-
-def validate(filename, hash):
-	import hashlib
+import hashlib
 
-	try:
-		file = open(filename, 'r')
-	except IOError:
-		return False
-
+def validate(file, hash):
 	algorithm, hashvalue = hash.split(':')
 	try:
 		m = hashlib.new(algorithm)
@@ -55,6 +49,34 @@
 
 	return "%s:%s" % (algorithm, m.hexdigest())
 
+def validate_container(filename, hash):
+	try:
+		file = open(filename, 'r')
+	except IOError:
+		return False
+	return validate(file, hash)
+
+
+def validate_payload(filename, hash):
+	import re
+	import gzip
+	import bz2
+
+	expr_bz = re.compile('.+\.bz2$', re.IGNORECASE)
+	expr_gz = re.compile('.+\.gz$', re.IGNORECASE)
+
+	try:
+		if expr_bz.match(filename):
+			file = bz2.BZFile(filename, 'r')
+		elif expr_gz.match(filename):
+			file = gzip.GzipFile(filename, 'r')
+		else:
+			return False
+	except IOError:
+		return False
+	return validate(file, hash)
+
+
 def download(url, filename = None):
 	src = None
 
@@ -178,14 +200,19 @@
 			print "skipping (no hash)"
 			sys.exit(0)
 			
-		realhash = validate(name, hash_arg)
+		realhash = validate_container(name, hash_arg)
 		if realhash == hash_arg:
 			print "ok"
 			sys.exit(0)
 		else:
+			payloadhash = validate_payload(name, hash_arg)
+			if payloadhash == hash_arg:
+				print "ok"
+				sys.exit(0)
 			print "corruption detected"
 			print "    expected: %s" % hash_arg
 			print "    actual:   %s" % realhash
+			print "    payload:  %s" % payloadhash
 
 		try:
 			os.remove(name)