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