From 2268091521ce8581f85c7e1be1063560b4ff843a Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Fri, 19 May 2023 13:12:59 -0700 Subject: [PATCH] Unsparse images before generating OTA Test: th Bug: 283172692 Change-Id: Ie6d3dc704fd9a8c107e2888222e4c2bf804dad3e --- tools/releasetools/common.py | 13 +++++++++++++ tools/releasetools/ota_utils.py | 22 +++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py index 06de622ae9..7adc9fac99 100644 --- a/tools/releasetools/common.py +++ b/tools/releasetools/common.py @@ -2782,6 +2782,8 @@ def MakeTempDir(prefix='tmp', suffix=''): def Cleanup(): for i in OPTIONS.tempfiles: + if not os.path.exists(i): + continue if os.path.isdir(i): shutil.rmtree(i, ignore_errors=True) else: @@ -4117,6 +4119,17 @@ def IsSparseImage(filepath): return fp.read(4) == b'\x3A\xFF\x26\xED' +def UnsparseImage(filepath, target_path=None): + if not IsSparseImage(filepath): + return + if target_path is None: + tmp_img = MakeTempFile(suffix=".img") + RunAndCheckOutput(["simg2img", filepath, tmp_img]) + os.rename(tmp_img, filepath) + else: + RunAndCheckOutput(["simg2img", filepath, target_path]) + + def ParseUpdateEngineConfig(path: str): """Parse the update_engine config stored in file `path` Args diff --git a/tools/releasetools/ota_utils.py b/tools/releasetools/ota_utils.py index 9067e78315..fa9516edcb 100644 --- a/tools/releasetools/ota_utils.py +++ b/tools/releasetools/ota_utils.py @@ -48,6 +48,7 @@ METADATA_PROTO_NAME = 'META-INF/com/android/metadata.pb' UNZIP_PATTERN = ['IMAGES/*', 'META/*', 'OTA/*', 'RADIO/*', '*/build.prop', '*/default.prop', '*/build.default', "*/etc/vintf/*"] SECURITY_PATCH_LEVEL_PROP_NAME = "ro.build.version.security_patch" +TARGET_FILES_IMAGES_SUBDIR = ["IMAGES", "PREBUILT_IMAGES", "RADIO"] def FinalizeMetadata(metadata, input_file, output_file, needed_property_files=None, package_key=None, pw=None): @@ -727,6 +728,15 @@ def ExtractTargetFiles(path: str): return path extracted_dir = common.MakeTempDir("target_files") common.UnzipToDir(path, extracted_dir, UNZIP_PATTERN + [""]) + for subdir in TARGET_FILES_IMAGES_SUBDIR: + image_dir = os.path.join(extracted_dir, subdir) + if not os.path.exists(image_dir): + continue + for filename in os.listdir(image_dir): + if not filename.endswith(".img"): + continue + common.UnsparseImage(os.path.join(image_dir, filename)) + return extracted_dir @@ -1047,12 +1057,18 @@ def Fnmatch(filename, pattersn): def CopyTargetFilesDir(input_dir): output_dir = common.MakeTempDir("target_files") - IMAGES_DIR = ["IMAGES", "PREBUILT_IMAGES", "RADIO"] - for subdir in IMAGES_DIR: + + def SymlinkIfNotSparse(src, dst): + if common.IsSparseImage(src): + return common.UnsparseImage(src, dst) + else: + return os.link(src, dst) + + for subdir in TARGET_FILES_IMAGES_SUBDIR: if not os.path.exists(os.path.join(input_dir, subdir)): continue shutil.copytree(os.path.join(input_dir, subdir), os.path.join( - output_dir, subdir), dirs_exist_ok=True, copy_function=os.link) + output_dir, subdir), dirs_exist_ok=True, copy_function=SymlinkIfNotSparse) shutil.copytree(os.path.join(input_dir, "META"), os.path.join( output_dir, "META"), dirs_exist_ok=True)