]>
git.saurik.com Git - android/aapt.git/blob - Package.cpp
2 // Copyright 2006 The Android Open Source Project
4 // Package assets into Zip files.
7 #include "AaptAssets.h"
8 #include "ResourceTable.h"
10 #include <utils/Log.h>
11 #include <utils/threads.h>
12 #include <utils/List.h>
13 #include <utils/Errors.h>
14 #include <utils/ZipFile.h>
16 #include <sys/types.h>
21 using namespace android
;
23 static const char* kExcludeExtension
= ".EXCLUDE";
25 /* these formats are already compressed, or don't compress well */
26 static const char* kNoCompressExt
[] = {
27 ".jpg", ".jpeg", ".png", ".gif",
28 ".wav", ".mp2", ".mp3", ".ogg", ".aac",
29 ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
30 ".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
31 ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
32 ".amr", ".awb", ".wma", ".wmv"
35 /* fwd decls, so I can write this downward */
36 ssize_t
processAssets(Bundle
* bundle
, ZipFile
* zip
, const sp
<AaptAssets
>& assets
);
37 ssize_t
processAssets(Bundle
* bundle
, ZipFile
* zip
,
38 const sp
<AaptDir
>& dir
, const AaptGroupEntry
& ge
);
39 bool processFile(Bundle
* bundle
, ZipFile
* zip
,
40 const sp
<AaptGroup
>& group
, const sp
<AaptFile
>& file
);
41 bool okayToCompress(Bundle
* bundle
, const String8
& pathName
);
42 ssize_t
processJarFiles(Bundle
* bundle
, ZipFile
* zip
);
45 * The directory hierarchy looks like this:
46 * "outputDir" and "assetRoot" are existing directories.
48 * On success, "bundle->numPackages" will be the number of Zip packages
51 status_t
writeAPK(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
52 const String8
& outputFile
)
54 status_t result
= NO_ERROR
;
58 //bundle->setPackageCount(0);
61 * Prep the Zip archive.
63 * If the file already exists, fail unless "update" or "force" is set.
64 * If "update" is set, update the contents of the existing archive.
65 * Else, if "force" is set, remove the existing archive.
67 FileType fileType
= getFileType(outputFile
.string());
68 if (fileType
== kFileTypeNonexistent
) {
69 // okay, create it below
70 } else if (fileType
== kFileTypeRegular
) {
71 if (bundle
->getUpdate()) {
72 // okay, open it below
73 } else if (bundle
->getForce()) {
74 if (unlink(outputFile
.string()) != 0) {
75 fprintf(stderr
, "ERROR: unable to remove '%s': %s\n", outputFile
.string(),
80 fprintf(stderr
, "ERROR: '%s' exists (use '-f' to force overwrite)\n",
85 fprintf(stderr
, "ERROR: '%s' exists and is not a regular file\n", outputFile
.string());
89 if (bundle
->getVerbose()) {
90 printf("%s '%s'\n", (fileType
== kFileTypeNonexistent
) ? "Creating" : "Opening",
96 status
= zip
->open(outputFile
.string(), ZipFile::kOpenReadWrite
| ZipFile::kOpenCreate
);
97 if (status
!= NO_ERROR
) {
98 fprintf(stderr
, "ERROR: unable to open '%s' as Zip file for writing\n",
103 if (bundle
->getVerbose()) {
104 printf("Writing all files...\n");
107 count
= processAssets(bundle
, zip
, assets
);
109 fprintf(stderr
, "ERROR: unable to process assets while packaging '%s'\n",
110 outputFile
.string());
115 if (bundle
->getVerbose()) {
116 printf("Generated %d file%s\n", count
, (count
==1) ? "" : "s");
119 count
= processJarFiles(bundle
, zip
);
121 fprintf(stderr
, "ERROR: unable to process jar files while packaging '%s'\n",
122 outputFile
.string());
127 if (bundle
->getVerbose())
128 printf("Included %d file%s from jar/zip files.\n", count
, (count
==1) ? "" : "s");
133 * Check for cruft. We set the "marked" flag on all entries we created
134 * or decided not to update. If the entry isn't already slated for
135 * deletion, remove it now.
138 if (bundle
->getVerbose())
139 printf("Checking for deleted files\n");
141 for (i
= 0; i
< zip
->getNumEntries(); i
++) {
142 ZipEntry
* entry
= zip
->getEntryByIndex(i
);
144 if (!entry
->getMarked() && entry
->getDeleted()) {
145 if (bundle
->getVerbose()) {
146 printf(" (removing crufty '%s')\n",
147 entry
->getFileName());
153 if (bundle
->getVerbose() && removed
> 0)
154 printf("Removed %d file%s\n", removed
, (removed
==1) ? "" : "s");
157 /* tell Zip lib to process deletions and other pending changes */
158 result
= zip
->flush();
159 if (result
!= NO_ERROR
) {
160 fprintf(stderr
, "ERROR: Zip flush failed, archive may be hosed\n");
165 if (zip
->getNumEntries() == 0) {
166 if (bundle
->getVerbose()) {
167 printf("Archive is empty -- removing %s\n", outputFile
.getPathLeaf().string());
169 delete zip
; // close the file so we can remove it in Win32
171 if (unlink(outputFile
.string()) != 0) {
172 fprintf(stderr
, "WARNING: could not unlink '%s'\n", outputFile
.string());
176 assert(result
== NO_ERROR
);
179 delete zip
; // must close before remove in Win32
180 if (result
!= NO_ERROR
) {
181 if (bundle
->getVerbose()) {
182 printf("Removing %s due to earlier failures\n", outputFile
.string());
184 if (unlink(outputFile
.string()) != 0) {
185 fprintf(stderr
, "WARNING: could not unlink '%s'\n", outputFile
.string());
189 if (result
== NO_ERROR
&& bundle
->getVerbose())
194 ssize_t
processAssets(Bundle
* bundle
, ZipFile
* zip
,
195 const sp
<AaptAssets
>& assets
)
197 ResourceFilter filter
;
198 status_t status
= filter
.parse(bundle
->getConfigurations());
199 if (status
!= NO_ERROR
) {
205 const size_t N
= assets
->getGroupEntries().size();
206 for (size_t i
=0; i
<N
; i
++) {
207 const AaptGroupEntry
& ge
= assets
->getGroupEntries()[i
];
208 if (!filter
.match(ge
.toParams())) {
211 ssize_t res
= processAssets(bundle
, zip
, assets
, ge
);
221 ssize_t
processAssets(Bundle
* bundle
, ZipFile
* zip
,
222 const sp
<AaptDir
>& dir
, const AaptGroupEntry
& ge
)
226 const size_t ND
= dir
->getDirs().size();
228 for (i
=0; i
<ND
; i
++) {
229 ssize_t res
= processAssets(bundle
, zip
, dir
->getDirs().valueAt(i
), ge
);
236 const size_t NF
= dir
->getFiles().size();
237 for (i
=0; i
<NF
; i
++) {
238 sp
<AaptGroup
> gp
= dir
->getFiles().valueAt(i
);
239 ssize_t fi
= gp
->getFiles().indexOfKey(ge
);
241 sp
<AaptFile
> fl
= gp
->getFiles().valueAt(fi
);
242 if (!processFile(bundle
, zip
, gp
, fl
)) {
243 return UNKNOWN_ERROR
;
253 * Process a regular file, adding it to the archive if appropriate.
255 * If we're in "update" mode, and the file already exists in the archive,
256 * delete the existing entry before adding the new one.
258 bool processFile(Bundle
* bundle
, ZipFile
* zip
,
259 const sp
<AaptGroup
>& group
, const sp
<AaptFile
>& file
)
261 const bool hasData
= file
->hasData();
263 String8
storageName(group
->getPath());
264 storageName
.convertToResPath();
266 bool fromGzip
= false;
270 * See if the filename ends in ".EXCLUDE". We can't use
271 * String8::getPathExtension() because the length of what it considers
272 * to be an extension is capped.
274 * The Asset Manager doesn't check for ".EXCLUDE" in Zip archives,
275 * so there's no value in adding them (and it makes life easier on
276 * the AssetManager lib if we don't).
278 * NOTE: this restriction has been removed. If you're in this code, you
279 * should clean this up, but I'm in here getting rid of Path Name, and I
280 * don't want to make other potentially breaking changes --joeo
282 int fileNameLen
= storageName
.length();
283 int excludeExtensionLen
= strlen(kExcludeExtension
);
284 if (fileNameLen
> excludeExtensionLen
285 && (0 == strcmp(storageName
.string() + (fileNameLen
- excludeExtensionLen
),
286 kExcludeExtension
))) {
287 fprintf(stderr
, "WARNING: '%s' not added to Zip\n", storageName
.string());
291 if (strcasecmp(storageName
.getPathExtension().string(), ".gz") == 0) {
293 storageName
= storageName
.getBasePath();
296 if (bundle
->getUpdate()) {
297 entry
= zip
->getEntryByName(storageName
.string());
299 /* file already exists in archive; there can be only one */
300 if (entry
->getMarked()) {
302 "ERROR: '%s' exists twice (check for with & w/o '.gz'?)\n",
303 file
->getPrintableSource().string());
307 const String8
& srcName
= file
->getSourceFile();
309 fileModWhen
= getFileModDate(srcName
.string());
310 if (fileModWhen
== (time_t) -1) { // file existence tested earlier,
311 return false; // not expecting an error here
314 if (fileModWhen
> entry
->getModWhen()) {
315 // mark as deleted so add() will succeed
316 if (bundle
->getVerbose()) {
317 printf(" (removing old '%s')\n", storageName
.string());
322 // version in archive is newer
323 if (bundle
->getVerbose()) {
324 printf(" (not updating '%s')\n", storageName
.string());
326 entry
->setMarked(true);
330 // Generated files are always replaced.
336 //android_setMinPriority(NULL, ANDROID_LOG_VERBOSE);
339 result
= zip
->addGzip(file
->getSourceFile().string(), storageName
.string(), &entry
);
340 } else if (!hasData
) {
341 /* don't compress certain files, e.g. PNGs */
342 int compressionMethod
= bundle
->getCompressionMethod();
343 if (!okayToCompress(bundle
, storageName
)) {
344 compressionMethod
= ZipEntry::kCompressStored
;
346 result
= zip
->add(file
->getSourceFile().string(), storageName
.string(), compressionMethod
,
349 result
= zip
->add(file
->getData(), file
->getSize(), storageName
.string(),
350 file
->getCompressionMethod(), &entry
);
352 if (result
== NO_ERROR
) {
353 if (bundle
->getVerbose()) {
354 printf(" '%s'%s", storageName
.string(), fromGzip
? " (from .gz)" : "");
355 if (entry
->getCompressionMethod() == ZipEntry::kCompressStored
) {
356 printf(" (not compressed)\n");
358 printf(" (compressed %d%%)\n", calcPercent(entry
->getUncompressedLen(),
359 entry
->getCompressedLen()));
362 entry
->setMarked(true);
364 if (result
== ALREADY_EXISTS
) {
365 fprintf(stderr
, " Unable to add '%s': file already in archive (try '-u'?)\n",
366 file
->getPrintableSource().string());
368 fprintf(stderr
, " Unable to add '%s': Zip add failed\n",
369 file
->getPrintableSource().string());
378 * Determine whether or not we want to try to compress this file based
379 * on the file extension.
381 bool okayToCompress(Bundle
* bundle
, const String8
& pathName
)
383 String8 ext
= pathName
.getPathExtension();
386 if (ext
.length() == 0)
389 for (i
= 0; i
< NELEM(kNoCompressExt
); i
++) {
390 if (strcasecmp(ext
.string(), kNoCompressExt
[i
]) == 0)
394 const android::Vector
<const char*>& others(bundle
->getNoCompressExtensions());
395 for (i
= 0; i
< (int)others
.size(); i
++) {
396 const char* str
= others
[i
];
397 int pos
= pathName
.length() - strlen(str
);
401 const char* path
= pathName
.string();
402 if (strcasecmp(path
+ pos
, str
) == 0) {
410 bool endsWith(const char* haystack
, const char* needle
)
412 size_t a
= strlen(haystack
);
413 size_t b
= strlen(needle
);
414 if (a
< b
) return false;
415 return strcasecmp(haystack
+(a
-b
), needle
) == 0;
418 ssize_t
processJarFile(ZipFile
* jar
, ZipFile
* out
)
421 size_t N
= jar
->getNumEntries();
423 for (size_t i
=0; i
<N
; i
++) {
424 ZipEntry
* entry
= jar
->getEntryByIndex(i
);
425 const char* storageName
= entry
->getFileName();
426 if (endsWith(storageName
, ".class")) {
427 int compressionMethod
= entry
->getCompressionMethod();
428 size_t size
= entry
->getUncompressedLen();
429 const void* data
= jar
->uncompress(entry
);
431 fprintf(stderr
, "ERROR: unable to uncompress entry '%s'\n",
435 out
->add(data
, size
, storageName
, compressionMethod
, NULL
);
443 ssize_t
processJarFiles(Bundle
* bundle
, ZipFile
* zip
)
447 const android::Vector
<const char*>& jars
= bundle
->getJarFiles();
449 size_t N
= jars
.size();
450 for (size_t i
=0; i
<N
; i
++) {
452 err
= jar
.open(jars
[i
], ZipFile::kOpenReadOnly
);
454 fprintf(stderr
, "ERROR: unable to open '%s' as a zip file: %zd\n",
458 err
+= processJarFile(&jar
, zip
);
460 fprintf(stderr
, "ERROR: unable to process '%s'\n", jars
[i
]);