ar | aa34220 | 2012-09-04 14:59:34 -0300 | [diff] [blame] | 1 | #!/system/xbin/bash |
| 2 | : ' |
| 3 | ============ Copyright (C) 2010 Jared Rummler (JRummy16) ============ |
| 4 | |
| 5 | This program is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | ===================================================================== |
| 19 | ' |
| 20 | |
| 21 | : ' ========================================================= |
| 22 | function name: fix_permissions |
| 23 | parameters: void |
| 24 | returns: void |
| 25 | description: |
| 26 | Sets permissions for Android data directories and apks |
| 27 | ============================================================= ' |
| 28 | function fix_permissions() |
| 29 | { |
| 30 | START=` busybox date +%s ` |
| 31 | PKGLINE=(` pm list packages -f | busybox cut -d ':' -f2 `); |
| 32 | MAX=${#PKGLINE[*]} |
| 33 | INCREMENT=5 |
| 34 | PROGRESS=0 |
| 35 | PROGRESS_BAR="" |
| 36 | |
| 37 | echo |
| 38 | echo "Fixing permissions..." |
| 39 | echo |
| 40 | |
| 41 | busybox mount -o remount,rw /system |
| 42 | |
| 43 | for pkgline in ${PKGLINE[*]}; do |
| 44 | |
| 45 | PKGNAME=` echo $pkgline | busybox cut -d '=' -f2 ` |
| 46 | CODEPATH=` echo $pkgline | busybox cut -d '=' -f1 ` |
| 47 | DATAPATH=/data/data/$PKGNAME |
| 48 | PKGUID=` busybox grep $CODEPATH /data/system/packages.xml | busybox sed 's%.*serId="\(.*\)".*%\1%' | busybox cut -d '"' -f1 ` |
| 49 | |
| 50 | let PROGRESS++ |
| 51 | PERCENT=$(( $PROGRESS * 100 / $MAX )) |
| 52 | if busybox [ $PERCENT -eq $INCREMENT ]; then |
| 53 | INCREMENT=$(( $INCREMENT + 5 )) |
| 54 | PROGRESS_BAR="${PROGRESS_BAR}=" |
| 55 | fi |
| 56 | |
| 57 | echo -ne " \r ${PROGRESS}/${MAX} ${PERCENT}% [ ${PROGRESS_BAR}> ]" |
| 58 | |
| 59 | if busybox [ -e $CODEPATH ]; then |
| 60 | |
| 61 | APPDIR=` busybox dirname $CODEPATH ` |
| 62 | |
| 63 | if busybox [ $APPDIR = /system/app ]; then |
| 64 | busybox chown 0 $CODEPATH |
| 65 | busybox chown :0 $CODEPATH |
| 66 | busybox chmod 644 $CODEPATH |
| 67 | elif busybox [ $APPDIR = /data/app ]; then |
| 68 | busybox chown 1000 $CODEPATH |
| 69 | busybox chown :1000 $CODEPATH |
| 70 | busybox chmod 644 $CODEPATH |
| 71 | elif busybox [ $APPDIR = /data/app-private ]; then |
| 72 | busybox chown 1000 $CODEPATH |
| 73 | busybox chown :$PKGUID $CODEPATH |
| 74 | busybox chmod 640 $CODEPATH |
| 75 | fi |
| 76 | |
| 77 | if busybox [ -d $DATAPATH ]; then |
| 78 | |
| 79 | busybox chmod 755 $DATAPATH |
| 80 | busybox chown $PKGUID $DATAPATH |
| 81 | busybox chown :$PKGUID $DATAPATH |
| 82 | |
| 83 | DIRS=` busybox find $DATAPATH -mindepth 1 -type d ` |
| 84 | |
| 85 | for file in $DIRS; do |
| 86 | |
| 87 | PERM=755 |
| 88 | NEWUID=$PKGUID |
| 89 | NEWGID=$PKGUID |
| 90 | FNAME=` busybox basename $file ` |
| 91 | |
| 92 | case $FNAME in |
| 93 | lib) |
| 94 | busybox chmod 755 $file |
| 95 | NEWUID=1000 |
| 96 | NEWGID=1000 |
| 97 | PERM=755 |
| 98 | ;; |
| 99 | shared_prefs) |
| 100 | busybox chmod 771 $file |
| 101 | PERM=660 |
| 102 | ;; |
| 103 | databases) |
| 104 | busybox chmod 771 $file |
| 105 | PERM=660 |
| 106 | ;; |
| 107 | cache) |
| 108 | busybox chmod 771 $file |
| 109 | PERM=600 |
| 110 | ;; |
| 111 | *) |
| 112 | busybox chmod 771 $file |
| 113 | PERM=771 |
| 114 | ;; |
| 115 | esac |
| 116 | |
| 117 | busybox chown $NEWUID $file |
| 118 | busybox chown :$NEWGID $file |
| 119 | |
| 120 | busybox find $file -type f -maxdepth 1 ! -perm $PERM -exec busybox chmod $PERM {} ';' |
| 121 | busybox find $file -type f -maxdepth 1 ! -user $NEWUID -exec busybox chown $NEWUID {} ';' |
| 122 | busybox find $file -type f -maxdepth 1 ! -group $NEWGID -exec busybox chown :$NEWGID {} ';' |
| 123 | |
| 124 | done |
| 125 | fi |
| 126 | fi |
| 127 | done |
| 128 | |
| 129 | busybox mount -o remount,ro /system |
| 130 | sync |
| 131 | |
| 132 | STOP=` busybox date +%s ` |
| 133 | RUNTIME=` runtime $START $STOP ` |
| 134 | echo |
| 135 | echo |
| 136 | echo "Fix permissions complete! Runtime: ${RUNTIME}" |
| 137 | echo |
| 138 | } |
| 139 | |
| 140 | : ' ========================================================= |
| 141 | function name: getFastReboot |
| 142 | parameters: void |
| 143 | returns: void |
| 144 | description: |
| 145 | Hot reboots the device |
| 146 | ============================================================= ' |
| 147 | function getFastReboot() |
| 148 | { |
| 149 | busybox killall system_server |
| 150 | } |
| 151 | |
| 152 | : ' ========================================================= |
| 153 | function name: getFastReboot |
| 154 | parameters: void |
| 155 | returns: rw|ro |
| 156 | description: |
| 157 | Gets mount |
| 158 | ' |
| 159 | function getMount() |
| 160 | { |
| 161 | mount | busybox grep /system | busybox awk '{print $4}' | busybox cut -d ',' -f1 |
| 162 | } |
| 163 | |
| 164 | : ' ========================================================= |
| 165 | function name: installApks |
| 166 | parameters: $1 - "-r" search all sub directories for apks, or... |
| 167 | path to directory |
| 168 | $2 - path to directory (if recursize) |
| 169 | returns: void |
| 170 | description: |
| 171 | Installs apks to device using package manager |
| 172 | ============================================================= ' |
| 173 | function installApks() |
| 174 | { |
| 175 | RECURSIZE=0 |
| 176 | case $1 in |
| 177 | -r) |
| 178 | RECURSIZE=1 |
| 179 | shift; |
| 180 | ;; |
| 181 | esac |
| 182 | |
| 183 | APKS_DIR=${1:-/mnt/sdcard/} |
| 184 | declare -a APKS |
| 185 | |
| 186 | if busybox [ -d $APKS_DIR ]; then |
| 187 | |
| 188 | if busybox [ $RECURSIZE -eq 1 ]; then |
| 189 | APKS=(` busybox find $APKS_DIR -type f -name *.apk -print `); |
| 190 | else |
| 191 | APKS=(` ls $APKS_DIR/*.apk `); |
| 192 | fi |
| 193 | |
| 194 | for apk in ${APKS[*]}; do |
| 195 | pm install -i com.google.android.feedback -r $apk |
| 196 | done |
| 197 | fi |
| 198 | } |
| 199 | |
| 200 | : ' ========================================================= |
| 201 | function name: isBusyboxInstalled |
| 202 | parameters: void |
| 203 | returns: 0: busybox is installed |
| 204 | 1: busybox is not installed |
| 205 | description: |
| 206 | Checks if busybox is installed correctly |
| 207 | ============================================================= ' |
| 208 | function isBusyboxInstalled() |
| 209 | { |
| 210 | if ! busybox > /dev/nul 2>&1; then |
| 211 | return 1 |
| 212 | fi |
| 213 | return 0 |
| 214 | } |
| 215 | |
| 216 | : ' ========================================================= |
| 217 | function name: isRoot |
| 218 | parameters: void |
| 219 | returns: 0: if user is root |
| 220 | 1: if user is not root |
| 221 | description: |
| 222 | Checks if user is running as root |
| 223 | ============================================================= ' |
| 224 | function isRoot() |
| 225 | { |
| 226 | if busybox [ `whoami` = root ]; then |
| 227 | return 0 |
| 228 | fi |
| 229 | return 1 |
| 230 | } |
| 231 | |
| 232 | : ' ========================================================= |
| 233 | function name: isProcessRunning |
| 234 | parameters: $1 - process name |
| 235 | returns: 0: Process is running |
| 236 | 1: Process is not running |
| 237 | description: |
| 238 | Checks if SD card is mounted |
| 239 | ============================================================= ' |
| 240 | function isProcessRunning() |
| 241 | { |
| 242 | if busybox [ $# -ne 1 ]; then |
| 243 | return 1 |
| 244 | fi |
| 245 | |
| 246 | PROCESS=$1 |
| 247 | |
| 248 | PROCESSES=(` ps | busybox awk '{print $9}' `); |
| 249 | for p in ${PROCESSES[*]}; do |
| 250 | if busybox [ "$p" = "$PROCESS" ]; then |
| 251 | return 0 |
| 252 | fi |
| 253 | done |
| 254 | return 1 |
| 255 | } |
| 256 | |
| 257 | : ' ========================================================= |
| 258 | function name: isSdPresent |
| 259 | parameters: void |
| 260 | returns: 0: SD card is present |
| 261 | 1: SD card is not present |
| 262 | description: |
| 263 | Checks if SD card is mounted |
| 264 | ============================================================= ' |
| 265 | function isSdPresent() |
| 266 | { |
| 267 | if busybox [ -z "$( busybox mount | busybox grep /sdcard )" ]; then |
| 268 | return 1 |
| 269 | fi |
| 270 | return 0 |
| 271 | } |
| 272 | |
| 273 | : ' ========================================================= |
| 274 | function name: runtime |
| 275 | parameters: $1 - Start time of task |
| 276 | $2 - End time of task |
| 277 | returns: task runtime |
| 278 | description: |
| 279 | Prints runtime of a task |
| 280 | ============================================================= ' |
| 281 | function runtime() |
| 282 | { |
| 283 | START=$1 |
| 284 | STOP=$2 |
| 285 | |
| 286 | RUNTIME=` busybox expr $STOP - $START` |
| 287 | HOURS=` busybox expr $RUNTIME / 3600` |
| 288 | REMAINDER=` busybox expr $RUNTIME % 3600` |
| 289 | MINS=` busybox expr $REMAINDER / 60` |
| 290 | SECS=` busybox expr $REMAINDER % 60` |
| 291 | busybox printf "%02d:%02d:%02d\n" "$HOURS" "$MINS" "$SECS" |
| 292 | } |
| 293 | |
| 294 | : ' ========================================================= |
| 295 | function name: setProp |
| 296 | parameters: $1 - Key |
| 297 | $2 - Value |
| 298 | $3 - File |
| 299 | returns: void |
| 300 | description: |
| 301 | Sets build properties |
| 302 | ============================================================= ' |
| 303 | function setProp() |
| 304 | { |
| 305 | if busybox [ $# -lt 2 ]; then |
| 306 | return |
| 307 | fi |
| 308 | |
| 309 | KEY=$1 |
| 310 | VALUE=$2 |
| 311 | PROP_FILE=${3:-/system/build.prop} |
| 312 | SEPERATOR="=" |
| 313 | LINE="" |
| 314 | |
| 315 | if busybox [ -f $PROP_FILE ]; then |
| 316 | LINE=` busybox grep $KEY $PROP_FILE ` |
| 317 | if busybox [ -n "${LINE}" ]; then |
| 318 | if busybox [ -n "$( echo $LINE | busybox grep ' = ' )" ]; then |
| 319 | SEPERATOR=" = " |
| 320 | fi |
| 321 | busybox sed -i "s|${KEY}${SEPERATOR}.*|${KEY}${SEPERATOR}${VALUE}|" $PROP_FILE |
| 322 | fi |
| 323 | else |
| 324 | echo "$PROP_FILE does not exist" |
| 325 | fi |
| 326 | setprop $KEY $VALUE |
| 327 | } |
| 328 | |
| 329 | : ' ========================================================= |
| 330 | function name: sysro |
| 331 | parameters: void |
| 332 | returns: void |
| 333 | description: |
| 334 | Mounts system read-only |
| 335 | ============================================================= ' |
| 336 | function sysro() |
| 337 | { |
| 338 | busybox mount -o remount,ro /system |
| 339 | } |
| 340 | |
| 341 | : ' ========================================================= |
| 342 | function name: sysrw |
| 343 | parameters: void |
| 344 | returns: void |
| 345 | description: |
| 346 | Mounts system rear/write |
| 347 | ============================================================= ' |
| 348 | function sysrw() |
| 349 | { |
| 350 | busybox mount -o remount,rw /system |
| 351 | } |
| 352 | |
| 353 | : ' ========================================================= |
| 354 | function name: zipalign_apks |
| 355 | parameters: void |
| 356 | returns: void |
| 357 | description: |
| 358 | Checks for unaligned apks and aligns them. |
| 359 | ============================================================= ' |
| 360 | function zipalign_apks() |
| 361 | { |
| 362 | if busybox [ -z "$( busybox which zipalign )" ]; then |
| 363 | echo "Error: zipalign binary missing." |
| 364 | return |
| 365 | fi |
| 366 | |
| 367 | START=` busybox date +%s ` |
| 368 | CODEPATH=(` pm list packages -f | busybox cut -d ':' -f2 | busybox cut -d '=' -f1 `); |
| 369 | MAX=${#CODEPATH[*]} |
| 370 | INCREMENT=5 |
| 371 | PROGRESS=0 |
| 372 | PROGRESS_BAR="" |
| 373 | |
| 374 | echo |
| 375 | echo "Zipaligning..." |
| 376 | echo |
| 377 | |
| 378 | busybox mount -o remount,rw /system |
| 379 | |
| 380 | for codepath in ${CODEPATH[*]}; do |
| 381 | |
| 382 | let PROGRESS++ |
| 383 | PERCENT=$(( $PROGRESS * 100 / $MAX)) |
| 384 | if busybox [ $PERCENT -eq $INCREMENT ]; then |
| 385 | INCREMENT=$(( $INCREMENT + 5 )) |
| 386 | PROGRESS_BAR="${PROGRESS_BAR}=" |
| 387 | fi |
| 388 | |
| 389 | echo -ne " \r ${PROGRESS}/${MAX} ${PERCENT}% [ ${PROGRESS_BAR}> ]" |
| 390 | |
| 391 | if busybox [ -e $codepath ]; then |
| 392 | zipalign -c 4 $codepath |
| 393 | ZIP_CHECK=$? |
| 394 | case $ZIP_CHECK in |
| 395 | 1) |
| 396 | if zipalign -f 4 $codepath /data/local/pkg.apk; then |
| 397 | busybox cp -f /data/local/pkg.apk $codepath |
| 398 | busybox rm -f /data/local/pkg.apk |
| 399 | fi |
| 400 | ;; |
| 401 | esac |
| 402 | fi |
| 403 | |
| 404 | done |
| 405 | |
| 406 | busybox mount -o remount,ro /system |
| 407 | sync |
| 408 | |
| 409 | STOP=` busybox date +%s ` |
| 410 | RUNTIME=` runtime $START $STOP ` |
| 411 | echo |
| 412 | echo |
| 413 | echo "Zipalign complete! Runtime: ${RUNTIME}" |
| 414 | echo |
| 415 | } |