From 929902e95bdb7c807baf391e4155a35fe6f63b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Fri, 24 Mar 2006 19:02:04 +0000 Subject: [PATCH] * File: check integrity when reading, and also skip downloading if sha1sum matches * apply integrity check for index (fix #2340) * some small improvements to fetcher * fix: check write access to destination file if it already exists (permissions may change) --- pisi/file.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pisi/file.py b/pisi/file.py index 5a28aad0..81a921c0 100644 --- a/pisi/file.py +++ b/pisi/file.py @@ -31,6 +31,12 @@ from pisi.util import join_path as join from pisi.fetcher import fetch_url import pisi.context as ctx +class AlreadyHaveException(pisi.Exception): + def __init__(self, url, localfile): + pisi.Exception.__init__(self, "URL %s already downloaded as %s" % (url, localfile)) + self.url = url + self.localfile = localfile + class Error(pisi.Error): pass @@ -51,13 +57,35 @@ class File: @staticmethod def download(uri, transfer_dir = "/tmp", sha1sum = False, compress = None, sign = None): + assert type(uri == URI) + if sha1sum: + sha1filename = File.download(URI(uri.get_uri() + '.sha1sum'), transfer_dir) + sha1f = file(sha1filename) + newsha1 = sha1f.readlines()[0] + if uri.is_remote_file(): ctx.ui.info(_("Fetching %s") % uri.get_uri()) localfile = join(transfer_dir, uri.filename()) fetch_url(uri, transfer_dir) # FIXME: localfile would look better for fetch iface? + + # TODO: code to use old .sha1sum file, is this a necessary optimization? + #oldsha1fn = localfile + '.sha1sum' + #if os.exists(oldsha1fn): + #oldsha1 = file(oldsha1fn).readlines()[0] + if sha1sum and os.path.exists(localfile): + oldsha1 = pisi.util.sha1_file(localfile) + if (newsha1 == oldsha1): + # early terminate, we already got it ;) + raise AlreadyHaveException(uri, localfile) else: localfile = uri.get_uri() #TODO: use a special function here? + + if sha1sum: + + if (pisi.util.sha1_file(localfile) != newsha1): + raise Error(_("File integrity of %s compromised.") % uri) + return localfile def __init__(self, uri, mode, transfer_dir = "/tmp",