From 7bc4d6c8b150314184a0eb1f23156339e255fb1e Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Sun, 6 Nov 2022 08:24:11 +0100 Subject: [PATCH] Make downloading a little more careful for races As we're downloading packages in parallel, it could happen that we copy a file that isn't fully copied yet --- lib/obs.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/obs.py b/lib/obs.py index 94aae4f..c4558cf 100644 --- a/lib/obs.py +++ b/lib/obs.py @@ -1,5 +1,6 @@ import errno import logging +import os import shutil import time import urllib.parse @@ -169,11 +170,12 @@ class OBS: with (dirpath / name).open("wb") as f: f.write(self._download(project, package, name, revision).read()) - shutil.copy(dirpath / name, cached_file) - # Validate the MD5 of the downloaded file - if md5(cached_file) != file_md5: + if md5(dirpath / name) != file_md5: raise Exception(f"Download error in {name}") + + shutil.copy(dirpath / name, cached_file.with_suffix(".new")) + os.rename(cached_file.with_suffix(".new"), cached_file) else: shutil.copy(cached_file, dirpath / name)