Skip to content

Commit f0fc9cf

Browse files
author
Fastace
committed
Fix:Restic初始化保存密码到数据库的BUG,Restic备份缺少package_restore_config.json,以及正确恢复package_restore_config.json的BUG,TODO预计参考现有恢复逻辑重写Restic快照恢复之后的第二阶段APK恢复逻辑,统一在Restic快照回复页面中完成
1 parent 5f41550 commit f0fc9cf

5 files changed

Lines changed: 44 additions & 21 deletions

File tree

source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepository.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ class ResticRepository @Inject constructor(
435435
"data" -> DataType.PACKAGE_DATA
436436
"obb" -> DataType.PACKAGE_OBB
437437
"media" -> DataType.PACKAGE_MEDIA
438+
"config" -> DataType.PACKAGE_CONFIG
438439
else -> null
439440
}
440441

source/core/service/src/main/kotlin/com/xayah/core/service/packages/backup/AbstractBackupService.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,20 @@ internal abstract class AbstractBackupService : AbstractPackagesService() {
288288
indexInfo = p.indexInfo.copy(opType = OpType.RESTORE, cloud = mTaskEntity.cloud, backupDir = mTaskEntity.backupDir),
289289
extraInfo = p.extraInfo.copy(activated = false)
290290
)
291+
291292
val configDst = PathUtil.getPackageRestoreConfigDst(dstDir = dstDir)
292293
mRootService.writeJson(data = restoreEntity, dst = configDst)
294+
val configFile = File(configDst)
295+
if (configFile.exists()) {
296+
val configResticSuccess = backupWithRestic(
297+
packageName = p.packageName,
298+
compressedFile = configFile,
299+
dataType = DataType.PACKAGE_CONFIG
300+
)
301+
if (configResticSuccess) {
302+
log { "Restic backup successful for config file" }
303+
}
304+
}
293305
onConfigSaved(path = configDst, archivesRelativeDir = p.archivesRelativeDir)
294306
mPackageDao.upsert(restoreEntity)
295307
mPackageDao.upsert(p)

source/core/service/src/main/kotlin/com/xayah/core/service/packages/backup/BackupServiceLocalImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ internal class BackupServiceLocalImpl @Inject constructor() : AbstractBackupServ
112112
DataType.PACKAGE_DATA -> File("$dstDir/${DataType.PACKAGE_DATA.type}.tar.zst")
113113
DataType.PACKAGE_OBB -> File("$dstDir/${DataType.PACKAGE_OBB.type}.tar.zst")
114114
DataType.PACKAGE_MEDIA -> File("$dstDir/${DataType.PACKAGE_MEDIA.type}.tar.zst")
115+
DataType.PACKAGE_CONFIG -> File("$dstDir/package_restore_config.json")
115116
else -> null
116117
}
117118

