Skip to content

Commit

Permalink
remove corrupt cache files and retry download 3 times (#8806)
Browse files Browse the repository at this point in the history
* remove corrupt cache files and retry download 3 times

* typo

* Update conans/client/downloaders/cached_file_downloader.py

* proposed refactor, .download() already tries 3 times

Co-authored-by: memsharded <james@conan.io>
  • Loading branch information
blackliner and memsharded committed Apr 20, 2021
1 parent 257cb5c commit fab3b48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
21 changes: 12 additions & 9 deletions conans/client/downloaders/cached_file_downloader.py
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions conans/test/integration/cache/download_cache_test.py
Expand Up @@ -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):
Expand Down

0 comments on commit fab3b48

Please sign in to comment.