Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <unistd.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include "edify/expr.h" |
| 22 | #include "updater.h" |
| 23 | #include "install.h" |
| 24 | #include "minzip/Zip.h" |
| 25 | |
| 26 | // Where in the package we expect to find the edify script to execute. |
| 27 | // (Note it's "updateR-script", not the older "update-script".) |
| 28 | #define SCRIPT_NAME "META-INF/com/google/android/updater-script" |
| 29 | |
| 30 | int main(int argc, char** argv) { |
| 31 | if (argc != 4) { |
| 32 | fprintf(stderr, "unexpected number of arguments (%d)\n", argc); |
| 33 | return 1; |
| 34 | } |
| 35 | |
| 36 | char* version = argv[1]; |
| 37 | if (version[0] != '1' || version[1] != '\0') { |
| 38 | fprintf(stderr, "wrong updater binary API; expected 1, got %s\n", |
| 39 | version); |
| 40 | return 2; |
| 41 | } |
| 42 | |
| 43 | // Set up the pipe for sending commands back to the parent process. |
| 44 | |
| 45 | int fd = atoi(argv[2]); |
| 46 | FILE* cmd_pipe = fdopen(fd, "wb"); |
| 47 | setlinebuf(cmd_pipe); |
| 48 | |
| 49 | // Extract the script from the package. |
| 50 | |
| 51 | char* package_data = argv[3]; |
| 52 | ZipArchive za; |
| 53 | int err; |
| 54 | err = mzOpenZipArchive(package_data, &za); |
| 55 | if (err != 0) { |
| 56 | fprintf(stderr, "failed to open package %s: %s\n", |
| 57 | package_data, strerror(err)); |
| 58 | return 3; |
| 59 | } |
| 60 | |
| 61 | const ZipEntry* script_entry = mzFindZipEntry(&za, SCRIPT_NAME); |
| 62 | if (script_entry == NULL) { |
| 63 | fprintf(stderr, "failed to find %s in %s\n", SCRIPT_NAME, package_data); |
| 64 | return 4; |
| 65 | } |
| 66 | |
| 67 | char* script = malloc(script_entry->uncompLen+1); |
| 68 | if (!mzReadZipEntry(&za, script_entry, script, script_entry->uncompLen)) { |
| 69 | fprintf(stderr, "failed to read script from package\n"); |
| 70 | return 5; |
| 71 | } |
| 72 | script[script_entry->uncompLen] = '\0'; |
| 73 | |
| 74 | // Configure edify's functions. |
| 75 | |
| 76 | RegisterBuiltins(); |
| 77 | RegisterInstallFunctions(); |
| 78 | FinishRegistration(); |
| 79 | |
| 80 | // Parse the script. |
| 81 | |
| 82 | Expr* root; |
| 83 | yy_scan_string(script); |
| 84 | int error = yyparse(&root); |
| 85 | if (error != 0) { |
| 86 | fprintf(stderr, "%d parse errors\n", error); |
| 87 | return 6; |
| 88 | } |
| 89 | |
| 90 | // Evaluate the parsed script. |
| 91 | |
| 92 | UpdaterInfo updater_info; |
| 93 | updater_info.cmd_pipe = cmd_pipe; |
| 94 | updater_info.package_zip = &za; |
| 95 | |
| 96 | char* result = Evaluate(&updater_info, root); |
| 97 | if (result == NULL) { |
| 98 | const char* errmsg = GetError(); |
| 99 | fprintf(stderr, "script aborted with error: %s\n", |
| 100 | errmsg == NULL ? "(none)" : errmsg); |
| 101 | ClearError(); |
| 102 | return 7; |
| 103 | } else { |
| 104 | fprintf(stderr, "script result was [%s]\n", result); |
| 105 | free(result); |
| 106 | } |
| 107 | |
| 108 | mzCloseZipArchive(&za); |
| 109 | |
| 110 | return 0; |
| 111 | } |