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