From 49017c00caa56255a6946003fd8a62804d6c061f Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Tue, 30 Jul 2024 00:14:01 +0200 Subject: utils: tip_check_csum() uses local checksum file .full.sum file in the local cache contains the local checksum for this file, this checksum is calculated by tip_write_csum(), therefore, there is no need to calculate this checksum again from the image file in the cache, use this checksum content instead. if .full.sum is not available, then cache is inconsistent, tip_check_csum() is called after checking if image file exists, raise an exception. checksum could mismatch in two situations: a) new image version (checksum is different) b) image is corrupted in both cases, a new fresh image needs to be retrieved. this speeds up checksum validation. --- src/utils/tiptorrent.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/utils/tiptorrent.py b/src/utils/tiptorrent.py index 77cc78b..932f164 100644 --- a/src/utils/tiptorrent.py +++ b/src/utils/tiptorrent.py @@ -41,6 +41,16 @@ def tip_fetch_csum(tip_addr, image_name): return r +def _tip_read_csum(image_checksum_path): + try: + with open(image_checksum_path, 'r') as f: + checksum = f.read().strip("\n") + except OSError as e: + return "unavailable" + + return checksum + + def tip_write_csum(image_name): if not mount_cache(): raise OgError(f'Failed to checksum {image_name}: cache partition is not available') @@ -71,13 +81,12 @@ def tip_check_csum(tip_addr, image_name): image_path = f'{OG_CACHE_IMAGE_PATH}{image_name}.img' if not os.path.exists(image_path): raise OgError(f'File {image_path} does not exist') - - cache_csum = _compute_md5(image_path) - remote_csum = tip_fetch_csum(tip_addr, image_name) - if not os.path.exists(f"{image_path}.full.sum"): raise OgError(f'File {image_path}.full.sum does not exist in repository {tip_addr}') + cache_csum = _tip_read_csum(f"{image_path}.full.sum") + remote_csum = tip_fetch_csum(tip_addr, image_name) + if cache_csum == remote_csum: ret = True logging.info(f'Checksum is OK for {image_name}.img') -- cgit v1.2.3-18-g5258