diff --git a/gns3server/utils/images.py b/gns3server/utils/images.py index 3d45092c..ae3eb197 100644 --- a/gns3server/utils/images.py +++ b/gns3server/utils/images.py @@ -27,7 +27,7 @@ def md5sum(path): :returns: Digest of the image """ - if path is None or len(path) == 0: + if path is None or len(path) == 0 or not os.path.exists(path): return None try: @@ -36,14 +36,18 @@ def md5sum(path): except OSError: pass - m = hashlib.md5() - with open(path, 'rb') as f: - while True: - buf = f.read(128) - if not buf: - break - m.update(buf) - digest = m.hexdigest() + try: + m = hashlib.md5() + with open(path, 'rb') as f: + while True: + buf = f.read(128) + if not buf: + break + m.update(buf) + digest = m.hexdigest() + except OSError as e: + log.error("Can't create digest of %s: %s", path, str(e)) + return None try: with open('{}.md5sum'.format(path), 'w+') as f: diff --git a/tests/utils/test_images.py b/tests/utils/test_images.py index d13bda69..42811c84 100644 --- a/tests/utils/test_images.py +++ b/tests/utils/test_images.py @@ -34,12 +34,24 @@ def test_md5sum(tmpdir): def test_md5sum_existing_digest(tmpdir): fake_img = str(tmpdir / 'hello') + with open(fake_img, 'w+') as f: + f.write('hello') + with open(str(tmpdir / 'hello.md5sum'), 'w+') as f: f.write('aaaaa02abc4b2a76b9719d911017c592') assert md5sum(fake_img) == 'aaaaa02abc4b2a76b9719d911017c592' +def test_md5sum_existing_digest_but_missing_image(tmpdir): + fake_img = str(tmpdir / 'hello') + + with open(str(tmpdir / 'hello.md5sum'), 'w+') as f: + f.write('aaaaa02abc4b2a76b9719d911017c592') + + assert md5sum(fake_img) is None + + def test_md5sum_none(tmpdir): assert md5sum(None) is None