From d8a2bfbc1caf56fbfc2c2b590abf027e589dd231 Mon Sep 17 00:00:00 2001 From: Liyan Zhao Date: Sat, 7 Dec 2024 17:01:49 +0800 Subject: [PATCH] fix --- .../github/zly2006/xbackup/api/XBackupApi.java | 7 +++++++ .../zly2006/xbackup/api/XBackupKotlinAsyncApi.kt | 2 ++ .../zly2006/xbackup/BackupDatabaseService.kt | 16 ++++++++++++---- .../com/github/zly2006/xbackup/Commands.kt | 2 +- .../kotlin/com/github/zly2006/xbackup/XBackup.kt | 4 ++-- .../com/github/zly2006/xbackup/gui/BackupsGui.kt | 2 +- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/github/zly2006/xbackup/api/XBackupApi.java b/api/src/main/java/com/github/zly2006/xbackup/api/XBackupApi.java index fbc09b9..5b4af9c 100644 --- a/api/src/main/java/com/github/zly2006/xbackup/api/XBackupApi.java +++ b/api/src/main/java/com/github/zly2006/xbackup/api/XBackupApi.java @@ -6,6 +6,7 @@ import java.nio.file.Path; import java.util.List; +import java.util.zip.ZipOutputStream; class Instance { static XBackupApi instance; @@ -37,4 +38,10 @@ static XBackupApi setInstance(XBackupApi instance) { void setCloudStorageProvider(@NotNull CloudStorageProvider provider); @NotNull CloudStorageProvider getCloudStorageProvider(); + + void zipArchive(@NotNull ZipOutputStream stream, @NotNull IBackup backup); + + void restoreBackup(@NotNull IBackup backup, @NotNull Path target); + + void deleteBackup(@NotNull IBackup backup); } diff --git a/api/src/main/java/com/github/zly2006/xbackup/api/XBackupKotlinAsyncApi.kt b/api/src/main/java/com/github/zly2006/xbackup/api/XBackupKotlinAsyncApi.kt index 9f075d8..032a7aa 100644 --- a/api/src/main/java/com/github/zly2006/xbackup/api/XBackupKotlinAsyncApi.kt +++ b/api/src/main/java/com/github/zly2006/xbackup/api/XBackupKotlinAsyncApi.kt @@ -1,11 +1,13 @@ package com.github.zly2006.xbackup.api import org.jetbrains.exposed.sql.Transaction +import java.nio.file.Path interface XBackupKotlinAsyncApi: XBackupApi { var activeTaskProgress: Int var activeTask: String + suspend fun restore(id: Int, target: Path, ignored: (Path) -> Boolean) suspend fun dbQuery(block: suspend Transaction.() -> T): T suspend fun syncDbQuery(block: suspend Transaction.() -> T): T } diff --git a/common/src/main/kotlin/com/github/zly2006/xbackup/BackupDatabaseService.kt b/common/src/main/kotlin/com/github/zly2006/xbackup/BackupDatabaseService.kt index 4838816..63ce165 100644 --- a/common/src/main/kotlin/com/github/zly2006/xbackup/BackupDatabaseService.kt +++ b/common/src/main/kotlin/com/github/zly2006/xbackup/BackupDatabaseService.kt @@ -401,8 +401,12 @@ class BackupDatabaseService( ) } - suspend fun deleteBackup(backup: IBackup) { - dbQuery { + override fun deleteBackup(backup: IBackup) = runBlocking { + deleteBackupInternal(backup) + } + + suspend fun deleteBackupInternal(backup: IBackup) { + syncDbQuery { backup.entries.forEach { entry -> if (BackupEntryBackupTable.selectAll().where { BackupEntryBackupTable.entry eq entry.id and @@ -437,7 +441,7 @@ class BackupDatabaseService( * @param ignored Predicate to ignore files, this prevents files from being deleted, * usually should be opposite of the predicate used in [createBackup] */ - suspend fun restore(id: Int, target: Path, ignored: (Path) -> Boolean) = dbQuery { + override suspend fun restore(id: Int, target: Path, ignored: (Path) -> Boolean) = dbQuery { val backup = getBackupInternal(id) ?: error("Backup not found") val map = backup.entries.associateBy { it.path }.filter { !ignored(Path(it.key)) } for (it in target.normalize().toFile().walk().drop(1)) { @@ -523,6 +527,10 @@ class BackupDatabaseService( log.info("Restored backup $id") } + override fun restoreBackup(backup: IBackup, target: Path) = runBlocking { + restore(backup.id, target) { false } + } + /** * Check if this backup is valid */ @@ -603,7 +611,7 @@ class BackupDatabaseService( } } - fun zipArchive(outputStream: ZipOutputStream, backup: IBackup) { + override fun zipArchive(outputStream: ZipOutputStream, backup: IBackup) { backup.entries.forEach { if (!it.isDirectory) { outputStream.putNextEntry(ZipEntry(it.path).apply { diff --git a/src/main/kotlin/com/github/zly2006/xbackup/Commands.kt b/src/main/kotlin/com/github/zly2006/xbackup/Commands.kt index 72296c4..36bcb6b 100644 --- a/src/main/kotlin/com/github/zly2006/xbackup/Commands.kt +++ b/src/main/kotlin/com/github/zly2006/xbackup/Commands.kt @@ -344,7 +344,7 @@ object Commands { val id = IntegerArgumentType.getInteger(it, "id") val backup = getBackup(id) XBackup.ensureNotBusy { - XBackup.service.deleteBackup(backup) + XBackup.service.deleteBackupInternal(backup) it.source.send(Utils.translate("command.xb.backup_deleted", backupIdText(id))) } 1 diff --git a/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt b/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt index 9ba29bc..4299f49 100644 --- a/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt +++ b/src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt @@ -348,7 +348,7 @@ object XBackup : ModInitializer { return@forEach } server.broadcast(Utils.translate("message.xb.pruning_backup", it)) - service.deleteBackup(service.getBackup(it.toInt())!!) + service.deleteBackupInternal(service.getBackup(it.toInt())!!) count++ } server.broadcast(Utils.translate("message.xb.prune_finished", toPrune.size)) @@ -360,7 +360,7 @@ object XBackup : ModInitializer { } backups.filter { it.temporary }.forEach { - service.deleteBackup(it) + service.deleteBackupInternal(it) count++ } return count diff --git a/src/main/kotlin/com/github/zly2006/xbackup/gui/BackupsGui.kt b/src/main/kotlin/com/github/zly2006/xbackup/gui/BackupsGui.kt index a5eada7..41665c8 100644 --- a/src/main/kotlin/com/github/zly2006/xbackup/gui/BackupsGui.kt +++ b/src/main/kotlin/com/github/zly2006/xbackup/gui/BackupsGui.kt @@ -112,7 +112,7 @@ class BackupsGui(private val service: BackupDatabaseService, val worldRoot: Path private fun deleteSelected(gui: ModularGui?) { if (selected == null) return runBlocking { - service.deleteBackup(selected!!) + service.deleteBackupInternal(selected!!) } selected = null updateList()