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

Commit 6b2bb3d9 authored by Doug Zongker's avatar Doug Zongker
Browse files

better patching for zip files

Adds a zip mode ("-z") to imgdiff to construct efficient patches for
zip files (including jars and apks).  We identify the regions within
the zip file containing deflated data, and when a corresponding file
can be found in the source zip, a patch is generated for the
uncompressed version of the data.

The GZIP chunk type is replaced with a DEFLATE chunk type that handles
a raw deflated data stream.  This new DEFLATE chunk can be used for
both gzipped pieces (as found within boot and recovery images) and zip
files (apks, etc.)  The gzip header and footer are handled by NORMAL
chunks on either side of the main DEFLATE chunks.  (Typically these
tiny NORMAL chunks will get merged with adjacent chunks, so the number
of output chunks is unaffected.)

Add a test script that tests the generate-apply cycle on all the zips
and images within a pair of full OTA packages.
parent 3b72436d
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ ifneq ($(TARGET_SIMULATOR),true)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := applypatch.c bsdiff.c freecache.c imgpatch.c
LOCAL_SRC_FILES := applypatch.c bsdiff.c freecache.c imgpatch.c utils.c
LOCAL_MODULE := libapplypatch
LOCAL_MODULE_TAGS := eng
LOCAL_C_INCLUDES += external/bzip2 external/zlib bootable/recovery
@@ -39,7 +39,7 @@ include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := imgdiff.c
LOCAL_SRC_FILES := imgdiff.c utils.c
LOCAL_MODULE := imgdiff
LOCAL_FORCE_STATIC_EXECUTABLE := true
LOCAL_MODULE_TAGS := eng
+3 −2
Original line number Diff line number Diff line
@@ -765,7 +765,8 @@ int applypatch(int argc, char** argv) {
      return result;
    }
  } else if (header_bytes_read >= 8 &&
             memcmp(header, "IMGDIFF1", 8) == 0) {
             memcmp(header, "IMGDIFF", 7) == 0 &&
             (header[7] == '1' || header[7] == '2')) {
    int result = ApplyImagePatch(source_to_use->data, source_to_use->size,
                                 patch_filename, output, &ctx);
    if (result != 0) {
@@ -773,7 +774,7 @@ int applypatch(int argc, char** argv) {
      return result;
    }
  } else {
    fprintf(stderr, "Unknown patch file format");
    fprintf(stderr, "Unknown patch file format\n");
    return 1;
  }

+506 −152

File changed.

Preview size limit exceeded, changes collapsed.

+4 −2
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

// Image patch chunk types
#define CHUNK_NORMAL   0
#define CHUNK_GZIP   1
#define CHUNK_GZIP     1   // version 1 only
#define CHUNK_DEFLATE  2   // version 2 only
#define CHUNK_RAW      3   // version 2 only

// The gzip header size is actually variable, but we currently don't
// support gzipped data with any of the optional fields, so for now it
+118 −0
Original line number Diff line number Diff line
#!/bin/bash
#
# A script for testing imgdiff/applypatch.  It takes two full OTA
# packages as arguments.  It generates (on the host) patches for all
# the zip/jar/apk files they have in common, as well as boot and
# recovery images.  It then applies the patches on the device (or
# emulator) and checks that the resulting file is correct.

EMULATOR_PORT=5580

# set to 0 to use a device instead
USE_EMULATOR=0

# where on the device to do all the patching.
WORK_DIR=/data/local/tmp

START_OTA_PACKAGE=$1
END_OTA_PACKAGE=$2

# ------------------------

tmpdir=$(mktemp -d)

if [ "$USE_EMULATOR" == 1 ]; then
  emulator -wipe-data -noaudio -no-window -port $EMULATOR_PORT &
  pid_emulator=$!
  ADB="adb -s emulator-$EMULATOR_PORT "
else
  ADB="adb -d "
fi

echo "waiting to connect to device"
$ADB wait-for-device

# run a command on the device; exit with the exit status of the device
# command.
run_command() {
  $ADB shell "$@" \; echo \$? | awk '{if (b) {print a}; a=$0; b=1} END {exit a}'
}

testname() {
  echo
  echo "$1"...
  testname="$1"
}

fail() {
  echo
  echo FAIL: $testname
  echo
  [ "$open_pid" == "" ] || kill $open_pid
  [ "$pid_emulator" == "" ] || kill $pid_emulator
  exit 1
}

sha1() {
  sha1sum $1 | awk '{print $1}'
}

size() {
  stat -c %s $1 | tr -d '\n'
}

cleanup() {
  # not necessary if we're about to kill the emulator, but nice for
  # running on real devices or already-running emulators.
  testname "removing test files"
  run_command rm $WORK_DIR/applypatch
  run_command rm $WORK_DIR/source
  run_command rm $WORK_DIR/target
  run_command rm $WORK_DIR/patch

  [ "$pid_emulator" == "" ] || kill $pid_emulator

  rm -rf $tmpdir
}

$ADB push $ANDROID_PRODUCT_OUT/system/bin/applypatch $WORK_DIR/applypatch

patch_and_apply() {
  local fn=$1
  shift

  unzip -p $START_OTA_PACKAGE $fn > $tmpdir/source
  unzip -p $END_OTA_PACKAGE $fn > $tmpdir/target
  imgdiff "$@" $tmpdir/source $tmpdir/target $tmpdir/patch
  bsdiff $tmpdir/source $tmpdir/target $tmpdir/patch.bs
  echo "patch for $fn is $(size $tmpdir/patch) [of $(size $tmpdir/target)] ($(size $tmpdir/patch.bs) with bsdiff)"
  echo "$fn $(size $tmpdir/patch) of $(size $tmpdir/target) bsdiff $(size $tmpdir/patch.bs)" >> /tmp/stats.txt
  $ADB push $tmpdir/source $WORK_DIR/source || fail "source push failed"
  run_command rm /data/local/tmp/target
  $ADB push $tmpdir/patch $WORK_DIR/patch || fail "patch push failed"
  run_command /data/local/tmp/applypatch /data/local/tmp/source \
    /data/local/tmp/target $(sha1 $tmpdir/target) $(size $tmpdir/target) \
    $(sha1 $tmpdir/source):/data/local/tmp/patch \
    || fail "applypatch of $fn failed"
  $ADB pull /data/local/tmp/target $tmpdir/result
  diff -q $tmpdir/target $tmpdir/result || fail "patch output not correct!"
}

# --------------- basic execution ----------------------

for i in $((zipinfo -1 $START_OTA_PACKAGE; zipinfo -1 $END_OTA_PACKAGE) | \
           sort | uniq -d | egrep -e '[.](apk|jar|zip)$'); do
  patch_and_apply $i -z
done
patch_and_apply boot.img
patch_and_apply system/recovery.img


# --------------- cleanup ----------------------

cleanup

echo
echo PASS
echo
Loading