blob: 09776256b970765c4f22cc6b6a8732e25726f83c [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
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
30int 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;
Doug Zongker8edb00c2009-06-11 17:21:44 -070083 int error_count = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -070084 yy_scan_string(script);
Doug Zongker8edb00c2009-06-11 17:21:44 -070085 int error = yyparse(&root, &error_count);
86 if (error != 0 || error_count > 0) {
87 fprintf(stderr, "%d parse errors\n", error_count);
Doug Zongker9931f7f2009-06-10 14:11:53 -070088 return 6;
89 }
90
91 // Evaluate the parsed script.
92
93 UpdaterInfo updater_info;
94 updater_info.cmd_pipe = cmd_pipe;
95 updater_info.package_zip = &za;
96
97 char* result = Evaluate(&updater_info, root);
98 if (result == NULL) {
99 const char* errmsg = GetError();
100 fprintf(stderr, "script aborted with error: %s\n",
101 errmsg == NULL ? "(none)" : errmsg);
102 ClearError();
103 return 7;
104 } else {
105 fprintf(stderr, "script result was [%s]\n", result);
106 free(result);
107 }
108
109 mzCloseZipArchive(&za);
110
111 return 0;
112}