Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Dec 7, 2024
1 parent 0e25e3c commit d8a2bfb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.nio.file.Path;
import java.util.List;
import java.util.zip.ZipOutputStream;

class Instance {
static XBackupApi instance;
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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 <T> dbQuery(block: suspend Transaction.() -> T): T
suspend fun <T> syncDbQuery(block: suspend Transaction.() -> T): T
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/zly2006/xbackup/Commands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/github/zly2006/xbackup/XBackup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -360,7 +360,7 @@ object XBackup : ModInitializer {
}

backups.filter { it.temporary }.forEach {
service.deleteBackup(it)
service.deleteBackupInternal(it)
count++
}
return count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d8a2bfb

Please sign in to comment.