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]; |
Doug Zongker | fb2e3af | 2009-06-17 17:29:40 -0700 | [diff] [blame] | 37 | if ((version[0] != '1' && version[0] != '2') || version[1] != '\0') { |
| 38 | // We support version "1" or "2". |
| 39 | fprintf(stderr, "wrong updater binary API; expected 1 or 2, got %s\n", |
| 40 | argv[1]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 41 | return 2; |
| 42 | } |
| 43 | |
| 44 | // Set up the pipe for sending commands back to the parent process. |
| 45 | |
| 46 | int fd = atoi(argv[2]); |
| 47 | FILE* cmd_pipe = fdopen(fd, "wb"); |
| 48 | setlinebuf(cmd_pipe); |
| 49 | |
| 50 | // Extract the script from the package. |
| 51 | |
| 52 | char* package_data = argv[3]; |
| 53 | ZipArchive za; |
| 54 | int err; |
| 55 | err = mzOpenZipArchive(package_data, &za); |
| 56 | if (err != 0) { |
| 57 | fprintf(stderr, "failed to open package %s: %s\n", |
| 58 | package_data, strerror(err)); |
| 59 | return 3; |
| 60 | } |
| 61 | |
| 62 | const ZipEntry* script_entry = mzFindZipEntry(&za, SCRIPT_NAME); |
| 63 | if (script_entry == NULL) { |
| 64 | fprintf(stderr, "failed to find %s in %s\n", SCRIPT_NAME, package_data); |
| 65 | return 4; |
| 66 | } |
| 67 | |
| 68 | char* script = malloc(script_entry->uncompLen+1); |
| 69 | if (!mzReadZipEntry(&za, script_entry, script, script_entry->uncompLen)) { |
| 70 | fprintf(stderr, "failed to read script from package\n"); |
| 71 | return 5; |
| 72 | } |
| 73 | script[script_entry->uncompLen] = '\0'; |
| 74 | |
| 75 | // Configure edify's functions. |
| 76 | |
| 77 | RegisterBuiltins(); |
| 78 | RegisterInstallFunctions(); |
| 79 | FinishRegistration(); |
| 80 | |
| 81 | // Parse the script. |
| 82 | |
| 83 | Expr* root; |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 84 | int error_count = 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 85 | yy_scan_string(script); |
Doug Zongker | 8edb00c | 2009-06-11 17:21:44 -0700 | [diff] [blame] | 86 | int error = yyparse(&root, &error_count); |
| 87 | if (error != 0 || error_count > 0) { |
| 88 | fprintf(stderr, "%d parse errors\n", error_count); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 89 | return 6; |
| 90 | } |
| 91 | |
| 92 | // Evaluate the parsed script. |
| 93 | |
| 94 | UpdaterInfo updater_info; |
| 95 | updater_info.cmd_pipe = cmd_pipe; |
| 96 | updater_info.package_zip = &za; |
| 97 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 98 | State state; |
| 99 | state.cookie = &updater_info; |
| 100 | state.script = script; |
| 101 | state.errmsg = NULL; |
| 102 | |
| 103 | char* result = Evaluate(&state, root); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 104 | if (result == NULL) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 105 | if (state.errmsg == NULL) { |
| 106 | fprintf(stderr, "script aborted (no error message)\n"); |
| 107 | fprintf(cmd_pipe, "ui_print script aborted (no error message)\n"); |
| 108 | } else { |
| 109 | fprintf(stderr, "script aborted: %s\n", state.errmsg); |
| 110 | char* line = strtok(state.errmsg, "\n"); |
| 111 | while (line) { |
| 112 | fprintf(cmd_pipe, "ui_print %s\n", line); |
| 113 | line = strtok(NULL, "\n"); |
| 114 | } |
| 115 | fprintf(cmd_pipe, "ui_print\n"); |
| 116 | } |
| 117 | free(state.errmsg); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 118 | return 7; |
| 119 | } else { |
| 120 | fprintf(stderr, "script result was [%s]\n", result); |
| 121 | free(result); |
| 122 | } |
| 123 | |
| 124 | mzCloseZipArchive(&za); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 125 | free(script); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 126 | |
| 127 | return 0; |
| 128 | } |