From: Michael Tremer Date: Wed, 7 Dec 2016 20:27:37 +0000 (+0100) Subject: http: Truncate the downloaded file instead re-opening it X-Git-Tag: 0.9.28~1285^2~1389 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7478979acb6d311a54be585aadd04fdc7e1fa9f8;p=pakfire.git http: Truncate the downloaded file instead re-opening it Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/http.py b/src/pakfire/http.py index 39ea1f7e8..8aae2492c 100644 --- a/src/pakfire/http.py +++ b/src/pakfire/http.py @@ -289,16 +289,19 @@ class Client(object): try: with self._make_progressbar(message) as p: - # Reset the progressbar in case the download restarts - p.reset() - - while True: - with open(filename, "wb") as f: - # Exclusively lock the file for download - try: - fcntl.flock(f, fcntl.LOCK_EX) - except OSError as e: - raise DownloadError(_("Could not lock target file")) from e + with open(filename, "wb") as f: + # Exclusively lock the file for download + try: + fcntl.flock(f, fcntl.LOCK_EX) + except OSError as e: + raise DownloadError(_("Could not lock target file")) from e + + while True: + # Reset the progressbar in case the download restarts + p.reset() + + # Truncate the target file and drop any downloaded content + f.truncate() # Prepare HTTP request r = self._make_request(url, mirror=self.mirror, **kwargs)