source/feature/main/restore/src/main/kotlin/com/xayah/feature/main/restore/ResticRestoreViewModel.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class ResticRestoreViewModel @Inject constructor(
4949

5050
val repoPath = context.readResticRepoPath()
5151
val password = context.readResticPassword()
52+
// 添加调试日志
53+
Log.d("ResticRestore", "读取到的 repoPath: $repoPath")
54+
Log.d("ResticRestore", "读取到的 password: ${if (password.isNullOrEmpty()) "" else "已设置"}")
5255

5356
if (repoPath.isNullOrEmpty() || password.isNullOrEmpty()) {
5457
_uiState.value = ResticRestoreUiState.Error("Restic not configured")
@@ -120,7 +123,8 @@ class ResticRestoreViewModel @Inject constructor(
120123
DataType.PACKAGE_DATA -> 3
121124
DataType.PACKAGE_OBB -> 4
122125
DataType.PACKAGE_MEDIA -> 5
123-
else -> 6
126+
DataType.PACKAGE_CONFIG -> 6
127+
else -> 7
124128
}
125129
}
126130
Log.d("ResticRestore", "排序后的数据类型: ${sortedBackups.map { it.dataType.type }}")
@@ -179,7 +183,10 @@ class ResticRestoreViewModel @Inject constructor(
179183
Log.d("ResticRestore", "恢复到用户备份目录: $targetPath")
180184
val backupBaseDir = context.readBackupDirectory() ?: context.localBackupSaveDir()
181185
val snapshotSubPath = "$backupBaseDir/apps/${backup.packageName}/user_${backup.userId}"
182-
val includePath = "${backup.dataType.type}.tar.zst"
186+
val includePath = when (backup.dataType) {
187+
DataType.PACKAGE_CONFIG -> "package_restore_config.json"
188+
else -> "${backup.dataType.type}.tar.zst"
189+
}
183190
val fullTargetPath = "${targetPath}apps/${backup.packageName}/user_${backup.userId}/"
184191
Log.d("ResticRestore", "恢复 ${backup.dataType.type} 到目标: $targetPath")
185192
Log.d("ResticRestore", "快照子路径: $snapshotSubPath")

source/feature/main/settings/src/main/kotlin/com/xayah/feature/main/settings/restic/ResticViewModel.kt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class ResticViewModel @Inject constructor(
8787
// 获取仓库路径
8888
val repoPath = getRepoPath()
8989
_repoPathState.value = repoPath
90+
Log.d(TAG, "checkResticStatus: repoPath = $repoPath")
9091

9192
// Check initialization status and snapshot count
9293
val defaultRepoPath = repoPath // 重命名变量
@@ -152,7 +153,10 @@ class ResticViewModel @Inject constructor(
152153
_resticSnapshotCountState.value = snapshots.size
153154

154155
// 保存到 DataStore
155-
context.saveResticRepoPath(repoPath)
156+
withContext(Dispatchers.IO) {
157+
context.saveResticRepoPath(repoPath)
158+
context.saveResticPassword(password)
159+
}
156160
_repoPathState.value = repoPath
157161
true
158162
} else {
@@ -164,24 +168,19 @@ class ResticViewModel @Inject constructor(
164168
// 仓库不存在,进行初始化
165169
_initializationState.value = InitializationState.Initializing
166170

167-
resticRepo.initRepository(repoPath, password) { success, message ->
168-
// 在协程中处理回调
169-
viewModelScope.launch {
170-
if (success) {
171-
_resticInitializedState.value = true
172-
_resticRepoPathState.value = repoPath
173-
_resticSnapshotCountState.value = 0
174-
_initializationState.value = InitializationState.ReadyToUse(repoPath)
175-
176-
// 保存到 DataStore
177-
context.saveResticRepoPath(repoPath)
178-
_repoPathState.value = repoPath
179-
} else {
180-
_initializationState.value = InitializationState.Error(message)
181-
}
182-
}
171+
// 直接调用初始化方法
172+
val initSuccess = initializeRepository(repoPath, password)
173+
174+
if (initSuccess) {
175+
_resticInitializedState.value = true
176+
_resticRepoPathState.value = repoPath
177+
_resticSnapshotCountState.value = 0
178+
_initializationState.value = InitializationState.ReadyToUse(repoPath)
179+
} else {
180+
_initializationState.value = InitializationState.Error("Initialization failed")
183181
}
184-
true
182+
183+
initSuccess
185184
}
186185
} catch (e: Exception) {
187186
_initializationState.value = InitializationState.Error(e.message ?: "Unknown error")
@@ -284,7 +283,10 @@ class ResticViewModel @Inject constructor(
284283
_resticRepoPathState.value = repoPath
285284
_resticSnapshotCountState.value = 0
286285
// 保存到 DataStore
287-
context.saveResticRepoPath(repoPath)
286+
withContext(Dispatchers.IO) {
287+
context.saveResticRepoPath(repoPath)
288+
context.saveResticPassword(password)
289+
}
288290
_repoPathState.value = repoPath
289291
}
290292
}

0 commit comments

Comments
 (0)