vendor: add custom backuptools and postinstall script for A/B OTAs

* A/B OTA devices wont run backuptools in recovery (because they don't
  go in to recovery to do an OTA). In these cases let's use a modified
  version to backup/restore from within android upon postinstall.

* Add backuptool_postinstall.sh which will be run prior to the normal
  postinstall script in order to backup/restore via addon.d scripts.

* This needs to be done in such a manner because we need /postinstall
  mounted rw instead of the ro with context= options which are used for
  the normal postinstall (dexopt) script.

Change-Id: I51511870634dd1ec5388adafddb446f95cc5a950
diff --git a/prebuilt/common/bin/backuptool_ab.functions b/prebuilt/common/bin/backuptool_ab.functions
new file mode 100644
index 0000000..5a9ce56
--- /dev/null
+++ b/prebuilt/common/bin/backuptool_ab.functions
@@ -0,0 +1,51 @@
+#!/system/bin/sh
+#
+# Functions for backuptool_ab.sh
+#
+
+export S=/system
+export C=/postinstall/tmp/backupdir
+export V=8.1
+export backuptool_ab=true
+
+copy_file() {
+  # toybox's cp doesn't do directories correctly for whatever reason
+  mkdir -p `dirname $2`
+
+  cp -dp "$1" "$2"
+  # symlinks don't have a context
+  if [ ! -L "$1" ]; then
+    # it is assumed that every label starts with 'u:object_r' and has no white-spaces
+    local context=`ls -Z "$1" | grep -o 'u:object_r:[^ ]*' | head -1`
+    chcon "$context" "$2"
+  fi
+}
+
+backup_file() {
+  if [ -e "$1" -o -L "$1" ]; then
+    local FILE=`basename "$1"`
+    local DIR=`dirname "$1"`
+    # dont backup any apps that have odex files, they are useless
+    if ( echo "$FILE" | grep -q "\.apk$" ) && [ -e `echo "$1" | sed -e 's/\.apk$/\.odex/'` ]; then
+      echo "Skipping odexed apk $1";
+    else
+      mkdir -p "$C/$DIR"
+      copy_file "$1" "$C/$DIR/$FILE"
+    fi
+  fi
+}
+
+restore_file() {
+  local FILE=`basename "$1"`
+  local DIR=`dirname "$1"`
+  if [ -e "$C/$DIR/$FILE" -o -L "$C/$DIR/$FILE" ]; then
+    if [ ! -d "/postinstall/$DIR" ]; then
+      mkdir -p "/postinstall/$DIR";
+    fi
+    copy_file "$C/$DIR/$FILE" "/postinstall/$1";
+    if [ -n "$2" ]; then
+      echo "Deleting obsolete file $2"
+      rm "$2";
+    fi
+  fi
+}