Dan Pasanen | 894b352 | 2017-01-16 19:26:50 -0600 | [diff] [blame^] | 1 | #!/system/bin/sh |
| 2 | # |
| 3 | # Backup and restore addon /system files |
| 4 | # |
| 5 | |
| 6 | export S=/system |
| 7 | export C=/postinstall/tmp/backupdir |
| 8 | export V=8.1 |
| 9 | |
| 10 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
| 11 | mkdir -p /postinstall/tmp/ |
| 12 | cp -f /postinstall/system/bin/backuptool_ab.functions /postinstall/tmp/backuptool.functions |
| 13 | |
| 14 | # Preserve /system/addon.d in /tmp/addon.d |
| 15 | preserve_addon_d() { |
| 16 | if [ -d /system/addon.d/ ]; then |
| 17 | mkdir -p /postinstall/tmp/addon.d/ |
| 18 | cp -a /system/addon.d/* /postinstall/tmp/addon.d/ |
| 19 | chmod 755 /postinstall/tmp/addon.d/*.sh |
| 20 | fi |
| 21 | } |
| 22 | |
| 23 | # Restore /postinstall/system/addon.d from /postinstall/tmp/addon.d |
| 24 | restore_addon_d() { |
| 25 | if [ -d /postinstall/tmp/addon.d/ ]; then |
| 26 | mkdir -p /postinstall/system/addon.d/ |
| 27 | cp -a /postinstall/tmp/addon.d/* /postinstall/system/addon.d/ |
| 28 | rm -rf /postinstall/tmp/addon.d/ |
| 29 | fi |
| 30 | } |
| 31 | |
| 32 | # Proceed only if /system is the expected major and minor version |
| 33 | check_prereq() { |
| 34 | # If there is no build.prop file the partition is probably empty. |
| 35 | if [ ! -r /system/build.prop ]; then |
| 36 | return 0 |
| 37 | fi |
| 38 | |
| 39 | grep -q "^ro.omni.version=$V.*" /system/etc/prop.default /system/build.prop && return 1 |
| 40 | |
| 41 | echo "Not backing up files from incompatible version: $V" |
| 42 | return 0 |
| 43 | } |
| 44 | |
| 45 | check_blacklist() { |
| 46 | if [ -f /system/addon.d/blacklist -a -d /$1/addon.d/ ]; then |
| 47 | ## Discard any known bad backup scripts |
| 48 | cd /$1/addon.d/ |
| 49 | for f in *sh; do |
| 50 | [ -f $f ] || continue |
| 51 | s=$(md5sum $f | cut -c-32) |
| 52 | grep -q $s /system/addon.d/blacklist && rm -f $f |
| 53 | done |
| 54 | fi |
| 55 | } |
| 56 | |
| 57 | check_whitelist() { |
| 58 | found=0 |
| 59 | if [ -f /system/addon.d/whitelist ];then |
| 60 | ## forcefully keep any version-independent stuff |
| 61 | cd /$1/addon.d/ |
| 62 | for f in *sh; do |
| 63 | s=$(md5sum $f | cut -c-32) |
| 64 | grep -q $s /system/addon.d/whitelist |
| 65 | if [ $? -eq 0 ]; then |
| 66 | found=1 |
| 67 | else |
| 68 | rm -f $f |
| 69 | fi |
| 70 | done |
| 71 | fi |
| 72 | return $found |
| 73 | } |
| 74 | |
| 75 | # Execute /system/addon.d/*.sh scripts with $1 parameter |
| 76 | run_stage() { |
| 77 | if [ -d /postinstall/tmp/addon.d/ ]; then |
| 78 | for script in $(find /postinstall/tmp/addon.d/ -name '*.sh' |sort -n); do |
| 79 | # we have no /sbin/sh in android, only recovery |
| 80 | # use /system/bin/sh here instead |
| 81 | sed -i '0,/#!\/sbin\/sh/{s|#!/sbin/sh|#!/system/bin/sh|}' $script |
| 82 | # we can't count on /tmp existing on an A/B device, so utilize /postinstall/tmp |
| 83 | # as a pseudo-/tmp dir |
| 84 | sed -i 's|. /tmp/backuptool.functions|. /postinstall/tmp/backuptool.functions|g' $script |
| 85 | $script $1 |
| 86 | done |
| 87 | fi |
| 88 | } |
| 89 | |
| 90 | case "$1" in |
| 91 | backup) |
| 92 | mkdir -p $C |
| 93 | if check_prereq; then |
| 94 | if check_whitelist postinstall/system; then |
| 95 | exit 127 |
| 96 | fi |
| 97 | fi |
| 98 | check_blacklist postinstall/system |
| 99 | preserve_addon_d |
| 100 | run_stage pre-backup |
| 101 | run_stage backup |
| 102 | run_stage post-backup |
| 103 | ;; |
| 104 | restore) |
| 105 | if check_prereq; then |
| 106 | if check_whitelist postinstall/tmp; then |
| 107 | exit 127 |
| 108 | fi |
| 109 | fi |
| 110 | check_blacklist postinstall/tmp |
| 111 | run_stage pre-restore |
| 112 | run_stage restore |
| 113 | run_stage post-restore |
| 114 | restore_addon_d |
| 115 | rm -rf $C |
| 116 | rm -rf /postinstall/tmp |
| 117 | sync |
| 118 | ;; |
| 119 | *) |
| 120 | echo "Usage: $0 {backup|restore}" |
| 121 | exit 1 |
| 122 | esac |
| 123 | |
| 124 | exit 0 |