Zipalign: Don't align directory entries
Directories are entries with uncompressed size zero and ending with
character '/' or '\' are allowed in apks since b/204425803. These
entries should not be considered for alignment since they are not
mmap by the framework.
Test: align_test.cpp
Bug: 250872480
Change-Id: I964aad118a82839f9ed230acc4c2c76f51888c67
diff --git a/tools/zipalign/ZipAlign.cpp b/tools/zipalign/ZipAlign.cpp
index 08f67ff..23840e3 100644
--- a/tools/zipalign/ZipAlign.cpp
+++ b/tools/zipalign/ZipAlign.cpp
@@ -22,6 +22,19 @@
namespace android {
+// An entry is considered a directory if it has a stored size of zero
+// and it ends with '/' or '\' character.
+static bool isDirectory(ZipEntry* entry) {
+ if (entry->getUncompressedLen() != 0) {
+ return false;
+ }
+
+ const char* name = entry->getFileName();
+ size_t nameLength = strlen(name);
+ char lastChar = name[nameLength-1];
+ return lastChar == '/' || lastChar == '\\';
+}
+
static int getAlignment(bool pageAlignSharedLibs, int defaultAlignment,
ZipEntry* pEntry) {
@@ -59,7 +72,7 @@
return 1;
}
- if (pEntry->isCompressed()) {
+ if (pEntry->isCompressed() || isDirectory(pEntry)) {
/* copy the entry without padding */
//printf("--- %s: orig at %ld len=%ld (compressed)\n",
// pEntry->getFileName(), (long) pEntry->getFileOffset(),
@@ -160,7 +173,13 @@
printf("%8jd %s (OK - compressed)\n",
(intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
}
- } else {
+ } else if(isDirectory(pEntry)) {
+ // Directory entries do not need to be aligned.
+ if (verbose)
+ printf("%8jd %s (OK - directory)\n",
+ (intmax_t) pEntry->getFileOffset(), pEntry->getFileName());
+ continue;
+ } else {
off_t offset = pEntry->getFileOffset();
const int alignTo = getAlignment(pageAlignSharedLibs, alignment, pEntry);
if ((offset % alignTo) != 0) {