fix: permission error when using installed app

This commit is contained in:
Ax333l 2023-08-12 14:52:34 +02:00
parent 7aea9473de
commit f6563b265b
2 changed files with 14 additions and 3 deletions

View File

@ -30,7 +30,7 @@ object Aligning {
} }
file.copyEntriesFromFileAligned( file.copyEntriesFromFileAligned(
ZipFile(inputFile), ZipFile(inputFile, readonly = true),
ZipAligner::getEntryAlignment ZipAligner::getEntryAlignment
) )
} }

View File

@ -5,16 +5,17 @@ import app.revanced.manager.patcher.alignment.zip.structures.ZipEntry
import java.io.Closeable import java.io.Closeable
import java.io.File import java.io.File
import java.io.IOException
import java.io.RandomAccessFile import java.io.RandomAccessFile
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.nio.channels.FileChannel import java.nio.channels.FileChannel
import java.util.zip.CRC32 import java.util.zip.CRC32
import java.util.zip.Deflater import java.util.zip.Deflater
class ZipFile(file: File) : Closeable { class ZipFile(file: File, private val readonly: Boolean = false) : Closeable {
var entries: MutableList<ZipEntry> = mutableListOf() var entries: MutableList<ZipEntry> = mutableListOf()
private val filePointer: RandomAccessFile = RandomAccessFile(file, "rw") private val filePointer: RandomAccessFile = RandomAccessFile(file, if (readonly) "r" else "rw")
private var CDNeedsRewrite = false private var CDNeedsRewrite = false
private val compressionLevel = 5 private val compressionLevel = 5
@ -34,6 +35,10 @@ class ZipFile(file: File) : Closeable {
filePointer.seek(0) filePointer.seek(0)
} }
private fun assertWritable() {
if (readonly) throw IOException("Archive is read-only")
}
private fun findEndRecord(): ZipEndRecord { private fun findEndRecord(): ZipEndRecord {
//look from end to start since end record is at the end //look from end to start since end record is at the end
for (i in filePointer.length() - 1 downTo 0) { for (i in filePointer.length() - 1 downTo 0) {
@ -110,6 +115,8 @@ class ZipFile(file: File) : Closeable {
} }
fun addEntryCompressData(entry: ZipEntry, data: ByteArray) { fun addEntryCompressData(entry: ZipEntry, data: ByteArray) {
assertWritable()
val compressor = Deflater(compressionLevel, true) val compressor = Deflater(compressionLevel, true)
compressor.setInput(data) compressor.setInput(data)
compressor.finish() compressor.finish()
@ -136,6 +143,8 @@ class ZipFile(file: File) : Closeable {
} }
private fun addEntryCopyData(entry: ZipEntry, data: ByteBuffer, alignment: Int? = null) { private fun addEntryCopyData(entry: ZipEntry, data: ByteBuffer, alignment: Int? = null) {
assertWritable()
alignment?.let { alignment?.let {
//calculate where data would end up //calculate where data would end up
val dataOffset = filePointer.filePointer + entry.LFHSize val dataOffset = filePointer.filePointer + entry.LFHSize
@ -162,6 +171,8 @@ class ZipFile(file: File) : Closeable {
} }
fun copyEntriesFromFileAligned(file: ZipFile, entryAlignment: (entry: ZipEntry) -> Int?) { fun copyEntriesFromFileAligned(file: ZipFile, entryAlignment: (entry: ZipEntry) -> Int?) {
assertWritable()
for (entry in file.entries) { for (entry in file.entries) {
if (entries.any { it.fileName == entry.fileName }) continue //don't add duplicates if (entries.any { it.fileName == entry.fileName }) continue //don't add duplicates