]> git.saurik.com Git - android/aapt.git/blame - Package.cpp
am 6abf2f50: am 0b45ca88: am cd01ad7c: am 20339b24: Merge "Remove Debug Code"
[android/aapt.git] / Package.cpp
CommitLineData
a534180c
TAOSP
1//
2// Copyright 2006 The Android Open Source Project
3//
4// Package assets into Zip files.
5//
6#include "Main.h"
7#include "AaptAssets.h"
8#include "ResourceTable.h"
9
79e5d372
MA
10#include <utils/Log.h>
11#include <utils/threads.h>
12#include <utils/List.h>
13#include <utils/Errors.h>
a534180c
TAOSP
14
15#include <sys/types.h>
16#include <dirent.h>
17#include <ctype.h>
18#include <errno.h>
19
20using namespace android;
21
22static const char* kExcludeExtension = ".EXCLUDE";
23
24/* these formats are already compressed, or don't compress well */
25static const char* kNoCompressExt[] = {
26 ".jpg", ".jpeg", ".png", ".gif",
27 ".wav", ".mp2", ".mp3", ".ogg", ".aac",
28 ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
29 ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
30 ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
31 ".amr", ".awb", ".wma", ".wmv"
32};
33
34/* fwd decls, so I can write this downward */
35ssize_t processAssets(Bundle* bundle, ZipFile* zip, const sp<AaptAssets>& assets);
460ed1b2
KR
36ssize_t processAssets(Bundle* bundle, ZipFile* zip, const sp<AaptDir>& dir,
37 const AaptGroupEntry& ge, const ResourceFilter* filter);
a534180c
TAOSP
38bool processFile(Bundle* bundle, ZipFile* zip,
39 const sp<AaptGroup>& group, const sp<AaptFile>& file);
40bool okayToCompress(Bundle* bundle, const String8& pathName);
41ssize_t processJarFiles(Bundle* bundle, ZipFile* zip);
42
43/*
44 * The directory hierarchy looks like this:
45 * "outputDir" and "assetRoot" are existing directories.
46 *
47 * On success, "bundle->numPackages" will be the number of Zip packages
48 * we created.
49 */
50status_t writeAPK(Bundle* bundle, const sp<AaptAssets>& assets,
51 const String8& outputFile)
52{
dddb1fc7
JG
53 #if BENCHMARK
54 fprintf(stdout, "BENCHMARK: Starting APK Bundling \n");
55 long startAPKTime = clock();
56 #endif /* BENCHMARK */
57
a534180c
TAOSP
58 status_t result = NO_ERROR;
59 ZipFile* zip = NULL;
60 int count;
61
62 //bundle->setPackageCount(0);
63
64 /*
65 * Prep the Zip archive.
66 *
67 * If the file already exists, fail unless "update" or "force" is set.
68 * If "update" is set, update the contents of the existing archive.
69 * Else, if "force" is set, remove the existing archive.
70 */
71 FileType fileType = getFileType(outputFile.string());
72 if (fileType == kFileTypeNonexistent) {
73 // okay, create it below
74 } else if (fileType == kFileTypeRegular) {
75 if (bundle->getUpdate()) {
76 // okay, open it below
77 } else if (bundle->getForce()) {
78 if (unlink(outputFile.string()) != 0) {
79 fprintf(stderr, "ERROR: unable to remove '%s': %s\n", outputFile.string(),
80 strerror(errno));
81 goto bail;
82 }
83 } else {
84 fprintf(stderr, "ERROR: '%s' exists (use '-f' to force overwrite)\n",
85 outputFile.string());
86 goto bail;
87 }
88 } else {
89 fprintf(stderr, "ERROR: '%s' exists and is not a regular file\n", outputFile.string());
90 goto bail;
91 }
92
93 if (bundle->getVerbose()) {
94 printf("%s '%s'\n", (fileType == kFileTypeNonexistent) ? "Creating" : "Opening",
95 outputFile.string());
96 }
97
98 status_t status;
99 zip = new ZipFile;
100 status = zip->open(outputFile.string(), ZipFile::kOpenReadWrite | ZipFile::kOpenCreate);
101 if (status != NO_ERROR) {
102 fprintf(stderr, "ERROR: unable to open '%s' as Zip file for writing\n",
103 outputFile.string());
104 goto bail;
105 }
106
107 if (bundle->getVerbose()) {
108 printf("Writing all files...\n");
109 }
110
111 count = processAssets(bundle, zip, assets);
112 if (count < 0) {
113 fprintf(stderr, "ERROR: unable to process assets while packaging '%s'\n",
114 outputFile.string());
115 result = count;
116 goto bail;
117 }
118
119 if (bundle->getVerbose()) {
120 printf("Generated %d file%s\n", count, (count==1) ? "" : "s");
121 }
122
123 count = processJarFiles(bundle, zip);
124 if (count < 0) {
125 fprintf(stderr, "ERROR: unable to process jar files while packaging '%s'\n",
126 outputFile.string());
127 result = count;
128 goto bail;
129 }
130
131 if (bundle->getVerbose())
132 printf("Included %d file%s from jar/zip files.\n", count, (count==1) ? "" : "s");
133
134 result = NO_ERROR;
135
136 /*
137 * Check for cruft. We set the "marked" flag on all entries we created
138 * or decided not to update. If the entry isn't already slated for
139 * deletion, remove it now.
140 */
141 {
142 if (bundle->getVerbose())
143 printf("Checking for deleted files\n");
144 int i, removed = 0;
145 for (i = 0; i < zip->getNumEntries(); i++) {
146 ZipEntry* entry = zip->getEntryByIndex(i);
147
148 if (!entry->getMarked() && entry->getDeleted()) {
149 if (bundle->getVerbose()) {
150 printf(" (removing crufty '%s')\n",
151 entry->getFileName());
152 }
153 zip->remove(entry);
154 removed++;
155 }
156 }
157 if (bundle->getVerbose() && removed > 0)
158 printf("Removed %d file%s\n", removed, (removed==1) ? "" : "s");
159 }
160
161 /* tell Zip lib to process deletions and other pending changes */
162 result = zip->flush();
163 if (result != NO_ERROR) {
164 fprintf(stderr, "ERROR: Zip flush failed, archive may be hosed\n");
165 goto bail;
166 }
167
168 /* anything here? */
169 if (zip->getNumEntries() == 0) {
170 if (bundle->getVerbose()) {
171 printf("Archive is empty -- removing %s\n", outputFile.getPathLeaf().string());
172 }
173 delete zip; // close the file so we can remove it in Win32
174 zip = NULL;
175 if (unlink(outputFile.string()) != 0) {
406a85e3 176 fprintf(stderr, "warning: could not unlink '%s'\n", outputFile.string());
a534180c
TAOSP
177 }
178 }
179
b5a473da
JG
180 if (bundle->getGenDependencies()) {
181 // Add this file to the dependency file
182 String8 dependencyFile = outputFile.getBasePath();
183 dependencyFile.append(".d");
184
185 FILE* fp = fopen(dependencyFile.string(), "a");
186 fprintf(fp, "%s \\\n", outputFile.string());
187 fclose(fp);
188 }
189
a534180c
TAOSP
190 assert(result == NO_ERROR);
191
192bail:
193 delete zip; // must close before remove in Win32
194 if (result != NO_ERROR) {
195 if (bundle->getVerbose()) {
196 printf("Removing %s due to earlier failures\n", outputFile.string());
197 }
198 if (unlink(outputFile.string()) != 0) {
406a85e3 199 fprintf(stderr, "warning: could not unlink '%s'\n", outputFile.string());
a534180c
TAOSP
200 }
201 }
202
203 if (result == NO_ERROR && bundle->getVerbose())
204 printf("Done!\n");
dddb1fc7
JG
205
206 #if BENCHMARK
207 fprintf(stdout, "BENCHMARK: End APK Bundling. Time Elapsed: %f ms \n",(clock() - startAPKTime)/1000.0);
208 #endif /* BENCHMARK */
a534180c
TAOSP
209 return result;
210}
211
212ssize_t processAssets(Bundle* bundle, ZipFile* zip,
213 const sp<AaptAssets>& assets)
214{
215 ResourceFilter filter;
216 status_t status = filter.parse(bundle->getConfigurations());
217 if (status != NO_ERROR) {
218 return -1;
219 }
220
221 ssize_t count = 0;
222
223 const size_t N = assets->getGroupEntries().size();
224 for (size_t i=0; i<N; i++) {
225 const AaptGroupEntry& ge = assets->getGroupEntries()[i];
460ed1b2
KR
226
227 ssize_t res = processAssets(bundle, zip, assets, ge, &filter);
a534180c
TAOSP
228 if (res < 0) {
229 return res;
230 }
460ed1b2 231
a534180c
TAOSP
232 count += res;
233 }
234
235 return count;
236}
237
460ed1b2
KR
238ssize_t processAssets(Bundle* bundle, ZipFile* zip, const sp<AaptDir>& dir,
239 const AaptGroupEntry& ge, const ResourceFilter* filter)
a534180c
TAOSP
240{
241 ssize_t count = 0;
242
243 const size_t ND = dir->getDirs().size();
244 size_t i;
245 for (i=0; i<ND; i++) {
460ed1b2
KR
246 const sp<AaptDir>& subDir = dir->getDirs().valueAt(i);
247
248 const bool filterable = filter != NULL && subDir->getLeaf().find("mipmap-") != 0;
249
250 if (filterable && subDir->getLeaf() != subDir->getPath() && !filter->match(ge.toParams())) {
251 continue;
252 }
253
254 ssize_t res = processAssets(bundle, zip, subDir, ge, filterable ? filter : NULL);
a534180c
TAOSP
255 if (res < 0) {
256 return res;
257 }
258 count += res;
259 }
260
460ed1b2
KR
261 if (filter != NULL && !filter->match(ge.toParams())) {
262 return count;
263 }
264
a534180c
TAOSP
265 const size_t NF = dir->getFiles().size();
266 for (i=0; i<NF; i++) {
267 sp<AaptGroup> gp = dir->getFiles().valueAt(i);
268 ssize_t fi = gp->getFiles().indexOfKey(ge);
269 if (fi >= 0) {
270 sp<AaptFile> fl = gp->getFiles().valueAt(fi);
271 if (!processFile(bundle, zip, gp, fl)) {
272 return UNKNOWN_ERROR;
273 }
274 count++;
275 }
276 }
277
278 return count;
279}
280
281/*
282 * Process a regular file, adding it to the archive if appropriate.
283 *
284 * If we're in "update" mode, and the file already exists in the archive,
285 * delete the existing entry before adding the new one.
286 */
287bool processFile(Bundle* bundle, ZipFile* zip,
288 const sp<AaptGroup>& group, const sp<AaptFile>& file)
289{
290 const bool hasData = file->hasData();
291
292 String8 storageName(group->getPath());
293 storageName.convertToResPath();
294 ZipEntry* entry;
295 bool fromGzip = false;
296 status_t result;
297
298 /*
299 * See if the filename ends in ".EXCLUDE". We can't use
300 * String8::getPathExtension() because the length of what it considers
301 * to be an extension is capped.
302 *
303 * The Asset Manager doesn't check for ".EXCLUDE" in Zip archives,
304 * so there's no value in adding them (and it makes life easier on
305 * the AssetManager lib if we don't).
306 *
307 * NOTE: this restriction has been removed. If you're in this code, you
308 * should clean this up, but I'm in here getting rid of Path Name, and I
309 * don't want to make other potentially breaking changes --joeo
310 */
311 int fileNameLen = storageName.length();
312 int excludeExtensionLen = strlen(kExcludeExtension);
313 if (fileNameLen > excludeExtensionLen
314 && (0 == strcmp(storageName.string() + (fileNameLen - excludeExtensionLen),
315 kExcludeExtension))) {
406a85e3 316 fprintf(stderr, "warning: '%s' not added to Zip\n", storageName.string());
a534180c
TAOSP
317 return true;
318 }
319
320 if (strcasecmp(storageName.getPathExtension().string(), ".gz") == 0) {
321 fromGzip = true;
322 storageName = storageName.getBasePath();
323 }
324
325 if (bundle->getUpdate()) {
326 entry = zip->getEntryByName(storageName.string());
327 if (entry != NULL) {
328 /* file already exists in archive; there can be only one */
329 if (entry->getMarked()) {
330 fprintf(stderr,
331 "ERROR: '%s' exists twice (check for with & w/o '.gz'?)\n",
332 file->getPrintableSource().string());
333 return false;
334 }
335 if (!hasData) {
336 const String8& srcName = file->getSourceFile();
337 time_t fileModWhen;
338 fileModWhen = getFileModDate(srcName.string());
339 if (fileModWhen == (time_t) -1) { // file existence tested earlier,
340 return false; // not expecting an error here
341 }
342
343 if (fileModWhen > entry->getModWhen()) {
344 // mark as deleted so add() will succeed
345 if (bundle->getVerbose()) {
346 printf(" (removing old '%s')\n", storageName.string());
347 }
348
349 zip->remove(entry);
350 } else {
351 // version in archive is newer
352 if (bundle->getVerbose()) {
353 printf(" (not updating '%s')\n", storageName.string());
354 }
355 entry->setMarked(true);
356 return true;
357 }
358 } else {
359 // Generated files are always replaced.
360 zip->remove(entry);
361 }
362 }
363 }
364
365 //android_setMinPriority(NULL, ANDROID_LOG_VERBOSE);
366
367 if (fromGzip) {
368 result = zip->addGzip(file->getSourceFile().string(), storageName.string(), &entry);
369 } else if (!hasData) {
370 /* don't compress certain files, e.g. PNGs */
371 int compressionMethod = bundle->getCompressionMethod();
372 if (!okayToCompress(bundle, storageName)) {
373 compressionMethod = ZipEntry::kCompressStored;
374 }
375 result = zip->add(file->getSourceFile().string(), storageName.string(), compressionMethod,
376 &entry);
377 } else {
378 result = zip->add(file->getData(), file->getSize(), storageName.string(),
379 file->getCompressionMethod(), &entry);
380 }
381 if (result == NO_ERROR) {
382 if (bundle->getVerbose()) {
383 printf(" '%s'%s", storageName.string(), fromGzip ? " (from .gz)" : "");
384 if (entry->getCompressionMethod() == ZipEntry::kCompressStored) {
385 printf(" (not compressed)\n");
386 } else {
387 printf(" (compressed %d%%)\n", calcPercent(entry->getUncompressedLen(),
388 entry->getCompressedLen()));
389 }
390 }
391 entry->setMarked(true);
392 } else {
393 if (result == ALREADY_EXISTS) {
394 fprintf(stderr, " Unable to add '%s': file already in archive (try '-u'?)\n",
395 file->getPrintableSource().string());
396 } else {
397 fprintf(stderr, " Unable to add '%s': Zip add failed\n",
398 file->getPrintableSource().string());
399 }
400 return false;
401 }
402
403 return true;
404}
405
406/*
407 * Determine whether or not we want to try to compress this file based
408 * on the file extension.
409 */
410bool okayToCompress(Bundle* bundle, const String8& pathName)
411{
412 String8 ext = pathName.getPathExtension();
413 int i;
414
415 if (ext.length() == 0)
416 return true;
417
418 for (i = 0; i < NELEM(kNoCompressExt); i++) {
419 if (strcasecmp(ext.string(), kNoCompressExt[i]) == 0)
420 return false;
421 }
422
423 const android::Vector<const char*>& others(bundle->getNoCompressExtensions());
424 for (i = 0; i < (int)others.size(); i++) {
425 const char* str = others[i];
426 int pos = pathName.length() - strlen(str);
427 if (pos < 0) {
428 continue;
429 }
430 const char* path = pathName.string();
431 if (strcasecmp(path + pos, str) == 0) {
432 return false;
433 }
434 }
435
436 return true;
437}
438
439bool endsWith(const char* haystack, const char* needle)
440{
441 size_t a = strlen(haystack);
442 size_t b = strlen(needle);
443 if (a < b) return false;
444 return strcasecmp(haystack+(a-b), needle) == 0;
445}
446
447ssize_t processJarFile(ZipFile* jar, ZipFile* out)
448{
449 status_t err;
450 size_t N = jar->getNumEntries();
451 size_t count = 0;
452 for (size_t i=0; i<N; i++) {
453 ZipEntry* entry = jar->getEntryByIndex(i);
454 const char* storageName = entry->getFileName();
455 if (endsWith(storageName, ".class")) {
456 int compressionMethod = entry->getCompressionMethod();
457 size_t size = entry->getUncompressedLen();
458 const void* data = jar->uncompress(entry);
459 if (data == NULL) {
460 fprintf(stderr, "ERROR: unable to uncompress entry '%s'\n",
461 storageName);
462 return -1;
463 }
464 out->add(data, size, storageName, compressionMethod, NULL);
465 free((void*)data);
466 }
467 count++;
468 }
469 return count;
470}
471
472ssize_t processJarFiles(Bundle* bundle, ZipFile* zip)
473{
c70f9935 474 status_t err;
a534180c
TAOSP
475 ssize_t count = 0;
476 const android::Vector<const char*>& jars = bundle->getJarFiles();
477
478 size_t N = jars.size();
479 for (size_t i=0; i<N; i++) {
480 ZipFile jar;
481 err = jar.open(jars[i], ZipFile::kOpenReadOnly);
482 if (err != 0) {
58f944ba 483 fprintf(stderr, "ERROR: unable to open '%s' as a zip file: %d\n",
a534180c
TAOSP
484 jars[i], err);
485 return err;
486 }
487 err += processJarFile(&jar, zip);
488 if (err < 0) {
489 fprintf(stderr, "ERROR: unable to process '%s'\n", jars[i]);
490 return err;
491 }
492 count += err;
493 }
494
495 return count;
496}