Package.cpp revision d98e1be20e1cca5c36c7e0344500d4a5574568aa
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
10#include <utils/Log.h>
11#include <utils/threads.h>
12#include <utils/List.h>
13#include <utils/Errors.h>
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);
36ssize_t processAssets(Bundle* bundle, ZipFile* zip, const sp<AaptDir>& dir,
37                        const AaptGroupEntry& ge, const ResourceFilter* filter);
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{
53    #if BENCHMARK
54    fprintf(stdout, "BENCHMARK: Starting APK Bundling \n");
55    long startAPKTime = clock();
56    #endif /* BENCHMARK */
57
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) {
176            fprintf(stderr, "warning: could not unlink '%s'\n", outputFile.string());
177        }
178    }
179
180    // If we've been asked to generate a dependency file for the .ap_ package,
181    // do so here
182    if (bundle->getGenDependencies()) {
183        // The dependency file gets output to the same directory
184        // as the specified output file with an additional .d extension.
185        // e.g. bin/resources.ap_.d
186        String8 dependencyFile = outputFile;
187        dependencyFile.append(".d");
188
189        FILE* fp = fopen(dependencyFile.string(), "a");
190        // Add this file to the dependency file
191        fprintf(fp, "%s \\\n", outputFile.string());
192        fclose(fp);
193    }
194
195    assert(result == NO_ERROR);
196
197bail:
198    delete zip;        // must close before remove in Win32
199    if (result != NO_ERROR) {
200        if (bundle->getVerbose()) {
201            printf("Removing %s due to earlier failures\n", outputFile.string());
202        }
203        if (unlink(outputFile.string()) != 0) {
204            fprintf(stderr, "warning: could not unlink '%s'\n", outputFile.string());
205        }
206    }
207
208    if (result == NO_ERROR && bundle->getVerbose())
209        printf("Done!\n");
210
211    #if BENCHMARK
212    fprintf(stdout, "BENCHMARK: End APK Bundling. Time Elapsed: %f ms \n",(clock() - startAPKTime)/1000.0);
213    #endif /* BENCHMARK */
214    return result;
215}
216
217ssize_t processAssets(Bundle* bundle, ZipFile* zip,
218                      const sp<AaptAssets>& assets)
219{
220    ResourceFilter filter;
221    status_t status = filter.parse(bundle->getConfigurations());
222    if (status != NO_ERROR) {
223        return -1;
224    }
225
226    ssize_t count = 0;
227
228    const size_t N = assets->getGroupEntries().size();
229    for (size_t i=0; i<N; i++) {
230        const AaptGroupEntry& ge = assets->getGroupEntries()[i];
231
232        ssize_t res = processAssets(bundle, zip, assets, ge, &filter);
233        if (res < 0) {
234            return res;
235        }
236
237        count += res;
238    }
239
240    return count;
241}
242
243ssize_t processAssets(Bundle* bundle, ZipFile* zip, const sp<AaptDir>& dir,
244        const AaptGroupEntry& ge, const ResourceFilter* filter)
245{
246    ssize_t count = 0;
247
248    const size_t ND = dir->getDirs().size();
249    size_t i;
250    for (i=0; i<ND; i++) {
251        const sp<AaptDir>& subDir = dir->getDirs().valueAt(i);
252
253        const bool filterable = filter != NULL && subDir->getLeaf().find("mipmap-") != 0;
254
255        if (filterable && subDir->getLeaf() != subDir->getPath() && !filter->match(ge.toParams())) {
256            continue;
257        }
258
259        ssize_t res = processAssets(bundle, zip, subDir, ge, filterable ? filter : NULL);
260        if (res < 0) {
261            return res;
262        }
263        count += res;
264    }
265
266    if (filter != NULL && !filter->match(ge.toParams())) {
267        return count;
268    }
269
270    const size_t NF = dir->getFiles().size();
271    for (i=0; i<NF; i++) {
272        sp<AaptGroup> gp = dir->getFiles().valueAt(i);
273        ssize_t fi = gp->getFiles().indexOfKey(ge);
274        if (fi >= 0) {
275            sp<AaptFile> fl = gp->getFiles().valueAt(fi);
276            if (!processFile(bundle, zip, gp, fl)) {
277                return UNKNOWN_ERROR;
278            }
279            count++;
280        }
281    }
282
283    return count;
284}
285
286/*
287 * Process a regular file, adding it to the archive if appropriate.
288 *
289 * If we're in "update" mode, and the file already exists in the archive,
290 * delete the existing entry before adding the new one.
291 */
292bool processFile(Bundle* bundle, ZipFile* zip,
293                 const sp<AaptGroup>& group, const sp<AaptFile>& file)
294{
295    const bool hasData = file->hasData();
296
297    String8 storageName(group->getPath());
298    storageName.convertToResPath();
299    ZipEntry* entry;
300    bool fromGzip = false;
301    status_t result;
302
303    /*
304     * See if the filename ends in ".EXCLUDE".  We can't use
305     * String8::getPathExtension() because the length of what it considers
306     * to be an extension is capped.
307     *
308     * The Asset Manager doesn't check for ".EXCLUDE" in Zip archives,
309     * so there's no value in adding them (and it makes life easier on
310     * the AssetManager lib if we don't).
311     *
312     * NOTE: this restriction has been removed.  If you're in this code, you
313     * should clean this up, but I'm in here getting rid of Path Name, and I
314     * don't want to make other potentially breaking changes --joeo
315     */
316    int fileNameLen = storageName.length();
317    int excludeExtensionLen = strlen(kExcludeExtension);
318    if (fileNameLen > excludeExtensionLen
319            && (0 == strcmp(storageName.string() + (fileNameLen - excludeExtensionLen),
320                            kExcludeExtension))) {
321        fprintf(stderr, "warning: '%s' not added to Zip\n", storageName.string());
322        return true;
323    }
324
325    if (strcasecmp(storageName.getPathExtension().string(), ".gz") == 0) {
326        fromGzip = true;
327        storageName = storageName.getBasePath();
328    }
329
330    if (bundle->getUpdate()) {
331        entry = zip->getEntryByName(storageName.string());
332        if (entry != NULL) {
333            /* file already exists in archive; there can be only one */
334            if (entry->getMarked()) {
335                fprintf(stderr,
336                        "ERROR: '%s' exists twice (check for with & w/o '.gz'?)\n",
337                        file->getPrintableSource().string());
338                return false;
339            }
340            if (!hasData) {
341                const String8& srcName = file->getSourceFile();
342                time_t fileModWhen;
343                fileModWhen = getFileModDate(srcName.string());
344                if (fileModWhen == (time_t) -1) { // file existence tested earlier,
345                    return false;                 //  not expecting an error here
346                }
347
348                if (fileModWhen > entry->getModWhen()) {
349                    // mark as deleted so add() will succeed
350                    if (bundle->getVerbose()) {
351                        printf("      (removing old '%s')\n", storageName.string());
352                    }
353
354                    zip->remove(entry);
355                } else {
356                    // version in archive is newer
357                    if (bundle->getVerbose()) {
358                        printf("      (not updating '%s')\n", storageName.string());
359                    }
360                    entry->setMarked(true);
361                    return true;
362                }
363            } else {
364                // Generated files are always replaced.
365                zip->remove(entry);
366            }
367        }
368    }
369
370    //android_setMinPriority(NULL, ANDROID_LOG_VERBOSE);
371
372    if (fromGzip) {
373        result = zip->addGzip(file->getSourceFile().string(), storageName.string(), &entry);
374    } else if (!hasData) {
375        /* don't compress certain files, e.g. PNGs */
376        int compressionMethod = bundle->getCompressionMethod();
377        if (!okayToCompress(bundle, storageName)) {
378            compressionMethod = ZipEntry::kCompressStored;
379        }
380        result = zip->add(file->getSourceFile().string(), storageName.string(), compressionMethod,
381                            &entry);
382    } else {
383        result = zip->add(file->getData(), file->getSize(), storageName.string(),
384                           file->getCompressionMethod(), &entry);
385    }
386    if (result == NO_ERROR) {
387        if (bundle->getVerbose()) {
388            printf("      '%s'%s", storageName.string(), fromGzip ? " (from .gz)" : "");
389            if (entry->getCompressionMethod() == ZipEntry::kCompressStored) {
390                printf(" (not compressed)\n");
391            } else {
392                printf(" (compressed %d%%)\n", calcPercent(entry->getUncompressedLen(),
393                            entry->getCompressedLen()));
394            }
395        }
396        entry->setMarked(true);
397    } else {
398        if (result == ALREADY_EXISTS) {
399            fprintf(stderr, "      Unable to add '%s': file already in archive (try '-u'?)\n",
400                    file->getPrintableSource().string());
401        } else {
402            fprintf(stderr, "      Unable to add '%s': Zip add failed\n",
403                    file->getPrintableSource().string());
404        }
405        return false;
406    }
407
408    return true;
409}
410
411/*
412 * Determine whether or not we want to try to compress this file based
413 * on the file extension.
414 */
415bool okayToCompress(Bundle* bundle, const String8& pathName)
416{
417    String8 ext = pathName.getPathExtension();
418    int i;
419
420    if (ext.length() == 0)
421        return true;
422
423    for (i = 0; i < NELEM(kNoCompressExt); i++) {
424        if (strcasecmp(ext.string(), kNoCompressExt[i]) == 0)
425            return false;
426    }
427
428    const android::Vector<const char*>& others(bundle->getNoCompressExtensions());
429    for (i = 0; i < (int)others.size(); i++) {
430        const char* str = others[i];
431        int pos = pathName.length() - strlen(str);
432        if (pos < 0) {
433            continue;
434        }
435        const char* path = pathName.string();
436        if (strcasecmp(path + pos, str) == 0) {
437            return false;
438        }
439    }
440
441    return true;
442}
443
444bool endsWith(const char* haystack, const char* needle)
445{
446    size_t a = strlen(haystack);
447    size_t b = strlen(needle);
448    if (a < b) return false;
449    return strcasecmp(haystack+(a-b), needle) == 0;
450}
451
452ssize_t processJarFile(ZipFile* jar, ZipFile* out)
453{
454    status_t err;
455    size_t N = jar->getNumEntries();
456    size_t count = 0;
457    for (size_t i=0; i<N; i++) {
458        ZipEntry* entry = jar->getEntryByIndex(i);
459        const char* storageName = entry->getFileName();
460        if (endsWith(storageName, ".class")) {
461            int compressionMethod = entry->getCompressionMethod();
462            size_t size = entry->getUncompressedLen();
463            const void* data = jar->uncompress(entry);
464            if (data == NULL) {
465                fprintf(stderr, "ERROR: unable to uncompress entry '%s'\n",
466                    storageName);
467                return -1;
468            }
469            out->add(data, size, storageName, compressionMethod, NULL);
470            free((void*)data);
471        }
472        count++;
473    }
474    return count;
475}
476
477ssize_t processJarFiles(Bundle* bundle, ZipFile* zip)
478{
479    status_t err;
480    ssize_t count = 0;
481    const android::Vector<const char*>& jars = bundle->getJarFiles();
482
483    size_t N = jars.size();
484    for (size_t i=0; i<N; i++) {
485        ZipFile jar;
486        err = jar.open(jars[i], ZipFile::kOpenReadOnly);
487        if (err != 0) {
488            fprintf(stderr, "ERROR: unable to open '%s' as a zip file: %d\n",
489                jars[i], err);
490            return err;
491        }
492        err += processJarFile(&jar, zip);
493        if (err < 0) {
494            fprintf(stderr, "ERROR: unable to process '%s'\n", jars[i]);
495            return err;
496        }
497        count += err;
498    }
499
500    return count;
501}
502