Modified Kpm file type validation and check module ID extraction logic

Fix the problem that the specified kpm module could not be deleted after uninstallation due to a mismatch between the file type and the actual module name.

Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
This commit is contained in:
ShirkNeko
2025-04-23 03:33:46 +08:00
parent 9ea6de340d
commit cc0b272770
2 changed files with 59 additions and 22 deletions

View File

@@ -162,15 +162,45 @@ fun KpmScreen(
} }
} }
if (!tempFile.name.endsWith(".kpm")) { val mimeType = context.contentResolver.getType(uri)
snackBarHost.showSnackbar( val isCorrectMimeType = mimeType == null || mimeType.contains("application/octet-stream")
message = invalidFileTypeMessage,
duration = SnackbarDuration.Short if (!isCorrectMimeType) {
) var shouldShowSnackbar = true
try {
val command = arrayOf("su", "-c", "strings ${tempFile.absolutePath} | grep -E 'name=|version=|license=|author='")
val process = Runtime.getRuntime().exec(command)
val inputStream = process.inputStream
val reader = java.io.BufferedReader(java.io.InputStreamReader(inputStream))
var line: String?
var matchCount = 0
val keywords = listOf("name=", "version=", "license=", "author=")
while (reader.readLine().also { line = it } != null) {
for (keyword in keywords) {
if (line!!.startsWith(keyword)) {
matchCount++
break
}
}
}
process.waitFor()
if (matchCount < 2) {
shouldShowSnackbar = true
} else {
shouldShowSnackbar = false
}
} catch (e: Exception) {
Log.e("KsuCli", "Failed to execute strings command: ${e.message}", e)
}
if (shouldShowSnackbar) {
snackBarHost.showSnackbar(
message = invalidFileTypeMessage,
duration = SnackbarDuration.Short
)
}
tempFile.delete() tempFile.delete()
return@launch return@launch
} }
tempFileForInstall = tempFile tempFileForInstall = tempFile
installModeDialog.show() installModeDialog.show()
} }
@@ -209,7 +239,7 @@ fun KpmScreen(
onClick = { onClick = {
selectPatchLauncher.launch( selectPatchLauncher.launch(
Intent(Intent.ACTION_GET_CONTENT).apply { Intent(Intent.ACTION_GET_CONTENT).apply {
type = "*/*" type = "application/octet-stream"
} }
) )
}, },
@@ -307,8 +337,25 @@ private suspend fun handleModuleInstall(
kpmInstallSuccess: String, kpmInstallSuccess: String,
kpmInstallFailed: String kpmInstallFailed: String
) { ) {
val moduleId = extractModuleId(tempFile.name) var moduleId: String? = null
if (moduleId == null) { try {
val command = arrayOf("su", "-c", "strings ${tempFile.absolutePath} | grep 'name='")
val process = Runtime.getRuntime().exec(command)
val inputStream = process.inputStream
val reader = java.io.BufferedReader(java.io.InputStreamReader(inputStream))
var line: String?
while (reader.readLine().also { line = it } != null) {
if (line!!.startsWith("name=")) {
moduleId = line.substringAfter("name=").trim()
break
}
}
process.waitFor()
} catch (e: Exception) {
Log.e("KsuCli", "Failed to get module ID from strings command: ${e.message}", e)
}
if (moduleId == null || moduleId.isEmpty()) {
Log.e("KsuCli", "Failed to extract module ID from file: ${tempFile.name}") Log.e("KsuCli", "Failed to extract module ID from file: ${tempFile.name}")
snackBarHost.showSnackbar( snackBarHost.showSnackbar(
message = kpmInstallFailed, message = kpmInstallFailed,
@@ -350,18 +397,6 @@ private suspend fun handleModuleInstall(
tempFile.delete() tempFile.delete()
} }
private fun extractModuleId(fileName: String): String? {
return try {
val decodedFileName = URLDecoder.decode(fileName, "UTF-8")
val pattern = "([^/]*?)\\.kpm$".toRegex()
val matchResult = pattern.find(decodedFileName)
matchResult?.groupValues?.get(1)
} catch (e: Exception) {
Log.e("KsuCli", "Failed to extract module ID: ${e.message}", e)
null
}
}
private suspend fun handleModuleUninstall( private suspend fun handleModuleUninstall(
module: KpmViewModel.ModuleInfo, module: KpmViewModel.ModuleInfo,
viewModel: KpmViewModel, viewModel: KpmViewModel,

View File

@@ -1,3 +1,5 @@
libzakozako.so libzakozako.so
libzakozakozako.so libzakozakozako.so
libkpmmgr.so libkpmmgr.so
libzako.so
libandroidx.graphics.path.so