Compare commits
No commits in common. "2.x_zh_cn_translation" and "2.5.0" have entirely different histories.
2.x_zh_cn_
...
2.5.0
19
build.gradle
19
build.gradle
|
@ -1,5 +1,5 @@
|
||||||
plugins {
|
plugins {
|
||||||
id 'fabric-loom' version '1.2-SNAPSHOT'
|
id 'fabric-loom' version '1.0-SNAPSHOT'
|
||||||
id 'maven-publish'
|
id 'maven-publish'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,17 @@ repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loom {
|
||||||
|
runs {
|
||||||
|
testServer {
|
||||||
|
server()
|
||||||
|
ideConfigGenerated project.rootProject == project
|
||||||
|
name = "Testmod Server"
|
||||||
|
source sourceSets.test
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
//to change the versions see the gradle.properties file
|
//to change the versions see the gradle.properties file
|
||||||
minecraft "com.mojang:minecraft:${project.minecraft_version}"
|
minecraft "com.mojang:minecraft:${project.minecraft_version}"
|
||||||
|
@ -35,8 +46,8 @@ dependencies {
|
||||||
modImplementation("com.terraformersmc:modmenu:${project.modmenu_version}")
|
modImplementation("com.terraformersmc:modmenu:${project.modmenu_version}")
|
||||||
|
|
||||||
//General compression library
|
//General compression library
|
||||||
implementation "org.apache.commons:commons-compress:1.22"
|
implementation "org.apache.commons:commons-compress:1.21"
|
||||||
include "org.apache.commons:commons-compress:1.22"
|
include "org.apache.commons:commons-compress:1.21"
|
||||||
|
|
||||||
//LZMA support
|
//LZMA support
|
||||||
implementation 'org.tukaani:xz:1.9'
|
implementation 'org.tukaani:xz:1.9'
|
||||||
|
@ -105,7 +116,7 @@ publishing {
|
||||||
}
|
}
|
||||||
|
|
||||||
static def getMcMinor(ver) {
|
static def getMcMinor(ver) {
|
||||||
String[] arr = ((String)ver).split("[.-]")
|
String[] arr = ((String)ver).split("\\.")
|
||||||
|
|
||||||
if(arr.length < 2) return ver
|
if(arr.length < 2) return ver
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,26 @@
|
||||||
# Done to increase the memory available to gradle.
|
# Done to increase the memory available to gradle.
|
||||||
org.gradle.jvmargs=-Xmx1G
|
org.gradle.jvmargs=-Xmx1G
|
||||||
|
|
||||||
minecraft_version=1.20
|
minecraft_version=1.19.2
|
||||||
yarn_mappings=1.20+build.1
|
yarn_mappings=1.19.2+build.28
|
||||||
loader_version=0.14.21
|
loader_version=0.14.10
|
||||||
|
|
||||||
#Fabric api
|
#Fabric api
|
||||||
fabric_version=0.83.0+1.20
|
fabric_version=0.64.0+1.19.2
|
||||||
|
|
||||||
#Cloth Config
|
#Cloth Config
|
||||||
cloth_version=11.0.98
|
cloth_version=8.2.88
|
||||||
|
|
||||||
#ModMenu
|
#ModMenu
|
||||||
modmenu_version=7.0.0-beta.2
|
modmenu_version=4.1.0
|
||||||
|
|
||||||
databreaker_version=0.2.10
|
#Lazy DFU for faster dev start
|
||||||
|
lazydfu_version=v0.1.3
|
||||||
|
|
||||||
#Hash of commit form which parallel gzip will be build
|
#Hash of commit form which parallel gzip will be build
|
||||||
pgzip_commit_hash=af5f5c297e735f3f2df7aa4eb0e19a5810b8aff6
|
pgzip_commit_hash=af5f5c297e735f3f2df7aa4eb0e19a5810b8aff6
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version = 3.1.0
|
mod_version = 2.5.0
|
||||||
maven_group = net.szum123321
|
maven_group = net.szum123321
|
||||||
archives_base_name = textile_backup
|
archives_base_name = textile_backup
|
|
@ -1,5 +1,5 @@
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|
|
@ -1,33 +1,30 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2022 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup;
|
package net.szum123321.textile_backup;
|
||||||
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.szum123321.textile_backup.core.digest.BalticHash;
|
|
||||||
import net.szum123321.textile_backup.core.digest.Hash;
|
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
|
import net.szum123321.textile_backup.core.create.MakeBackupRunnable;
|
||||||
import net.szum123321.textile_backup.core.restore.AwaitThread;
|
import net.szum123321.textile_backup.core.restore.AwaitThread;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.ByteOrder;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
@ -37,48 +34,19 @@ import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.Supplier;
|
|
||||||
import java.util.zip.CRC32;
|
|
||||||
|
|
||||||
public class Globals {
|
public class Globals {
|
||||||
public static final Globals INSTANCE = new Globals();
|
public static final Globals INSTANCE = new Globals();
|
||||||
private static final TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||||
public static final DateTimeFormatter defaultDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss");
|
public final static DateTimeFormatter defaultDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss");
|
||||||
public static final Supplier<Hash> CHECKSUM_SUPPLIER = BalticHash::new;/*() -> new Hash() {
|
|
||||||
private final CRC32 crc = new CRC32();
|
|
||||||
|
|
||||||
@Override
|
private ExecutorService executorService = null;// = Executors.newSingleThreadExecutor();
|
||||||
public void update ( int b){
|
|
||||||
crc.update(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update ( long b) {
|
|
||||||
ByteBuffer v = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
|
|
||||||
v.putLong(b);
|
|
||||||
crc.update(v.array());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update ( byte[] b, int off, int len){
|
|
||||||
crc.update(b, off, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getValue () {
|
|
||||||
return crc.getValue();
|
|
||||||
}
|
|
||||||
};*/
|
|
||||||
|
|
||||||
private ExecutorService executorService = null;//TODO: AAAAAAAAAAAAAAA MEMORY LEAK!!!!!!!!!
|
|
||||||
public final AtomicBoolean globalShutdownBackupFlag = new AtomicBoolean(true);
|
public final AtomicBoolean globalShutdownBackupFlag = new AtomicBoolean(true);
|
||||||
public boolean disableWatchdog = false;
|
public boolean disableWatchdog = false;
|
||||||
private boolean disableTMPFiles = false;
|
private boolean disableTMPFiles = false;
|
||||||
private AwaitThread restoreAwaitThread = null;
|
private AwaitThread restoreAwaitThread = null;
|
||||||
private Path lockedPath = null;
|
private Path lockedPath = null;
|
||||||
|
|
||||||
private String combinedVersionString;
|
|
||||||
|
|
||||||
private Globals() {}
|
private Globals() {}
|
||||||
|
|
||||||
public ExecutorService getQueueExecutor() { return executorService; }
|
public ExecutorService getQueueExecutor() { return executorService; }
|
||||||
|
@ -94,16 +62,16 @@ public class Globals {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if(!executorService.awaitTermination(timeout, TimeUnit.MICROSECONDS)) {
|
if(!executorService.awaitTermination(timeout, TimeUnit.MICROSECONDS)) {
|
||||||
log.error("在等待当前运行的备份完成时发生了超时!");
|
log.error("Timeout occurred while waiting for currently running backups to finish!");
|
||||||
executorService.shutdownNow().stream()
|
executorService.shutdownNow().stream()
|
||||||
// .filter(r -> r instanceof ExecutableBackup)
|
.filter(r -> r instanceof MakeBackupRunnable)
|
||||||
// .map(r -> (ExecutableBackup)r)
|
.map(r -> (MakeBackupRunnable)r)
|
||||||
.forEach(r -> log.error("Dropping: {}", r.toString()));
|
.forEach(r -> log.error("Dropping: {}", r.toString()));
|
||||||
if(!executorService.awaitTermination(1000, TimeUnit.MICROSECONDS))
|
if(!executorService.awaitTermination(1000, TimeUnit.MICROSECONDS))
|
||||||
log.error("无法关闭执行器!");
|
log.error("Couldn't shut down the executor!");
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
log.error("发生了一个异常!(灾难性的错误)", e);
|
log.error("An exception occurred!", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -116,31 +84,23 @@ public class Globals {
|
||||||
public Optional<Path> getLockedFile() { return Optional.ofNullable(lockedPath); }
|
public Optional<Path> getLockedFile() { return Optional.ofNullable(lockedPath); }
|
||||||
public void setLockedFile(Path p) { lockedPath = p; }
|
public void setLockedFile(Path p) { lockedPath = p; }
|
||||||
|
|
||||||
public synchronized boolean disableTMPFS() { return disableTMPFiles; }
|
public boolean disableTMPFS() { return disableTMPFiles; }
|
||||||
public synchronized void updateTMPFSFlag(MinecraftServer server) {
|
public void updateTMPFSFlag(MinecraftServer server) {
|
||||||
disableTMPFiles = false;
|
disableTMPFiles = false;
|
||||||
Path tmp_dir = Path.of(System.getProperty("java.io.tmpdir"));
|
Path tmp_dir = Path.of(System.getProperty("java.io.tmpdir"));
|
||||||
if(
|
if(
|
||||||
FileUtils.sizeOfDirectory(Utilities.getWorldFolder(server).toFile()) >=
|
FileUtils.sizeOfDirectory(Utilities.getWorldFolder(server).toFile()) >=
|
||||||
tmp_dir.toFile().getUsableSpace()
|
tmp_dir.toFile().getUsableSpace()
|
||||||
) {
|
) {
|
||||||
log.error("TMP(临时)文件目录没有可用空间 ({})", tmp_dir);
|
log.error("Not enough space left in TMP directory! ({})", tmp_dir);
|
||||||
disableTMPFiles = true;
|
disableTMPFiles = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Files.isWritable(tmp_dir)) {
|
if(!Files.isWritable(tmp_dir)) {
|
||||||
log.error("TMP(临时)文件目录({})是只读的", tmp_dir);
|
log.error("TMP filesystem ({}) is read-only!", tmp_dir);
|
||||||
disableTMPFiles = true;
|
disableTMPFiles = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(disableTMPFiles) log.error("Might cause: https://github.com/Szum123321/textile_backup/wiki/ZIP-Problems");
|
if(disableTMPFiles) log.error("Might cause: https://github.com/Szum123321/textile_backup/wiki/ZIP-Problems");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCombinedVersionString() {
|
|
||||||
return combinedVersionString;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCombinedVersionString(String combinedVersionString) {
|
|
||||||
this.combinedVersionString = combinedVersionString;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
Copyright (C) 2020 Szum123321
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup;
|
package net.szum123321.textile_backup;
|
||||||
|
|
||||||
|
@ -26,7 +26,6 @@ import net.fabricmc.api.ModInitializer;
|
||||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||||
import net.fabricmc.loader.api.FabricLoader;
|
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
import net.szum123321.textile_backup.commands.create.CleanupCommand;
|
import net.szum123321.textile_backup.commands.create.CleanupCommand;
|
||||||
import net.szum123321.textile_backup.commands.create.StartBackupCommand;
|
import net.szum123321.textile_backup.commands.create.StartBackupCommand;
|
||||||
|
@ -39,9 +38,9 @@ import net.szum123321.textile_backup.commands.restore.RestoreBackupCommand;
|
||||||
import net.szum123321.textile_backup.config.ConfigHelper;
|
import net.szum123321.textile_backup.config.ConfigHelper;
|
||||||
import net.szum123321.textile_backup.config.ConfigPOJO;
|
import net.szum123321.textile_backup.config.ConfigPOJO;
|
||||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||||
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
import net.szum123321.textile_backup.core.create.BackupScheduler;
|
import net.szum123321.textile_backup.core.create.BackupScheduler;
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.MakeBackupRunnableFactory;
|
||||||
import net.szum123321.textile_backup.test.BalticHashTest;
|
|
||||||
|
|
||||||
public class TextileBackup implements ModInitializer {
|
public class TextileBackup implements ModInitializer {
|
||||||
public static final String MOD_NAME = "Textile Backup";
|
public static final String MOD_NAME = "Textile Backup";
|
||||||
|
@ -52,13 +51,7 @@ public class TextileBackup implements ModInitializer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
Globals.INSTANCE.setCombinedVersionString(
|
log.info("Starting Textile Backup by Szum123321");
|
||||||
FabricLoader.getInstance().getModContainer(MOD_ID).orElseThrow().getMetadata().getVersion().getFriendlyString() +
|
|
||||||
":" +
|
|
||||||
FabricLoader.getInstance().getModContainer("minecraft").orElseThrow().getMetadata().getVersion().getFriendlyString()
|
|
||||||
);
|
|
||||||
|
|
||||||
log.info("Starting Textile Backup {} by Szum123321", Globals.INSTANCE.getCombinedVersionString());
|
|
||||||
|
|
||||||
ConfigHelper.updateInstance(AutoConfig.register(ConfigPOJO.class, JanksonConfigSerializer::new));
|
ConfigHelper.updateInstance(AutoConfig.register(ConfigPOJO.class, JanksonConfigSerializer::new));
|
||||||
|
|
||||||
|
@ -70,21 +63,19 @@ public class TextileBackup implements ModInitializer {
|
||||||
Globals.INSTANCE.updateTMPFSFlag(server);
|
Globals.INSTANCE.updateTMPFSFlag(server);
|
||||||
});
|
});
|
||||||
|
|
||||||
//Wait 60s for already submitted backups to finish. After that kill the bastards and run the one last if required
|
//Wait 60s for already submited backups to finish. After that kill the bastards and run the one last if required
|
||||||
ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
|
ServerLifecycleEvents.SERVER_STOPPED.register(server -> {
|
||||||
Globals.INSTANCE.shutdownQueueExecutor(60000);
|
Globals.INSTANCE.shutdownQueueExecutor(60000);
|
||||||
|
|
||||||
if (config.get().shutdownBackup && Globals.INSTANCE.globalShutdownBackupFlag.get()) {
|
if (config.get().shutdownBackup && Globals.INSTANCE.globalShutdownBackupFlag.get()) {
|
||||||
try {
|
MakeBackupRunnableFactory.create(
|
||||||
ExecutableBackup.Builder
|
BackupContext.Builder
|
||||||
.newBackupContextBuilder()
|
.newBackupContextBuilder()
|
||||||
.setServer(server)
|
.setServer(server)
|
||||||
.setInitiator(ActionInitiator.Shutdown)
|
.setInitiator(ActionInitiator.Shutdown)
|
||||||
.setComment("shutdown")
|
.setComment("shutdown")
|
||||||
.announce()
|
.build()
|
||||||
.build()
|
).run();
|
||||||
.call();
|
|
||||||
} catch (Exception ignored) {}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -23,7 +23,7 @@ import net.minecraft.text.Text;
|
||||||
import net.minecraft.text.MutableText;
|
import net.minecraft.text.MutableText;
|
||||||
import net.minecraft.util.Formatting;
|
import net.minecraft.util.Formatting;
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
import org.apache.logging.log4j.Level;
|
import org.apache.logging.log4j.Level;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
@ -94,7 +94,7 @@ public class TextileLogger {
|
||||||
else if(level.intLevel() <= Level.WARN.intLevel()) text.formatted(Formatting.RED);
|
else if(level.intLevel() <= Level.WARN.intLevel()) text.formatted(Formatting.RED);
|
||||||
else text.formatted(Formatting.WHITE);
|
else text.formatted(Formatting.WHITE);
|
||||||
|
|
||||||
source.sendFeedback(() -> prefixText.copy().append(text), false);
|
source.sendFeedback(prefixText.copy().append(text), false);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -112,7 +112,7 @@ public class TextileLogger {
|
||||||
sendFeedback(Level.INFO, source, msg, args);
|
sendFeedback(Level.INFO, source, msg, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendInfo(ExecutableBackup context, String msg, Object... args) {
|
public void sendInfo(BackupContext context, String msg, Object... args) {
|
||||||
sendInfo(context.commandSource(), msg, args);
|
sendInfo(context.commandSource(), msg, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,8 +120,7 @@ public class TextileLogger {
|
||||||
sendFeedback(Level.ERROR, source, msg, args);
|
sendFeedback(Level.ERROR, source, msg, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendError(BackupContext context, String msg, Object... args) {
|
||||||
public void sendError(ExecutableBackup context, String msg, Object... args) {
|
|
||||||
sendError(context.commandSource(), msg, args);
|
sendError(context.commandSource(), msg, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +134,7 @@ public class TextileLogger {
|
||||||
sendToPlayerAndLog(Level.INFO, source, msg, args);
|
sendToPlayerAndLog(Level.INFO, source, msg, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendInfoAL(ExecutableBackup context, String msg, Object... args) {
|
public void sendInfoAL(BackupContext context, String msg, Object... args) {
|
||||||
sendInfoAL(context.commandSource(), msg, args);
|
sendInfoAL(context.commandSource(), msg, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,7 +142,7 @@ public class TextileLogger {
|
||||||
sendToPlayerAndLog(Level.ERROR, source, msg, args);
|
sendToPlayerAndLog(Level.ERROR, source, msg, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendErrorAL(ExecutableBackup context, String msg, Object... args) {
|
public void sendErrorAL(BackupContext context, String msg, Object... args) {
|
||||||
sendErrorAL(context.commandSource(), msg, args);
|
sendErrorAL(context.commandSource(), msg, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2022 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.client;
|
package net.szum123321.textile_backup.client;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
Copyright (C) 2020 Szum123321
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.commands.create;
|
package net.szum123321.textile_backup.commands.create;
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ public class CleanupCommand {
|
||||||
private static int execute(ServerCommandSource source) {
|
private static int execute(ServerCommandSource source) {
|
||||||
log.sendInfo(
|
log.sendInfo(
|
||||||
source,
|
source,
|
||||||
"删除了: {} 文件.",
|
"Deleted: {} files.",
|
||||||
new Cleanup(source, Utilities.getLevelName(source.getServer())).call()
|
new Cleanup(source, Utilities.getLevelName(source.getServer())).call()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
Copyright (C) 2020 Szum123321
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.commands.create;
|
package net.szum123321.textile_backup.commands.create;
|
||||||
|
|
||||||
|
@ -25,7 +25,8 @@ import net.minecraft.server.command.ServerCommandSource;
|
||||||
import net.szum123321.textile_backup.Globals;
|
import net.szum123321.textile_backup.Globals;
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
import net.szum123321.textile_backup.TextileBackup;
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
import net.szum123321.textile_backup.TextileLogger;
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
|
import net.szum123321.textile_backup.core.create.MakeBackupRunnableFactory;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
@ -40,15 +41,22 @@ public class StartBackupCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int execute(ServerCommandSource source, @Nullable String comment) {
|
private static int execute(ServerCommandSource source, @Nullable String comment) {
|
||||||
Globals.INSTANCE.getQueueExecutor().submit(
|
try {
|
||||||
ExecutableBackup.Builder
|
Globals.INSTANCE.getQueueExecutor().submit(
|
||||||
.newBackupContextBuilder()
|
MakeBackupRunnableFactory.create(
|
||||||
.setCommandSource(source)
|
BackupContext.Builder
|
||||||
.setComment(comment)
|
.newBackupContextBuilder()
|
||||||
.guessInitiator()
|
.setCommandSource(source)
|
||||||
.saveServer()
|
.setComment(comment)
|
||||||
.build()
|
.guessInitiator()
|
||||||
);
|
.saveServer()
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Something went wrong while executing command!", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -49,7 +49,7 @@ public class BlacklistCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int help(ServerCommandSource source) {
|
private static int help(ServerCommandSource source) {
|
||||||
log.sendInfo(source, "可用的命令有:add [player],remove [player],list. ");
|
log.sendInfo(source, "Available command are: add [player], remove [player], list.");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ public class BlacklistCommand {
|
||||||
private static int executeList(ServerCommandSource source) {
|
private static int executeList(ServerCommandSource source) {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
builder.append("目前在黑名单上的有:");
|
builder.append("Currently on the blacklist are: ");
|
||||||
|
|
||||||
for(String name : config.get().playerBlacklist){
|
for(String name : config.get().playerBlacklist){
|
||||||
builder.append(name);
|
builder.append(name);
|
||||||
|
@ -73,24 +73,24 @@ public class BlacklistCommand {
|
||||||
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
||||||
|
|
||||||
if(config.get().playerBlacklist.contains(player.getEntityName())) {
|
if(config.get().playerBlacklist.contains(player.getEntityName())) {
|
||||||
log.sendInfo(ctx.getSource(), "玩家: {} 已经在黑名单中!", player.getEntityName());
|
log.sendInfo(ctx.getSource(), "Player: {} is already blacklisted.", player.getEntityName());
|
||||||
} else {
|
} else {
|
||||||
config.get().playerBlacklist.add(player.getEntityName());
|
config.get().playerBlacklist.add(player.getEntityName());
|
||||||
config.save();
|
config.save();
|
||||||
|
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
builder.append("玩家: ");
|
builder.append("Player: ");
|
||||||
builder.append(player.getEntityName());
|
builder.append(player.getEntityName());
|
||||||
builder.append(" 被添加到黑名单");
|
builder.append(" added to the blacklist");
|
||||||
|
|
||||||
if(config.get().playerWhitelist.contains(player.getEntityName())){
|
if(config.get().playerWhitelist.contains(player.getEntityName())){
|
||||||
config.get().playerWhitelist.remove(player.getEntityName());
|
config.get().playerWhitelist.remove(player.getEntityName());
|
||||||
config.save();
|
config.save();
|
||||||
builder.append(" 并且被移除白名单");
|
builder.append(" and removed form the whitelist");
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.append(" 成功.");
|
builder.append(" successfully.");
|
||||||
|
|
||||||
ctx.getSource().getServer().getCommandManager().sendCommandTree(player);
|
ctx.getSource().getServer().getCommandManager().sendCommandTree(player);
|
||||||
|
|
||||||
|
@ -104,14 +104,14 @@ public class BlacklistCommand {
|
||||||
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
||||||
|
|
||||||
if(!config.get().playerBlacklist.contains(player.getEntityName())) {
|
if(!config.get().playerBlacklist.contains(player.getEntityName())) {
|
||||||
log.sendInfo(ctx.getSource(), "玩家: {} 还从未被列入黑名单.", player.getEntityName());
|
log.sendInfo(ctx.getSource(), "Player: {} newer was blacklisted.", player.getEntityName());
|
||||||
} else {
|
} else {
|
||||||
config.get().playerBlacklist.remove(player.getEntityName());
|
config.get().playerBlacklist.remove(player.getEntityName());
|
||||||
config.save();
|
config.save();
|
||||||
|
|
||||||
ctx.getSource().getServer().getCommandManager().sendCommandTree(player);
|
ctx.getSource().getServer().getCommandManager().sendCommandTree(player);
|
||||||
|
|
||||||
log.sendInfo(ctx.getSource(), "玩家: {} 被移除黑名单成功! ", player.getEntityName());
|
log.sendInfo(ctx.getSource(), "Player: {} removed from the blacklist successfully.", player.getEntityName());
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -61,26 +61,26 @@ public class DeleteCommand {
|
||||||
Path root = Utilities.getBackupRootPath(Utilities.getLevelName(source.getServer()));
|
Path root = Utilities.getBackupRootPath(Utilities.getLevelName(source.getServer()));
|
||||||
|
|
||||||
RestoreableFile.applyOnFiles(root, Optional.empty(),
|
RestoreableFile.applyOnFiles(root, Optional.empty(),
|
||||||
e -> log.sendErrorAL(source, "在尝试删除备份文件时发生了异常!", e),
|
e -> log.sendErrorAL(source, "An exception occurred while trying to delete a file!", e),
|
||||||
stream -> stream.filter(f -> f.getCreationTime().equals(dateTime)).map(RestoreableFile::getFile).findFirst()
|
stream -> stream.filter(f -> f.getCreationTime().equals(dateTime)).map(RestoreableFile::getFile).findFirst()
|
||||||
).ifPresentOrElse(file -> {
|
).ifPresentOrElse(file -> {
|
||||||
if(Globals.INSTANCE.getLockedFile().filter(p -> p == file).isEmpty()) {
|
if(Globals.INSTANCE.getLockedFile().filter(p -> p == file).isEmpty()) {
|
||||||
try {
|
try {
|
||||||
Files.delete((Path) file);
|
Files.delete((Path) file);
|
||||||
log.sendInfo(source, "备份: {} 被成功删除!", file);
|
log.sendInfo(source, "File {} successfully deleted!", file);
|
||||||
|
|
||||||
if(Utilities.wasSentByPlayer(source))
|
if(Utilities.wasSentByPlayer(source))
|
||||||
log.info("玩家 {} 删除了备份: {}.", source.getPlayer().getName(), file);
|
log.info("Player {} deleted {}.", source.getPlayer().getName(), file);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.sendError(source, "在尝试删除备份文件时发生了异常!");
|
log.sendError(source, "Something went wrong while deleting file!");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.sendError(source, "由于备份正在恢复中,无法删除该文件.");
|
log.sendError(source, "Couldn't delete the file because it's being restored right now.");
|
||||||
log.sendHint(source, "如果您想中止恢复过程,请使用以下命令:/backup killR");
|
log.sendHint(source, "If you want to abort restoration then use: /backup killR");
|
||||||
}
|
}
|
||||||
}, () -> {
|
}, () -> {
|
||||||
log.sendInfo(source, "根据您提供的文件名找不到相应的文件.");
|
log.sendInfo(source, "Couldn't find file by this name.");
|
||||||
log.sendInfo(source, "也许您可以试试: /backup list");
|
log.sendInfo(source, "Maybe try /backup list");
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -37,14 +37,14 @@ public class ListBackupsCommand {
|
||||||
var backups = RestoreHelper.getAvailableBackups(ctx.getSource().getServer());
|
var backups = RestoreHelper.getAvailableBackups(ctx.getSource().getServer());
|
||||||
|
|
||||||
if(backups.size() == 0) {
|
if(backups.size() == 0) {
|
||||||
builder.append("该世界没有可用的备份文件. ");
|
builder.append("There a no backups available for this world.");
|
||||||
} else if(backups.size() == 1) {
|
} else if(backups.size() == 1) {
|
||||||
builder.append("只有一个可用的备份文件: ");
|
builder.append("There is only one backup available: ");
|
||||||
builder.append(backups.get(0).toString());
|
builder.append(backups.get(0).toString());
|
||||||
} else {
|
} else {
|
||||||
backups.sort(null);
|
backups.sort(null);
|
||||||
Iterator<RestoreableFile> iterator = backups.iterator();
|
Iterator<RestoreableFile> iterator = backups.iterator();
|
||||||
builder.append("可用的备份文件:\n");
|
builder.append("Available backups:\n");
|
||||||
|
|
||||||
builder.append(iterator.next());
|
builder.append(iterator.next());
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -49,7 +49,7 @@ public class WhitelistCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int help(ServerCommandSource source){
|
private static int help(ServerCommandSource source){
|
||||||
log.sendInfo(source, "可用的命令有: add [player], remove [player], list.");
|
log.sendInfo(source, "Available command are: add [player], remove [player], list.");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ public class WhitelistCommand {
|
||||||
private static int executeList(ServerCommandSource source){
|
private static int executeList(ServerCommandSource source){
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
builder.append("目前在白名单的有: ");
|
builder.append("Currently on the whitelist are: ");
|
||||||
|
|
||||||
for(String name : config.get().playerWhitelist){
|
for(String name : config.get().playerWhitelist){
|
||||||
builder.append(name);
|
builder.append(name);
|
||||||
|
@ -73,24 +73,24 @@ public class WhitelistCommand {
|
||||||
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
||||||
|
|
||||||
if(config.get().playerWhitelist.contains(player.getEntityName())) {
|
if(config.get().playerWhitelist.contains(player.getEntityName())) {
|
||||||
log.sendInfo(ctx.getSource(), "玩家: {} 已经在白名单列表里.", player.getEntityName());
|
log.sendInfo(ctx.getSource(), "Player: {} is already whitelisted.", player.getEntityName());
|
||||||
} else {
|
} else {
|
||||||
config.get().playerWhitelist.add(player.getEntityName());
|
config.get().playerWhitelist.add(player.getEntityName());
|
||||||
config.save();
|
config.save();
|
||||||
|
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
builder.append("玩家: ");
|
builder.append("Player: ");
|
||||||
builder.append(player.getEntityName());
|
builder.append(player.getEntityName());
|
||||||
builder.append(" 被添加的白名单");
|
builder.append(" added to the whitelist");
|
||||||
|
|
||||||
if(config.get().playerBlacklist.contains(player.getEntityName())){
|
if(config.get().playerBlacklist.contains(player.getEntityName())){
|
||||||
config.get().playerBlacklist.remove(player.getEntityName());
|
config.get().playerBlacklist.remove(player.getEntityName());
|
||||||
config.save();
|
config.save();
|
||||||
builder.append(" 并且被移除黑名单");
|
builder.append(" and removed form the blacklist");
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.append(" 成功.");
|
builder.append(" successfully.");
|
||||||
|
|
||||||
ctx.getSource().getServer().getCommandManager().sendCommandTree(player);
|
ctx.getSource().getServer().getCommandManager().sendCommandTree(player);
|
||||||
|
|
||||||
|
@ -104,14 +104,14 @@ public class WhitelistCommand {
|
||||||
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
ServerPlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
|
||||||
|
|
||||||
if(!config.get().playerWhitelist.contains(player.getEntityName())) {
|
if(!config.get().playerWhitelist.contains(player.getEntityName())) {
|
||||||
log.sendInfo(ctx.getSource(), "玩家: {} 还从未被列入白名单.", player.getEntityName());
|
log.sendInfo(ctx.getSource(), "Player: {} newer was whitelisted.", player.getEntityName());
|
||||||
} else {
|
} else {
|
||||||
config.get().playerWhitelist.remove(player.getEntityName());
|
config.get().playerWhitelist.remove(player.getEntityName());
|
||||||
config.save();
|
config.save();
|
||||||
|
|
||||||
ctx.getSource().getServer().getCommandManager().sendCommandTree(player);
|
ctx.getSource().getServer().getCommandManager().sendCommandTree(player);
|
||||||
|
|
||||||
log.sendInfo(ctx.getSource(), "玩家: {} 被移除白名单成功!", player.getEntityName());
|
log.sendInfo(ctx.getSource(), "Player: {} removed from the whitelist successfully.", player.getEntityName());
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -33,7 +33,7 @@ public class KillRestoreCommand {
|
||||||
return CommandManager.literal("killR")
|
return CommandManager.literal("killR")
|
||||||
.executes(ctx -> {
|
.executes(ctx -> {
|
||||||
if(Globals.INSTANCE.getAwaitThread().filter(Thread::isAlive).isEmpty()) {
|
if(Globals.INSTANCE.getAwaitThread().filter(Thread::isAlive).isEmpty()) {
|
||||||
log.sendInfo(ctx.getSource(), "无法停止备份恢复过程");
|
log.sendInfo(ctx.getSource(), "Failed to stop backup restoration");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,13 +43,13 @@ public class KillRestoreCommand {
|
||||||
Globals.INSTANCE.globalShutdownBackupFlag.set(true);
|
Globals.INSTANCE.globalShutdownBackupFlag.set(true);
|
||||||
Globals.INSTANCE.setLockedFile(null);
|
Globals.INSTANCE.setLockedFile(null);
|
||||||
|
|
||||||
log.info("{} 备份恢复操作已被取消", Utilities.wasSentByPlayer(ctx.getSource()) ?
|
log.info("{} cancelled backup restoration.", Utilities.wasSentByPlayer(ctx.getSource()) ?
|
||||||
"Player: " + ctx.getSource().getName() :
|
"Player: " + ctx.getSource().getName() :
|
||||||
"SERVER"
|
"SERVER"
|
||||||
);
|
);
|
||||||
|
|
||||||
if(Utilities.wasSentByPlayer(ctx.getSource()))
|
if(Utilities.wasSentByPlayer(ctx.getSource()))
|
||||||
log.sendInfo(ctx.getSource(), "备份恢复已成功停止. ");
|
log.sendInfo(ctx.getSource(), "Backup restoration successfully stopped.");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -63,10 +63,10 @@ public class RestoreBackupCommand {
|
||||||
).executes(context -> {
|
).executes(context -> {
|
||||||
ServerCommandSource source = context.getSource();
|
ServerCommandSource source = context.getSource();
|
||||||
|
|
||||||
log.sendInfo(source, "要恢复给定的备份,您必须以以下格式提供准确的创建时间:");
|
log.sendInfo(source, "To restore given backup you have to provide exact creation time in format:");
|
||||||
log.sendInfo(source, "[年]-[月]-[日]_[小时].[分钟].[秒]");
|
log.sendInfo(source, "[YEAR]-[MONTH]-[DAY]_[HOUR].[MINUTE].[SECOND]");
|
||||||
log.sendInfo(source, "示例:/backup restore 2020-08-05_10.58.33");
|
log.sendInfo(source, "Example: /backup restore 2020-08-05_10.58.33");
|
||||||
log.sendInfo(source, "您还可以输入 '/backup restore latest' 来恢复最新的备份。");
|
log.sendInfo(source, "You may also type '/backup restore latest' to restore the freshest backup");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
});
|
});
|
||||||
|
@ -74,7 +74,7 @@ public class RestoreBackupCommand {
|
||||||
|
|
||||||
private static int execute(String file, @Nullable String comment, ServerCommandSource source) throws CommandSyntaxException {
|
private static int execute(String file, @Nullable String comment, ServerCommandSource source) throws CommandSyntaxException {
|
||||||
if(Globals.INSTANCE.getAwaitThread().filter(Thread::isAlive).isPresent()) {
|
if(Globals.INSTANCE.getAwaitThread().filter(Thread::isAlive).isPresent()) {
|
||||||
log.sendInfo(source, "已经有其他人开始了另一个恢复操作。");
|
log.sendInfo(source, "Someone has already started another restoration.");
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -96,10 +96,10 @@ public class RestoreBackupCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(backupFile.isEmpty()) {
|
if(backupFile.isEmpty()) {
|
||||||
log.sendInfo(source, "在{}上没有找到创建的文件!", dateTime.format(Globals.defaultDateTimeFormatter));
|
log.sendInfo(source, "No file created on {} was found!", dateTime.format(Globals.defaultDateTimeFormatter));
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
log.info("找到要恢复的文件:{}", backupFile.get().getFile().getFileName().toString());
|
log.info("Found file to restore {}", backupFile.get().getFile().getFileName().toString());
|
||||||
|
|
||||||
Globals.INSTANCE.setAwaitThread(
|
Globals.INSTANCE.setAwaitThread(
|
||||||
RestoreHelper.create(
|
RestoreHelper.create(
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -18,17 +18,16 @@
|
||||||
|
|
||||||
package net.szum123321.textile_backup.config;
|
package net.szum123321.textile_backup.config;
|
||||||
|
|
||||||
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.annotation.SerializedName;
|
import blue.endless.jankson.annotation.SerializedName;
|
||||||
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Comment;
|
|
||||||
import me.shedaniel.autoconfig.ConfigData;
|
import me.shedaniel.autoconfig.ConfigData;
|
||||||
import me.shedaniel.autoconfig.annotation.Config;
|
import me.shedaniel.autoconfig.annotation.Config;
|
||||||
import me.shedaniel.autoconfig.annotation.ConfigEntry;
|
import me.shedaniel.autoconfig.annotation.ConfigEntry;
|
||||||
|
import me.shedaniel.cloth.clothconfig.shadowed.blue.endless.jankson.Comment;
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
import net.szum123321.textile_backup.TextileBackup;
|
||||||
|
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
//TODO: Remove BZIP2 and LZMA compressors. As for the popular vote
|
|
||||||
@Config(name = TextileBackup.MOD_ID)
|
@Config(name = TextileBackup.MOD_ID)
|
||||||
public class ConfigPOJO implements ConfigData {
|
public class ConfigPOJO implements ConfigData {
|
||||||
@Comment("\nShould every world have its own backup folder?\n")
|
@Comment("\nShould every world have its own backup folder?\n")
|
||||||
|
@ -91,7 +90,7 @@ public class ConfigPOJO implements ConfigData {
|
||||||
public long maxAge = 0;
|
public long maxAge = 0;
|
||||||
|
|
||||||
@Comment("""
|
@Comment("""
|
||||||
\nMaximum size of backup folder in kibi bytes (1024).
|
\nMaximum size of backup folder in kilo bytes (1024).
|
||||||
If set to 0 then backups will not be deleted
|
If set to 0 then backups will not be deleted
|
||||||
""")
|
""")
|
||||||
@ConfigEntry.Gui.Tooltip()
|
@ConfigEntry.Gui.Tooltip()
|
||||||
|
@ -115,6 +114,8 @@ public class ConfigPOJO implements ConfigData {
|
||||||
\nAvailable formats are:
|
\nAvailable formats are:
|
||||||
ZIP - normal zip archive using standard deflate compression
|
ZIP - normal zip archive using standard deflate compression
|
||||||
GZIP - tar.gz using gzip compression
|
GZIP - tar.gz using gzip compression
|
||||||
|
BZIP2 - tar.bz2 archive using bzip2 compression
|
||||||
|
LZMA - tar.xz using lzma compression
|
||||||
TAR - .tar with no compression
|
TAR - .tar with no compression
|
||||||
""")
|
""")
|
||||||
@ConfigEntry.Gui.Tooltip()
|
@ConfigEntry.Gui.Tooltip()
|
||||||
|
@ -161,14 +162,6 @@ public class ConfigPOJO implements ConfigData {
|
||||||
@ConfigEntry.Gui.Tooltip()
|
@ConfigEntry.Gui.Tooltip()
|
||||||
public String dateTimeFormat = "yyyy.MM.dd_HH-mm-ss";
|
public String dateTimeFormat = "yyyy.MM.dd_HH-mm-ss";
|
||||||
|
|
||||||
@Comment("""
|
|
||||||
\nThe Strict mode (default) aborts backup creation in case of any problem and deletes created files
|
|
||||||
Permissible mode keeps partial/damaged backup but won't allow to restore it
|
|
||||||
Very Permissible mode will skip the verification process. THIS MOST CERTAINLY WILL LEAD TO DATA LOSS OR CORRUPTION
|
|
||||||
""")
|
|
||||||
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
|
|
||||||
public IntegrityVerificationMode integrityVerificationMode = IntegrityVerificationMode.STRICT;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validatePostLoad() throws ValidationException {
|
public void validatePostLoad() throws ValidationException {
|
||||||
if(compressionCoreCountLimit > Runtime.getRuntime().availableProcessors())
|
if(compressionCoreCountLimit > Runtime.getRuntime().availableProcessors())
|
||||||
|
@ -184,16 +177,6 @@ public class ConfigPOJO implements ConfigData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum IntegrityVerificationMode {
|
|
||||||
STRICT,
|
|
||||||
PERMISSIBLE,
|
|
||||||
VERY_PERMISSIBLE;
|
|
||||||
|
|
||||||
public boolean isStrict() { return this == STRICT; }
|
|
||||||
|
|
||||||
public boolean verify() { return this != VERY_PERMISSIBLE; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum ArchiveFormat {
|
public enum ArchiveFormat {
|
||||||
ZIP("zip"),
|
ZIP("zip"),
|
||||||
GZIP("tar", "gz"),
|
GZIP("tar", "gz"),
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -26,7 +26,9 @@ public enum ActionInitiator {
|
||||||
ServerConsole("Server Console", "from"), //some/ting typed a command and it was not a player (command blocks and server console count)
|
ServerConsole("Server Console", "from"), //some/ting typed a command and it was not a player (command blocks and server console count)
|
||||||
Timer("Timer", "by"), //a.k.a scheduler
|
Timer("Timer", "by"), //a.k.a scheduler
|
||||||
Shutdown("Server Shutdown", "by"),
|
Shutdown("Server Shutdown", "by"),
|
||||||
Restore("Backup Restoration", "because of");
|
Restore("Backup Restoration", "because of"),
|
||||||
|
Null("Null (That shouldn't have happened)", "form");
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String prefix;
|
private final String prefix;
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2022 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core;
|
package net.szum123321.textile_backup.core;
|
||||||
|
@ -58,7 +59,7 @@ public class Cleanup implements Callable<Integer> {
|
||||||
final long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
|
final long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
|
||||||
|
|
||||||
deletedFiles += RestoreableFile.applyOnFiles(root, 0L,
|
deletedFiles += RestoreableFile.applyOnFiles(root, 0L,
|
||||||
e -> log.error("尝试删除旧文件时发生异常!", e),
|
e -> log.error("An exception occurred while trying to delete old files!", e),
|
||||||
stream -> stream.filter(f -> now - f.getCreationTime().toEpochSecond(ZoneOffset.UTC) > config.get().maxAge)
|
stream -> stream.filter(f -> now - f.getCreationTime().toEpochSecond(ZoneOffset.UTC) > config.get().maxAge)
|
||||||
.filter(f -> deleteFile(f.getFile(), ctx))
|
.filter(f -> deleteFile(f.getFile(), ctx))
|
||||||
.count()
|
.count()
|
||||||
|
@ -72,7 +73,7 @@ public class Cleanup implements Callable<Integer> {
|
||||||
long n = counts[0], size = counts[1];
|
long n = counts[0], size = counts[1];
|
||||||
|
|
||||||
var it = RestoreableFile.applyOnFiles(root, null,
|
var it = RestoreableFile.applyOnFiles(root, null,
|
||||||
e -> log.error("尝试删除旧文件时发生异常!", e),
|
e -> log.error("An exception occurred while trying to delete old files!", e),
|
||||||
s -> s.sorted().toList().iterator());
|
s -> s.sorted().toList().iterator());
|
||||||
|
|
||||||
if(Objects.isNull(it)) return deletedFiles;
|
if(Objects.isNull(it)) return deletedFiles;
|
||||||
|
@ -104,13 +105,13 @@ public class Cleanup implements Callable<Integer> {
|
||||||
try {
|
try {
|
||||||
size += Files.size(f.getFile());
|
size += Files.size(f.getFile());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("无法获取文件的大小 " + f.getFile(), e);
|
log.error("Couldn't get size of " + f.getFile(), e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("在计算文件数量时发生错误!", e);
|
log.error("Error while counting files!", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new long[]{n, size};
|
return new long[]{n, size};
|
||||||
|
@ -128,8 +129,8 @@ public class Cleanup implements Callable<Integer> {
|
||||||
Files.delete(f);
|
Files.delete(f);
|
||||||
log.sendInfoAL(ctx, "Deleted: {}", f);
|
log.sendInfoAL(ctx, "Deleted: {}", f);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if(Utilities.wasSentByPlayer(ctx)) log.sendError(ctx, "删除时发生了错误:{}.", f);
|
if(Utilities.wasSentByPlayer(ctx)) log.sendError(ctx, "Something went wrong while deleting: {}.", f);
|
||||||
log.error("删除时发生了错误:{}.", f, e);
|
log.error("Something went wrong while deleting: {}.", f, e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -1,100 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core;
|
|
||||||
|
|
||||||
import net.szum123321.textile_backup.core.restore.RestoreContext;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public record CompressionStatus(long treeHash, Map<String, Exception> brokenFiles, LocalDateTime date, long startTimestamp, long finishTimestamp, String version) implements Serializable {
|
|
||||||
public static final String DATA_FILENAME = "textile_status.data";
|
|
||||||
|
|
||||||
public Optional<String> validate(long hash, RestoreContext ctx) throws RuntimeException {
|
|
||||||
if(hash != treeHash)
|
|
||||||
return Optional.of("Tree Hash mismatch!\n Expected: " + hex(treeHash) + ", got: " + hex(hash));
|
|
||||||
|
|
||||||
if(!brokenFiles.isEmpty()) return Optional.of("Damaged files present! ^");
|
|
||||||
|
|
||||||
if(ctx.restoreableFile().getCreationTime().equals(date))
|
|
||||||
return Optional.of(
|
|
||||||
"Creation date mismatch!\n Expected: " +
|
|
||||||
date.format(DateTimeFormatter.ISO_DATE_TIME) + ", got: " +
|
|
||||||
ctx.restoreableFile().getCreationTime().format(DateTimeFormatter.ISO_DATE_TIME)
|
|
||||||
);
|
|
||||||
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Path resolveStatusFilename(Path directory) { return directory.resolve(DATA_FILENAME); }
|
|
||||||
|
|
||||||
public static CompressionStatus readFromFile(Path directory) throws IOException, ClassNotFoundException {
|
|
||||||
try(InputStream i = Files.newInputStream(directory.resolve(DATA_FILENAME));
|
|
||||||
ObjectInputStream obj = new ObjectInputStream(i)) {
|
|
||||||
return (CompressionStatus) obj.readObject();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] serialize() throws IOException {
|
|
||||||
try (ByteArrayOutputStream bo = new ByteArrayOutputStream();
|
|
||||||
ObjectOutputStream o = new ObjectOutputStream(bo)) {
|
|
||||||
o.writeObject(this);
|
|
||||||
return bo.toByteArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
builder.append("{ ");
|
|
||||||
builder.append("Hash: ")
|
|
||||||
.append(hex(treeHash))
|
|
||||||
.append(", Date: ")
|
|
||||||
.append(date.format(DateTimeFormatter.ISO_DATE_TIME))
|
|
||||||
.append(", Start timestamp: ").append(startTimestamp)
|
|
||||||
.append(", Finish timestamp: ").append(finishTimestamp)
|
|
||||||
.append(", Mod Version: ").append(version);
|
|
||||||
|
|
||||||
builder.append(", Broken files: ");
|
|
||||||
if(brokenFiles.isEmpty()) builder.append("[]");
|
|
||||||
else {
|
|
||||||
builder.append("[\n");
|
|
||||||
for(String i: brokenFiles.keySet()) {
|
|
||||||
builder.append(i)
|
|
||||||
.append(":");
|
|
||||||
|
|
||||||
ByteArrayOutputStream o = new ByteArrayOutputStream();
|
|
||||||
brokenFiles.get(i).printStackTrace(new PrintStream(o));
|
|
||||||
builder.append(o).append("\n");
|
|
||||||
}
|
|
||||||
builder.append("]");
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.append(" }");
|
|
||||||
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String hex(long val) { return "0x" + Long.toHexString(val).toUpperCase(); }
|
|
||||||
}
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -18,8 +18,6 @@
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core;
|
package net.szum123321.textile_backup.core;
|
||||||
|
|
||||||
import java.io.IOException;
|
public interface LivingServer {
|
||||||
|
boolean isAlive();
|
||||||
public class DataLeftException extends IOException {
|
|
||||||
public DataLeftException(long n) { super("Input stream closed with " + n + " bytes left!"); }
|
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -25,6 +25,6 @@ import java.io.IOException;
|
||||||
*/
|
*/
|
||||||
public class NoSpaceLeftOnDeviceException extends IOException {
|
public class NoSpaceLeftOnDeviceException extends IOException {
|
||||||
public NoSpaceLeftOnDeviceException(Throwable cause) {
|
public NoSpaceLeftOnDeviceException(Throwable cause) {
|
||||||
super("底层文件系统的可用空间已耗尽. \nSee: https://github.com/Szum123321/textile_backup/wiki/ZIP-Problems", cause);
|
super(cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2022 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core;
|
package net.szum123321.textile_backup.core;
|
||||||
|
@ -56,7 +57,7 @@ public class RestoreableFile implements Comparable<RestoreableFile> {
|
||||||
}
|
}
|
||||||
|
|
||||||
//removes repetition of the files stream thingy with awfully large lambdas
|
//removes repetition of the files stream thingy with awfully large lambdas
|
||||||
public static <T> T applyOnFiles(Path root, T def, Consumer<IOException> errorConsumer, Function<Stream<RestoreableFile>, T> streamConsumer) {
|
public static <T> T applyOnFiles(Path root, T def, Consumer<IOException> errorConsumer, Function<Stream<RestoreableFile>, T> streamConsumer) {
|
||||||
try (Stream<Path> stream = Files.list(root)) {
|
try (Stream<Path> stream = Files.list(root)) {
|
||||||
return streamConsumer.apply(stream.flatMap(f -> RestoreableFile.build(f).stream()));
|
return streamConsumer.apply(stream.flatMap(f -> RestoreableFile.build(f).stream()));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
Copyright (C) 2020 Szum123321
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core;
|
package net.szum123321.textile_backup.core;
|
||||||
|
|
||||||
|
@ -115,9 +115,9 @@ public class Utilities {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isBlacklisted(Path path) {
|
public static boolean isBlacklisted(Path path) {
|
||||||
if (path.getFileName().equals(Path.of("session.lock"))) return true;
|
if(isWindows()) { //hotfix!
|
||||||
|
if (path.getFileName().toString().equals("session.lock")) return true;
|
||||||
if(path.getFileName().equals(Path.of(CompressionStatus.DATA_FILENAME))) return true;
|
}
|
||||||
|
|
||||||
return config.get().fileBlacklist.stream().anyMatch(path::startsWith);
|
return config.get().fileBlacklist.stream().anyMatch(path::startsWith);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,109 @@
|
||||||
|
/*
|
||||||
|
* A simple backup mod for Fabric
|
||||||
|
* Copyright (C) 2020 Szum123321
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.szum123321.textile_backup.core.create;
|
||||||
|
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
|
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public record BackupContext(@NotNull MinecraftServer server,
|
||||||
|
ServerCommandSource commandSource,
|
||||||
|
ActionInitiator initiator,
|
||||||
|
boolean save,
|
||||||
|
String comment) {
|
||||||
|
|
||||||
|
public boolean startedByPlayer() {
|
||||||
|
return initiator == ActionInitiator.Player;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean shouldSave() {
|
||||||
|
return save;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private MinecraftServer server;
|
||||||
|
private ServerCommandSource commandSource;
|
||||||
|
private ActionInitiator initiator;
|
||||||
|
private boolean save;
|
||||||
|
private String comment;
|
||||||
|
|
||||||
|
private boolean guessInitiator;
|
||||||
|
|
||||||
|
public Builder() {
|
||||||
|
this.server = null;
|
||||||
|
this.commandSource = null;
|
||||||
|
this.initiator = null;
|
||||||
|
this.save = false;
|
||||||
|
this.comment = null;
|
||||||
|
|
||||||
|
guessInitiator = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Builder newBackupContextBuilder() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder setCommandSource(ServerCommandSource commandSource) {
|
||||||
|
this.commandSource = commandSource;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder setServer(MinecraftServer server) {
|
||||||
|
this.server = server;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder setInitiator(ActionInitiator initiator) {
|
||||||
|
this.initiator = initiator;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder setComment(String comment) {
|
||||||
|
this.comment = comment;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder guessInitiator() {
|
||||||
|
this.guessInitiator = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder saveServer() {
|
||||||
|
this.save = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BackupContext build() {
|
||||||
|
if (guessInitiator) {
|
||||||
|
initiator = commandSource.getEntity() instanceof PlayerEntity ? ActionInitiator.Player : ActionInitiator.ServerConsole;
|
||||||
|
} else if (initiator == null) {
|
||||||
|
initiator = ActionInitiator.Null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (server == null) {
|
||||||
|
if (commandSource != null) setServer(commandSource.getServer());
|
||||||
|
else throw new RuntimeException("Neither MinecraftServer or ServerCommandSource were provided!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new BackupContext(server, commandSource, initiator, save, comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -53,13 +53,14 @@ public class BackupScheduler {
|
||||||
if(nextBackup <= now) {
|
if(nextBackup <= now) {
|
||||||
//It's time to run
|
//It's time to run
|
||||||
Globals.INSTANCE.getQueueExecutor().submit(
|
Globals.INSTANCE.getQueueExecutor().submit(
|
||||||
ExecutableBackup.Builder
|
MakeBackupRunnableFactory.create(
|
||||||
.newBackupContextBuilder()
|
BackupContext.Builder
|
||||||
.setServer(server)
|
.newBackupContextBuilder()
|
||||||
.setInitiator(ActionInitiator.Timer)
|
.setServer(server)
|
||||||
.saveServer()
|
.setInitiator(ActionInitiator.Timer)
|
||||||
.announce()
|
.saveServer()
|
||||||
.build()
|
.build()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
nextBackup = now + config.get().backupInterval;
|
nextBackup = now + config.get().backupInterval;
|
||||||
|
@ -75,13 +76,14 @@ public class BackupScheduler {
|
||||||
if(scheduled && nextBackup <= now) {
|
if(scheduled && nextBackup <= now) {
|
||||||
//Verify we hadn't done the final one, and it's time to do so
|
//Verify we hadn't done the final one, and it's time to do so
|
||||||
Globals.INSTANCE.getQueueExecutor().submit(
|
Globals.INSTANCE.getQueueExecutor().submit(
|
||||||
ExecutableBackup.Builder
|
MakeBackupRunnableFactory.create(
|
||||||
.newBackupContextBuilder()
|
BackupContext.Builder
|
||||||
.setServer(server)
|
.newBackupContextBuilder()
|
||||||
.setInitiator(ActionInitiator.Timer)
|
.setServer(server)
|
||||||
.saveServer()
|
.setInitiator(ActionInitiator.Timer)
|
||||||
.announce()
|
.saveServer()
|
||||||
.build()
|
.build()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
scheduled = false;
|
scheduled = false;
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.create;
|
|
||||||
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class BrokenFileHandler {
|
|
||||||
private final Map<String, Exception> store = new HashMap<>();
|
|
||||||
public void handle(Path file, Exception e) { store.put(file.toString(), e); }
|
|
||||||
|
|
||||||
public boolean valid() { return store.isEmpty(); }
|
|
||||||
|
|
||||||
public Map<String, Exception> get() {
|
|
||||||
return store;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,231 +0,0 @@
|
||||||
package net.szum123321.textile_backup.core.create;
|
|
||||||
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
|
||||||
import net.szum123321.textile_backup.Globals;
|
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
|
||||||
import net.szum123321.textile_backup.config.ConfigHelper;
|
|
||||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
|
||||||
import net.szum123321.textile_backup.core.Cleanup;
|
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
|
||||||
import net.szum123321.textile_backup.core.create.compressors.ParallelZipCompressor;
|
|
||||||
import net.szum123321.textile_backup.core.create.compressors.ZipCompressor;
|
|
||||||
import net.szum123321.textile_backup.core.create.compressors.tar.AbstractTarArchiver;
|
|
||||||
import net.szum123321.textile_backup.core.create.compressors.tar.ParallelGzipCompressor;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
|
|
||||||
public record ExecutableBackup(@NotNull MinecraftServer server,
|
|
||||||
ServerCommandSource commandSource,
|
|
||||||
ActionInitiator initiator,
|
|
||||||
boolean save,
|
|
||||||
boolean cleanup,
|
|
||||||
String comment,
|
|
||||||
LocalDateTime startDate) implements Callable<Void> {
|
|
||||||
|
|
||||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
|
||||||
private final static ConfigHelper config = ConfigHelper.INSTANCE;
|
|
||||||
|
|
||||||
public boolean startedByPlayer() {
|
|
||||||
return initiator == ActionInitiator.Player;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void announce() {
|
|
||||||
if(config.get().broadcastBackupStart) {
|
|
||||||
Utilities.notifyPlayers(server,
|
|
||||||
"警告!服务器备份即将开始。您可能会遇到一些延迟."
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
log.sendInfoAL(this, "Something went wrong while deleting: {}.");
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
|
|
||||||
builder.append("备份开始 ");
|
|
||||||
|
|
||||||
builder.append(initiator.getPrefix());
|
|
||||||
|
|
||||||
if(startedByPlayer())
|
|
||||||
builder.append(commandSource.getDisplayName().getString());
|
|
||||||
else
|
|
||||||
builder.append(initiator.getName());
|
|
||||||
|
|
||||||
builder.append(" on: ");
|
|
||||||
builder.append(Utilities.getDateTimeFormatter().format(LocalDateTime.now()));
|
|
||||||
|
|
||||||
log.info(builder.toString());
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public Void call() throws Exception {
|
|
||||||
if (save) { //save the world
|
|
||||||
log.sendInfoAL(this, "保存世界中...");
|
|
||||||
server.saveAll(true, true, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
Path outFile = Utilities.getBackupRootPath(Utilities.getLevelName(server)).resolve(getFileName());
|
|
||||||
|
|
||||||
log.trace("输出备份文件为: {}", outFile);
|
|
||||||
|
|
||||||
try {
|
|
||||||
//I think I should synchronise these two next calls...
|
|
||||||
Utilities.disableWorldSaving(server);
|
|
||||||
Globals.INSTANCE.disableWatchdog = true;
|
|
||||||
|
|
||||||
Globals.INSTANCE.updateTMPFSFlag(server);
|
|
||||||
|
|
||||||
log.sendInfoAL(this, "开始备份");
|
|
||||||
|
|
||||||
Path world = Utilities.getWorldFolder(server);
|
|
||||||
|
|
||||||
log.trace("Minecraft 存档目录: {}", world);
|
|
||||||
|
|
||||||
Files.createDirectories(outFile.getParent());
|
|
||||||
Files.createFile(outFile);
|
|
||||||
|
|
||||||
int coreCount;
|
|
||||||
|
|
||||||
if (config.get().compressionCoreCountLimit <= 0) coreCount = Runtime.getRuntime().availableProcessors();
|
|
||||||
else
|
|
||||||
coreCount = Math.min(config.get().compressionCoreCountLimit, Runtime.getRuntime().availableProcessors());
|
|
||||||
|
|
||||||
log.trace("正在使用{}个线程对{}进行压缩。可用核心数:{}", coreCount, Runtime.getRuntime().availableProcessors());
|
|
||||||
|
|
||||||
switch (config.get().format) {
|
|
||||||
case ZIP -> {
|
|
||||||
if (coreCount > 1 && !Globals.INSTANCE.disableTMPFS()) {
|
|
||||||
log.trace("使用并行压缩器进行压缩。线程数:{}", coreCount);
|
|
||||||
ParallelZipCompressor.getInstance().createArchive(world, outFile, this, coreCount);
|
|
||||||
} else {
|
|
||||||
log.trace("使用普通的Zip压缩器进行压缩 (单线程)");
|
|
||||||
ZipCompressor.getInstance().createArchive(world, outFile, this, coreCount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case GZIP -> ParallelGzipCompressor.getInstance().createArchive(world, outFile, this, coreCount);
|
|
||||||
case TAR -> new AbstractTarArchiver().createArchive(world, outFile, this, coreCount);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(cleanup) new Cleanup(commandSource, Utilities.getLevelName(server)).call();
|
|
||||||
|
|
||||||
if (config.get().broadcastBackupDone) Utilities.notifyPlayers(server, "完成!");
|
|
||||||
else log.sendInfoAL(this, "完成!");
|
|
||||||
|
|
||||||
} catch (Throwable e) {
|
|
||||||
//ExecutorService swallows exception, so I need to catch everything
|
|
||||||
log.error("在尝试创建新的备份文件时发生了异常!", e);
|
|
||||||
|
|
||||||
if (ConfigHelper.INSTANCE.get().integrityVerificationMode.isStrict()) {
|
|
||||||
try {
|
|
||||||
Files.delete(outFile);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
log.error("在尝试删除{}时发生了异常!", outFile, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (initiator == ActionInitiator.Player)
|
|
||||||
log.sendError(this, "在尝试创建新的备份文件时发生了异常!");
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
} finally {
|
|
||||||
Utilities.enableWorldSaving(server);
|
|
||||||
Globals.INSTANCE.disableWatchdog = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getFileName() {
|
|
||||||
return Utilities.getDateTimeFormatter().format(startDate) +
|
|
||||||
(comment != null ? "#" + comment.replaceAll("[\\\\/:*?\"<>|#]", "") : "") +
|
|
||||||
config.get().format.getCompleteString();
|
|
||||||
}
|
|
||||||
public static class Builder {
|
|
||||||
private MinecraftServer server;
|
|
||||||
private ServerCommandSource commandSource;
|
|
||||||
private ActionInitiator initiator;
|
|
||||||
private boolean save;
|
|
||||||
private boolean cleanup;
|
|
||||||
private String comment;
|
|
||||||
private boolean announce;
|
|
||||||
|
|
||||||
private boolean guessInitiator;
|
|
||||||
|
|
||||||
public Builder() {
|
|
||||||
this.server = null;
|
|
||||||
this.commandSource = null;
|
|
||||||
this.initiator = null;
|
|
||||||
this.save = false;
|
|
||||||
cleanup = true; //defaults
|
|
||||||
this.comment = null;
|
|
||||||
this.announce = false;
|
|
||||||
|
|
||||||
guessInitiator = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ExecutableBackup.Builder newBackupContextBuilder() {
|
|
||||||
return new ExecutableBackup.Builder();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup.Builder setCommandSource(ServerCommandSource commandSource) {
|
|
||||||
this.commandSource = commandSource;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup.Builder setServer(MinecraftServer server) {
|
|
||||||
this.server = server;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup.Builder setInitiator(ActionInitiator initiator) {
|
|
||||||
this.initiator = initiator;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup.Builder setComment(String comment) {
|
|
||||||
this.comment = comment;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup.Builder guessInitiator() {
|
|
||||||
this.guessInitiator = true;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup.Builder saveServer() {
|
|
||||||
this.save = true;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup.Builder noCleanup() {
|
|
||||||
this.cleanup = false;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup.Builder announce() {
|
|
||||||
this.announce = true;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutableBackup build() {
|
|
||||||
if (guessInitiator) {
|
|
||||||
initiator = Utilities.wasSentByPlayer(commandSource) ? ActionInitiator.Player : ActionInitiator.ServerConsole;
|
|
||||||
} else if (initiator == null) throw new NoSuchElementException("未提供发起者!");
|
|
||||||
|
|
||||||
if (server == null) {
|
|
||||||
if (commandSource != null) setServer(commandSource.getServer());
|
|
||||||
else throw new RuntimeException("未提供MinecraftServer或ServerCommandSource!");
|
|
||||||
}
|
|
||||||
|
|
||||||
ExecutableBackup v = new ExecutableBackup(server, commandSource, initiator, save, cleanup, comment, LocalDateTime.now());
|
|
||||||
|
|
||||||
if(announce) v.announce();
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,70 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.create;
|
|
||||||
|
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
|
||||||
import net.szum123321.textile_backup.core.digest.FileTreeHashBuilder;
|
|
||||||
import net.szum123321.textile_backup.core.digest.HashingInputStream;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public record FileInputStreamSupplier(Path path, String name, FileTreeHashBuilder hashTreeBuilder, BrokenFileHandler brokenFileHandler) implements InputSupplier {
|
|
||||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public InputStream getInputStream() throws IOException {
|
|
||||||
try {
|
|
||||||
return new HashingInputStream(Files.newInputStream(path), path, hashTreeBuilder, brokenFileHandler);
|
|
||||||
} catch (IOException e) {
|
|
||||||
//Probably good idea to just put it here. In the case an exception is thrown here, it could be possible
|
|
||||||
//The latch would have never been lifted
|
|
||||||
hashTreeBuilder.update(path, 0, 0);
|
|
||||||
brokenFileHandler.handle(path, e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<Path> getPath() { return Optional.of(path); }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long size() throws IOException { return Files.size(path); }
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public InputStream get() {
|
|
||||||
try {
|
|
||||||
return getInputStream();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("尝试从文件{}创建输入流时发生了异常!", path.toString(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.create;
|
|
||||||
|
|
||||||
import org.apache.commons.compress.parallel.InputStreamSupplier;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface InputSupplier extends InputStreamSupplier {
|
|
||||||
InputStream getInputStream() throws IOException;
|
|
||||||
//If an entry is virtual (a.k.a. there is no actual file to open, only input stream)
|
|
||||||
Optional<Path> getPath();
|
|
||||||
String getName();
|
|
||||||
|
|
||||||
long size() throws IOException;
|
|
||||||
}
|
|
|
@ -0,0 +1,136 @@
|
||||||
|
/*
|
||||||
|
A simple backup mod for Fabric
|
||||||
|
Copyright (C) 2020 Szum123321
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.szum123321.textile_backup.core.create;
|
||||||
|
|
||||||
|
import net.szum123321.textile_backup.Globals;
|
||||||
|
import net.szum123321.textile_backup.TextileBackup;
|
||||||
|
import net.szum123321.textile_backup.TextileLogger;
|
||||||
|
import net.szum123321.textile_backup.config.ConfigHelper;
|
||||||
|
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||||
|
import net.szum123321.textile_backup.core.Cleanup;
|
||||||
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
|
import net.szum123321.textile_backup.core.create.compressors.ParallelZipCompressor;
|
||||||
|
import net.szum123321.textile_backup.core.create.compressors.ZipCompressor;
|
||||||
|
import net.szum123321.textile_backup.core.create.compressors.tar.AbstractTarArchiver;
|
||||||
|
import net.szum123321.textile_backup.core.create.compressors.tar.ParallelBZip2Compressor;
|
||||||
|
import net.szum123321.textile_backup.core.create.compressors.tar.ParallelGzipCompressor;
|
||||||
|
import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The actual object responsible for creating the backup
|
||||||
|
*/
|
||||||
|
public class MakeBackupRunnable implements Runnable {
|
||||||
|
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||||
|
private final static ConfigHelper config = ConfigHelper.INSTANCE;
|
||||||
|
|
||||||
|
private final BackupContext context;
|
||||||
|
|
||||||
|
public MakeBackupRunnable(BackupContext context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
Utilities.disableWorldSaving(context.server());
|
||||||
|
Globals.INSTANCE.disableWatchdog = true;
|
||||||
|
|
||||||
|
Globals.INSTANCE.updateTMPFSFlag(context.server());
|
||||||
|
|
||||||
|
log.sendInfoAL(context, "Starting backup");
|
||||||
|
|
||||||
|
Path world = Utilities.getWorldFolder(context.server());
|
||||||
|
|
||||||
|
log.trace("Minecraft world is: {}", world);
|
||||||
|
|
||||||
|
Path outFile = Utilities
|
||||||
|
.getBackupRootPath(Utilities.getLevelName(context.server()))
|
||||||
|
.resolve(getFileName());
|
||||||
|
|
||||||
|
log.trace("Outfile is: {}", outFile);
|
||||||
|
|
||||||
|
Files.createDirectories(outFile.getParent());
|
||||||
|
Files.createFile(outFile);
|
||||||
|
|
||||||
|
int coreCount;
|
||||||
|
|
||||||
|
if(config.get().compressionCoreCountLimit <= 0) {
|
||||||
|
coreCount = Runtime.getRuntime().availableProcessors();
|
||||||
|
} else {
|
||||||
|
coreCount = Math.min(config.get().compressionCoreCountLimit, Runtime.getRuntime().availableProcessors());
|
||||||
|
}
|
||||||
|
|
||||||
|
log.trace("Running compression on {} threads. Available cores: {}", coreCount, Runtime.getRuntime().availableProcessors());
|
||||||
|
|
||||||
|
switch (config.get().format) {
|
||||||
|
case ZIP -> {
|
||||||
|
if (coreCount > 1 && !Globals.INSTANCE.disableTMPFS()) {
|
||||||
|
log.trace("Using PARALLEL Zip Compressor. Threads: {}", coreCount);
|
||||||
|
ParallelZipCompressor.getInstance().createArchive(world, outFile, context, coreCount);
|
||||||
|
} else {
|
||||||
|
log.trace("Using REGULAR Zip Compressor.");
|
||||||
|
ZipCompressor.getInstance().createArchive(world, outFile, context, coreCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case BZIP2 -> ParallelBZip2Compressor.getInstance().createArchive(world, outFile, context, coreCount);
|
||||||
|
case GZIP -> ParallelGzipCompressor.getInstance().createArchive(world, outFile, context, coreCount);
|
||||||
|
case LZMA -> new AbstractTarArchiver() {
|
||||||
|
protected OutputStream getCompressorOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException {
|
||||||
|
return new LZMACompressorOutputStream(stream);
|
||||||
|
}
|
||||||
|
}.createArchive(world, outFile, context, coreCount);
|
||||||
|
case TAR -> new AbstractTarArchiver().createArchive(world, outFile, context, coreCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
new Cleanup(context.commandSource(), Utilities.getLevelName(context.server())).call();
|
||||||
|
|
||||||
|
if(config.get().broadcastBackupDone) {
|
||||||
|
Utilities.notifyPlayers(
|
||||||
|
context.server(),
|
||||||
|
"Done!"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
log.sendInfoAL(context, "Done!");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
//ExecutorService swallows exception, so I need to catch everything
|
||||||
|
log.error("An exception occurred when trying to create new backup file!", e);
|
||||||
|
|
||||||
|
if(context.initiator() == ActionInitiator.Player)
|
||||||
|
log.sendError(context, "An exception occurred when trying to create new backup file!");
|
||||||
|
} finally {
|
||||||
|
Utilities.enableWorldSaving(context.server());
|
||||||
|
Globals.INSTANCE.disableWatchdog = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFileName(){
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
|
||||||
|
return Utilities.getDateTimeFormatter().format(now) +
|
||||||
|
(context.comment() != null ? "#" + context.comment().replaceAll("[\\\\/:*?\"<>|#]", "") : "") +
|
||||||
|
config.get().format.getCompleteString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,72 @@
|
||||||
|
/*
|
||||||
|
* A simple backup mod for Fabric
|
||||||
|
* Copyright (C) 2022 Szum123321
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.szum123321.textile_backup.core.create;
|
||||||
|
|
||||||
|
import net.szum123321.textile_backup.TextileBackup;
|
||||||
|
import net.szum123321.textile_backup.TextileLogger;
|
||||||
|
import net.szum123321.textile_backup.config.ConfigHelper;
|
||||||
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
public class MakeBackupRunnableFactory {
|
||||||
|
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||||
|
private final static ConfigHelper config = ConfigHelper.INSTANCE;
|
||||||
|
|
||||||
|
public static Runnable create(BackupContext ctx) {
|
||||||
|
if(config.get().broadcastBackupStart) {
|
||||||
|
Utilities.notifyPlayers(ctx.server(),
|
||||||
|
"Warning! Server backup will begin shortly. You may experience some lag."
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
log.sendInfoAL(ctx, "Warning! Server backup will begin shortly. You may experience some lag.");
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
|
||||||
|
builder.append("Backup started ");
|
||||||
|
|
||||||
|
builder.append(ctx.initiator().getPrefix());
|
||||||
|
|
||||||
|
if(ctx.startedByPlayer())
|
||||||
|
builder.append(ctx.commandSource().getDisplayName().getString());
|
||||||
|
else
|
||||||
|
builder.append(ctx.initiator().getName());
|
||||||
|
|
||||||
|
builder.append(" on: ");
|
||||||
|
builder.append(Utilities.getDateTimeFormatter().format(LocalDateTime.now()));
|
||||||
|
|
||||||
|
log.info(builder.toString());
|
||||||
|
|
||||||
|
if (ctx.shouldSave()) {
|
||||||
|
log.sendInfoAL(ctx, "Saving server...");
|
||||||
|
|
||||||
|
ctx.server().getPlayerManager().saveAllPlayerData();
|
||||||
|
|
||||||
|
try {
|
||||||
|
ctx.server().save(false, true, true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.sendErrorAL(ctx,"An exception occurred when trying to save the world!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new MakeBackupRunnable(ctx);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -18,96 +18,77 @@
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.create.compressors;
|
package net.szum123321.textile_backup.core.create.compressors;
|
||||||
|
|
||||||
import net.szum123321.textile_backup.Globals;
|
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
import net.szum123321.textile_backup.TextileBackup;
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
import net.szum123321.textile_backup.TextileLogger;
|
||||||
import net.szum123321.textile_backup.config.ConfigHelper;
|
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||||
import net.szum123321.textile_backup.core.*;
|
import net.szum123321.textile_backup.core.NoSpaceLeftOnDeviceException;
|
||||||
import net.szum123321.textile_backup.core.create.BrokenFileHandler;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
import net.szum123321.textile_backup.core.create.FileInputStreamSupplier;
|
|
||||||
import net.szum123321.textile_backup.core.create.InputSupplier;
|
|
||||||
import net.szum123321.textile_backup.core.digest.FileTreeHashBuilder;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic abstract class representing directory compressor with all the bells and whistles
|
* Basic abstract class representing directory compressor
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractCompressor {
|
public abstract class AbstractCompressor {
|
||||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||||
|
|
||||||
public void createArchive(Path inputFile, Path outputFile, ExecutableBackup ctx, int coreLimit) throws IOException, ExecutionException, InterruptedException {
|
public void createArchive(Path inputFile, Path outputFile, BackupContext ctx, int coreLimit) {
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
|
|
||||||
BrokenFileHandler brokenFileHandler = new BrokenFileHandler(); //Basically a hashmap storing files and their respective exceptions
|
|
||||||
|
|
||||||
try (OutputStream outStream = Files.newOutputStream(outputFile);
|
try (OutputStream outStream = Files.newOutputStream(outputFile);
|
||||||
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outStream);
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outStream);
|
||||||
OutputStream arc = createArchiveOutputStream(bufferedOutputStream, ctx, coreLimit);
|
OutputStream arc = createArchiveOutputStream(bufferedOutputStream, ctx, coreLimit);
|
||||||
Stream<Path> fileStream = Files.walk(inputFile)) {
|
Stream<Path> fileStream = Files.walk(inputFile)) {
|
||||||
|
|
||||||
var fileList = fileStream
|
fileStream
|
||||||
.filter(path -> !Utilities.isBlacklisted(inputFile.relativize(path)))
|
.filter(path -> !Utilities.isBlacklisted(inputFile.relativize(path)))
|
||||||
.filter(Files::isRegularFile)
|
.filter(Files::isRegularFile).forEach(file -> {
|
||||||
.toList();
|
try {
|
||||||
|
//hopefully one broken file won't spoil the whole archive
|
||||||
|
addEntry(file, inputFile.relativize(file).toString(), arc);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("An exception occurred while trying to compress: {}", inputFile.relativize(file).toString(), e);
|
||||||
|
|
||||||
FileTreeHashBuilder fileHashBuilder = new FileTreeHashBuilder(fileList.size());
|
if (ctx.initiator() == ActionInitiator.Player)
|
||||||
|
log.sendError(ctx, "Something went wrong while compressing files!");
|
||||||
for (Path file : fileList) {
|
}
|
||||||
try {
|
});
|
||||||
addEntry(
|
|
||||||
new FileInputStreamSupplier(
|
|
||||||
file,
|
|
||||||
inputFile.relativize(file).toString(),
|
|
||||||
fileHashBuilder,
|
|
||||||
brokenFileHandler),
|
|
||||||
arc
|
|
||||||
);
|
|
||||||
} catch (IOException e) {
|
|
||||||
brokenFileHandler.handle(file, e);
|
|
||||||
fileHashBuilder.update(file, 0, 0);
|
|
||||||
//In Permissive mode we allow partial backups
|
|
||||||
if (ConfigHelper.INSTANCE.get().integrityVerificationMode.isStrict()) throw e;
|
|
||||||
else log.sendErrorAL(ctx, "在尝试压缩{}时发生了异常!",
|
|
||||||
inputFile.relativize(file).toString(), e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
arc.flush();
|
|
||||||
|
|
||||||
//wait for all the InputStreams to close/fail with InputSupplier
|
|
||||||
|
|
||||||
Instant now = Instant.now();
|
|
||||||
|
|
||||||
long treeHash = fileHashBuilder.getValue(true);
|
|
||||||
CompressionStatus status = new CompressionStatus (
|
|
||||||
treeHash,
|
|
||||||
brokenFileHandler.get(),
|
|
||||||
ctx.startDate(), start.toEpochMilli(), now.toEpochMilli(),
|
|
||||||
Globals.INSTANCE.getCombinedVersionString()
|
|
||||||
);
|
|
||||||
|
|
||||||
addEntry(new StatusFileInputSupplier(status.serialize()), arc);
|
|
||||||
|
|
||||||
finish(arc);
|
finish(arc);
|
||||||
|
} catch(NoSpaceLeftOnDeviceException e) {
|
||||||
|
log.error("""
|
||||||
|
CRITICAL ERROR OCCURRED!
|
||||||
|
The backup is corrupt!
|
||||||
|
Don't panic! This is a known issue!
|
||||||
|
For help see: https://github.com/Szum123321/textile_backup/wiki/ZIP-Problems
|
||||||
|
In case this isn't it here's also the exception itself""", e);
|
||||||
|
|
||||||
|
if(ctx.initiator() == ActionInitiator.Player) {
|
||||||
|
log.sendError(ctx, "Backup failed. The file is corrupt.");
|
||||||
|
log.error("For help see: https://github.com/Szum123321/textile_backup/wiki/ZIP-Problems");
|
||||||
|
}
|
||||||
|
} catch (IOException | InterruptedException | ExecutionException e) {
|
||||||
|
log.error("An exception occurred!", e);
|
||||||
|
if(ctx.initiator() == ActionInitiator.Player)
|
||||||
|
log.sendError(ctx, "Something went wrong while compressing files!");
|
||||||
} finally {
|
} finally {
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
log.sendInfoAL(ctx, "压缩耗时:{}秒. ", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
// close();
|
||||||
|
|
||||||
|
log.sendInfoAL(ctx, "Compression took: {} seconds.", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract OutputStream createArchiveOutputStream(OutputStream stream, ExecutableBackup ctx, int coreLimit) throws IOException;
|
protected abstract OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException;
|
||||||
protected abstract void addEntry(InputSupplier inputSupplier, OutputStream arc) throws IOException;
|
protected abstract void addEntry(Path file, String entryName, OutputStream arc) throws IOException;
|
||||||
|
|
||||||
protected void finish(OutputStream arc) throws InterruptedException, ExecutionException, IOException {
|
protected void finish(OutputStream arc) throws InterruptedException, ExecutionException, IOException {
|
||||||
//This function is only needed for the ParallelZipCompressor to write out ParallelScatterZipCreator
|
//This function is only needed for the ParallelZipCompressor to write out ParallelScatterZipCreator
|
||||||
|
@ -116,16 +97,4 @@ public abstract class AbstractCompressor {
|
||||||
protected void close() {
|
protected void close() {
|
||||||
//Same as above, just for ParallelGzipCompressor to shut down ExecutorService
|
//Same as above, just for ParallelGzipCompressor to shut down ExecutorService
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private record StatusFileInputSupplier(byte[] data) implements InputSupplier {
|
|
||||||
public InputStream getInputStream() { return new ByteArrayInputStream(data); }
|
|
||||||
|
|
||||||
public Optional<Path> getPath() { return Optional.empty(); }
|
|
||||||
|
|
||||||
public String getName() { return CompressionStatus.DATA_FILENAME; }
|
|
||||||
|
|
||||||
public long size() { return data.length; }
|
|
||||||
|
|
||||||
public InputStream get() { return getInputStream(); }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -21,9 +21,9 @@ package net.szum123321.textile_backup.core.create.compressors;
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
import net.szum123321.textile_backup.TextileBackup;
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
import net.szum123321.textile_backup.TextileLogger;
|
||||||
import net.szum123321.textile_backup.core.NoSpaceLeftOnDeviceException;
|
import net.szum123321.textile_backup.core.NoSpaceLeftOnDeviceException;
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
import net.szum123321.textile_backup.core.create.InputSupplier;
|
|
||||||
import org.apache.commons.compress.archivers.zip.*;
|
import org.apache.commons.compress.archivers.zip.*;
|
||||||
|
import org.apache.commons.compress.parallel.InputStreamSupplier;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
@ -61,32 +61,25 @@ public class ParallelZipCompressor extends ZipCompressor {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OutputStream createArchiveOutputStream(OutputStream stream, ExecutableBackup ctx, int coreLimit) {
|
protected OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) {
|
||||||
scatterZipCreator = new ParallelScatterZipCreator(Executors.newFixedThreadPool(coreLimit));
|
scatterZipCreator = new ParallelScatterZipCreator(Executors.newFixedThreadPool(coreLimit));
|
||||||
return super.createArchiveOutputStream(stream, ctx, coreLimit);
|
return super.createArchiveOutputStream(stream, ctx, coreLimit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void addEntry(InputSupplier input, OutputStream arc) throws IOException {
|
protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException {
|
||||||
ZipArchiveEntry entry;
|
ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(file, entryName);
|
||||||
if(input.getPath().isEmpty()) {
|
|
||||||
entry = new ZipArchiveEntry(input.getName());
|
if(ZipCompressor.isDotDat(file.getFileName().toString())) {
|
||||||
entry.setMethod(ZipEntry.STORED);
|
entry.setMethod(ZipEntry.STORED);
|
||||||
entry.setSize(input.size());
|
entry.setSize(Files.size(file));
|
||||||
} else {
|
entry.setCompressedSize(Files.size(file));
|
||||||
Path file = input.getPath().get();
|
entry.setCrc(getCRC(file));
|
||||||
entry = (ZipArchiveEntry) ((ZipArchiveOutputStream) arc).createArchiveEntry(file, input.getName());
|
} else entry.setMethod(ZipEntry.DEFLATED);
|
||||||
if (ZipCompressor.isDotDat(file.toString())) {
|
|
||||||
entry.setMethod(ZipEntry.STORED);
|
|
||||||
entry.setSize(Files.size(file));
|
|
||||||
entry.setCompressedSize(Files.size(file));
|
|
||||||
entry.setCrc(getCRC(file));
|
|
||||||
} else entry.setMethod(ZipEntry.DEFLATED);
|
|
||||||
}
|
|
||||||
|
|
||||||
entry.setTime(System.currentTimeMillis());
|
entry.setTime(System.currentTimeMillis());
|
||||||
|
|
||||||
scatterZipCreator.addArchiveEntry(entry, input);
|
scatterZipCreator.addArchiveEntry(entry, new FileInputStreamSupplier(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -104,9 +97,10 @@ public class ParallelZipCompressor extends ZipCompressor {
|
||||||
boolean match = (cause.getStackTrace().length >= STACKTRACE_NO_SPACE_ON_LEFT_ON_DEVICE.length);
|
boolean match = (cause.getStackTrace().length >= STACKTRACE_NO_SPACE_ON_LEFT_ON_DEVICE.length);
|
||||||
if(match) {
|
if(match) {
|
||||||
for(int i = 0; i < STACKTRACE_NO_SPACE_ON_LEFT_ON_DEVICE.length && match; i++)
|
for(int i = 0; i < STACKTRACE_NO_SPACE_ON_LEFT_ON_DEVICE.length && match; i++)
|
||||||
if(!STACKTRACE_NO_SPACE_ON_LEFT_ON_DEVICE[i].matches(cause.getStackTrace()[i])) match = false;
|
if(!STACKTRACE_NO_SPACE_ON_LEFT_ON_DEVICE[i].equals(cause.getStackTrace()[i])) match = false;
|
||||||
|
|
||||||
//For clarity's sake let's not throw the ExecutionException itself rather only the cause, as the EE is just the wrapper
|
|
||||||
|
//For clarity' sake let's not throw the ExecutionException itself rather only the cause, as the EE is just the wrapper
|
||||||
if(match) throw new NoSpaceLeftOnDeviceException(cause);
|
if(match) throw new NoSpaceLeftOnDeviceException(cause);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,8 +114,29 @@ public class ParallelZipCompressor extends ZipCompressor {
|
||||||
String methodName,
|
String methodName,
|
||||||
boolean isNative
|
boolean isNative
|
||||||
) {
|
) {
|
||||||
public boolean matches(StackTraceElement o) {
|
@Override
|
||||||
return (isNative == o.isNativeMethod()) && Objects.equals(className, o.getClassName()) && Objects.equals(methodName, o.getMethodName());
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null) return false;
|
||||||
|
if(o.getClass() == StackTraceElement.class) {
|
||||||
|
StackTraceElement that = (StackTraceElement) o;
|
||||||
|
return (isNative == that.isNativeMethod()) && Objects.equals(className, that.getClassName()) && Objects.equals(methodName, that.getMethodName());
|
||||||
|
}
|
||||||
|
if(getClass() != o.getClass()) return false;
|
||||||
|
SimpleStackTraceElement that = (SimpleStackTraceElement) o;
|
||||||
|
return isNative == that.isNative && Objects.equals(className, that.className) && Objects.equals(methodName, that.methodName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record FileInputStreamSupplier(Path sourceFile) implements InputStreamSupplier {
|
||||||
|
public InputStream get() {
|
||||||
|
try {
|
||||||
|
return Files.newInputStream(sourceFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("An exception occurred while trying to create an input stream from file: {}!", sourceFile.toString(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -20,8 +20,7 @@ package net.szum123321.textile_backup.core.create.compressors;
|
||||||
|
|
||||||
import net.szum123321.textile_backup.config.ConfigHelper;
|
import net.szum123321.textile_backup.config.ConfigHelper;
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
import net.szum123321.textile_backup.core.create.InputSupplier;
|
|
||||||
import org.apache.commons.compress.archivers.zip.Zip64Mode;
|
import org.apache.commons.compress.archivers.zip.Zip64Mode;
|
||||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
|
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
|
||||||
|
@ -43,7 +42,7 @@ public class ZipCompressor extends AbstractCompressor {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OutputStream createArchiveOutputStream(OutputStream stream, ExecutableBackup ctx, int coreLimit) {
|
protected OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) {
|
||||||
ZipArchiveOutputStream arc = new ZipArchiveOutputStream(stream);
|
ZipArchiveOutputStream arc = new ZipArchiveOutputStream(stream);
|
||||||
|
|
||||||
arc.setMethod(ZipArchiveOutputStream.DEFLATED);
|
arc.setMethod(ZipArchiveOutputStream.DEFLATED);
|
||||||
|
@ -55,23 +54,15 @@ public class ZipCompressor extends AbstractCompressor {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void addEntry(InputSupplier input, OutputStream arc) throws IOException {
|
protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException {
|
||||||
try (InputStream fileInputStream = input.getInputStream()) {
|
try (InputStream fileInputStream = Files.newInputStream(file)){
|
||||||
ZipArchiveEntry entry;
|
ZipArchiveEntry entry = (ZipArchiveEntry)((ZipArchiveOutputStream)arc).createArchiveEntry(file, entryName);
|
||||||
|
|
||||||
if(input.getPath().isEmpty()) {
|
if(isDotDat(file.getFileName().toString())) {
|
||||||
entry = new ZipArchiveEntry(input.getName());
|
|
||||||
entry.setMethod(ZipEntry.STORED);
|
entry.setMethod(ZipEntry.STORED);
|
||||||
entry.setSize(input.size());
|
entry.setSize(Files.size(file));
|
||||||
} else {
|
entry.setCompressedSize(Files.size(file));
|
||||||
Path file = input.getPath().get();
|
entry.setCrc(getCRC(file));
|
||||||
entry = (ZipArchiveEntry) ((ZipArchiveOutputStream) arc).createArchiveEntry(file, input.getName());
|
|
||||||
if (isDotDat(file.toString())) {
|
|
||||||
entry.setMethod(ZipEntry.STORED);
|
|
||||||
entry.setSize(Files.size(file));
|
|
||||||
entry.setCompressedSize(Files.size(file));
|
|
||||||
entry.setCrc(getCRC(file));
|
|
||||||
} else entry.setMethod(ZipEntry.DEFLATED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
((ZipArchiveOutputStream)arc).putArchiveEntry(entry);
|
((ZipArchiveOutputStream)arc).putArchiveEntry(entry);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -18,22 +18,23 @@
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.create.compressors.tar;
|
package net.szum123321.textile_backup.core.create.compressors.tar;
|
||||||
|
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
import net.szum123321.textile_backup.core.create.compressors.AbstractCompressor;
|
import net.szum123321.textile_backup.core.create.compressors.AbstractCompressor;
|
||||||
import net.szum123321.textile_backup.core.create.InputSupplier;
|
|
||||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||||
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
||||||
import org.apache.commons.compress.utils.IOUtils;
|
import org.apache.commons.compress.utils.IOUtils;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
public class AbstractTarArchiver extends AbstractCompressor {
|
public class AbstractTarArchiver extends AbstractCompressor {
|
||||||
protected OutputStream getCompressorOutputStream(OutputStream stream, ExecutableBackup ctx, int coreLimit) throws IOException {
|
protected OutputStream getCompressorOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException {
|
||||||
return stream;
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OutputStream createArchiveOutputStream(OutputStream stream, ExecutableBackup ctx, int coreLimit) throws IOException {
|
protected OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException {
|
||||||
TarArchiveOutputStream tar = new TarArchiveOutputStream(getCompressorOutputStream(stream, ctx, coreLimit));
|
TarArchiveOutputStream tar = new TarArchiveOutputStream(getCompressorOutputStream(stream, ctx, coreLimit));
|
||||||
tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
|
tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
|
||||||
tar.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
|
tar.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
|
||||||
|
@ -42,15 +43,9 @@ public class AbstractTarArchiver extends AbstractCompressor {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void addEntry(InputSupplier input, OutputStream arc) throws IOException {
|
protected void addEntry(Path file, String entryName, OutputStream arc) throws IOException {
|
||||||
try (InputStream fileInputStream = input.getInputStream()) {
|
try (InputStream fileInputStream = Files.newInputStream(file)){
|
||||||
TarArchiveEntry entry;
|
TarArchiveEntry entry = (TarArchiveEntry)((TarArchiveOutputStream) arc).createArchiveEntry(file, entryName);
|
||||||
if(input.getPath().isEmpty()) { //Virtual entry
|
|
||||||
entry = new TarArchiveEntry(input.getName());
|
|
||||||
entry.setSize(input.size());
|
|
||||||
} else
|
|
||||||
entry = (TarArchiveEntry)((TarArchiveOutputStream) arc).createArchiveEntry(input.getPath().get(), input.getName());
|
|
||||||
|
|
||||||
((TarArchiveOutputStream)arc).putArchiveEntry(entry);
|
((TarArchiveOutputStream)arc).putArchiveEntry(entry);
|
||||||
|
|
||||||
IOUtils.copy(fileInputStream, arc);
|
IOUtils.copy(fileInputStream, arc);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.create.compressors.tar;
|
package net.szum123321.textile_backup.core.create.compressors.tar;
|
||||||
|
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
import org.at4j.comp.bzip2.BZip2OutputStream;
|
import org.at4j.comp.bzip2.BZip2OutputStream;
|
||||||
import org.at4j.comp.bzip2.BZip2OutputStreamSettings;
|
import org.at4j.comp.bzip2.BZip2OutputStreamSettings;
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ public class ParallelBZip2Compressor extends AbstractTarArchiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OutputStream getCompressorOutputStream(OutputStream stream, ExecutableBackup ctx, int coreLimit) throws IOException {
|
protected OutputStream getCompressorOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException {
|
||||||
return new BZip2OutputStream(stream, new BZip2OutputStreamSettings().setNumberOfEncoderThreads(coreLimit));
|
return new BZip2OutputStream(stream, new BZip2OutputStreamSettings().setNumberOfEncoderThreads(coreLimit));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.create.compressors.tar;
|
package net.szum123321.textile_backup.core.create.compressors.tar;
|
||||||
|
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
import org.anarres.parallelgzip.ParallelGZIPOutputStream;
|
import org.anarres.parallelgzip.ParallelGZIPOutputStream;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
@ -33,7 +33,7 @@ public class ParallelGzipCompressor extends AbstractTarArchiver {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected OutputStream getCompressorOutputStream(OutputStream stream, ExecutableBackup ctx, int coreLimit) throws IOException {
|
protected OutputStream getCompressorOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException {
|
||||||
executorService = Executors.newFixedThreadPool(coreLimit);
|
executorService = Executors.newFixedThreadPool(coreLimit);
|
||||||
|
|
||||||
return new ParallelGZIPOutputStream(stream, executorService);
|
return new ParallelGZIPOutputStream(stream, executorService);
|
||||||
|
|
|
@ -1,100 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.digest;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.ByteOrder;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This algorithm copies the construction of <a href="https://ticki.github.io/blog/seahash-explained/">SeaHash</a> including its IV.
|
|
||||||
* What it differs in is that it uses Xoroshift64* instead of PCG as its pseudo-random function. Although it might lower
|
|
||||||
* the output quality, I don't think it matters that much, honestly. One advantage of xoroshift is that it should be
|
|
||||||
* easier to implement with AVX. Java should soon ship its vector api by default.
|
|
||||||
*/
|
|
||||||
public class BalticHash implements Hash {
|
|
||||||
//SeaHash IV
|
|
||||||
protected final static long[] IV = { 0x16f11fe89b0d677cL, 0xb480a793d8e6c86cL, 0x6fe2e5aaf078ebc9L, 0x14f994a4c5259381L };
|
|
||||||
private final long[] state = Arrays.copyOf(IV, IV.length);
|
|
||||||
protected final int buffer_limit = state.length * Long.BYTES;
|
|
||||||
protected final byte[] _byte_buffer = new byte[(state.length + 1) * Long.BYTES];
|
|
||||||
//Enforce endianness
|
|
||||||
protected final ByteBuffer buffer = ByteBuffer.wrap(_byte_buffer).order(ByteOrder.LITTLE_ENDIAN);
|
|
||||||
protected long hashed_data_length = 0;
|
|
||||||
|
|
||||||
public void update(int b) {
|
|
||||||
buffer.put((byte)b);
|
|
||||||
hashed_data_length += 1;
|
|
||||||
if (buffer.position() >= buffer_limit) round();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update(long b) {
|
|
||||||
buffer.putLong(b);
|
|
||||||
hashed_data_length += Long.BYTES;
|
|
||||||
if(buffer.position() >= buffer_limit) round();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update(byte[] data, int off, int len) {
|
|
||||||
int pos = 0;
|
|
||||||
while(pos < len) {
|
|
||||||
int n = Math.min(len - pos, buffer_limit - buffer.position());
|
|
||||||
System.arraycopy(data, off + pos, _byte_buffer, buffer.position(), n);
|
|
||||||
pos += n;
|
|
||||||
buffer.position(buffer.position() + n);
|
|
||||||
if(buffer.position() >= buffer_limit) round();
|
|
||||||
}
|
|
||||||
|
|
||||||
hashed_data_length += len;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getValue() {
|
|
||||||
if(buffer.position() != 0) {
|
|
||||||
while(buffer.position() < buffer_limit) buffer.put((byte)0);
|
|
||||||
round();
|
|
||||||
}
|
|
||||||
|
|
||||||
long result = state[0];
|
|
||||||
result ^= state[1];
|
|
||||||
result ^= state[2];
|
|
||||||
result ^= state[3];
|
|
||||||
result ^= hashed_data_length;
|
|
||||||
|
|
||||||
return xorshift64star(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void round() {
|
|
||||||
int p = buffer.position();
|
|
||||||
buffer.rewind();
|
|
||||||
|
|
||||||
for(int i = 0; i < 4; i++) state[i] ^= buffer.getLong();
|
|
||||||
for(int i = 0; i < 4; i++) state[i] = xorshift64star(state[i]);
|
|
||||||
|
|
||||||
if(p > buffer_limit) {
|
|
||||||
System.arraycopy(_byte_buffer, buffer_limit, _byte_buffer, 0, buffer.limit() - p);
|
|
||||||
buffer.position(buffer.limit() - p);
|
|
||||||
} else buffer.rewind();
|
|
||||||
}
|
|
||||||
|
|
||||||
long xorshift64star(long s) {
|
|
||||||
s ^= (s >> 12);
|
|
||||||
s ^= (s << 25);
|
|
||||||
s ^= (s >> 27);
|
|
||||||
return s * 0x2545F4914F6CDD1DL;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,73 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.digest;
|
|
||||||
|
|
||||||
import net.szum123321.textile_backup.Globals;
|
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
|
||||||
import net.szum123321.textile_backup.core.CompressionStatus;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* What this class does is it collects the hashed files and combines them into a single number,
|
|
||||||
* thus we can verify file tree integrity
|
|
||||||
*/
|
|
||||||
public class FileTreeHashBuilder {
|
|
||||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
|
||||||
private final Object lock = new Object();
|
|
||||||
private long hash = 0, filesProcessed = 0, filesTotalSize = 0;
|
|
||||||
|
|
||||||
private final CountDownLatch latch;
|
|
||||||
|
|
||||||
public FileTreeHashBuilder(int filesToProcess) {
|
|
||||||
latch = new CountDownLatch(filesToProcess);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void update(Path path, long newHash, long bytes) throws IOException {
|
|
||||||
if(path.getFileName().toString().equals(CompressionStatus.DATA_FILENAME)) return;
|
|
||||||
|
|
||||||
latch.countDown();
|
|
||||||
|
|
||||||
synchronized (lock) {
|
|
||||||
this.hash ^= newHash;
|
|
||||||
filesTotalSize += bytes;
|
|
||||||
filesProcessed++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getRemaining() { return (int) latch.getCount(); }
|
|
||||||
|
|
||||||
public long getValue(boolean lock) throws InterruptedException {
|
|
||||||
long leftover = latch.getCount();
|
|
||||||
if(lock) latch.await();
|
|
||||||
else if(leftover != 0) log.warn("处理中,剩余{}个文件未处理!", leftover);
|
|
||||||
|
|
||||||
var hasher = Globals.CHECKSUM_SUPPLIER.get();
|
|
||||||
|
|
||||||
log.debug("文件数:{},字节数:{},原始哈希值:{}", filesProcessed, filesTotalSize, hash);
|
|
||||||
hasher.update(hash);
|
|
||||||
hasher.update(filesProcessed);
|
|
||||||
hasher.update(filesTotalSize);
|
|
||||||
|
|
||||||
return hasher.getValue();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.digest;
|
|
||||||
|
|
||||||
public interface Hash {
|
|
||||||
|
|
||||||
void update(int b);
|
|
||||||
|
|
||||||
void update(long b);
|
|
||||||
|
|
||||||
default void update(byte[] b) { update(b, 0, b.length); }
|
|
||||||
|
|
||||||
void update(byte[] b, int off, int len);
|
|
||||||
|
|
||||||
long getValue();
|
|
||||||
}
|
|
|
@ -1,98 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.digest;
|
|
||||||
|
|
||||||
import net.szum123321.textile_backup.Globals;
|
|
||||||
import net.szum123321.textile_backup.core.DataLeftException;
|
|
||||||
import net.szum123321.textile_backup.core.create.BrokenFileHandler;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class calculates a hash of the file on the input stream, submits it to FileTreeHashBuilder.
|
|
||||||
* In case the underlying stream hasn't been read completely in, puts it into BrokeFileHandler
|
|
||||||
|
|
||||||
* Furthermore, ParallelZip works by putting all the file requests into a queue and then compressing them
|
|
||||||
* with multiple threads. Thus, we have to make sure that all the files have been read before requesting the final value
|
|
||||||
* That is what CountDownLatch does
|
|
||||||
*/
|
|
||||||
public class HashingInputStream extends FilterInputStream {
|
|
||||||
private final Path path;
|
|
||||||
private final Hash hash = Globals.CHECKSUM_SUPPLIER.get();
|
|
||||||
private final FileTreeHashBuilder hashBuilder;
|
|
||||||
private final BrokenFileHandler brokenFileHandler;
|
|
||||||
|
|
||||||
private long bytesWritten = 0;
|
|
||||||
|
|
||||||
public HashingInputStream(InputStream in, Path path, FileTreeHashBuilder hashBuilder, BrokenFileHandler brokenFileHandler) {
|
|
||||||
super(in);
|
|
||||||
this.path = path;
|
|
||||||
this.hashBuilder = hashBuilder;
|
|
||||||
this.brokenFileHandler = brokenFileHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read(byte @NotNull [] b, int off, int len) throws IOException {
|
|
||||||
int i;
|
|
||||||
try {
|
|
||||||
i = in.read(b, off, len);
|
|
||||||
} catch(IOException e) {
|
|
||||||
throw new IOException("An exception occurred while trying to access: [" + path.toString() + "]", e);
|
|
||||||
}
|
|
||||||
if(i != -1) {
|
|
||||||
hash.update(b, off, i);
|
|
||||||
bytesWritten += i;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read() throws IOException {
|
|
||||||
int i;
|
|
||||||
try {
|
|
||||||
i = in.read();
|
|
||||||
} catch(IOException e) {
|
|
||||||
throw new IOException("An exception occurred while trying to access: [" + path.toString() + "]", e);
|
|
||||||
}
|
|
||||||
if(i != -1) {
|
|
||||||
hash.update(i);
|
|
||||||
bytesWritten++;
|
|
||||||
}
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean markSupported() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException {
|
|
||||||
hash.update(path.getFileName().toString().getBytes(StandardCharsets.UTF_8));
|
|
||||||
|
|
||||||
hashBuilder.update(path, hash.getValue(), bytesWritten);
|
|
||||||
|
|
||||||
if(in.available() != 0) brokenFileHandler.handle(path, new DataLeftException(in.available()));
|
|
||||||
|
|
||||||
super.close();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,63 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.digest;
|
|
||||||
|
|
||||||
import net.szum123321.textile_backup.Globals;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.io.FilterOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
|
|
||||||
public class HashingOutputStream extends FilterOutputStream {
|
|
||||||
private final Path path;
|
|
||||||
private final Hash hash = Globals.CHECKSUM_SUPPLIER.get();
|
|
||||||
private final FileTreeHashBuilder hashBuilder;
|
|
||||||
|
|
||||||
private long bytesWritten = 0;
|
|
||||||
|
|
||||||
public HashingOutputStream(OutputStream out, Path path, FileTreeHashBuilder hashBuilder) {
|
|
||||||
super(out);
|
|
||||||
this.path = path;
|
|
||||||
this.hashBuilder = hashBuilder;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void write(int b) throws IOException {
|
|
||||||
out.write(b);
|
|
||||||
hash.update(b);
|
|
||||||
bytesWritten++;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void write(byte @NotNull [] b, int off, int len) throws IOException {
|
|
||||||
out.write(b, off, len);
|
|
||||||
hash.update(b, off, len);
|
|
||||||
bytesWritten += len;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException {
|
|
||||||
hash.update(path.getFileName().toString().getBytes(StandardCharsets.UTF_8));
|
|
||||||
hashBuilder.update(path, hash.getValue(), bytesWritten);
|
|
||||||
super.close();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -42,13 +42,13 @@ public class AwaitThread extends Thread {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
log.info("开始倒计时...等待{}秒.", delay);
|
log.info("Countdown begins... Waiting {} second.", delay);
|
||||||
|
|
||||||
// 𝄞 This is final count down! Tu ruru Tu, Tu Ru Tu Tu ♪
|
// 𝄞 This is final count down! Tu ruru Tu, Tu Ru Tu Tu ♪
|
||||||
try {
|
try {
|
||||||
Thread.sleep(delay * 1000L);
|
Thread.sleep(delay * 1000L);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
log.info("备份恢复已取消.");
|
log.info("Backup restoration cancelled.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
Copyright (C) 2020 Szum123321
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.restore;
|
package net.szum123321.textile_backup.core.restore;
|
||||||
|
|
||||||
|
@ -24,22 +24,18 @@ import net.szum123321.textile_backup.TextileLogger;
|
||||||
import net.szum123321.textile_backup.config.ConfigHelper;
|
import net.szum123321.textile_backup.config.ConfigHelper;
|
||||||
import net.szum123321.textile_backup.config.ConfigPOJO;
|
import net.szum123321.textile_backup.config.ConfigPOJO;
|
||||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||||
import net.szum123321.textile_backup.core.CompressionStatus;
|
import net.szum123321.textile_backup.core.LivingServer;
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
import net.szum123321.textile_backup.core.create.ExecutableBackup;
|
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||||
|
import net.szum123321.textile_backup.core.create.MakeBackupRunnableFactory;
|
||||||
import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecompressor;
|
import net.szum123321.textile_backup.core.restore.decompressors.GenericTarDecompressor;
|
||||||
import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
|
import net.szum123321.textile_backup.core.restore.decompressors.ZipDecompressor;
|
||||||
import net.szum123321.textile_backup.mixin.MinecraftServerSessionAccessor;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.FutureTask;
|
|
||||||
|
|
||||||
/**
|
//TODO: Verify backup's validity?
|
||||||
* This class restores a file provided by RestoreContext.
|
|
||||||
*/
|
|
||||||
public class RestoreBackupRunnable implements Runnable {
|
public class RestoreBackupRunnable implements Runnable {
|
||||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||||
private final static ConfigHelper config = ConfigHelper.INSTANCE;
|
private final static ConfigHelper config = ConfigHelper.INSTANCE;
|
||||||
|
@ -54,105 +50,63 @@ public class RestoreBackupRunnable implements Runnable {
|
||||||
public void run() {
|
public void run() {
|
||||||
Globals.INSTANCE.globalShutdownBackupFlag.set(false);
|
Globals.INSTANCE.globalShutdownBackupFlag.set(false);
|
||||||
|
|
||||||
log.info("关闭服务器...");
|
log.info("Shutting down server...");
|
||||||
|
|
||||||
ctx.server().stop(false);
|
ctx.server().stop(false);
|
||||||
|
awaitServerShutdown();
|
||||||
|
|
||||||
Path worldFile = Utilities.getWorldFolder(ctx.server()),
|
if(config.get().backupOldWorlds) {
|
||||||
tmp;
|
MakeBackupRunnableFactory.create(
|
||||||
|
BackupContext.Builder
|
||||||
try {
|
.newBackupContextBuilder()
|
||||||
tmp = Files.createTempDirectory(
|
.setServer(ctx.server())
|
||||||
ctx.server().getRunDirectory().toPath(),
|
.setInitiator(ActionInitiator.Restore)
|
||||||
ctx.restoreableFile().getFile().getFileName().toString()
|
.setComment("Old_World" + (ctx.comment() != null ? "_" + ctx.comment() : ""))
|
||||||
);
|
.build()
|
||||||
} catch (IOException e) {
|
).run();
|
||||||
log.error("在解压备份时发生了异常.", e);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//By making a separate thread we can start unpacking an old backup instantly
|
Path worldFile = Utilities.getWorldFolder(ctx.server());
|
||||||
//Let the server shut down gracefully, and wait for the old world backup to complete
|
|
||||||
FutureTask<Void> waitForShutdown = new FutureTask<>(() -> {
|
|
||||||
ctx.server().getThread().join(); //wait for server thread to die and save all its state
|
|
||||||
|
|
||||||
if(config.get().backupOldWorlds) {
|
|
||||||
return ExecutableBackup.Builder
|
|
||||||
.newBackupContextBuilder()
|
|
||||||
.setServer(ctx.server())
|
|
||||||
.setInitiator(ActionInitiator.Restore)
|
|
||||||
.noCleanup()
|
|
||||||
.setComment("Old_World" + (ctx.comment() != null ? "_" + ctx.comment() : ""))
|
|
||||||
.announce()
|
|
||||||
.build().call();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
|
|
||||||
//run the thread.
|
|
||||||
new Thread(waitForShutdown, "Server shutdown wait thread").start();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log.info("开始解压...");
|
Path tmp = Files.createTempDirectory(
|
||||||
|
worldFile.getParent(),
|
||||||
|
ctx.restoreableFile().getFile().getFileName().toString());
|
||||||
|
|
||||||
long hash;
|
log.info("Starting decompression...");
|
||||||
|
|
||||||
if (ctx.restoreableFile().getArchiveFormat() == ConfigPOJO.ArchiveFormat.ZIP)
|
if (ctx.restoreableFile().getArchiveFormat() == ConfigPOJO.ArchiveFormat.ZIP)
|
||||||
hash = ZipDecompressor.decompress(ctx.restoreableFile().getFile(), tmp);
|
ZipDecompressor.decompress(ctx.restoreableFile().getFile(), tmp);
|
||||||
else
|
else
|
||||||
hash = GenericTarDecompressor.decompress(ctx.restoreableFile().getFile(), tmp);
|
GenericTarDecompressor.decompress(ctx.restoreableFile().getFile(), tmp);
|
||||||
|
|
||||||
log.info("等待服务器完全终止...");
|
log.info("Deleting old world...");
|
||||||
|
|
||||||
//locks until the backup is finished and the server is dead
|
Utilities.deleteDirectory(worldFile);
|
||||||
waitForShutdown.get();
|
Files.move(tmp, worldFile);
|
||||||
|
|
||||||
Optional<String> errorMsg;
|
if (config.get().deleteOldBackupAfterRestore) {
|
||||||
|
log.info("Deleting old backup");
|
||||||
|
|
||||||
if(Files.notExists(CompressionStatus.resolveStatusFilename(tmp))) {
|
Files.delete(ctx.restoreableFile().getFile());
|
||||||
errorMsg = Optional.of("未找到状态文件!");
|
|
||||||
} else {
|
|
||||||
CompressionStatus status = CompressionStatus.readFromFile(tmp);
|
|
||||||
|
|
||||||
log.info("状态: {}", status);
|
|
||||||
|
|
||||||
Files.delete(tmp.resolve(CompressionStatus.DATA_FILENAME));
|
|
||||||
|
|
||||||
errorMsg = status.validate(hash, ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(errorMsg.isEmpty() || !config.get().integrityVerificationMode.verify()) {
|
|
||||||
if (errorMsg.isEmpty()) log.info("备份验证有效, 正在恢复.");
|
|
||||||
else log.info("备份已损坏,但验证已禁用[{}]。正在恢复. ", errorMsg.get());
|
|
||||||
|
|
||||||
//Disables write lock to override world file
|
|
||||||
((MinecraftServerSessionAccessor) ctx.server()).getSession().close();
|
|
||||||
|
|
||||||
Utilities.deleteDirectory(worldFile);
|
|
||||||
Files.move(tmp, worldFile);
|
|
||||||
|
|
||||||
if (config.get().deleteOldBackupAfterRestore) {
|
|
||||||
log.info("正在删除恢复的备份文件.");
|
|
||||||
Files.delete(ctx.restoreableFile().getFile());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.error(errorMsg.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("在尝试恢复备份时发生了异常!", e);
|
|
||||||
} finally {
|
|
||||||
//Regardless of what happened, we should still clean up
|
|
||||||
if(Files.exists(tmp)) {
|
|
||||||
try {
|
|
||||||
Utilities.deleteDirectory(tmp);
|
|
||||||
} catch (IOException ignored) {}
|
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("An exception occurred while trying to restore a backup!", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
//in case we're playing on client
|
//in case we're playing on client
|
||||||
Globals.INSTANCE.globalShutdownBackupFlag.set(true);
|
Globals.INSTANCE.globalShutdownBackupFlag.set(true);
|
||||||
|
|
||||||
log.info("完成!");
|
log.info("Done!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void awaitServerShutdown() {
|
||||||
|
while(((LivingServer)ctx.server()).isAlive()) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("Exception occurred!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2020 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
Copyright (C) 2020 Szum123321
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.restore;
|
package net.szum123321.textile_backup.core.restore;
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ public class RestoreHelper {
|
||||||
|
|
||||||
Optional<RestoreableFile> optionalFile =
|
Optional<RestoreableFile> optionalFile =
|
||||||
RestoreableFile.applyOnFiles(root, Optional.empty(),
|
RestoreableFile.applyOnFiles(root, Optional.empty(),
|
||||||
e -> log.error("在尝试锁定文件时发生了异常!", e),
|
e -> log.error("An exception occurred while trying to lock the file!", e),
|
||||||
s -> s.filter(rf -> rf.getCreationTime().equals(backupTime))
|
s -> s.filter(rf -> rf.getCreationTime().equals(backupTime))
|
||||||
.findFirst());
|
.findFirst());
|
||||||
|
|
||||||
|
@ -63,12 +63,13 @@ public class RestoreHelper {
|
||||||
|
|
||||||
public static AwaitThread create(RestoreContext ctx) {
|
public static AwaitThread create(RestoreContext ctx) {
|
||||||
if(ctx.initiator() == ActionInitiator.Player)
|
if(ctx.initiator() == ActionInitiator.Player)
|
||||||
log.info("备份恢复由以下玩家发起:{}", ctx.commandSource().getName());
|
log.info("Backup restoration was initiated by: {}", ctx.commandSource().getName());
|
||||||
else
|
else
|
||||||
log.info("备份恢复由服务器控制台发起");
|
log.info("Backup restoration was initiated form Server Console");
|
||||||
|
|
||||||
Utilities.notifyPlayers(
|
Utilities.notifyPlayers(
|
||||||
ctx.server(),
|
ctx.server(),
|
||||||
"警告!服务器将在" + config.get().restoreDelay + "秒后关闭!"
|
"Warning! The server is going to shut down in " + config.get().restoreDelay + " seconds!"
|
||||||
);
|
);
|
||||||
|
|
||||||
return new AwaitThread(
|
return new AwaitThread(
|
||||||
|
@ -81,7 +82,7 @@ public class RestoreHelper {
|
||||||
Path root = Utilities.getBackupRootPath(Utilities.getLevelName(server));
|
Path root = Utilities.getBackupRootPath(Utilities.getLevelName(server));
|
||||||
|
|
||||||
return RestoreableFile.applyOnFiles(root, new LinkedList<>(),
|
return RestoreableFile.applyOnFiles(root, new LinkedList<>(),
|
||||||
e -> log.error("列出可用备份时发生错误.", e),
|
e -> log.error("Error while listing available backups", e),
|
||||||
s -> s.sorted().collect(Collectors.toCollection(LinkedList::new)));
|
s -> s.sorted().collect(Collectors.toCollection(LinkedList::new)));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,28 +1,26 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
Copyright (C) 2020 Szum123321
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.restore.decompressors;
|
package net.szum123321.textile_backup.core.restore.decompressors;
|
||||||
|
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
import net.szum123321.textile_backup.TextileBackup;
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
import net.szum123321.textile_backup.TextileLogger;
|
||||||
import net.szum123321.textile_backup.core.digest.FileTreeHashBuilder;
|
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
import net.szum123321.textile_backup.core.digest.HashingOutputStream;
|
|
||||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||||
import org.apache.commons.compress.compressors.CompressorException;
|
import org.apache.commons.compress.compressors.CompressorException;
|
||||||
|
@ -38,9 +36,8 @@ import java.time.Instant;
|
||||||
public class GenericTarDecompressor {
|
public class GenericTarDecompressor {
|
||||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||||
|
|
||||||
public static long decompress(Path input, Path target) throws IOException {
|
public static void decompress(Path input, Path target) throws IOException {
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
FileTreeHashBuilder treeBuilder = new FileTreeHashBuilder(0);
|
|
||||||
|
|
||||||
try (InputStream fileInputStream = Files.newInputStream(input);
|
try (InputStream fileInputStream = Files.newInputStream(input);
|
||||||
InputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
InputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
|
||||||
|
@ -49,8 +46,10 @@ public class GenericTarDecompressor {
|
||||||
TarArchiveEntry entry;
|
TarArchiveEntry entry;
|
||||||
|
|
||||||
while ((entry = archiveInputStream.getNextTarEntry()) != null) {
|
while ((entry = archiveInputStream.getNextTarEntry()) != null) {
|
||||||
if(!archiveInputStream.canReadEntryData(entry))
|
if(!archiveInputStream.canReadEntryData(entry)) {
|
||||||
throw new IOException("Couldn't read archive entry! " + entry.getName());
|
log.error("Something when wrong while trying to decompress {}", entry.getName());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Path file = target.resolve(entry.getName());
|
Path file = target.resolve(entry.getName());
|
||||||
|
|
||||||
|
@ -59,8 +58,8 @@ public class GenericTarDecompressor {
|
||||||
} else {
|
} else {
|
||||||
Files.createDirectories(file.getParent());
|
Files.createDirectories(file.getParent());
|
||||||
try (OutputStream outputStream = Files.newOutputStream(file);
|
try (OutputStream outputStream = Files.newOutputStream(file);
|
||||||
HashingOutputStream out = new HashingOutputStream(outputStream, file, treeBuilder)) {
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
|
||||||
IOUtils.copy(archiveInputStream, out);
|
IOUtils.copy(archiveInputStream, bufferedOutputStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,13 +67,7 @@ public class GenericTarDecompressor {
|
||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("解压缩耗时{}秒. ", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
log.info("Decompression took {} seconds.", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
||||||
|
|
||||||
try {
|
|
||||||
return treeBuilder.getValue(false);
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static InputStream getCompressorInputStream(InputStream inputStream) throws CompressorException {
|
private static InputStream getCompressorInputStream(InputStream inputStream) throws CompressorException {
|
||||||
|
|
|
@ -1,28 +1,26 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
Copyright (C) 2020 Szum123321
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package net.szum123321.textile_backup.core.restore.decompressors;
|
package net.szum123321.textile_backup.core.restore.decompressors;
|
||||||
|
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
import net.szum123321.textile_backup.TextileBackup;
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
import net.szum123321.textile_backup.TextileLogger;
|
||||||
import net.szum123321.textile_backup.core.digest.FileTreeHashBuilder;
|
|
||||||
import net.szum123321.textile_backup.core.Utilities;
|
import net.szum123321.textile_backup.core.Utilities;
|
||||||
import net.szum123321.textile_backup.core.digest.HashingOutputStream;
|
|
||||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||||
import org.apache.commons.compress.archivers.zip.ZipFile;
|
import org.apache.commons.compress.archivers.zip.ZipFile;
|
||||||
import org.apache.commons.compress.utils.IOUtils;
|
import org.apache.commons.compress.utils.IOUtils;
|
||||||
|
@ -37,11 +35,9 @@ import java.util.Iterator;
|
||||||
public class ZipDecompressor {
|
public class ZipDecompressor {
|
||||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||||
|
|
||||||
public static long decompress(Path inputFile, Path target) throws IOException {
|
public static void decompress(Path inputFile, Path target) throws IOException {
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
|
|
||||||
FileTreeHashBuilder hashBuilder = new FileTreeHashBuilder(0);
|
|
||||||
|
|
||||||
try(ZipFile zipFile = new ZipFile(inputFile.toFile())) {
|
try(ZipFile zipFile = new ZipFile(inputFile.toFile())) {
|
||||||
for (Iterator<ZipArchiveEntry> it = zipFile.getEntries().asIterator(); it.hasNext(); ) {
|
for (Iterator<ZipArchiveEntry> it = zipFile.getEntries().asIterator(); it.hasNext(); ) {
|
||||||
ZipArchiveEntry entry = it.next();
|
ZipArchiveEntry entry = it.next();
|
||||||
|
@ -52,21 +48,13 @@ public class ZipDecompressor {
|
||||||
} else {
|
} else {
|
||||||
Files.createDirectories(file.getParent());
|
Files.createDirectories(file.getParent());
|
||||||
try (OutputStream outputStream = Files.newOutputStream(file);
|
try (OutputStream outputStream = Files.newOutputStream(file);
|
||||||
HashingOutputStream out = new HashingOutputStream(outputStream, file, hashBuilder);
|
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)) {
|
||||||
InputStream in = zipFile.getInputStream(entry)) {
|
IOUtils.copy(zipFile.getInputStream(entry), bufferedOutputStream);
|
||||||
|
|
||||||
IOUtils.copy(in, out);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("解压缩耗时{}秒。", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
log.info("Decompression took: {} seconds.", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
||||||
|
|
||||||
try {
|
|
||||||
return hashBuilder.getValue(false);
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
A simple backup mod for Fabric
|
||||||
|
Copyright (C) 2020 Szum123321
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.szum123321.textile_backup.mixin;
|
||||||
|
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.szum123321.textile_backup.core.LivingServer;
|
||||||
|
import org.spongepowered.asm.mixin.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Unique;
|
||||||
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
|
|
||||||
|
@Mixin(MinecraftServer.class)
|
||||||
|
public class MinecraftServerMixin implements LivingServer {
|
||||||
|
@Unique
|
||||||
|
private boolean isAlive = true;
|
||||||
|
|
||||||
|
@Inject(method = "shutdown", at = @At("TAIL"))
|
||||||
|
public void onFinalWorldSave(CallbackInfo ci) {
|
||||||
|
isAlive = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Unique
|
||||||
|
@Override
|
||||||
|
public boolean isAlive() {
|
||||||
|
return isAlive;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* A simple backup mod for Fabric
|
* A simple backup mod for Fabric
|
||||||
* Copyright (C) 2022 Szum123321
|
* Copyright (C) 2021 Szum123321
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
|
@ -1,63 +0,0 @@
|
||||||
/*
|
|
||||||
* A simple backup mod for Fabric
|
|
||||||
* Copyright (C) 2022 Szum123321
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package net.szum123321.textile_backup.test;
|
|
||||||
|
|
||||||
import net.minecraft.util.math.random.Random;
|
|
||||||
import net.szum123321.textile_backup.TextileBackup;
|
|
||||||
import net.szum123321.textile_backup.TextileLogger;
|
|
||||||
import net.szum123321.textile_backup.core.digest.BalticHash;
|
|
||||||
|
|
||||||
public class BalticHashTest {
|
|
||||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
|
||||||
final static int TEST_LEN = 21377; //simple prime
|
|
||||||
public static void run() throws RuntimeException {
|
|
||||||
log.info("Running hash test");
|
|
||||||
Random r = Random.create(2137);
|
|
||||||
long x = 0;
|
|
||||||
|
|
||||||
byte[] data = new byte[TEST_LEN];
|
|
||||||
|
|
||||||
for(int i = 0; i < TEST_LEN; i++) data[i] = (byte)r.nextInt();
|
|
||||||
|
|
||||||
//Test block mode
|
|
||||||
for(int i = 0; i < 5*2; i++) x ^= randomHash(data, r);
|
|
||||||
if(x != 0) throw new RuntimeException("Hash mismatch!");
|
|
||||||
|
|
||||||
log.info("Test passed");
|
|
||||||
}
|
|
||||||
|
|
||||||
static long randomHash(byte[] data, Random r) {
|
|
||||||
int n = data.length;
|
|
||||||
|
|
||||||
BalticHash h = new BalticHash();
|
|
||||||
|
|
||||||
int m = r.nextBetween(1, n);
|
|
||||||
|
|
||||||
int nn = n, p = 0;
|
|
||||||
|
|
||||||
for(int i = 0; i < m; i++) {
|
|
||||||
int k = r.nextBetween(1, nn - (m - i - 1));
|
|
||||||
h.update(data, p, k);
|
|
||||||
p += k;
|
|
||||||
nn -= k;
|
|
||||||
}
|
|
||||||
|
|
||||||
return h.getValue();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -47,9 +47,6 @@
|
||||||
"text.autoconfig.textile_backup.option.format": "Archive and compression format",
|
"text.autoconfig.textile_backup.option.format": "Archive and compression format",
|
||||||
"text.autoconfig.textile_backup.option.format.@Tooltip": "See: https://github.com/Szum123321/textile_backup/wiki/Configuration#format",
|
"text.autoconfig.textile_backup.option.format.@Tooltip": "See: https://github.com/Szum123321/textile_backup/wiki/Configuration#format",
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.integrityVerificationMode": "Verify backup integrity",
|
|
||||||
"text.autoconfig.textile_backup.option.integrityVerificationMode.@Tooltip": "DO NOT ALTER unless fully aware of consequences",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.permissionLevel": "Min permission level",
|
"text.autoconfig.textile_backup.option.permissionLevel": "Min permission level",
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.alwaysSingleplayerAllowed": "Always allow on single-player",
|
"text.autoconfig.textile_backup.option.alwaysSingleplayerAllowed": "Always allow on single-player",
|
||||||
|
|
|
@ -1,63 +0,0 @@
|
||||||
{
|
|
||||||
"text.autoconfig.textile_backup.title": "Textile备份配置",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.category.default": "常规",
|
|
||||||
"text.autoconfig.textile_backup.category.Create": "备份设置",
|
|
||||||
"text.autoconfig.textile_backup.category.Restore": "还原",
|
|
||||||
"text.autoconfig.textile_backup.category.Manage": "管理",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.backupInterval": "备份间隔",
|
|
||||||
"text.autoconfig.textile_backup.option.backupInterval.@Tooltip": "每次自动备份之间的时间间隔(以秒为单位)",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.restoreDelay": "还原延迟",
|
|
||||||
"text.autoconfig.textile_backup.option.restoreDelay.@Tooltip": "以秒为单位",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.broadcastBackupStart": "通告备份开始",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.broadcastBackupDone": "通告备份完成",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.doBackupsOnEmptyServer": "在服务器为空时进行自动备份",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.shutdownBackup": "关机时进行备份",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.backupOldWorlds": "备份旧世界",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.perWorldBackup": "使用不同的文件夹来存储不同的世界备份",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.backupDirectoryPath": "备份文件夹路径",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.fileBlacklist": "黑名单文件",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.deleteOldBackupAfterRestore": "还原后删除备份",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.backupsToKeep": "要保留的备份数量",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.maxAge": "备份的最大存储期",
|
|
||||||
"text.autoconfig.textile_backup.option.maxAge.@Tooltip": "以创建时间为准,以秒为单位",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.maxSize": "备份文件夹的最大大小",
|
|
||||||
"text.autoconfig.textile_backup.option.maxSize.@Tooltip": "以KBytes为单位",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.compression": "压缩级别",
|
|
||||||
"text.autoconfig.textile_backup.option.compression.@Tooltip": "仅适用于zip格式",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.compressionCoreCountLimit": "用于压缩的最大核心数",
|
|
||||||
"text.autoconfig.textile_backup.option.compressionCoreCountLimit.@Tooltip": "设置为0可使用所有可用核心",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.format": "存档和压缩格式",
|
|
||||||
"text.autoconfig.textile_backup.option.format.@Tooltip": "参见:https://github.com/Szum123321/textile_backup/wiki/Configuration#format",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.integrityVerificationMode": "验证备份完整性",
|
|
||||||
"text.autoconfig.textile_backup.option.integrityVerificationMode.@Tooltip": "除非完全了解后果,否则不要修改",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.permissionLevel": "最低权限等级",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.alwaysSingleplayerAllowed": "始终允许单人游戏",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.playerWhitelist": "管理员白名单",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.playerBlacklist": "管理员黑名单",
|
|
||||||
|
|
||||||
"text.autoconfig.textile_backup.option.dateTimeFormat": "日期和时间格式",
|
|
||||||
"text.autoconfig.textile_backup.option.dateTimeFormat.@Tooltip": "参见:https://github.com/Szum123321/textile_backup/wiki/Date-time-format"
|
|
||||||
}
|
|
|
@ -38,9 +38,9 @@
|
||||||
],
|
],
|
||||||
|
|
||||||
"depends": {
|
"depends": {
|
||||||
"fabricloader": ">=0.14.0",
|
"fabricloader": ">=0.14.6",
|
||||||
"fabric": "*",
|
"fabric": "*",
|
||||||
"minecraft": "^1.20-",
|
"minecraft": ">=1.19.1",
|
||||||
"cloth-config2": "*",
|
"cloth-config2": "*",
|
||||||
"java": ">=16"
|
"java": ">=16"
|
||||||
},
|
},
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
"compatibilityLevel": "JAVA_16",
|
"compatibilityLevel": "JAVA_16",
|
||||||
"mixins": [
|
"mixins": [
|
||||||
"DedicatedServerWatchdogMixin",
|
"DedicatedServerWatchdogMixin",
|
||||||
|
"MinecraftServerMixin",
|
||||||
"MinecraftServerSessionAccessor"
|
"MinecraftServerSessionAccessor"
|
||||||
],
|
],
|
||||||
"client": [
|
"client": [
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package net.szum123321.test.textile_backup;
|
||||||
|
|
||||||
|
import net.fabricmc.api.ModInitializer;
|
||||||
|
|
||||||
|
public class TextileBackupTest implements ModInitializer {
|
||||||
|
@Override
|
||||||
|
public void onInitialize() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
{
|
||||||
|
"schemaVersion": 1,
|
||||||
|
"id": "textile_backup",
|
||||||
|
"version": "${version}",
|
||||||
|
|
||||||
|
"name": "Textile Backup Test",
|
||||||
|
"authors": [
|
||||||
|
"Szum123321"
|
||||||
|
],
|
||||||
|
"contact": {
|
||||||
|
"homepage": "https://www.curseforge.com/minecraft/mc-mods/textile-backup",
|
||||||
|
"issues": "https://github.com/Szum123321/textile_backup/issues",
|
||||||
|
"sources": "https://github.com/Szum123321/textile_backup"
|
||||||
|
},
|
||||||
|
|
||||||
|
"license": "GPLv3",
|
||||||
|
|
||||||
|
"environment": "*",
|
||||||
|
"entrypoints": {
|
||||||
|
"main": [
|
||||||
|
"net.szum123321.test.textile_backup.TextileBackupTest"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"mixins": [
|
||||||
|
],
|
||||||
|
|
||||||
|
"depends": {
|
||||||
|
"fabricloader": ">=0.14.6",
|
||||||
|
"fabric": "*",
|
||||||
|
"minecraft": ">=1.19.1",
|
||||||
|
"cloth-config2": "*",
|
||||||
|
"java": ">=16",
|
||||||
|
"textile_backup": "*"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"required": true,
|
||||||
|
"package": "net.szum123321.test.textile_backup.mixin",
|
||||||
|
"compatibilityLevel": "JAVA_16",
|
||||||
|
"mixins": [
|
||||||
|
],
|
||||||
|
"client": [
|
||||||
|
],
|
||||||
|
"injectors": {
|
||||||
|
"defaultRequire": 1
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue