The Android Open Source Project | 23580ca | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2007 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | PROGNAME=`basename "$0"` |
| 19 | |
| 20 | INSTALL_SCRIPT_NAME=META-INF/com/android/update-script |
| 21 | |
| 22 | function cleantmp |
| 23 | { |
| 24 | if [ ! -z "$TMPDIR" ] |
| 25 | then |
| 26 | rm -rf "$TMPDIR" |
| 27 | TMPDIR= |
| 28 | fi |
| 29 | } |
| 30 | |
| 31 | function println |
| 32 | { |
| 33 | if [ $# -gt 0 ] |
| 34 | then |
| 35 | echo "$PROGNAME: $@" |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | function fail |
| 40 | { |
| 41 | println "$@" |
| 42 | cleantmp |
| 43 | exit 1 |
| 44 | } |
| 45 | |
| 46 | function usage |
| 47 | { |
| 48 | println "$@" |
| 49 | echo "Usage: $PROGNAME <command> [command-options] <ota-file>" |
| 50 | echo " Where <command> is one of:" |
| 51 | echo " --dump" |
| 52 | echo " Dump a description of the ota file" |
| 53 | echo " --dump-script" |
| 54 | echo " Dumps the install script to standard out" |
| 55 | echo " --append-script <file> -o|--output <outfile>" |
| 56 | echo " Append the contents of <file> to the install script" |
| 57 | echo " --replace-script <file> -o|--output <outfile>" |
| 58 | echo " Replace the install script with the contents of <file>" |
| 59 | fail |
| 60 | } |
| 61 | |
| 62 | if [ $# -lt 2 ] |
| 63 | then |
| 64 | usage |
| 65 | fi |
| 66 | CMD="$1" |
| 67 | shift |
| 68 | if [ "$CMD" = --dump ] |
| 69 | then |
| 70 | CMD_DUMP=1 |
| 71 | UNPACK_FILE=1 |
| 72 | elif [ "$CMD" = --dump-script ] |
| 73 | then |
| 74 | CMD_DUMP_SCRIPT=1 |
| 75 | elif [ "$CMD" = --append-script ] |
| 76 | then |
| 77 | CMD_APPEND_SCRIPT=1 |
| 78 | SCRIPT_FILE=$1 |
| 79 | shift |
| 80 | NEEDS_SCRIPT_FILE=1 |
| 81 | NEEDS_OUTPUT=1 |
| 82 | elif [ "$CMD" = --replace-script ] |
| 83 | then |
| 84 | CMD_REPLACE_SCRIPT=1 |
| 85 | SCRIPT_FILE=$1 |
| 86 | shift |
| 87 | NEEDS_SCRIPT_FILE=1 |
| 88 | NEEDS_OUTPUT=1 |
| 89 | else |
| 90 | usage "Unknown command $CMD" |
| 91 | fi |
| 92 | |
| 93 | if [ ! -z "$NEED_SCRIPT_FILE" ] |
| 94 | then |
| 95 | if [ -z "$SCRIPT_FILE" -o ! -f "$SCRIPT_FILE" ] |
| 96 | then |
| 97 | usage "$CMD requires a valid script file" |
| 98 | fi |
| 99 | fi |
| 100 | |
| 101 | if [ ! -z "$NEEDS_OUTPUT" ] |
| 102 | then |
| 103 | if [ "x$1" != "x-o" -a "x$1" != "x--output" ] |
| 104 | then |
| 105 | usage "$CMD requires \"-o <file>\" or \"--output <file>\"" |
| 106 | fi |
| 107 | shift |
| 108 | |
| 109 | OUTFILE="$1" |
| 110 | shift |
| 111 | if [ -z "$OUTFILE" ] |
| 112 | then |
| 113 | usage "$CMD requires \"-o <file>\" or \"--output <file>\"" |
| 114 | fi |
| 115 | if [ -d "$OUTFILE" ] |
| 116 | then |
| 117 | fail "Output file \"$OUTFILE\" is a directory" |
| 118 | fi |
| 119 | fi |
| 120 | |
| 121 | FILE="$1" |
| 122 | if [ ! -f "$FILE" ] |
| 123 | then |
| 124 | fail "$FILE doesn't exist or isn't a file" |
| 125 | fi |
| 126 | if [ ! -z "$OUTFILE" -a "$FILE" -ef "$OUTFILE" ] |
| 127 | then |
| 128 | fail "Refusing to use the input file as the output file" |
| 129 | fi |
| 130 | |
| 131 | if [ $CMD_DUMP_SCRIPT ] |
| 132 | then |
| 133 | unzip -p "$FILE" "$INSTALL_SCRIPT_NAME" |
| 134 | exit 0 |
| 135 | fi |
| 136 | |
| 137 | # Create a temporary directory for scratch files. |
| 138 | # |
| 139 | TMPDIR=`mktemp -d /tmp/$PROGNAME.XXXXXX` |
| 140 | if [ $? -ne 0 ] |
| 141 | then |
| 142 | TMPDIR= |
| 143 | fail "Can't create temporary directory" |
| 144 | fi |
| 145 | |
| 146 | |
| 147 | if [ $UNPACK_FILE ] |
| 148 | then |
| 149 | ROOTDIR="$TMPDIR/root" |
| 150 | mkdir -p "$ROOTDIR" |
| 151 | |
| 152 | println "Unpacking `basename $FILE`..." |
| 153 | |
| 154 | unzip -q -d "$ROOTDIR" "$FILE" |
| 155 | if [ $? -ne 0 ] |
| 156 | then |
| 157 | fail "Couldn't unpack $FILE" |
| 158 | fi |
| 159 | fi |
| 160 | |
| 161 | |
| 162 | if [ $CMD_DUMP ] |
| 163 | then |
| 164 | function dumpfile |
| 165 | { |
| 166 | echo "BEGIN `basename $1`" |
| 167 | cat "$1" | sed -e 's/^/ /' |
| 168 | echo "END `basename $1`" |
| 169 | } |
| 170 | |
| 171 | echo Contents of root: |
| 172 | ls -1 "$ROOTDIR" | sed -e 's/^/ /' |
| 173 | echo |
| 174 | echo Contents of META-INF: |
| 175 | (cd "$ROOTDIR" && find META-INF -type f) | sed -e 's/^/ /' |
| 176 | |
| 177 | echo |
| 178 | dumpfile "$ROOTDIR/META-INF/MANIFEST.MF" |
| 179 | echo |
| 180 | dumpfile "$ROOTDIR/$INSTALL_SCRIPT_NAME" |
| 181 | echo |
| 182 | dumpfile "$ROOTDIR/android-product.txt" |
| 183 | echo |
| 184 | dumpfile "$ROOTDIR/system/build.prop" |
| 185 | fi |
| 186 | |
| 187 | if [ $CMD_APPEND_SCRIPT ] |
| 188 | then |
| 189 | TMPSCRIPT="$TMPDIR/script" |
| 190 | NEWSCRIPT="$TMPDIR/$INSTALL_SCRIPT_NAME" |
| 191 | unzip -p "$FILE" "$INSTALL_SCRIPT_NAME" > "$TMPSCRIPT" |
| 192 | if [ $? -ne 0 ] |
| 193 | then |
| 194 | fail "Couldn't extract $INSTALL_SCRIPT_NAME from $FILE" |
| 195 | fi |
| 196 | mkdir -p `dirname "$NEWSCRIPT"` |
| 197 | cat "$TMPSCRIPT" "$SCRIPT_FILE" > "$NEWSCRIPT" |
| 198 | |
| 199 | OVERWRITE_SCRIPT=1 |
| 200 | fi |
| 201 | |
| 202 | if [ $CMD_REPLACE_SCRIPT ] |
| 203 | then |
| 204 | NEWSCRIPT="$TMPDIR/$INSTALL_SCRIPT_NAME" |
| 205 | mkdir -p `dirname "$NEWSCRIPT"` |
| 206 | cp "$SCRIPT_FILE" "$NEWSCRIPT" |
| 207 | |
| 208 | OVERWRITE_SCRIPT=1 |
| 209 | fi |
| 210 | |
| 211 | if [ $OVERWRITE_SCRIPT ] |
| 212 | then |
| 213 | cp "$FILE" "$TMPDIR/outfile.zip" |
| 214 | (cd "$TMPDIR" && zip -qu outfile.zip "$INSTALL_SCRIPT_NAME") |
| 215 | if [ $? -ne 0 ] |
| 216 | then |
| 217 | fail "Couldn't add new $INSTALL_SCRIPT_NAME to output file" |
| 218 | fi |
| 219 | |
| 220 | rm -f "$OUTFILE" |
| 221 | mkdir -p `dirname "$OUTFILE"` |
| 222 | mv "$TMPDIR/outfile.zip" "$OUTFILE" |
| 223 | fi |
| 224 | |
| 225 | cleantmp |