Skip to content

Commit

Permalink
excludeCache -> backupCache, restoreCache, use positive logic for con…
Browse files Browse the repository at this point in the history
…sistency
  • Loading branch information
hg42 committed Jul 12, 2024
1 parent 87bd51c commit c7b1282
Show file tree
Hide file tree
Showing 45 changed files with 251 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import com.machiav3lli.backup.items.ActionResult
import com.machiav3lli.backup.items.Package
import com.machiav3lli.backup.items.RootFile
import com.machiav3lli.backup.items.StorageFile
import com.machiav3lli.backup.preferences.pref_backupCache
import com.machiav3lli.backup.preferences.pref_backupPauseApps
import com.machiav3lli.backup.preferences.pref_backupTarCmd
import com.machiav3lli.backup.preferences.pref_excludeCache
import com.machiav3lli.backup.preferences.pref_fakeBackupSeconds
import com.machiav3lli.backup.tasks.AppActionWork
import com.machiav3lli.backup.utils.CIPHER_ALGORITHM
Expand Down Expand Up @@ -344,7 +344,7 @@ open class BackupAppAction(context: Context, work: AppActionWork?, shell: ShellH
private fun assembleFileList(sourcePath: String): List<ShellHandler.FileInfo> {
// get and filter the whole tree at once //TODO use iterator instead of list
return try {
val excludeCache = pref_excludeCache.value
val excludeCache = ! pref_backupCache.value
val allFilesToBackup =
shell.suGetDetailedDirectoryContents(sourcePath, true, sourcePath)
.filterNot { f: ShellHandler.FileInfo -> f.filename in OABX.assets.DATA_BACKUP_EXCLUDED_BASENAMES } //TODO basenames! not all levels
Expand Down Expand Up @@ -431,7 +431,7 @@ open class BackupAppAction(context: Context, work: AppActionWork?, shell: ShellH

var options = ""
options += " --exclude ${quote(ShellHandler.BACKUP_EXCLUDE_FILE)}"
if (pref_excludeCache.value) {
if (! pref_backupCache.value) {
options += " --exclude ${quote(ShellHandler.EXCLUDE_CACHE_FILE)}"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ import com.machiav3lli.backup.items.RootFile
import com.machiav3lli.backup.items.StorageFile
import com.machiav3lli.backup.preferences.pref_delayBeforeRefreshAppInfo
import com.machiav3lli.backup.preferences.pref_enableSessionInstaller
import com.machiav3lli.backup.preferences.pref_excludeCache
import com.machiav3lli.backup.preferences.pref_installationPackage
import com.machiav3lli.backup.preferences.pref_refreshAppInfoTimeout
import com.machiav3lli.backup.preferences.pref_restoreAvoidTemporaryCopy
import com.machiav3lli.backup.preferences.pref_restoreCache
import com.machiav3lli.backup.preferences.pref_restoreKillApps
import com.machiav3lli.backup.preferences.pref_restorePermissions
import com.machiav3lli.backup.preferences.pref_restoreTarCmd
Expand Down Expand Up @@ -95,12 +95,12 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
}
try {
val backupDir = backup.dir
?: run {
val backups =
context.findBackups(backup.packageName)[backup.packageName]
val found = backups?.find { it.backupDate == backup.backupDate }
found?.dir
}
?: run {
val backups =
context.findBackups(backup.packageName)[backup.packageName]
val found = backups?.find { it.backupDate == backup.backupDate }
found?.dir
}
if (backupDir != null) {
if (backupMode and MODE_APK == MODE_APK) {
work?.setOperation("apk")
Expand Down Expand Up @@ -132,6 +132,7 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
extractErrorMessage(cause.shellResult)
}"
}

else -> {
"${e.javaClass.simpleName}: ${e.message}"
}
Expand Down Expand Up @@ -225,7 +226,7 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
val apkTargetPath = File(backup.sourceDir ?: BASE_APK_FILENAME)
val baseApkName = apkTargetPath.name
val baseApkFile = backupDir.findFile(baseApkName)
?: throw RestoreFailedException("$baseApkName is missing in backup", null)
?: throw RestoreFailedException("$baseApkName is missing in backup", null)
Timber.d("<$packageName> Found $baseApkName in backup archive")
val splitApksInBackup: Array<StorageFile> = try {
backupDir.listFiles()
Expand Down Expand Up @@ -331,6 +332,7 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
}
}
}

else -> {
// Install main package
sb.append(
Expand Down Expand Up @@ -364,9 +366,9 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
runAsRoot("pm grant ${backup.packageName} $p")
} catch (e: ShellCommandFailedException) {
val details = e.shellResult.err
.joinToString("\n")
.splitToSequence("\n\tat ")
.first()
.joinToString("\n")
.splitToSequence("\n\tat ")
.first()
val error = "Restoring permission $p failed: $details"
Timber.e(error)
// TODO integrate this exception in the result
Expand Down Expand Up @@ -420,12 +422,12 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
) {
try {
val backupDirToRestore = backupInstanceDir.findFile(what)
?: throw RestoreFailedException(
String.format(
LOG_DIR_IS_MISSING_CANNOT_RESTORE,
what
)
)
?: throw RestoreFailedException(
String.format(
LOG_DIR_IS_MISSING_CANNOT_RESTORE,
what
)
)
suRecursiveCopyFileFromDocument(backupDirToRestore, targetPath)
} catch (e: IOException) {
throw RestoreFailedException("Could not read the input file due to IOException", e)
Expand Down Expand Up @@ -457,9 +459,9 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
if (isCompressed) {
when (compressionType) {
"no" -> {}
"gz" -> inputStream = GzipCompressorInputStream(inputStream)
"gz" -> inputStream = GzipCompressorInputStream(inputStream)
"zst" -> inputStream = ZstdCompressorInputStream(inputStream)
else -> throw RestoreFailedException("Unsupported compression algorithm: ${compressionType}")
else -> throw RestoreFailedException("Unsupported compression algorithm: ${compressionType}")
}
}
return inputStream
Expand Down Expand Up @@ -555,7 +557,13 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
throw RestoreFailedException("Backup archive at $archive is missing")
}
try {
openArchiveFile(archive, isCompressed, compressionType, isEncrypted, iv).use { archiveStream ->
openArchiveFile(
archive,
isCompressed,
compressionType,
isEncrypted,
iv
).use { archiveStream ->

targetDir.mkdirs() // in case it doesn't exist

Expand All @@ -569,7 +577,7 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell

var options = ""
options += " --exclude ${quote(ShellHandler.RESTORE_EXCLUDE_FILE)}"
if (pref_excludeCache.value) {
if (!pref_restoreCache.value) {
options += " --exclude ${quote(ShellHandler.EXCLUDE_CACHE_FILE)}"
}

Expand All @@ -592,8 +600,8 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
.split("\n")
.filterNot { line ->
line.isBlank()
|| line.contains("tar: unknown file type") // e.g. socket 140000
|| line.contains("tar: had errors") // summary at the end
|| line.contains("tar: unknown file type") // e.g. socket 140000
|| line.contains("tar: had errors") // summary at the end
}
if (errLines.isNotEmpty()) {
val errFiltered = errLines.joinToString("\n")
Expand Down Expand Up @@ -778,11 +786,16 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
val isEncrypted: Boolean,
)

fun findBackupArchive(dataType: String, backup: Backup, backupDir: StorageFile) : FoundBackupArchive {
fun findBackupArchive(
dataType: String,
backup: Backup,
backupDir: StorageFile,
): FoundBackupArchive {

// try all variants starting with that described in the properties file
val encryptionsToTry = listOf(backup.isEncrypted, ! backup.isEncrypted)
val compressionsToTry = (listOf(if(backup.isCompressed) backup.compressionType else "no") + COMPRESSION_TYPES.keys).toSet()
val encryptionsToTry = listOf(backup.isEncrypted, !backup.isEncrypted)
val compressionsToTry =
(listOf(if (backup.isCompressed) backup.compressionType else "no") + COMPRESSION_TYPES.keys).toSet()

var count = 0
for (tryEncrypted in encryptionsToTry) {
Expand All @@ -803,7 +816,12 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell
if (count > 1)
Timber.w("found $backupFilename but properties file describes: compression=${backup.isCompressed}/${backup.compressionType} encryption=${backup.isEncrypted}")

return FoundBackupArchive(backupArchive, tryCompressed, tryCompression, tryEncrypted)
return FoundBackupArchive(
backupArchive,
tryCompressed,
tryCompression,
tryEncrypted
)

} catch (e: Exception) {
continue
Expand Down Expand Up @@ -1099,8 +1117,8 @@ open class RestoreAppAction(context: Context, work: AppActionWork?, shell: Shell

private fun isPlausiblePackageInfo(app: Package): Boolean {
return app.dataPath.isNotBlank()
&& app.apkPath.isNotBlank()
&& app.devicesProtectedDataPath.isNotBlank()
&& app.apkPath.isNotBlank()
&& app.devicesProtectedDataPath.isNotBlank()
}

private fun isPlausiblePath(path: String, packageName: String): Boolean {
Expand Down
35 changes: 21 additions & 14 deletions app/src/main/java/com/machiav3lli/backup/handler/AssetHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.machiav3lli.backup.handler
import android.content.Context
import android.content.res.AssetManager
import com.machiav3lli.backup.BuildConfig
import com.machiav3lli.backup.preferences.pref_backupCache
import com.machiav3lli.backup.preferences.pref_backupNoBackupData
import com.machiav3lli.backup.preferences.pref_restoreCache
import com.machiav3lli.backup.preferences.pref_restoreNoBackupData
import timber.log.Timber
import java.io.File
Expand Down Expand Up @@ -45,27 +47,32 @@ class AssetHandler(context: Context) {
}
}

// @hg42 why exclude lib? how is it restored?
// @machiav3lli libs are generally created while installing the app. Backing them up
// would result a compatibility problem between devices with different cpu_arch

val DATA_BACKUP_EXCLUDED_BASENAMES get() = listOfNotNull(
"lib", //TODO hg42 what about architecture dependent names? or may be application specific? lib* ???
if (!pref_backupNoBackupData.value) "no_backup" else null //TODO hg42 use Context.getNoBackupFilesDir() ??? tricky, because it's an absolute path (remove common part...)
val DATA_EXCLUDED_CACHE_DIRS = listOf(
"cache",
"code_cache"
)

val DATA_RESTORE_EXCLUDED_BASENAMES get() = listOfNotNull(
val LIB_DIRS = listOf(
"lib", //TODO hg42 what about architecture dependent names? or may be application specific? lib* ???
if (!pref_restoreNoBackupData.value) "no_backup" else null //TODO hg42 use Context.getNoBackupFilesDir() ??? tricky, because it's an absolute path (remove common part...)
)

val DATA_EXCLUDED_CACHE_DIRS get() = listOf(
"cache",
"code_cache"
)
// libs are generally created while installing the app. Backing them up
// would result in a compatibility problem between devices with different cpu_arch

val DATA_BACKUP_EXCLUDED_BASENAMES get() = (
LIB_DIRS
+ if (pref_backupNoBackupData.value) listOf() else listOf("no_backup") //TODO hg42 use Context.getNoBackupFilesDir() ??? tricky, because it's an absolute path (remove common part...)
+ if (pref_backupCache.value) listOf() else DATA_EXCLUDED_CACHE_DIRS
)

val DATA_RESTORE_EXCLUDED_BASENAMES get() = (
LIB_DIRS
+ if (pref_restoreNoBackupData.value) listOf() else listOf("no_backup") //TODO hg42 use Context.getNoBackupFilesDir() ??? tricky, because it's an absolute path (remove common part...)
+ if (pref_restoreCache.value) listOf() else DATA_EXCLUDED_CACHE_DIRS
)

val DATA_EXCLUDED_NAMES get() = listOfNotNull(
"com.google.android.gms.appid.xml",
"com.google.android.gms.appid.xml", // appid needs to be recreated
"com.machiav3lli.backup.xml", // encrypted prefs file
//"cache", // don't, this also excludes the cache
"trash",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.machiav3lli.backup.BuildConfig
Expand Down Expand Up @@ -57,7 +56,6 @@ import com.machiav3lli.backup.ui.item.StringPref

@Composable
fun ServicePrefsPage() {
val context = LocalContext.current
val openDialog = remember { mutableStateOf(false) }
var dialogsPref by remember { mutableStateOf<Pref?>(null) }

Expand Down Expand Up @@ -204,6 +202,14 @@ val pref_backupNoBackupData = BooleanPref(
onChanged = { OABX.assets.updateExcludeFiles() },
)

val pref_backupCache = BooleanPref(
key = "srv-bkp.backupCache",
titleId = R.string.prefs_backupcache,
summaryId = R.string.prefs_backupcache_summary,
icon = Phosphor.Prohibit,
defaultValue = false
)

val pref_restoreDeviceProtectedData = BooleanPref(
key = "srv-rst.restoreDeviceProtectedData",
titleId = R.string.prefs_deviceprotecteddata_rst,
Expand Down Expand Up @@ -250,6 +256,14 @@ val pref_restoreNoBackupData = BooleanPref(
onChanged = { OABX.assets.updateExcludeFiles() },
)

val pref_restoreCache = BooleanPref(
key = "srv-rst.restoreCache",
titleId = R.string.prefs_restorecache,
summaryId = R.string.prefs_restorecache_summary,
icon = Phosphor.Prohibit,
defaultValue = false
)

val pref_restorePermissions = BooleanPref(
key = "srv.restorePermissions",
titleId = R.string.prefs_restorepermissions,
Expand Down Expand Up @@ -304,11 +318,3 @@ val pref_installationPackage = StringPref(
iconTint = ColorOBB,
defaultValue = BuildConfig.APPLICATION_ID
)

val pref_excludeCache = BooleanPref(
key = "srv.excludeCache",
titleId = R.string.prefs_excludecache,
summaryId = R.string.prefs_excludecache_summary,
icon = Phosphor.Prohibit,
defaultValue = false
)
4 changes: 4 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,8 @@
<string name="prefs_theme_dynamic_dark">داكن بالألوان المتكيفة</string>
<string name="enforce_backups_limit">فرض حد النسخ الاحتياطية</string>
<string name="enforce_backups_limit_description">قم بحذف النسخ الاحتياطية القديمة لفرض الحد الأقصى لـ %d</string>
<string name="prefs_backupcache">Back up cached data</string>
<string name="prefs_backupcache_summary">Back up files in cache directories</string>
<string name="prefs_restorecache">Restore cached data</string>
<string name="prefs_restorecache_summary">Restore files in cache directories</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values-bs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,8 @@
<string name="sched_timeLeft">Vrijeme do sljedeće izrade sigurnosne kopije:</string>
<string name="sched_startingbackup">Pokretanje planirane izrade sigurnosne kopije…</string>
<string name="sched_notificationMessage">Planirana izrada sigurnosne kopije</string>
<string name="prefs_backupcache">Back up cached data</string>
<string name="prefs_backupcache_summary">Back up files in cache directories</string>
<string name="prefs_restorecache">Restore cached data</string>
<string name="prefs_restorecache_summary">Restore files in cache directories</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,8 @@
<string name="prefs_theme_black_summary">Força el fons negre en utilitzar el tema fosc</string>
<string name="prefs_multilineinfochips_summary">Permet que la informació sigui mostrada en diverses línies.</string>
<string name="prefs_squeezenavtext_summary">Permet reduir la mida dels textos de la barra de navegació per adequar-se a l\'amplada disponible.</string>
<string name="prefs_backupcache">Back up cached data</string>
<string name="prefs_backupcache_summary">Back up files in cache directories</string>
<string name="prefs_restorecache">Restore cached data</string>
<string name="prefs_restorecache_summary">Restore files in cache directories</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,8 @@
<string name="prefs_theme_dynamic_dark">Tmavý s dynamickými barvami</string>
<string name="enforce_backups_limit">Vynutit limit záloh</string>
<string name="enforce_backups_limit_description">Smazat starší zálohy pro vynucení limitu na %d</string>
<string name="prefs_backupcache">Back up cached data</string>
<string name="prefs_backupcache_summary">Back up files in cache directories</string>
<string name="prefs_restorecache">Restore cached data</string>
<string name="prefs_restorecache_summary">Restore files in cache directories</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values-da/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="prefs_backupcache">Back up cached data</string>
<string name="prefs_backupcache_summary">Back up files in cache directories</string>
<string name="prefs_restorecache">Restore cached data</string>
<string name="prefs_restorecache_summary">Restore files in cache directories</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,8 @@
<string name="prefs_theme_dynamic_light">Hell mit dynamischen Farben</string>
<string name="prefs_theme_system_black">Systemthema (Schwarz)</string>
<string name="prefs_theme_dynamic_dark">Dunkel mit dynamischen Farben</string>
<string name="prefs_backupcache">Cache Dateien sichern</string>
<string name="prefs_backupcache_summary">Sichert Dateien, die in Cache-Ordnern abgelegt sind</string>
<string name="prefs_restorecache">Cache Dateien wiederherstellen</string>
<string name="prefs_restorecache_summary">Stellt Dateien wieder her, die in Cache-Ordnern abgelegt sind</string>
</resources>
Loading

0 comments on commit c7b1282

Please sign in to comment.