check the source archive's hash before unpack.
This commit is contained in:
+11
-4
@@ -37,7 +37,9 @@ class SourceArchive:
|
|||||||
def __init__(self, ctx):
|
def __init__(self, ctx):
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
self.url = PUrl(self.ctx.spec.source.archiveUri)
|
self.url = PUrl(self.ctx.spec.source.archiveUri)
|
||||||
self.dest = join(config.archives_dir(), self.url.filename())
|
self.archiveFile = join(config.archives_dir(), self.url.filename())
|
||||||
|
self.archiveType = self.ctx.spec.source.archiveType
|
||||||
|
self.archiveSHA1 = self.ctx.spec.source.archiveSHA1
|
||||||
|
|
||||||
def fetch(self, interactive=True):
|
def fetch(self, interactive=True):
|
||||||
if not self.isCached(interactive):
|
if not self.isCached(interactive):
|
||||||
@@ -47,11 +49,11 @@ class SourceArchive:
|
|||||||
fetchUrl(self.url, config.archives_dir(), progress)
|
fetchUrl(self.url, config.archives_dir(), progress)
|
||||||
|
|
||||||
def isCached(self, interactive=True):
|
def isCached(self, interactive=True):
|
||||||
if not access(self.dest, R_OK):
|
if not access(self.archiveFile, R_OK):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# check hash
|
# check hash
|
||||||
if util.sha1_file(self.dest) == self.ctx.spec.source.archiveSHA1:
|
if util.check_file_hash(self.archiveFile, self.archiveSHA1):
|
||||||
if interactive:
|
if interactive:
|
||||||
ui.info('%s [cached]\n' % self.ctx.spec.source.archiveName)
|
ui.info('%s [cached]\n' % self.ctx.spec.source.archiveName)
|
||||||
return True
|
return True
|
||||||
@@ -59,5 +61,10 @@ class SourceArchive:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def unpack(self, cleanDir=True):
|
def unpack(self, cleanDir=True):
|
||||||
archive = Archive(self.dest, self.ctx.spec.source.archiveType)
|
|
||||||
|
# check archive file's integrity
|
||||||
|
if not util.check_file_hash(self.archiveFile, self.archiveSHA1):
|
||||||
|
raise SourceArchiveError, "unpack: check_file_hash failed"
|
||||||
|
|
||||||
|
archive = Archive(self.archiveFile, self.archiveType)
|
||||||
archive.unpack(self.ctx.pkg_work_dir(), cleanDir)
|
archive.unpack(self.ctx.pkg_work_dir(), cleanDir)
|
||||||
|
|||||||
@@ -205,6 +205,13 @@ def copy_dir(src, dest):
|
|||||||
"""copy source dir to destination dir recursively"""
|
"""copy source dir to destination dir recursively"""
|
||||||
shutil.copytree(src, dest)
|
shutil.copytree(src, dest)
|
||||||
|
|
||||||
|
def check_file_hash(filename, hash):
|
||||||
|
"""Check the files integrity with a given hash"""
|
||||||
|
if sha1_file(filename) == hash:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
def sha1_file(filename):
|
def sha1_file(filename):
|
||||||
"""calculate sha1 hash of filename"""
|
"""calculate sha1 hash of filename"""
|
||||||
# Broken links can cause problem!
|
# Broken links can cause problem!
|
||||||
|
|||||||
Reference in New Issue
Block a user