backuptool: Add support for cross-version whitelists

Change-Id: I415eb4d64d693a7e150656802be7f7c4a0e5ab8b
diff --git a/prebuilt/common/bin/backuptool.sh b/prebuilt/common/bin/backuptool.sh
index 0ef562f..a45721e 100755
--- a/prebuilt/common/bin/backuptool.sh
+++ b/prebuilt/common/bin/backuptool.sh
@@ -24,8 +24,9 @@
 check_prereq() {
 if ( ! grep -q "^ro.cm.version=$V.*" /system/build.prop ); then
   echo "Not backing up files from incompatible version: $V"
-  exit 127
+  return 0
 fi
+return 1
 }
 
 check_blacklist() {
@@ -39,6 +40,24 @@
   fi
 }
 
+check_whitelist() {
+  found=0
+  if [ -f /system/addon.d/whitelist ];then
+      ## forcefully keep any version-independent stuff
+      cd /$1/addon.d/
+      for f in *sh; do
+          s=$(md5sum $f | awk {'print $1'})
+          grep -q $s /system/addon.d/whitelist
+          if [ $? -eq 0 ]; then
+              found=1
+          else
+              rm -f $f
+          fi
+      done
+  fi
+  return $found
+}
+
 # Execute /system/addon.d/*.sh scripts with $1 parameter
 run_stage() {
 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
@@ -49,7 +68,11 @@
 case "$1" in
   backup)
     mkdir -p $C
-    check_prereq
+    if check_prereq; then
+        if check_whitelist system; then
+            exit 127
+        fi
+    fi
     check_blacklist system
     preserve_addon_d
     run_stage pre-backup
@@ -57,7 +80,11 @@
     run_stage post-backup
   ;;
   restore)
-    check_prereq
+    if check_prereq; then
+        if check_whitelist tmp; then
+            exit 127
+        fi
+    fi
     check_blacklist tmp
     run_stage pre-restore
     run_stage restore
diff --git a/prebuilt/common/bin/whitelist b/prebuilt/common/bin/whitelist
new file mode 100644
index 0000000..ca3f017
--- /dev/null
+++ b/prebuilt/common/bin/whitelist
@@ -0,0 +1 @@
+b0a27bcb5c7504a81e1450a8313e37cb