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.
master
OpenGnSys Support Team 2024-07-30 00:14:01 +02:00
parent a846d5e343
commit 49017c00ca
1 changed files with 13 additions and 4 deletions

View File

@ -41,6 +41,16 @@ def tip_fetch_csum(tip_addr, image_name):
return r 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): def tip_write_csum(image_name):
if not mount_cache(): if not mount_cache():
raise OgError(f'Failed to checksum {image_name}: cache partition is not available') 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' image_path = f'{OG_CACHE_IMAGE_PATH}{image_name}.img'
if not os.path.exists(image_path): if not os.path.exists(image_path):
raise OgError(f'File {image_path} does not exist') 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"): 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}') 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: if cache_csum == remote_csum:
ret = True ret = True
logging.info(f'Checksum is OK for {image_name}.img') logging.info(f'Checksum is OK for {image_name}.img')