Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 22680915 authored by Kelvin Zhang's avatar Kelvin Zhang
Browse files

Unsparse images before generating OTA

Test: th
Bug: 283172692
Change-Id: Ie6d3dc704fd9a8c107e2888222e4c2bf804dad3e
parent d104c386
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -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
+19 −3
Original line number Diff line number Diff line
@@ -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)