]> git.saurik.com Git - android/aapt.git/blame - Command.cpp
Merge donut into master
[android/aapt.git] / Command.cpp
CommitLineData
a534180c
TAOSP
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
17using namespace android;
18
19/*
20 * Show version info. All the cool kids do it.
21 */
22int 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 */
37ZipFile* 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 */
65ZipFile* 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
83bail:
84 return zip;
85}
86
87
88/*
89 * Return a short string describing the compression method.
90 */
91const 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 */
104int 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 */
119int 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 }
99343a6d 193
a534180c
TAOSP
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 }
99343a6d 201
a534180c
TAOSP
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 }
99343a6d 215
a534180c
TAOSP
216 result = 0;
217
218bail:
219 delete zip;
220 return result;
221}
222
223static 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
234static 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
253static 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
271static 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
287static 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
315enum {
316 NAME_ATTR = 0x01010003,
317 VERSION_CODE_ATTR = 0x0101021b,
318 VERSION_NAME_ATTR = 0x0101021c,
319 LABEL_ATTR = 0x01010001,
320 ICON_ATTR = 0x01010002,
99343a6d 321 MIN_SDK_VERSION_ATTR = 0x0101020c
a534180c
TAOSP
322};
323
99343a6d
SA
324const char *getComponentName(String8 &pkgName, String8 &componentName) {
325 ssize_t idx = componentName.find(".");
326 String8 retStr(pkgName);
327 if (idx == 0) {
328 retStr += componentName;
329 } else if (idx < 0) {
330 retStr += ".";
331 retStr += componentName;
332 } else {
333 return componentName.string();
334 }
335 return retStr.string();
336}
337
a534180c
TAOSP
338/*
339 * Handle the "dump" command, to extract select data from an archive.
340 */
341int doDump(Bundle* bundle)
342{
343 status_t result = UNKNOWN_ERROR;
344 Asset* asset = NULL;
99343a6d 345
a534180c
TAOSP
346 if (bundle->getFileSpecCount() < 1) {
347 fprintf(stderr, "ERROR: no dump option specified\n");
348 return 1;
349 }
99343a6d 350
a534180c
TAOSP
351 if (bundle->getFileSpecCount() < 2) {
352 fprintf(stderr, "ERROR: no dump file specified\n");
353 return 1;
354 }
99343a6d 355
a534180c
TAOSP
356 const char* option = bundle->getFileSpecEntry(0);
357 const char* filename = bundle->getFileSpecEntry(1);
99343a6d 358
a534180c
TAOSP
359 AssetManager assets;
360 if (!assets.addAssetPath(String8(filename), NULL)) {
361 fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n");
362 return 1;
363 }
99343a6d 364
a534180c
TAOSP
365 const ResTable& res = assets.getResources(false);
366 if (&res == NULL) {
367 fprintf(stderr, "ERROR: dump failed because no resource table was found\n");
368 goto bail;
369 }
99343a6d 370
a534180c
TAOSP
371 if (strcmp("resources", option) == 0) {
372 res.print();
99343a6d 373
a534180c
TAOSP
374 } else if (strcmp("xmltree", option) == 0) {
375 if (bundle->getFileSpecCount() < 3) {
376 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
377 goto bail;
378 }
99343a6d 379
a534180c
TAOSP
380 for (int i=2; i<bundle->getFileSpecCount(); i++) {
381 const char* resname = bundle->getFileSpecEntry(i);
382 ResXMLTree tree;
383 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
384 if (asset == NULL) {
385 fprintf(stderr, "ERROR: dump failed because resource %p found\n", resname);
386 goto bail;
387 }
99343a6d 388
a534180c
TAOSP
389 if (tree.setTo(asset->getBuffer(true),
390 asset->getLength()) != NO_ERROR) {
391 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
392 goto bail;
393 }
394 tree.restart();
395 printXMLBlock(&tree);
396 delete asset;
397 asset = NULL;
398 }
99343a6d 399
a534180c
TAOSP
400 } else if (strcmp("xmlstrings", option) == 0) {
401 if (bundle->getFileSpecCount() < 3) {
402 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
403 goto bail;
404 }
99343a6d 405
a534180c
TAOSP
406 for (int i=2; i<bundle->getFileSpecCount(); i++) {
407 const char* resname = bundle->getFileSpecEntry(i);
408 ResXMLTree tree;
409 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
410 if (asset == NULL) {
411 fprintf(stderr, "ERROR: dump failed because resource %p found\n", resname);
412 goto bail;
413 }
99343a6d 414
a534180c
TAOSP
415 if (tree.setTo(asset->getBuffer(true),
416 asset->getLength()) != NO_ERROR) {
417 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
418 goto bail;
419 }
420 printStringPool(&tree.getStrings());
421 delete asset;
422 asset = NULL;
423 }
99343a6d 424
a534180c
TAOSP
425 } else {
426 ResXMLTree tree;
427 asset = assets.openNonAsset("AndroidManifest.xml",
428 Asset::ACCESS_BUFFER);
429 if (asset == NULL) {
430 fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n");
431 goto bail;
432 }
99343a6d 433
a534180c
TAOSP
434 if (tree.setTo(asset->getBuffer(true),
435 asset->getLength()) != NO_ERROR) {
436 fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n");
437 goto bail;
438 }
439 tree.restart();
99343a6d 440
a534180c
TAOSP
441 if (strcmp("permissions", option) == 0) {
442 size_t len;
443 ResXMLTree::event_code_t code;
444 int depth = 0;
445 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
446 if (code == ResXMLTree::END_TAG) {
447 depth--;
448 continue;
449 }
450 if (code != ResXMLTree::START_TAG) {
451 continue;
452 }
453 depth++;
454 String8 tag(tree.getElementName(&len));
455 //printf("Depth %d tag %s\n", depth, tag.string());
456 if (depth == 1) {
457 if (tag != "manifest") {
458 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
459 goto bail;
460 }
461 String8 pkg = getAttribute(tree, NULL, "package", NULL);
462 printf("package: %s\n", pkg.string());
463 } else if (depth == 2 && tag == "permission") {
464 String8 error;
465 String8 name = getAttribute(tree, NAME_ATTR, &error);
466 if (error != "") {
467 fprintf(stderr, "ERROR: %s\n", error.string());
468 goto bail;
469 }
470 printf("permission: %s\n", name.string());
471 } else if (depth == 2 && tag == "uses-permission") {
472 String8 error;
473 String8 name = getAttribute(tree, NAME_ATTR, &error);
474 if (error != "") {
475 fprintf(stderr, "ERROR: %s\n", error.string());
476 goto bail;
477 }
478 printf("uses-permission: %s\n", name.string());
479 }
480 }
481 } else if (strcmp("badging", option) == 0) {
482 size_t len;
483 ResXMLTree::event_code_t code;
484 int depth = 0;
485 String8 error;
486 bool withinActivity = false;
487 bool isMainActivity = false;
488 bool isLauncherActivity = false;
99343a6d
SA
489 bool withinApplication = false;
490 bool withinReceiver = false;
491 String8 pkg;
a534180c
TAOSP
492 String8 activityName;
493 String8 activityLabel;
494 String8 activityIcon;
99343a6d 495 String8 receiverName;
a534180c
TAOSP
496 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
497 if (code == ResXMLTree::END_TAG) {
498 depth--;
499 continue;
500 }
501 if (code != ResXMLTree::START_TAG) {
502 continue;
503 }
504 depth++;
505 String8 tag(tree.getElementName(&len));
506 //printf("Depth %d tag %s\n", depth, tag.string());
507 if (depth == 1) {
508 if (tag != "manifest") {
509 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
510 goto bail;
511 }
99343a6d 512 pkg = getAttribute(tree, NULL, "package", NULL);
a534180c
TAOSP
513 printf("package: name='%s' ", pkg.string());
514 int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error);
515 if (error != "") {
516 fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string());
517 goto bail;
518 }
519 if (versionCode > 0) {
520 printf("versionCode='%d' ", versionCode);
521 } else {
522 printf("versionCode='' ");
523 }
524 String8 versionName = getAttribute(tree, VERSION_NAME_ATTR, &error);
525 if (error != "") {
526 fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string());
527 goto bail;
528 }
529 printf("versionName='%s'\n", versionName.string());
99343a6d
SA
530 } else if (depth == 2) {
531 withinApplication = false;
532 if (tag == "application") {
533 withinApplication = true;
534 String8 label = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
535 if (error != "") {
536 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
537 goto bail;
538 }
539 printf("application: label='%s' ", label.string());
540 String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
541 if (error != "") {
542 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
543 goto bail;
544 }
545 printf("icon='%s'\n", icon.string());
546 } else if (tag == "uses-sdk") {
547 int32_t sdkVersion = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error);
548 if (error != "") {
549 fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n", error.string());
550 goto bail;
551 }
552 if (sdkVersion != -1) {
553 printf("sdkVersion:'%d'\n", sdkVersion);
554 }
a534180c 555 }
99343a6d
SA
556 } else if (depth == 3 && withinApplication) {
557 withinActivity = false;
558 withinReceiver = false;
559 if(tag == "activity") {
560 withinActivity = true;
561 activityName = getAttribute(tree, NAME_ATTR, &error);
562 if (error != "") {
563 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
564 goto bail;
565 }
a534180c 566
99343a6d
SA
567 activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
568 if (error != "") {
569 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
570 goto bail;
571 }
a534180c 572
99343a6d 573 activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
a534180c 574 if (error != "") {
99343a6d 575 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
a534180c
TAOSP
576 goto bail;
577 }
99343a6d
SA
578 } else if (tag == "uses-library") {
579 String8 libraryName = getAttribute(tree, NAME_ATTR, &error);
580 if (error != "") {
581 fprintf(stderr, "ERROR getting 'android:name' attribute for uses-library: %s\n", error.string());
582 goto bail;
a534180c 583 }
99343a6d
SA
584 printf("uses-library:'%s'\n", libraryName.string());
585 } else if (tag == "receiver") {
586 withinReceiver = true;
587 receiverName = getAttribute(tree, NAME_ATTR, &error);
588
a534180c 589 if (error != "") {
99343a6d 590 fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string());
a534180c
TAOSP
591 goto bail;
592 }
99343a6d
SA
593 }
594 } else if (depth == 5) {
595 if (withinActivity) {
596 if (tag == "action") {
597 //printf("LOG: action tag\n");
598 String8 action = getAttribute(tree, NAME_ATTR, &error);
599 if (error != "") {
600 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
601 goto bail;
602 }
603 if (action == "android.intent.action.MAIN") {
604 isMainActivity = true;
605 //printf("LOG: isMainActivity==true\n");
606 }
607 } else if (tag == "category") {
608 String8 category = getAttribute(tree, NAME_ATTR, &error);
609 if (error != "") {
610 fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string());
611 goto bail;
612 }
613 if (category == "android.intent.category.LAUNCHER") {
614 isLauncherActivity = true;
615 //printf("LOG: isLauncherActivity==true\n");
616 }
617 }
618 } else if (withinReceiver) {
619 if (tag == "action") {
620 String8 action = getAttribute(tree, NAME_ATTR, &error);
621 if (error != "") {
622 fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string());
623 goto bail;
624 }
625 if (action == "android.appwidget.action.APPWIDGET_UPDATE") {
626 const char *rName = getComponentName(pkg, receiverName);
627 if (rName != NULL) {
628 printf("gadget-receiver:'%s/%s'\n", pkg.string(), rName);
629 }
630 }
631 }
a534180c
TAOSP
632 }
633 }
634
99343a6d
SA
635 if (depth < 2) {
636 withinApplication = false;
637 }
a534180c
TAOSP
638 if (depth < 3) {
639 //if (withinActivity) printf("LOG: withinActivity==false\n");
640 withinActivity = false;
99343a6d 641 withinReceiver = false;
a534180c
TAOSP
642 }
643
644 if (depth < 5) {
645 //if (isMainActivity) printf("LOG: isMainActivity==false\n");
646 //if (isLauncherActivity) printf("LOG: isLauncherActivity==false\n");
647 isMainActivity = false;
648 isLauncherActivity = false;
649 }
650
651 if (withinActivity && isMainActivity && isLauncherActivity) {
99343a6d
SA
652 printf("launchable activity:");
653 const char *aName = getComponentName(pkg, activityName);
654 if (aName != NULL) {
655 printf(" name='%s'", aName);
656 }
657 printf("label='%s' icon='%s'\n",
658 activityLabel.string(),
a534180c
TAOSP
659 activityIcon.string());
660 }
661 }
662 printf("locales:");
663 Vector<String8> locales;
664 res.getLocales(&locales);
665 const size_t N = locales.size();
666 for (size_t i=0; i<N; i++) {
667 const char* localeStr = locales[i].string();
668 if (localeStr == NULL || strlen(localeStr) == 0) {
669 localeStr = "--_--";
670 }
671 printf(" '%s'", localeStr);
672 }
673 printf("\n");
674 } else if (strcmp("configurations", option) == 0) {
675 Vector<ResTable_config> configs;
676 res.getConfigurations(&configs);
677 const size_t N = configs.size();
678 for (size_t i=0; i<N; i++) {
679 printf("%s\n", configs[i].toString().string());
680 }
681 } else {
682 fprintf(stderr, "ERROR: unknown dump option '%s'\n", option);
683 goto bail;
684 }
685 }
686
687 result = NO_ERROR;
99343a6d 688
a534180c
TAOSP
689bail:
690 if (asset) {
691 delete asset;
692 }
693 return (result != NO_ERROR);
694}
695
696
697/*
698 * Handle the "add" command, which wants to add files to a new or
699 * pre-existing archive.
700 */
701int doAdd(Bundle* bundle)
702{
703 ZipFile* zip = NULL;
704 status_t result = UNKNOWN_ERROR;
705 const char* zipFileName;
706
707 if (bundle->getUpdate()) {
708 /* avoid confusion */
709 fprintf(stderr, "ERROR: can't use '-u' with add\n");
710 goto bail;
711 }
712
713 if (bundle->getFileSpecCount() < 1) {
714 fprintf(stderr, "ERROR: must specify zip file name\n");
715 goto bail;
716 }
717 zipFileName = bundle->getFileSpecEntry(0);
718
719 if (bundle->getFileSpecCount() < 2) {
720 fprintf(stderr, "NOTE: nothing to do\n");
721 goto bail;
722 }
723
724 zip = openReadWrite(zipFileName, true);
725 if (zip == NULL) {
726 fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName);
727 goto bail;
728 }
729
730 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
731 const char* fileName = bundle->getFileSpecEntry(i);
732
733 if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) {
734 printf(" '%s'... (from gzip)\n", fileName);
735 result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL);
736 } else {
737 printf(" '%s'...\n", fileName);
738 result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
739 }
740 if (result != NO_ERROR) {
741 fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName);
742 if (result == NAME_NOT_FOUND)
743 fprintf(stderr, ": file not found\n");
744 else if (result == ALREADY_EXISTS)
745 fprintf(stderr, ": already exists in archive\n");
746 else
747 fprintf(stderr, "\n");
748 goto bail;
749 }
750 }
751
752 result = NO_ERROR;
753
754bail:
755 delete zip;
756 return (result != NO_ERROR);
757}
758
759
760/*
761 * Delete files from an existing archive.
762 */
763int doRemove(Bundle* bundle)
764{
765 ZipFile* zip = NULL;
766 status_t result = UNKNOWN_ERROR;
767 const char* zipFileName;
768
769 if (bundle->getFileSpecCount() < 1) {
770 fprintf(stderr, "ERROR: must specify zip file name\n");
771 goto bail;
772 }
773 zipFileName = bundle->getFileSpecEntry(0);
774
775 if (bundle->getFileSpecCount() < 2) {
776 fprintf(stderr, "NOTE: nothing to do\n");
777 goto bail;
778 }
779
780 zip = openReadWrite(zipFileName, false);
781 if (zip == NULL) {
782 fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n",
783 zipFileName);
784 goto bail;
785 }
786
787 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
788 const char* fileName = bundle->getFileSpecEntry(i);
789 ZipEntry* entry;
790
791 entry = zip->getEntryByName(fileName);
792 if (entry == NULL) {
793 printf(" '%s' NOT FOUND\n", fileName);
794 continue;
795 }
796
797 result = zip->remove(entry);
798
799 if (result != NO_ERROR) {
800 fprintf(stderr, "Unable to delete '%s' from '%s'\n",
801 bundle->getFileSpecEntry(i), zipFileName);
802 goto bail;
803 }
804 }
805
806 /* update the archive */
807 zip->flush();
808
809bail:
810 delete zip;
811 return (result != NO_ERROR);
812}
813
814
815/*
816 * Package up an asset directory and associated application files.
817 */
818int doPackage(Bundle* bundle)
819{
820 const char* outputAPKFile;
821 int retVal = 1;
822 status_t err;
823 sp<AaptAssets> assets;
824 int N;
825
826 // -c zz_ZZ means do pseudolocalization
827 ResourceFilter filter;
828 err = filter.parse(bundle->getConfigurations());
829 if (err != NO_ERROR) {
830 goto bail;
831 }
832 if (filter.containsPseudo()) {
833 bundle->setPseudolocalize(true);
834 }
835
836 N = bundle->getFileSpecCount();
837 if (N < 1 && bundle->getResourceSourceDirs().size() == 0 && bundle->getJarFiles().size() == 0
838 && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) {
839 fprintf(stderr, "ERROR: no input files\n");
840 goto bail;
841 }
842
843 outputAPKFile = bundle->getOutputAPKFile();
844
845 // Make sure the filenames provided exist and are of the appropriate type.
846 if (outputAPKFile) {
847 FileType type;
848 type = getFileType(outputAPKFile);
849 if (type != kFileTypeNonexistent && type != kFileTypeRegular) {
850 fprintf(stderr,
851 "ERROR: output file '%s' exists but is not regular file\n",
852 outputAPKFile);
853 goto bail;
854 }
855 }
856
857 // Load the assets.
858 assets = new AaptAssets();
859 err = assets->slurpFromArgs(bundle);
860 if (err < 0) {
861 goto bail;
862 }
863
864 if (bundle->getVerbose()) {
865 assets->print();
866 }
867
868 // If they asked for any files that need to be compiled, do so.
869 if (bundle->getResourceSourceDirs().size() || bundle->getAndroidManifestFile()) {
870 err = buildResources(bundle, assets);
871 if (err != 0) {
872 goto bail;
873 }
874 }
875
876 // At this point we've read everything and processed everything. From here
877 // on out it's just writing output files.
878 if (SourcePos::hasErrors()) {
879 goto bail;
880 }
881
882 // Write out R.java constants
883 if (assets->getPackage() == assets->getSymbolsPrivatePackage()) {
884 err = writeResourceSymbols(bundle, assets, assets->getPackage(), true);
885 if (err < 0) {
886 goto bail;
887 }
888 } else {
889 err = writeResourceSymbols(bundle, assets, assets->getPackage(), false);
890 if (err < 0) {
891 goto bail;
892 }
893 err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true);
894 if (err < 0) {
895 goto bail;
896 }
897 }
898
899 // Write the apk
900 if (outputAPKFile) {
901 err = writeAPK(bundle, assets, String8(outputAPKFile));
902 if (err != NO_ERROR) {
903 fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile);
904 goto bail;
905 }
906 }
907
908 retVal = 0;
909bail:
910 if (SourcePos::hasErrors()) {
911 SourcePos::printErrors(stderr);
912 }
913 return retVal;
914}