Fix corrupted backup file, fix #6424 (#6691)

Reappear stably on the api30 Android Studio Emulator,
first save a large backup file,
then save a small backup file, overwriting the previous larger backup file,
so you get a backup file with a larger size but only the first part is meaningful,

(cherry picked from commit 6f5328f663a7c3e148fa1d5b93ab5186b244fa1d)
This commit is contained in:
啊o额iu鱼 2022-02-23 22:12:24 +08:00 committed by Jobobby04
parent a7d7aa1ec5
commit 1803f49732

View File

@ -50,6 +50,7 @@ import logcat.LogPriority
import okio.buffer import okio.buffer
import okio.gzip import okio.gzip
import okio.sink import okio.sink
import java.io.FileOutputStream
import kotlin.math.max import kotlin.math.max
class FullBackupManager(context: Context) : AbstractBackupManager(context) { class FullBackupManager(context: Context) : AbstractBackupManager(context) {
@ -108,7 +109,10 @@ class FullBackupManager(context: Context) : AbstractBackupManager(context) {
?: throw Exception("Couldn't create backup file") ?: throw Exception("Couldn't create backup file")
val byteArray = parser.encodeToByteArray(BackupSerializer, backup!!) val byteArray = parser.encodeToByteArray(BackupSerializer, backup!!)
file.openOutputStream().sink().gzip().buffer().use { it.write(byteArray) } file.openOutputStream().also {
// Force overwrite old file size,
(it as? FileOutputStream)?.channel?.truncate(0)
}.sink().gzip().buffer().use { it.write(byteArray) }
val fileUri = file.uri val fileUri = file.uri
// Make sure it's a valid backup file // Make sure it's a valid backup file