You've already forked KernelSU-Next
mirror of
https://github.com/KernelSU-Next/KernelSU-Next.git
synced 2025-08-27 23:46:34 +00:00
manager: refactor backup and restore paths and logic for modules and allowlist
This commit is contained in:
@@ -443,7 +443,7 @@ fun getFileName(context: Context, uri: Uri): String {
|
|||||||
|
|
||||||
fun moduleBackupDir(): String? {
|
fun moduleBackupDir(): String? {
|
||||||
val shell = getRootShell()
|
val shell = getRootShell()
|
||||||
val baseBackupDir = "/data/adb/ksu/modules_bak"
|
val baseBackupDir = "/sdcard/.ksunext/modules"
|
||||||
val resultBase = ShellUtils.fastCmd(shell, "mkdir -p $baseBackupDir").trim()
|
val resultBase = ShellUtils.fastCmd(shell, "mkdir -p $baseBackupDir").trim()
|
||||||
if (resultBase.isNotEmpty()) return null
|
if (resultBase.isNotEmpty()) return null
|
||||||
|
|
||||||
@@ -462,60 +462,42 @@ fun moduleBackup(): Boolean {
|
|||||||
|
|
||||||
val checkEmptyCommand = "if [ -z \"$(ls -A /data/adb/modules)\" ]; then echo 'empty'; fi"
|
val checkEmptyCommand = "if [ -z \"$(ls -A /data/adb/modules)\" ]; then echo 'empty'; fi"
|
||||||
val resultCheckEmpty = ShellUtils.fastCmd(shell, checkEmptyCommand).trim()
|
val resultCheckEmpty = ShellUtils.fastCmd(shell, checkEmptyCommand).trim()
|
||||||
|
|
||||||
if (resultCheckEmpty == "empty") {
|
if (resultCheckEmpty == "empty") {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val backupDir = moduleBackupDir() ?: return false
|
val timestamp = ShellUtils.fastCmd(shell, "date +%Y%m%d_%H%M%S").trim()
|
||||||
val command = "cp -rp /data/adb/modules/* $backupDir"
|
if (timestamp.isEmpty()) return false
|
||||||
val result = ShellUtils.fastCmd(shell, command).trim()
|
|
||||||
|
|
||||||
return result.isEmpty()
|
val tarName = "modules_backup_$timestamp.tar"
|
||||||
}
|
val tarPath = "/data/local/tmp/$tarName"
|
||||||
|
val internalBackupDir = "/sdcard/.ksunext/modules"
|
||||||
|
val internalBackupPath = "$internalBackupDir/$tarName"
|
||||||
|
|
||||||
fun moduleMigration(): Boolean {
|
val tarCmd = "tar -cpf $tarPath -C /data/adb/modules $(ls /data/adb/modules)"
|
||||||
val shell = getRootShell()
|
val tarResult = ShellUtils.fastCmd(shell, tarCmd).trim()
|
||||||
val command = "cp -rp /data/adb/modules/* /data/adb/modules_update"
|
if (tarResult.isNotEmpty()) return false
|
||||||
val result = ShellUtils.fastCmd(shell, command).trim()
|
|
||||||
|
|
||||||
return result.isEmpty()
|
ShellUtils.fastCmd(shell, "mkdir -p $internalBackupDir")
|
||||||
|
|
||||||
|
val cpResult = ShellUtils.fastCmd(shell, "cp $tarPath $internalBackupPath").trim()
|
||||||
|
if (cpResult.isNotEmpty()) return false
|
||||||
|
|
||||||
|
ShellUtils.fastCmd(shell, "rm -f $tarPath")
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun moduleRestore(): Boolean {
|
fun moduleRestore(): Boolean {
|
||||||
val shell = getRootShell()
|
val shell = getRootShell()
|
||||||
|
|
||||||
val command = "ls -t /data/adb/ksu/modules_bak | head -n 1"
|
val findTarCmd = "ls -t /sdcard/.ksunext/modules/modules_backup_*.tar 2>/dev/null | head -n 1"
|
||||||
val latestBackupDir = ShellUtils.fastCmd(shell, command).trim()
|
val tarPath = ShellUtils.fastCmd(shell, findTarCmd).trim()
|
||||||
|
if (tarPath.isEmpty()) return false
|
||||||
|
|
||||||
if (latestBackupDir.isEmpty()) return false
|
val extractCmd = "tar -xpf $tarPath -C /data/adb/modules_update"
|
||||||
|
val extractResult = ShellUtils.fastCmd(shell, extractCmd).trim()
|
||||||
val sourceDir = "/data/adb/ksu/modules_bak/$latestBackupDir"
|
return extractResult.isEmpty()
|
||||||
val destinationDir = "/data/adb/modules_update"
|
|
||||||
|
|
||||||
val createDestDirCommand = "mkdir -p $destinationDir"
|
|
||||||
ShellUtils.fastCmd(shell, createDestDirCommand)
|
|
||||||
|
|
||||||
val moveCommand = "cp -rp $sourceDir/* $destinationDir"
|
|
||||||
val result = ShellUtils.fastCmd(shell, moveCommand).trim()
|
|
||||||
|
|
||||||
return result.isEmpty()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun allowlistBackupDir(): String? {
|
|
||||||
val shell = getRootShell()
|
|
||||||
val baseBackupDir = "/data/adb/ksu/allowlist_bak"
|
|
||||||
val resultBase = ShellUtils.fastCmd(shell, "mkdir -p $baseBackupDir").trim()
|
|
||||||
if (resultBase.isNotEmpty()) return null
|
|
||||||
|
|
||||||
val timestamp = ShellUtils.fastCmd(shell, "date +%Y%m%d_%H%M%S").trim()
|
|
||||||
if (timestamp.isEmpty()) return null
|
|
||||||
|
|
||||||
val newBackupDir = "$baseBackupDir/$timestamp"
|
|
||||||
val resultNewDir = ShellUtils.fastCmd(shell, "mkdir -p $newBackupDir").trim()
|
|
||||||
|
|
||||||
if (resultNewDir.isEmpty()) return newBackupDir
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun allowlistBackup(): Boolean {
|
fun allowlistBackup(): Boolean {
|
||||||
@@ -523,34 +505,50 @@ fun allowlistBackup(): Boolean {
|
|||||||
|
|
||||||
val checkEmptyCommand = "if [ -z \"$(ls -A /data/adb/ksu/.allowlist)\" ]; then echo 'empty'; fi"
|
val checkEmptyCommand = "if [ -z \"$(ls -A /data/adb/ksu/.allowlist)\" ]; then echo 'empty'; fi"
|
||||||
val resultCheckEmpty = ShellUtils.fastCmd(shell, checkEmptyCommand).trim()
|
val resultCheckEmpty = ShellUtils.fastCmd(shell, checkEmptyCommand).trim()
|
||||||
|
|
||||||
if (resultCheckEmpty == "empty") {
|
if (resultCheckEmpty == "empty") {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val backupDir = allowlistBackupDir() ?: return false
|
val timestamp = ShellUtils.fastCmd(shell, "date +%Y%m%d_%H%M%S").trim()
|
||||||
val command = "cp -rp /data/adb/ksu/.allowlist $backupDir"
|
if (timestamp.isEmpty()) return false
|
||||||
val result = ShellUtils.fastCmd(shell, command).trim()
|
|
||||||
|
|
||||||
return result.isEmpty()
|
val tarName = "allowlist_backup_$timestamp.tar"
|
||||||
|
val tarPath = "/data/local/tmp/$tarName"
|
||||||
|
val internalBackupDir = "/sdcard/.ksunext/allowlist"
|
||||||
|
val internalBackupPath = "$internalBackupDir/$tarName"
|
||||||
|
|
||||||
|
val tarCmd = "tar -cpf $tarPath -C /data/adb/ksu .allowlist"
|
||||||
|
val tarResult = ShellUtils.fastCmd(shell, tarCmd).trim()
|
||||||
|
if (tarResult.isNotEmpty()) return false
|
||||||
|
|
||||||
|
ShellUtils.fastCmd(shell, "mkdir -p $internalBackupDir")
|
||||||
|
|
||||||
|
val cpResult = ShellUtils.fastCmd(shell, "cp $tarPath $internalBackupPath").trim()
|
||||||
|
if (cpResult.isNotEmpty()) return false
|
||||||
|
|
||||||
|
ShellUtils.fastCmd(shell, "rm -f $tarPath")
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun allowlistRestore(): Boolean {
|
fun allowlistRestore(): Boolean {
|
||||||
val shell = getRootShell()
|
val shell = getRootShell()
|
||||||
|
|
||||||
val command = "ls -t /data/adb/ksu/allowlist_bak | head -n 1"
|
// Find the latest allowlist tar backup in /sdcard/.ksunext/allowlist
|
||||||
val latestBackupDir = ShellUtils.fastCmd(shell, command).trim()
|
val findTarCmd = "ls -t /sdcard/.ksunext/allowlist/allowlist_backup_*.tar 2>/dev/null | head -n 1"
|
||||||
|
val tarPath = ShellUtils.fastCmd(shell, findTarCmd).trim()
|
||||||
|
if (tarPath.isEmpty()) return false
|
||||||
|
|
||||||
if (latestBackupDir.isEmpty()) return false
|
// Extract the tar to /data/adb/ksu (restores .allowlist folder with permissions)
|
||||||
|
val extractCmd = "tar -xpf $tarPath -C /data/adb/ksu"
|
||||||
|
val extractResult = ShellUtils.fastCmd(shell, extractCmd).trim()
|
||||||
|
return extractResult.isEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
val sourceDir = "/data/adb/ksu/allowlist_bak/$latestBackupDir"
|
fun moduleMigration(): Boolean {
|
||||||
val destinationDir = "/data/adb/ksu/"
|
val shell = getRootShell()
|
||||||
|
val command = "cp -rp /data/adb/modules/* /data/adb/modules_update"
|
||||||
val createDestDirCommand = "mkdir -p $destinationDir"
|
val result = ShellUtils.fastCmd(shell, command).trim()
|
||||||
ShellUtils.fastCmd(shell, createDestDirCommand)
|
|
||||||
|
|
||||||
val moveCommand = "cp -rp $sourceDir/.allowlist $destinationDir"
|
|
||||||
val result = ShellUtils.fastCmd(shell, moveCommand).trim()
|
|
||||||
|
|
||||||
return result.isEmpty()
|
return result.isEmpty()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user