Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
weblate committed Mar 6, 2024
2 parents 63a1ce2 + b0cade3 commit 911bebf
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ 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.items.UndeterminedStorageFile
import com.machiav3lli.backup.preferences.pref_backupPauseApps
import com.machiav3lli.backup.preferences.pref_backupTarCmd
import com.machiav3lli.backup.preferences.pref_excludeCache
Expand Down Expand Up @@ -139,7 +138,7 @@ open class BackupAppAction(context: Context, work: AppActionWork?, shell: ShellH
val iv = initIv(CIPHER_ALGORITHM) // as we're using a static Cipher Algorithm
backupBuilder.setIv(iv)

val backupInstanceDir = backupBuilder.backupPath
val backupInstanceDir = backupBuilder.backupDir
val pauseApp = pref_backupPauseApps.value
if (pauseApp) {
Timber.d("pre-process package (to avoid file inconsistencies during backup etc.)")
Expand Down Expand Up @@ -204,7 +203,7 @@ open class BackupAppAction(context: Context, work: AppActionWork?, shell: ShellH

backup = backupBuilder.createBackup()

ok = saveBackupProperties(backupBuilder.backupPropsFile, backup)
ok = backup.file != null

} catch (e: BackupFailedException) {
return handleException(e)
Expand All @@ -224,7 +223,7 @@ open class BackupAppAction(context: Context, work: AppActionWork?, shell: ShellH
backup = backupBuilder.createBackup()
// TODO maybe need to handle some emergent props
if (ok)
app.addBackup(backup)
app.addNewBackup(backup)
else {
Timber.d("Backup failed -> deleting it")
app.deleteBackup(backup)
Expand All @@ -237,18 +236,6 @@ open class BackupAppAction(context: Context, work: AppActionWork?, shell: ShellH
return ActionResult(app, backup, "", true)
}

@Throws(IOException::class)
protected fun saveBackupProperties(
propertiesFile: UndeterminedStorageFile,
backup: Backup,
): Boolean {
propertiesFile.writeText(backup.toSerialized())?.let {
Timber.i("Wrote $it for backup: $backup")
return true
}
return false
}

@Throws(IOException::class, CryptoSetupException::class)
protected fun createBackupArchiveTarApi(
backupInstanceDir: StorageFile,
Expand Down
36 changes: 22 additions & 14 deletions app/src/main/java/com/machiav3lli/backup/dbs/entity/Backup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -182,37 +182,38 @@ data class Backup @OptIn(ExperimentalSerializationApi::class) constructor(
)

override fun toString(): String = "Backup{" +
"backupDate=" + backupDate +
"packageName=" + packageName +
", backupDate=" + backupDate +
", hasApk=" + hasApk +
", hasAppData=" + hasAppData +
", hasDevicesProtectedData=" + hasDevicesProtectedData +
", hasExternalData=" + hasExternalData +
", hasObbData=" + hasObbData +
", hasMediaData=" + hasMediaData +
", persistent='" + persistent + '\'' +
", size=" + size +
", backupVersionCode='" + backupVersionCode + '\'' +
", cpuArch='" + cpuArch + '\'' +
", compressionType='" + compressionType + '\'' +
", cipherType='" + cipherType + '\'' +
", iv='" + iv + '\'' +
", cpuArch='" + cpuArch + '\'' +
", backupVersionCode='" + backupVersionCode + '\'' +
", size=" + size +
", permissions='" + permissions + '\'' +
", persistent='" + persistent + '\'' +
'}'

override fun equals(other: Any?): Boolean = when {
this === other -> true
javaClass != other?.javaClass
|| other !is Backup
|| backupVersionCode != other.backupVersionCode
|| packageName != other.packageName
|| backupDate != other.backupDate
|| backupVersionCode != other.backupVersionCode
|| packageLabel != other.packageLabel
|| versionName != other.versionName
|| versionCode != other.versionCode
|| profileId != other.profileId
|| sourceDir != other.sourceDir
|| !splitSourceDirs.contentEquals(other.splitSourceDirs)
|| isSystem != other.isSystem
|| backupDate != other.backupDate
|| hasApk != other.hasApk
|| hasAppData != other.hasAppData
|| hasDevicesProtectedData != other.hasDevicesProtectedData
Expand Down Expand Up @@ -275,14 +276,21 @@ data class Backup @OptIn(ExperimentalSerializationApi::class) constructor(
@Transient
var file: StorageFile? = null

val dir: StorageFile?
get() = if (file?.name == BACKUP_INSTANCE_PROPERTIES_INDIR) {
file?.parent
} else {
val baseName = file?.name?.removeSuffix(".$PROP_NAME")
baseName?.let { dirName ->
file?.parent?.findFile(dirName)
@Ignore
@Transient
var dir: StorageFile? = null
get() {
if (field == null) {
field = if (file?.name == BACKUP_INSTANCE_PROPERTIES_INDIR) {
file?.parent
} else {
val baseName = file?.name?.removeSuffix(".$PROP_NAME")
baseName?.let { dirName ->
file?.parent?.findFile(dirName)
}
}
}
return field
}

val tag: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,23 @@ suspend fun scanBackups(
renamer: (() -> Unit)? = null,
) {
hitBusy()

try {
beginNanoTimer("scanBackups.${if (packageName.isEmpty()) "" else "package."}onPropsFile")
onPropsFile(file)
endNanoTimer("scanBackups.${if (packageName.isEmpty()) "" else "package."}onPropsFile")
if (file.name == BACKUP_INSTANCE_PROPERTIES_INDIR
||
(file.name?.removeSuffix(
".$PROP_NAME"
)?.let {
file.parent?.findFile(
it
)
} != null)
) {
beginNanoTimer("scanBackups.${if (packageName.isEmpty()) "" else "package."}onPropsFile")
onPropsFile(file)
endNanoTimer("scanBackups.${if (packageName.isEmpty()) "" else "package."}onPropsFile")
} else
renameDamagedToERROR(file, "no-dir")
} catch (_: Throwable) {
if (renamer != null)
renamer()
Expand Down Expand Up @@ -278,7 +291,6 @@ suspend fun scanBackups(
suspend fun processFile(
file: StorageFile,
) {

checkThreadStats()

val name = file.name ?: ""
Expand Down Expand Up @@ -431,6 +443,7 @@ suspend fun scanBackups(
}
}
}

if (packageName.isEmpty()) {
traceBackupsScanPackage { "queue total ----> $total" }
if (suspicious.get() > 0)
Expand Down
58 changes: 39 additions & 19 deletions app/src/main/java/com/machiav3lli/backup/handler/BackupBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import com.machiav3lli.backup.items.StorageFile
import com.machiav3lli.backup.items.UndeterminedStorageFile
import com.machiav3lli.backup.preferences.pref_flatStructure
import com.machiav3lli.backup.preferences.pref_propertiesInDir
import timber.log.Timber
import java.io.IOException
import java.time.LocalDateTime

class BackupBuilder(
Expand All @@ -49,7 +51,7 @@ class BackupBuilder(
private var cipherType: String? = null
private val cpuArch: String = Build.SUPPORTED_ABIS[0]
private var size: Long = 0L
val backupPath = ensureBackupPath(backupRoot)
val backupDir = ensureBackupPath(backupRoot)
val backupPropsFile = getPropsFile(backupRoot)

private fun ensureBackupPath(backupRoot: StorageFile): StorageFile {
Expand All @@ -75,14 +77,16 @@ class BackupBuilder(
when {
pref_propertiesInDir.value ->
return UndeterminedStorageFile(
backupPath,
backupDir,
BACKUP_INSTANCE_PROPERTIES_INDIR
)

pref_flatStructure.value ->
return UndeterminedStorageFile(
backupRoot,
backupInstancePropsFlat(packageInfo, dateTimeStr)
)

else ->
return UndeterminedStorageFile(
backupRoot,
Expand Down Expand Up @@ -131,24 +135,40 @@ class BackupBuilder(
this.size = size
}

@Throws(IOException::class)
protected fun saveBackupProperties(
propertiesFile: UndeterminedStorageFile,
backup: Backup,
): StorageFile? {
propertiesFile.writeText(backup.toSerialized())?.let {
Timber.i("Wrote $it for backup: $backup")
return it
}
return null
}

fun createBackup(): Backup {
return Backup(
base = packageInfo,
backupDate = backupDate,
hasApk = hasApk,
hasAppData = hasAppData,
hasDevicesProtectedData = hasDevicesProtectedData,
hasExternalData = hasExternalData,
hasObbData = hasObbData,
hasMediaData = hasMediaData,
compressionType = compressionType,
cipherType = cipherType,
iv = iv,
cpuArch = cpuArch,
permissions = if (packageInfo is AppInfo) packageInfo.permissions else emptyList(),
size = size,
persistent = false,
)
val backup =
Backup(
base = packageInfo,
backupDate = backupDate,
hasApk = hasApk,
hasAppData = hasAppData,
hasDevicesProtectedData = hasDevicesProtectedData,
hasExternalData = hasExternalData,
hasObbData = hasObbData,
hasMediaData = hasMediaData,
compressionType = compressionType,
cipherType = cipherType,
iv = iv,
cpuArch = cpuArch,
permissions = if (packageInfo is AppInfo) packageInfo.permissions else emptyList(),
size = size,
persistent = false,
)
backup.dir = backupDir
backup.file = saveBackupProperties(backupPropsFile, backup)
return backup
}

/*fun createBackupProperties(): BackupProperties {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.machiav3lli.backup.items.ActionResult
import com.machiav3lli.backup.items.Package
import com.machiav3lli.backup.items.StorageFile.Companion.invalidateCache
import com.machiav3lli.backup.preferences.pref_numBackupRevisions
import com.machiav3lli.backup.preferences.pref_paranoidHousekeeping
import com.machiav3lli.backup.tasks.AppActionWork
import com.machiav3lli.backup.utils.FileUtils.BackupLocationInAccessibleException
import com.machiav3lli.backup.utils.StorageLocationNotConfiguredException
Expand Down Expand Up @@ -140,7 +141,8 @@ object BackupRestoreHelper {

fun housekeepingPackageBackups(app: Package) {

app.refreshBackupList()
if (pref_paranoidHousekeeping.value)
app.refreshBackupList()

val numBackupRevisions =
pref_numBackupRevisions.value
Expand Down
Loading

0 comments on commit 911bebf

Please sign in to comment.