]>
Commit | Line | Data |
---|---|---|
1 | // | |
2 | // Copyright 2006 The Android Open Source Project | |
3 | // | |
4 | // Android Asset Packaging Tool main entry point. | |
5 | // | |
6 | #include "Main.h" | |
7 | #include "Bundle.h" | |
8 | #include "ResourceTable.h" | |
9 | #include "XMLNode.h" | |
10 | ||
11 | #include <utils.h> | |
12 | #include <utils/ZipFile.h> | |
13 | ||
14 | #include <fcntl.h> | |
15 | #include <errno.h> | |
16 | ||
17 | using namespace android; | |
18 | ||
19 | /* | |
20 | * Show version info. All the cool kids do it. | |
21 | */ | |
22 | int doVersion(Bundle* bundle) | |
23 | { | |
24 | if (bundle->getFileSpecCount() != 0) | |
25 | printf("(ignoring extra arguments)\n"); | |
26 | printf("Android Asset Packaging Tool, v0.2\n"); | |
27 | ||
28 | return 0; | |
29 | } | |
30 | ||
31 | ||
32 | /* | |
33 | * Open the file read only. The call fails if the file doesn't exist. | |
34 | * | |
35 | * Returns NULL on failure. | |
36 | */ | |
37 | ZipFile* openReadOnly(const char* fileName) | |
38 | { | |
39 | ZipFile* zip; | |
40 | status_t result; | |
41 | ||
42 | zip = new ZipFile; | |
43 | result = zip->open(fileName, ZipFile::kOpenReadOnly); | |
44 | if (result != NO_ERROR) { | |
45 | if (result == NAME_NOT_FOUND) | |
46 | fprintf(stderr, "ERROR: '%s' not found\n", fileName); | |
47 | else if (result == PERMISSION_DENIED) | |
48 | fprintf(stderr, "ERROR: '%s' access denied\n", fileName); | |
49 | else | |
50 | fprintf(stderr, "ERROR: failed opening '%s' as Zip file\n", | |
51 | fileName); | |
52 | delete zip; | |
53 | return NULL; | |
54 | } | |
55 | ||
56 | return zip; | |
57 | } | |
58 | ||
59 | /* | |
60 | * Open the file read-write. The file will be created if it doesn't | |
61 | * already exist and "okayToCreate" is set. | |
62 | * | |
63 | * Returns NULL on failure. | |
64 | */ | |
65 | ZipFile* openReadWrite(const char* fileName, bool okayToCreate) | |
66 | { | |
67 | ZipFile* zip = NULL; | |
68 | status_t result; | |
69 | int flags; | |
70 | ||
71 | flags = ZipFile::kOpenReadWrite; | |
72 | if (okayToCreate) | |
73 | flags |= ZipFile::kOpenCreate; | |
74 | ||
75 | zip = new ZipFile; | |
76 | result = zip->open(fileName, flags); | |
77 | if (result != NO_ERROR) { | |
78 | delete zip; | |
79 | zip = NULL; | |
80 | goto bail; | |
81 | } | |
82 | ||
83 | bail: | |
84 | return zip; | |
85 | } | |
86 | ||
87 | ||
88 | /* | |
89 | * Return a short string describing the compression method. | |
90 | */ | |
91 | const char* compressionName(int method) | |
92 | { | |
93 | if (method == ZipEntry::kCompressStored) | |
94 | return "Stored"; | |
95 | else if (method == ZipEntry::kCompressDeflated) | |
96 | return "Deflated"; | |
97 | else | |
98 | return "Unknown"; | |
99 | } | |
100 | ||
101 | /* | |
102 | * Return the percent reduction in size (0% == no compression). | |
103 | */ | |
104 | int calcPercent(long uncompressedLen, long compressedLen) | |
105 | { | |
106 | if (!uncompressedLen) | |
107 | return 0; | |
108 | else | |
109 | return (int) (100.0 - (compressedLen * 100.0) / uncompressedLen + 0.5); | |
110 | } | |
111 | ||
112 | /* | |
113 | * Handle the "list" command, which can be a simple file dump or | |
114 | * a verbose listing. | |
115 | * | |
116 | * The verbose listing closely matches the output of the Info-ZIP "unzip" | |
117 | * command. | |
118 | */ | |
119 | int doList(Bundle* bundle) | |
120 | { | |
121 | int result = 1; | |
122 | ZipFile* zip = NULL; | |
123 | const ZipEntry* entry; | |
124 | long totalUncLen, totalCompLen; | |
125 | const char* zipFileName; | |
126 | ||
127 | if (bundle->getFileSpecCount() != 1) { | |
128 | fprintf(stderr, "ERROR: specify zip file name (only)\n"); | |
129 | goto bail; | |
130 | } | |
131 | zipFileName = bundle->getFileSpecEntry(0); | |
132 | ||
133 | zip = openReadOnly(zipFileName); | |
134 | if (zip == NULL) | |
135 | goto bail; | |
136 | ||
137 | int count, i; | |
138 | ||
139 | if (bundle->getVerbose()) { | |
140 | printf("Archive: %s\n", zipFileName); | |
141 | printf( | |
142 | " Length Method Size Ratio Date Time CRC-32 Name\n"); | |
143 | printf( | |
144 | "-------- ------ ------- ----- ---- ---- ------ ----\n"); | |
145 | } | |
146 | ||
147 | totalUncLen = totalCompLen = 0; | |
148 | ||
149 | count = zip->getNumEntries(); | |
150 | for (i = 0; i < count; i++) { | |
151 | entry = zip->getEntryByIndex(i); | |
152 | if (bundle->getVerbose()) { | |
153 | char dateBuf[32]; | |
154 | time_t when; | |
155 | ||
156 | when = entry->getModWhen(); | |
157 | strftime(dateBuf, sizeof(dateBuf), "%m-%d-%y %H:%M", | |
158 | localtime(&when)); | |
159 | ||
160 | printf("%8ld %-7.7s %7ld %3d%% %s %08lx %s\n", | |
161 | (long) entry->getUncompressedLen(), | |
162 | compressionName(entry->getCompressionMethod()), | |
163 | (long) entry->getCompressedLen(), | |
164 | calcPercent(entry->getUncompressedLen(), | |
165 | entry->getCompressedLen()), | |
166 | dateBuf, | |
167 | entry->getCRC32(), | |
168 | entry->getFileName()); | |
169 | } else { | |
170 | printf("%s\n", entry->getFileName()); | |
171 | } | |
172 | ||
173 | totalUncLen += entry->getUncompressedLen(); | |
174 | totalCompLen += entry->getCompressedLen(); | |
175 | } | |
176 | ||
177 | if (bundle->getVerbose()) { | |
178 | printf( | |
179 | "-------- ------- --- -------\n"); | |
180 | printf("%8ld %7ld %2d%% %d files\n", | |
181 | totalUncLen, | |
182 | totalCompLen, | |
183 | calcPercent(totalUncLen, totalCompLen), | |
184 | zip->getNumEntries()); | |
185 | } | |
186 | ||
187 | if (bundle->getAndroidList()) { | |
188 | AssetManager assets; | |
189 | if (!assets.addAssetPath(String8(zipFileName), NULL)) { | |
190 | fprintf(stderr, "ERROR: list -a failed because assets could not be loaded\n"); | |
191 | goto bail; | |
192 | } | |
193 | ||
194 | const ResTable& res = assets.getResources(false); | |
195 | if (&res == NULL) { | |
196 | printf("\nNo resource table found.\n"); | |
197 | } else { | |
198 | printf("\nResource table:\n"); | |
199 | res.print(); | |
200 | } | |
201 | ||
202 | Asset* manifestAsset = assets.openNonAsset("AndroidManifest.xml", | |
203 | Asset::ACCESS_BUFFER); | |
204 | if (manifestAsset == NULL) { | |
205 | printf("\nNo AndroidManifest.xml found.\n"); | |
206 | } else { | |
207 | printf("\nAndroid manifest:\n"); | |
208 | ResXMLTree tree; | |
209 | tree.setTo(manifestAsset->getBuffer(true), | |
210 | manifestAsset->getLength()); | |
211 | printXMLBlock(&tree); | |
212 | } | |
213 | delete manifestAsset; | |
214 | } | |
215 | ||
216 | result = 0; | |
217 | ||
218 | bail: | |
219 | delete zip; | |
220 | return result; | |
221 | } | |
222 | ||
223 | static ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes) | |
224 | { | |
225 | size_t N = tree.getAttributeCount(); | |
226 | for (size_t i=0; i<N; i++) { | |
227 | if (tree.getAttributeNameResID(i) == attrRes) { | |
228 | return (ssize_t)i; | |
229 | } | |
230 | } | |
231 | return -1; | |
232 | } | |
233 | ||
234 | static String8 getAttribute(const ResXMLTree& tree, const char* ns, | |
235 | const char* attr, String8* outError) | |
236 | { | |
237 | ssize_t idx = tree.indexOfAttribute(ns, attr); | |
238 | if (idx < 0) { | |
239 | return String8(); | |
240 | } | |
241 | Res_value value; | |
242 | if (tree.getAttributeValue(idx, &value) != NO_ERROR) { | |
243 | if (value.dataType != Res_value::TYPE_STRING) { | |
244 | if (outError != NULL) *outError = "attribute is not a string value"; | |
245 | return String8(); | |
246 | } | |
247 | } | |
248 | size_t len; | |
249 | const uint16_t* str = tree.getAttributeStringValue(idx, &len); | |
250 | return str ? String8(str, len) : String8(); | |
251 | } | |
252 | ||
253 | static String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError) | |
254 | { | |
255 | ssize_t idx = indexOfAttribute(tree, attrRes); | |
256 | if (idx < 0) { | |
257 | return String8(); | |
258 | } | |
259 | Res_value value; | |
260 | if (tree.getAttributeValue(idx, &value) != NO_ERROR) { | |
261 | if (value.dataType != Res_value::TYPE_STRING) { | |
262 | if (outError != NULL) *outError = "attribute is not a string value"; | |
263 | return String8(); | |
264 | } | |
265 | } | |
266 | size_t len; | |
267 | const uint16_t* str = tree.getAttributeStringValue(idx, &len); | |
268 | return str ? String8(str, len) : String8(); | |
269 | } | |
270 | ||
271 | static int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError) | |
272 | { | |
273 | ssize_t idx = indexOfAttribute(tree, attrRes); | |
274 | if (idx < 0) { | |
275 | return -1; | |
276 | } | |
277 | Res_value value; | |
278 | if (tree.getAttributeValue(idx, &value) != NO_ERROR) { | |
279 | if (value.dataType != Res_value::TYPE_INT_DEC) { | |
280 | if (outError != NULL) *outError = "attribute is not an integer value"; | |
281 | return -1; | |
282 | } | |
283 | } | |
284 | return value.data; | |
285 | } | |
286 | ||
287 | static String8 getResolvedAttribute(const ResTable* resTable, const ResXMLTree& tree, | |
288 | uint32_t attrRes, String8* outError) | |
289 | { | |
290 | ssize_t idx = indexOfAttribute(tree, attrRes); | |
291 | if (idx < 0) { | |
292 | return String8(); | |
293 | } | |
294 | Res_value value; | |
295 | if (tree.getAttributeValue(idx, &value) != NO_ERROR) { | |
296 | if (value.dataType == Res_value::TYPE_STRING) { | |
297 | size_t len; | |
298 | const uint16_t* str = tree.getAttributeStringValue(idx, &len); | |
299 | return str ? String8(str, len) : String8(); | |
300 | } | |
301 | resTable->resolveReference(&value, 0); | |
302 | if (value.dataType != Res_value::TYPE_STRING) { | |
303 | if (outError != NULL) *outError = "attribute is not a string value"; | |
304 | return String8(); | |
305 | } | |
306 | } | |
307 | size_t len; | |
308 | const Res_value* value2 = &value; | |
309 | const char16_t* str = const_cast<ResTable*>(resTable)->valueToString(value2, 0, NULL, &len); | |
310 | return str ? String8(str, len) : String8(); | |
311 | } | |
312 | ||
313 | // These are attribute resource constants for the platform, as found | |
314 | // in android.R.attr | |
315 | enum { | |
316 | NAME_ATTR = 0x01010003, | |
317 | VERSION_CODE_ATTR = 0x0101021b, | |
318 | VERSION_NAME_ATTR = 0x0101021c, | |
319 | LABEL_ATTR = 0x01010001, | |
320 | ICON_ATTR = 0x01010002, | |
321 | }; | |
322 | ||
323 | /* | |
324 | * Handle the "dump" command, to extract select data from an archive. | |
325 | */ | |
326 | int doDump(Bundle* bundle) | |
327 | { | |
328 | status_t result = UNKNOWN_ERROR; | |
329 | Asset* asset = NULL; | |
330 | ||
331 | if (bundle->getFileSpecCount() < 1) { | |
332 | fprintf(stderr, "ERROR: no dump option specified\n"); | |
333 | return 1; | |
334 | } | |
335 | ||
336 | if (bundle->getFileSpecCount() < 2) { | |
337 | fprintf(stderr, "ERROR: no dump file specified\n"); | |
338 | return 1; | |
339 | } | |
340 | ||
341 | const char* option = bundle->getFileSpecEntry(0); | |
342 | const char* filename = bundle->getFileSpecEntry(1); | |
343 | ||
344 | AssetManager assets; | |
345 | if (!assets.addAssetPath(String8(filename), NULL)) { | |
346 | fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n"); | |
347 | return 1; | |
348 | } | |
349 | ||
350 | const ResTable& res = assets.getResources(false); | |
351 | if (&res == NULL) { | |
352 | fprintf(stderr, "ERROR: dump failed because no resource table was found\n"); | |
353 | goto bail; | |
354 | } | |
355 | ||
356 | if (strcmp("resources", option) == 0) { | |
357 | res.print(); | |
358 | ||
359 | } else if (strcmp("xmltree", option) == 0) { | |
360 | if (bundle->getFileSpecCount() < 3) { | |
361 | fprintf(stderr, "ERROR: no dump xmltree resource file specified\n"); | |
362 | goto bail; | |
363 | } | |
364 | ||
365 | for (int i=2; i<bundle->getFileSpecCount(); i++) { | |
366 | const char* resname = bundle->getFileSpecEntry(i); | |
367 | ResXMLTree tree; | |
368 | asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER); | |
369 | if (asset == NULL) { | |
370 | fprintf(stderr, "ERROR: dump failed because resource %p found\n", resname); | |
371 | goto bail; | |
372 | } | |
373 | ||
374 | if (tree.setTo(asset->getBuffer(true), | |
375 | asset->getLength()) != NO_ERROR) { | |
376 | fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname); | |
377 | goto bail; | |
378 | } | |
379 | tree.restart(); | |
380 | printXMLBlock(&tree); | |
381 | delete asset; | |
382 | asset = NULL; | |
383 | } | |
384 | ||
385 | } else if (strcmp("xmlstrings", option) == 0) { | |
386 | if (bundle->getFileSpecCount() < 3) { | |
387 | fprintf(stderr, "ERROR: no dump xmltree resource file specified\n"); | |
388 | goto bail; | |
389 | } | |
390 | ||
391 | for (int i=2; i<bundle->getFileSpecCount(); i++) { | |
392 | const char* resname = bundle->getFileSpecEntry(i); | |
393 | ResXMLTree tree; | |
394 | asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER); | |
395 | if (asset == NULL) { | |
396 | fprintf(stderr, "ERROR: dump failed because resource %p found\n", resname); | |
397 | goto bail; | |
398 | } | |
399 | ||
400 | if (tree.setTo(asset->getBuffer(true), | |
401 | asset->getLength()) != NO_ERROR) { | |
402 | fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname); | |
403 | goto bail; | |
404 | } | |
405 | printStringPool(&tree.getStrings()); | |
406 | delete asset; | |
407 | asset = NULL; | |
408 | } | |
409 | ||
410 | } else { | |
411 | ResXMLTree tree; | |
412 | asset = assets.openNonAsset("AndroidManifest.xml", | |
413 | Asset::ACCESS_BUFFER); | |
414 | if (asset == NULL) { | |
415 | fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n"); | |
416 | goto bail; | |
417 | } | |
418 | ||
419 | if (tree.setTo(asset->getBuffer(true), | |
420 | asset->getLength()) != NO_ERROR) { | |
421 | fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n"); | |
422 | goto bail; | |
423 | } | |
424 | tree.restart(); | |
425 | ||
426 | if (strcmp("permissions", option) == 0) { | |
427 | size_t len; | |
428 | ResXMLTree::event_code_t code; | |
429 | int depth = 0; | |
430 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
431 | if (code == ResXMLTree::END_TAG) { | |
432 | depth--; | |
433 | continue; | |
434 | } | |
435 | if (code != ResXMLTree::START_TAG) { | |
436 | continue; | |
437 | } | |
438 | depth++; | |
439 | String8 tag(tree.getElementName(&len)); | |
440 | //printf("Depth %d tag %s\n", depth, tag.string()); | |
441 | if (depth == 1) { | |
442 | if (tag != "manifest") { | |
443 | fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n"); | |
444 | goto bail; | |
445 | } | |
446 | String8 pkg = getAttribute(tree, NULL, "package", NULL); | |
447 | printf("package: %s\n", pkg.string()); | |
448 | } else if (depth == 2 && tag == "permission") { | |
449 | String8 error; | |
450 | String8 name = getAttribute(tree, NAME_ATTR, &error); | |
451 | if (error != "") { | |
452 | fprintf(stderr, "ERROR: %s\n", error.string()); | |
453 | goto bail; | |
454 | } | |
455 | printf("permission: %s\n", name.string()); | |
456 | } else if (depth == 2 && tag == "uses-permission") { | |
457 | String8 error; | |
458 | String8 name = getAttribute(tree, NAME_ATTR, &error); | |
459 | if (error != "") { | |
460 | fprintf(stderr, "ERROR: %s\n", error.string()); | |
461 | goto bail; | |
462 | } | |
463 | printf("uses-permission: %s\n", name.string()); | |
464 | } | |
465 | } | |
466 | } else if (strcmp("badging", option) == 0) { | |
467 | size_t len; | |
468 | ResXMLTree::event_code_t code; | |
469 | int depth = 0; | |
470 | String8 error; | |
471 | bool withinActivity = false; | |
472 | bool isMainActivity = false; | |
473 | bool isLauncherActivity = false; | |
474 | String8 activityName; | |
475 | String8 activityLabel; | |
476 | String8 activityIcon; | |
477 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { | |
478 | if (code == ResXMLTree::END_TAG) { | |
479 | depth--; | |
480 | continue; | |
481 | } | |
482 | if (code != ResXMLTree::START_TAG) { | |
483 | continue; | |
484 | } | |
485 | depth++; | |
486 | String8 tag(tree.getElementName(&len)); | |
487 | //printf("Depth %d tag %s\n", depth, tag.string()); | |
488 | if (depth == 1) { | |
489 | if (tag != "manifest") { | |
490 | fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n"); | |
491 | goto bail; | |
492 | } | |
493 | String8 pkg = getAttribute(tree, NULL, "package", NULL); | |
494 | printf("package: name='%s' ", pkg.string()); | |
495 | int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error); | |
496 | if (error != "") { | |
497 | fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string()); | |
498 | goto bail; | |
499 | } | |
500 | if (versionCode > 0) { | |
501 | printf("versionCode='%d' ", versionCode); | |
502 | } else { | |
503 | printf("versionCode='' "); | |
504 | } | |
505 | String8 versionName = getAttribute(tree, VERSION_NAME_ATTR, &error); | |
506 | if (error != "") { | |
507 | fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string()); | |
508 | goto bail; | |
509 | } | |
510 | printf("versionName='%s'\n", versionName.string()); | |
511 | } else if (depth == 2 && tag == "application") { | |
512 | String8 label = getResolvedAttribute(&res, tree, LABEL_ATTR, &error); | |
513 | if (error != "") { | |
514 | fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string()); | |
515 | goto bail; | |
516 | } | |
517 | printf("application: label='%s' ", label.string()); | |
518 | ||
519 | String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error); | |
520 | if (error != "") { | |
521 | fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string()); | |
522 | goto bail; | |
523 | } | |
524 | printf("icon='%s'\n", icon.string()); | |
525 | } else if (depth == 3 && tag == "activity") { | |
526 | withinActivity = true; | |
527 | //printf("LOG: withinActivity==true\n"); | |
528 | ||
529 | activityName = getAttribute(tree, NAME_ATTR, &error); | |
530 | if (error != "") { | |
531 | fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string()); | |
532 | goto bail; | |
533 | } | |
534 | ||
535 | activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error); | |
536 | if (error != "") { | |
537 | fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string()); | |
538 | goto bail; | |
539 | } | |
540 | ||
541 | activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error); | |
542 | if (error != "") { | |
543 | fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string()); | |
544 | goto bail; | |
545 | } | |
546 | } else if (depth == 5 && withinActivity) { | |
547 | if (tag == "action") { | |
548 | //printf("LOG: action tag\n"); | |
549 | String8 action = getAttribute(tree, NAME_ATTR, &error); | |
550 | if (error != "") { | |
551 | fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string()); | |
552 | goto bail; | |
553 | } | |
554 | if (action == "android.intent.action.MAIN") { | |
555 | isMainActivity = true; | |
556 | //printf("LOG: isMainActivity==true\n"); | |
557 | } | |
558 | } else if (tag == "category") { | |
559 | String8 category = getAttribute(tree, NAME_ATTR, &error); | |
560 | if (error != "") { | |
561 | fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string()); | |
562 | goto bail; | |
563 | } | |
564 | if (category == "android.intent.category.LAUNCHER") { | |
565 | isLauncherActivity = true; | |
566 | //printf("LOG: isLauncherActivity==true\n"); | |
567 | } | |
568 | } | |
569 | } | |
570 | ||
571 | if (depth < 3) { | |
572 | //if (withinActivity) printf("LOG: withinActivity==false\n"); | |
573 | withinActivity = false; | |
574 | } | |
575 | ||
576 | if (depth < 5) { | |
577 | //if (isMainActivity) printf("LOG: isMainActivity==false\n"); | |
578 | //if (isLauncherActivity) printf("LOG: isLauncherActivity==false\n"); | |
579 | isMainActivity = false; | |
580 | isLauncherActivity = false; | |
581 | } | |
582 | ||
583 | if (withinActivity && isMainActivity && isLauncherActivity) { | |
584 | printf("launchable activity: name='%s' label='%s' icon='%s'\n", | |
585 | activityName.string(), activityLabel.string(), | |
586 | activityIcon.string()); | |
587 | } | |
588 | } | |
589 | } else if (strcmp("configurations", option) == 0) { | |
590 | Vector<ResTable_config> configs; | |
591 | res.getConfigurations(&configs); | |
592 | const size_t N = configs.size(); | |
593 | for (size_t i=0; i<N; i++) { | |
594 | printf("%s\n", configs[i].toString().string()); | |
595 | } | |
596 | } else { | |
597 | fprintf(stderr, "ERROR: unknown dump option '%s'\n", option); | |
598 | goto bail; | |
599 | } | |
600 | } | |
601 | ||
602 | result = NO_ERROR; | |
603 | ||
604 | bail: | |
605 | if (asset) { | |
606 | delete asset; | |
607 | } | |
608 | return (result != NO_ERROR); | |
609 | } | |
610 | ||
611 | ||
612 | /* | |
613 | * Handle the "add" command, which wants to add files to a new or | |
614 | * pre-existing archive. | |
615 | */ | |
616 | int doAdd(Bundle* bundle) | |
617 | { | |
618 | ZipFile* zip = NULL; | |
619 | status_t result = UNKNOWN_ERROR; | |
620 | const char* zipFileName; | |
621 | ||
622 | if (bundle->getUpdate()) { | |
623 | /* avoid confusion */ | |
624 | fprintf(stderr, "ERROR: can't use '-u' with add\n"); | |
625 | goto bail; | |
626 | } | |
627 | ||
628 | if (bundle->getFileSpecCount() < 1) { | |
629 | fprintf(stderr, "ERROR: must specify zip file name\n"); | |
630 | goto bail; | |
631 | } | |
632 | zipFileName = bundle->getFileSpecEntry(0); | |
633 | ||
634 | if (bundle->getFileSpecCount() < 2) { | |
635 | fprintf(stderr, "NOTE: nothing to do\n"); | |
636 | goto bail; | |
637 | } | |
638 | ||
639 | zip = openReadWrite(zipFileName, true); | |
640 | if (zip == NULL) { | |
641 | fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName); | |
642 | goto bail; | |
643 | } | |
644 | ||
645 | for (int i = 1; i < bundle->getFileSpecCount(); i++) { | |
646 | const char* fileName = bundle->getFileSpecEntry(i); | |
647 | ||
648 | if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) { | |
649 | printf(" '%s'... (from gzip)\n", fileName); | |
650 | result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL); | |
651 | } else { | |
652 | printf(" '%s'...\n", fileName); | |
653 | result = zip->add(fileName, bundle->getCompressionMethod(), NULL); | |
654 | } | |
655 | if (result != NO_ERROR) { | |
656 | fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName); | |
657 | if (result == NAME_NOT_FOUND) | |
658 | fprintf(stderr, ": file not found\n"); | |
659 | else if (result == ALREADY_EXISTS) | |
660 | fprintf(stderr, ": already exists in archive\n"); | |
661 | else | |
662 | fprintf(stderr, "\n"); | |
663 | goto bail; | |
664 | } | |
665 | } | |
666 | ||
667 | result = NO_ERROR; | |
668 | ||
669 | bail: | |
670 | delete zip; | |
671 | return (result != NO_ERROR); | |
672 | } | |
673 | ||
674 | ||
675 | /* | |
676 | * Delete files from an existing archive. | |
677 | */ | |
678 | int doRemove(Bundle* bundle) | |
679 | { | |
680 | ZipFile* zip = NULL; | |
681 | status_t result = UNKNOWN_ERROR; | |
682 | const char* zipFileName; | |
683 | ||
684 | if (bundle->getFileSpecCount() < 1) { | |
685 | fprintf(stderr, "ERROR: must specify zip file name\n"); | |
686 | goto bail; | |
687 | } | |
688 | zipFileName = bundle->getFileSpecEntry(0); | |
689 | ||
690 | if (bundle->getFileSpecCount() < 2) { | |
691 | fprintf(stderr, "NOTE: nothing to do\n"); | |
692 | goto bail; | |
693 | } | |
694 | ||
695 | zip = openReadWrite(zipFileName, false); | |
696 | if (zip == NULL) { | |
697 | fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n", | |
698 | zipFileName); | |
699 | goto bail; | |
700 | } | |
701 | ||
702 | for (int i = 1; i < bundle->getFileSpecCount(); i++) { | |
703 | const char* fileName = bundle->getFileSpecEntry(i); | |
704 | ZipEntry* entry; | |
705 | ||
706 | entry = zip->getEntryByName(fileName); | |
707 | if (entry == NULL) { | |
708 | printf(" '%s' NOT FOUND\n", fileName); | |
709 | continue; | |
710 | } | |
711 | ||
712 | result = zip->remove(entry); | |
713 | ||
714 | if (result != NO_ERROR) { | |
715 | fprintf(stderr, "Unable to delete '%s' from '%s'\n", | |
716 | bundle->getFileSpecEntry(i), zipFileName); | |
717 | goto bail; | |
718 | } | |
719 | } | |
720 | ||
721 | /* update the archive */ | |
722 | zip->flush(); | |
723 | ||
724 | bail: | |
725 | delete zip; | |
726 | return (result != NO_ERROR); | |
727 | } | |
728 | ||
729 | ||
730 | /* | |
731 | * Package up an asset directory and associated application files. | |
732 | */ | |
733 | int doPackage(Bundle* bundle) | |
734 | { | |
735 | const char* outputAPKFile; | |
736 | int retVal = 1; | |
737 | status_t err; | |
738 | sp<AaptAssets> assets; | |
739 | int N; | |
740 | ||
741 | // -c zz_ZZ means do pseudolocalization | |
742 | ResourceFilter filter; | |
743 | err = filter.parse(bundle->getConfigurations()); | |
744 | if (err != NO_ERROR) { | |
745 | goto bail; | |
746 | } | |
747 | if (filter.containsPseudo()) { | |
748 | bundle->setPseudolocalize(true); | |
749 | } | |
750 | ||
751 | N = bundle->getFileSpecCount(); | |
752 | if (N < 1 && bundle->getResourceSourceDir() == NULL && bundle->getJarFiles().size() == 0 | |
753 | && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) { | |
754 | fprintf(stderr, "ERROR: no input files\n"); | |
755 | goto bail; | |
756 | } | |
757 | ||
758 | outputAPKFile = bundle->getOutputAPKFile(); | |
759 | ||
760 | // Make sure the filenames provided exist and are of the appropriate type. | |
761 | if (outputAPKFile) { | |
762 | FileType type; | |
763 | type = getFileType(outputAPKFile); | |
764 | if (type != kFileTypeNonexistent && type != kFileTypeRegular) { | |
765 | fprintf(stderr, | |
766 | "ERROR: output file '%s' exists but is not regular file\n", | |
767 | outputAPKFile); | |
768 | goto bail; | |
769 | } | |
770 | } | |
771 | ||
772 | // Load the assets. | |
773 | assets = new AaptAssets(); | |
774 | err = assets->slurpFromArgs(bundle); | |
775 | if (err < 0) { | |
776 | goto bail; | |
777 | } | |
778 | ||
779 | if (bundle->getVerbose()) { | |
780 | assets->print(); | |
781 | } | |
782 | ||
783 | // If they asked for any files that need to be compiled, do so. | |
784 | if (bundle->getResourceSourceDir() || bundle->getAndroidManifestFile()) { | |
785 | err = buildResources(bundle, assets); | |
786 | if (err != 0) { | |
787 | goto bail; | |
788 | } | |
789 | } | |
790 | ||
791 | // At this point we've read everything and processed everything. From here | |
792 | // on out it's just writing output files. | |
793 | if (SourcePos::hasErrors()) { | |
794 | goto bail; | |
795 | } | |
796 | ||
797 | // Write out R.java constants | |
798 | if (assets->getPackage() == assets->getSymbolsPrivatePackage()) { | |
799 | err = writeResourceSymbols(bundle, assets, assets->getPackage(), true); | |
800 | if (err < 0) { | |
801 | goto bail; | |
802 | } | |
803 | } else { | |
804 | err = writeResourceSymbols(bundle, assets, assets->getPackage(), false); | |
805 | if (err < 0) { | |
806 | goto bail; | |
807 | } | |
808 | err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true); | |
809 | if (err < 0) { | |
810 | goto bail; | |
811 | } | |
812 | } | |
813 | ||
814 | // Write the apk | |
815 | if (outputAPKFile) { | |
816 | err = writeAPK(bundle, assets, String8(outputAPKFile)); | |
817 | if (err != NO_ERROR) { | |
818 | fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile); | |
819 | goto bail; | |
820 | } | |
821 | } | |
822 | ||
823 | retVal = 0; | |
824 | bail: | |
825 | if (SourcePos::hasErrors()) { | |
826 | SourcePos::printErrors(stderr); | |
827 | } | |
828 | return retVal; | |
829 | } |