This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
forked from inotia00/revanced-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revert: feat: remove extra zipalign step
This reverts commit c8e793e.
- Loading branch information
Showing
8 changed files
with
154 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package app.revanced.cli.aligning | ||
|
||
import app.revanced.cli.command.MainCommand.logger | ||
import app.revanced.utils.signing.align.ZipAligner | ||
import java.io.File | ||
|
||
object Aligning { | ||
fun align(inputFile: File, outputFile: File) { | ||
logger.info("Aligning ${inputFile.name} to ${outputFile.name}") | ||
ZipAligner.align(inputFile, outputFile) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/kotlin/app/revanced/utils/filesystem/ZipFileSystemUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package app.revanced.utils.filesystem | ||
|
||
import java.io.Closeable | ||
import java.io.File | ||
import java.nio.file.FileSystems | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.util.zip.ZipEntry | ||
|
||
internal class ZipFileSystemUtils( | ||
file: File | ||
) : Closeable { | ||
private var zipFileSystem = FileSystems.newFileSystem(file.toPath(), mapOf("noCompression" to true)) | ||
|
||
private fun Path.deleteRecursively() { | ||
if (!Files.exists(this)) { | ||
throw IllegalStateException("File exists in real folder but not in zip file system") | ||
} | ||
|
||
if (Files.isDirectory(this)) { | ||
Files.list(this).forEach { path -> | ||
path.deleteRecursively() | ||
} | ||
} | ||
|
||
Files.delete(this) | ||
} | ||
|
||
internal fun getFile(path: String) = zipFileSystem.getPath(path) | ||
|
||
internal fun writePathRecursively(path: Path) { | ||
Files.list(path).use { fileStream -> | ||
fileStream.forEach { filePath -> | ||
val fileSystemPath = filePath.getRelativePath(path) | ||
fileSystemPath.deleteRecursively() | ||
} | ||
} | ||
|
||
Files.walk(path).use { fileStream -> | ||
// don't include build directory | ||
// by skipping the root node. | ||
fileStream.skip(1).forEach { filePath -> | ||
val relativePath = filePath.getRelativePath(path) | ||
|
||
if (Files.isDirectory(filePath)) { | ||
Files.createDirectory(relativePath) | ||
return@forEach | ||
} | ||
|
||
Files.copy(filePath, relativePath) | ||
} | ||
} | ||
} | ||
|
||
internal fun write(path: String, content: ByteArray) = Files.write(zipFileSystem.getPath(path), content) | ||
|
||
private fun Path.getRelativePath(path: Path): Path = zipFileSystem.getPath(path.relativize(this).toString()) | ||
|
||
// TODO: figure out why the file system is uncompressed by default and how to fix it | ||
internal fun uncompress(vararg paths: String) = | ||
paths.forEach { Files.setAttribute(zipFileSystem.getPath(it), "zip:method", ZipEntry.STORED) } | ||
|
||
override fun close() = zipFileSystem.close() | ||
} |
23 changes: 20 additions & 3 deletions
23
src/main/kotlin/app/revanced/utils/signing/align/ZipAligner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
package app.revanced.utils.signing.align | ||
|
||
import app.revanced.utils.signing.align.zip.structures.ZipEntry | ||
import app.revanced.utils.signing.align.zip.ZipFile | ||
import java.io.File | ||
|
||
internal object ZipAligner { | ||
private const val DEFAULT_ALIGNMENT = 4 | ||
private const val LIBRARY_ALIGNMENT = 4096 | ||
|
||
fun getEntryAlignment(entry: ZipEntry): Int? = | ||
if (entry.compression.toUInt() != 0u) null else if (entry.fileName.endsWith(".so")) LIBRARY_ALIGNMENT else DEFAULT_ALIGNMENT | ||
fun align(input: File, output: File) { | ||
val inputZip = ZipFile(input) | ||
val outputZip = ZipFile(output) | ||
|
||
for (entry in inputZip.entries) { | ||
val data = inputZip.getDataForEntry(entry) | ||
|
||
if (entry.compression == 0.toUShort()) { | ||
val alignment = if (entry.fileName.endsWith(".so")) LIBRARY_ALIGNMENT else DEFAULT_ALIGNMENT | ||
|
||
outputZip.addEntryAligned(entry, data, alignment) | ||
} else { | ||
outputZip.addEntry(entry, data) | ||
} | ||
} | ||
|
||
outputZip.finish() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.