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