Skip to content

Commit

Permalink
fix: permission error when using installed app
Browse files Browse the repository at this point in the history
  • Loading branch information
Axelen123 committed Aug 12, 2023
1 parent 7aea947 commit f6563b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/app/revanced/manager/patcher/Aligning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Aligning {
}

file.copyEntriesFromFileAligned(
ZipFile(inputFile),
ZipFile(inputFile, readonly = true),
ZipAligner::getEntryAlignment
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import app.revanced.manager.patcher.alignment.zip.structures.ZipEntry

import java.io.Closeable
import java.io.File
import java.io.IOException
import java.io.RandomAccessFile
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.util.zip.CRC32
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()

private val filePointer: RandomAccessFile = RandomAccessFile(file, "rw")
private val filePointer: RandomAccessFile = RandomAccessFile(file, if (readonly) "r" else "rw")
private var CDNeedsRewrite = false

private val compressionLevel = 5
Expand All @@ -34,6 +35,10 @@ class ZipFile(file: File) : Closeable {
filePointer.seek(0)
}

private fun assertWritable() {
if (readonly) throw IOException("Archive is read-only")
}

private fun findEndRecord(): ZipEndRecord {
//look from end to start since end record is at the end
for (i in filePointer.length() - 1 downTo 0) {
Expand Down Expand Up @@ -110,6 +115,8 @@ class ZipFile(file: File) : Closeable {
}

fun addEntryCompressData(entry: ZipEntry, data: ByteArray) {
assertWritable()

val compressor = Deflater(compressionLevel, true)
compressor.setInput(data)
compressor.finish()
Expand All @@ -136,6 +143,8 @@ class ZipFile(file: File) : Closeable {
}

private fun addEntryCopyData(entry: ZipEntry, data: ByteBuffer, alignment: Int? = null) {
assertWritable()

alignment?.let {
//calculate where data would end up
val dataOffset = filePointer.filePointer + entry.LFHSize
Expand All @@ -162,6 +171,8 @@ class ZipFile(file: File) : Closeable {
}

fun copyEntriesFromFileAligned(file: ZipFile, entryAlignment: (entry: ZipEntry) -> Int?) {
assertWritable()

for (entry in file.entries) {
if (entries.any { it.fileName == entry.fileName }) continue //don't add duplicates

Expand Down

0 comments on commit f6563b2

Please sign in to comment.