From e2d48ba3a0909f34afcf286ece339aa6a426678c Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 21 May 2024 10:35:15 +0200 Subject: live: add cache contents to the /refresh payload Add the list of images in the client's cache partition in the payload sent to the server. The information sent is a list of {image_name, img_size, checksum} elements where img_size is the size of the respective image in bytes. Resquest response structure: { ... 'cache': [ {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'}, {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'} ] ... } --- src/live/ogOperations.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/live/ogOperations.py b/src/live/ogOperations.py index d4ac84e..6948d98 100644 --- a/src/live/ogOperations.py +++ b/src/live/ogOperations.py @@ -29,7 +29,7 @@ from src.utils.menu import generate_menu from src.utils.fs import * from src.utils.probe import os_probe, get_cache_dev_path from src.utils.disk import * -from src.utils.cache import generate_cache_txt, umount_cache, init_cache +from src.utils.cache import * from src.utils.tiptorrent import * from src.utils.uefi import * from src.utils.boot import * @@ -111,6 +111,44 @@ class OgLiveOperations: part_setup['filesystem'] = 'CACHE' part_setup['code'] = 'ca' + def _get_cache_contents(self): + cache_contents = [] + + if not mount_cache(): + return cache_contents + + img_dir = OG_CACHE_IMAGE_PATH + + if not os.path.isdir(img_dir): + return cache_contents + + for file_name in os.listdir(img_dir): + file_path = os.path.join(img_dir, file_name) + + if not os.path.isfile(file_path): + continue + if not file_name.endswith('.img'): + continue + + checksum_file_path = file_path + '.full.sum' + image_checksum = '' + + try: + with open(checksum_file_path, 'r') as f: + image_checksum = f.read() + except (FileNotFoundError, PermissionError, OSError) as e: + logging.info(f'Error reading file at {checksum_file_path}: {e}') + + if not image_checksum: + logging.info(f'Warning: empty checksum for image {file_name}') + + image_size = os.stat(file_path).st_size + cache_contents.append({ + 'name': file_name, + 'size': image_size, + 'checksum': image_checksum}) + return cache_contents + def _compute_md5(self, path, bs=2**20): m = hashlib.md5() with open(path, 'rb') as f: @@ -568,6 +606,8 @@ class OgLiveOperations: self._refresh_part_setup_cache(cxt, pa, part_setup, cache) json_body['partition_setup'].append(part_setup) + json_body['cache'] = self._get_cache_contents() + generate_menu(json_body['partition_setup']) generate_cache_txt() self._restartBrowser(self._url) -- cgit v1.2.3-18-g5258