diff --git a/conans/client/downloaders/cached_file_downloader.py b/conans/client/downloaders/cached_file_downloader.py index 7a5216a9edd..ee1a5bc548a 100644 --- a/conans/client/downloaders/cached_file_downloader.py +++ b/conans/client/downloaders/cached_file_downloader.py @@ -7,7 +7,8 @@ from conans.client.downloaders.file_downloader import check_checksum from conans.errors import ConanException -from conans.util.files import mkdir, set_dirty, clean_dirty, is_dirty +from conans.util.log import logger +from conans.util.files import mkdir, set_dirty, clean_dirty, is_dirty, remove from conans.util.locks import SimpleLock from conans.util.sha import sha256 as sha256_sum @@ -47,19 +48,21 @@ def download(self, url, file_path=None, md5=None, sha1=None, sha256=None, **kwar if os.path.exists(cached_path): os.remove(cached_path) clean_dirty(cached_path) + + if os.path.exists(cached_path): + # If exists but it is corrupted, it is removed. Note that v2 downloads + # do not have checksums, this only works for user downloads + try: + check_checksum(cached_path, md5, sha1, sha256) + except ConanException: + logger.error("Cached file corrupt, redownloading") + remove(cached_path) + if not os.path.exists(cached_path): set_dirty(cached_path) self._file_downloader.download(url=url, file_path=cached_path, md5=md5, sha1=sha1, sha256=sha256, **kwargs) clean_dirty(cached_path) - else: - # specific check for corrupted cached files, will raise, but do nothing more - # user can report it or "rm -rf cache_folder/path/to/file" - try: - check_checksum(cached_path, md5, sha1, sha256) - except ConanException as e: - raise ConanException("%s\nCached downloaded file corrupted: %s" - % (str(e), cached_path)) if file_path is not None: file_path = os.path.abspath(file_path) diff --git a/conans/test/integration/cache/download_cache_test.py b/conans/test/integration/cache/download_cache_test.py index da36dcba8da..be5135d3d58 100644 --- a/conans/test/integration/cache/download_cache_test.py +++ b/conans/test/integration/cache/download_cache_test.py @@ -93,9 +93,8 @@ def package(self): continue save(f, load(f) + "a") client.run("remove * -f") - client.run("install mypkg/0.1@user/testing", assert_error=True) - self.assertIn("ERROR: md5 signature failed", client.out) - self.assertIn("Cached downloaded file corrupted", client.out) + client.run("install mypkg/0.1@user/testing") + self.assertIn("mypkg/0.1@user/testing: Downloaded package", client.out) @pytest.mark.skipif(not get_env("TESTING_REVISIONS_ENABLED", False), reason="Only revisions") def test_dirty_download(self):