]>
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>
15 #include <sys/types.h>
20 using namespace android
;
22 static const char* kExcludeExtension
= ".EXCLUDE";
24 /* these formats are already compressed, or don't compress well */
25 static 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"
34 /* fwd decls, so I can write this downward */
35 ssize_t
processAssets(Bundle
* bundle
, ZipFile
* zip
, const sp
<AaptAssets
>& assets
);
36 ssize_t
processAssets(Bundle
* bundle
, ZipFile
* zip
,
37 const sp
<AaptDir
>& dir
, const AaptGroupEntry
& ge
);
38 bool processFile(Bundle
* bundle
, ZipFile
* zip
,
39 const sp
<AaptGroup
>& group
, const sp
<AaptFile
>& file
);
40 bool okayToCompress(Bundle
* bundle
, const String8
& pathName
);
41 ssize_t
processJarFiles(Bundle
* bundle
, ZipFile
* zip
);
44 * The directory hierarchy looks like this:
45 * "outputDir" and "assetRoot" are existing directories.
47 * On success, "bundle->numPackages" will be the number of Zip packages
50 status_t
writeAPK(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
51 const String8
& outputFile
)
53 status_t result
= NO_ERROR
;
57 //bundle->setPackageCount(0);
60 * Prep the Zip archive.
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.
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(),
79 fprintf(stderr
, "ERROR: '%s' exists (use '-f' to force overwrite)\n",
84 fprintf(stderr
, "ERROR: '%s' exists and is not a regular file\n", outputFile
.string());
88 if (bundle
->getVerbose()) {
89 printf("%s '%s'\n", (fileType
== kFileTypeNonexistent
) ? "Creating" : "Opening",
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",
102 if (bundle
->getVerbose()) {
103 printf("Writing all files...\n");
106 count
= processAssets(bundle
, zip
, assets
);
108 fprintf(stderr
, "ERROR: unable to process assets while packaging '%s'\n",
109 outputFile
.string());
114 if (bundle
->getVerbose()) {
115 printf("Generated %d file%s\n", count
, (count
==1) ? "" : "s");
118 count
= processJarFiles(bundle
, zip
);
120 fprintf(stderr
, "ERROR: unable to process jar files while packaging '%s'\n",
121 outputFile
.string());
126 if (bundle
->getVerbose())
127 printf("Included %d file%s from jar/zip files.\n", count
, (count
==1) ? "" : "s");
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.
137 if (bundle
->getVerbose())
138 printf("Checking for deleted files\n");
140 for (i
= 0; i
< zip
->getNumEntries(); i
++) {
141 ZipEntry
* entry
= zip
->getEntryByIndex(i
);
143 if (!entry
->getMarked() && entry
->getDeleted()) {
144 if (bundle
->getVerbose()) {
145 printf(" (removing crufty '%s')\n",
146 entry
->getFileName());
152 if (bundle
->getVerbose() && removed
> 0)
153 printf("Removed %d file%s\n", removed
, (removed
==1) ? "" : "s");
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");
164 if (zip
->getNumEntries() == 0) {
165 if (bundle
->getVerbose()) {
166 printf("Archive is empty -- removing %s\n", outputFile
.getPathLeaf().string());
168 delete zip
; // close the file so we can remove it in Win32
170 if (unlink(outputFile
.string()) != 0) {
171 fprintf(stderr
, "warning: could not unlink '%s'\n", outputFile
.string());
175 assert(result
== NO_ERROR
);
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());
183 if (unlink(outputFile
.string()) != 0) {
184 fprintf(stderr
, "warning: could not unlink '%s'\n", outputFile
.string());
188 if (result
== NO_ERROR
&& bundle
->getVerbose())
193 ssize_t
processAssets(Bundle
* bundle
, ZipFile
* zip
,
194 const sp
<AaptAssets
>& assets
)
196 ResourceFilter filter
;
197 status_t status
= filter
.parse(bundle
->getConfigurations());
198 if (status
!= NO_ERROR
) {
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 if (!filter
.match(ge
.toParams())) {
210 ssize_t res
= processAssets(bundle
, zip
, assets
, ge
);
220 ssize_t
processAssets(Bundle
* bundle
, ZipFile
* zip
,
221 const sp
<AaptDir
>& dir
, const AaptGroupEntry
& ge
)
225 const size_t ND
= dir
->getDirs().size();
227 for (i
=0; i
<ND
; i
++) {
228 ssize_t res
= processAssets(bundle
, zip
, dir
->getDirs().valueAt(i
), ge
);
235 const size_t NF
= dir
->getFiles().size();
236 for (i
=0; i
<NF
; i
++) {
237 sp
<AaptGroup
> gp
= dir
->getFiles().valueAt(i
);
238 ssize_t fi
= gp
->getFiles().indexOfKey(ge
);
240 sp
<AaptFile
> fl
= gp
->getFiles().valueAt(fi
);
241 if (!processFile(bundle
, zip
, gp
, fl
)) {
242 return UNKNOWN_ERROR
;
252 * Process a regular file, adding it to the archive if appropriate.
254 * If we're in "update" mode, and the file already exists in the archive,
255 * delete the existing entry before adding the new one.
257 bool processFile(Bundle
* bundle
, ZipFile
* zip
,
258 const sp
<AaptGroup
>& group
, const sp
<AaptFile
>& file
)
260 const bool hasData
= file
->hasData();
262 String8
storageName(group
->getPath());
263 storageName
.convertToResPath();
265 bool fromGzip
= false;
269 * See if the filename ends in ".EXCLUDE". We can't use
270 * String8::getPathExtension() because the length of what it considers
271 * to be an extension is capped.
273 * The Asset Manager doesn't check for ".EXCLUDE" in Zip archives,
274 * so there's no value in adding them (and it makes life easier on
275 * the AssetManager lib if we don't).
277 * NOTE: this restriction has been removed. If you're in this code, you
278 * should clean this up, but I'm in here getting rid of Path Name, and I
279 * don't want to make other potentially breaking changes --joeo
281 int fileNameLen
= storageName
.length();
282 int excludeExtensionLen
= strlen(kExcludeExtension
);
283 if (fileNameLen
> excludeExtensionLen
284 && (0 == strcmp(storageName
.string() + (fileNameLen
- excludeExtensionLen
),
285 kExcludeExtension
))) {
286 fprintf(stderr
, "warning: '%s' not added to Zip\n", storageName
.string());
290 if (strcasecmp(storageName
.getPathExtension().string(), ".gz") == 0) {
292 storageName
= storageName
.getBasePath();
295 if (bundle
->getUpdate()) {
296 entry
= zip
->getEntryByName(storageName
.string());
298 /* file already exists in archive; there can be only one */
299 if (entry
->getMarked()) {
301 "ERROR: '%s' exists twice (check for with & w/o '.gz'?)\n",
302 file
->getPrintableSource().string());
306 const String8
& srcName
= file
->getSourceFile();
308 fileModWhen
= getFileModDate(srcName
.string());
309 if (fileModWhen
== (time_t) -1) { // file existence tested earlier,
310 return false; // not expecting an error here
313 if (fileModWhen
> entry
->getModWhen()) {
314 // mark as deleted so add() will succeed
315 if (bundle
->getVerbose()) {
316 printf(" (removing old '%s')\n", storageName
.string());
321 // version in archive is newer
322 if (bundle
->getVerbose()) {
323 printf(" (not updating '%s')\n", storageName
.string());
325 entry
->setMarked(true);
329 // Generated files are always replaced.
335 //android_setMinPriority(NULL, ANDROID_LOG_VERBOSE);
338 result
= zip
->addGzip(file
->getSourceFile().string(), storageName
.string(), &entry
);
339 } else if (!hasData
) {
340 /* don't compress certain files, e.g. PNGs */
341 int compressionMethod
= bundle
->getCompressionMethod();
342 if (!okayToCompress(bundle
, storageName
)) {
343 compressionMethod
= ZipEntry::kCompressStored
;
345 result
= zip
->add(file
->getSourceFile().string(), storageName
.string(), compressionMethod
,
348 result
= zip
->add(file
->getData(), file
->getSize(), storageName
.string(),
349 file
->getCompressionMethod(), &entry
);
351 if (result
== NO_ERROR
) {
352 if (bundle
->getVerbose()) {
353 printf(" '%s'%s", storageName
.string(), fromGzip
? " (from .gz)" : "");
354 if (entry
->getCompressionMethod() == ZipEntry::kCompressStored
) {
355 printf(" (not compressed)\n");
357 printf(" (compressed %d%%)\n", calcPercent(entry
->getUncompressedLen(),
358 entry
->getCompressedLen()));
361 entry
->setMarked(true);
363 if (result
== ALREADY_EXISTS
) {
364 fprintf(stderr
, " Unable to add '%s': file already in archive (try '-u'?)\n",
365 file
->getPrintableSource().string());
367 fprintf(stderr
, " Unable to add '%s': Zip add failed\n",
368 file
->getPrintableSource().string());
377 * Determine whether or not we want to try to compress this file based
378 * on the file extension.
380 bool okayToCompress(Bundle
* bundle
, const String8
& pathName
)
382 String8 ext
= pathName
.getPathExtension();
385 if (ext
.length() == 0)
388 for (i
= 0; i
< NELEM(kNoCompressExt
); i
++) {
389 if (strcasecmp(ext
.string(), kNoCompressExt
[i
]) == 0)
393 const android::Vector
<const char*>& others(bundle
->getNoCompressExtensions());
394 for (i
= 0; i
< (int)others
.size(); i
++) {
395 const char* str
= others
[i
];
396 int pos
= pathName
.length() - strlen(str
);
400 const char* path
= pathName
.string();
401 if (strcasecmp(path
+ pos
, str
) == 0) {
409 bool endsWith(const char* haystack
, const char* needle
)
411 size_t a
= strlen(haystack
);
412 size_t b
= strlen(needle
);
413 if (a
< b
) return false;
414 return strcasecmp(haystack
+(a
-b
), needle
) == 0;
417 ssize_t
processJarFile(ZipFile
* jar
, ZipFile
* out
)
420 size_t N
= jar
->getNumEntries();
422 for (size_t i
=0; i
<N
; i
++) {
423 ZipEntry
* entry
= jar
->getEntryByIndex(i
);
424 const char* storageName
= entry
->getFileName();
425 if (endsWith(storageName
, ".class")) {
426 int compressionMethod
= entry
->getCompressionMethod();
427 size_t size
= entry
->getUncompressedLen();
428 const void* data
= jar
->uncompress(entry
);
430 fprintf(stderr
, "ERROR: unable to uncompress entry '%s'\n",
434 out
->add(data
, size
, storageName
, compressionMethod
, NULL
);
442 ssize_t
processJarFiles(Bundle
* bundle
, ZipFile
* zip
)
446 const android::Vector
<const char*>& jars
= bundle
->getJarFiles();
448 size_t N
= jars
.size();
449 for (size_t i
=0; i
<N
; i
++) {
451 err
= jar
.open(jars
[i
], ZipFile::kOpenReadOnly
);
453 fprintf(stderr
, "ERROR: unable to open '%s' as a zip file: %zd\n",
457 err
+= processJarFile(&jar
, zip
);
459 fprintf(stderr
, "ERROR: unable to process '%s'\n", jars
[i
]);