From 6d53613a57d542a424d6368cb91c69dfd5c6772e Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 20 Jan 2024 12:44:17 +0800 Subject: add plugin-wide config; enable CoW through config file --- .../com/keuin/kbackupfabric/KBPluginEvents.java | 12 ++++- .../manager/IncrementalBackupStorageManager.java | 16 +++++-- .../keuin/kbackupfabric/config/KBackupConfig.java | 56 ++++++++++++++++++++++ 3 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java index 362abb8..8b55d1e 100644 --- a/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java +++ b/src/main/java/com/keuin/kbackupfabric/KBPluginEvents.java @@ -2,6 +2,7 @@ package com.keuin.kbackupfabric; import com.keuin.kbackupfabric.backup.BackupFilesystemUtil; import com.keuin.kbackupfabric.backup.suggestion.BackupNameSuggestionProvider; +import com.keuin.kbackupfabric.config.KBackupConfig; import com.keuin.kbackupfabric.event.OnPlayerConnect; import com.keuin.kbackupfabric.metadata.BackupMetadata; import com.keuin.kbackupfabric.metadata.MetadataHolder; @@ -29,10 +30,17 @@ import static org.apache.commons.io.FileUtils.forceDelete; public final class KBPluginEvents implements ModInitializer { //private static final Logger LOGGER = LogManager.getLogger(); - @Override public void onInitialize() { - System.out.println("Binding events and commands ..."); + PrintUtil.info("Reading config..."); + try { + KBackupConfig.load(); + } catch (IOException e) { + PrintUtil.error("Failed to read config file, using default values: " + e + e.getMessage()); + } + boolean incCow = KBackupConfig.getInstance().getIncbakCow(); + PrintUtil.info("Incremental backup CoW: " + (incCow ? "enabled" : "disabled")); + PrintUtil.info("Binding events and commands..."); CommandRegistrationCallback.EVENT.register(KBCommandsRegister::registerCommands); OnPlayerConnect.ON_PLAYER_CONNECT.register((connection, player) -> NotificationManager.INSTANCE.notifyPlayer(DistinctNotifiable.fromServerPlayerEntity(player))); diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java index a680db7..53143f8 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -4,6 +4,7 @@ import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2; import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionIterator; import com.keuin.kbackupfabric.backup.incremental.ObjectElement; import com.keuin.kbackupfabric.backup.incremental.identifier.ObjectIdentifier; +import com.keuin.kbackupfabric.config.KBackupConfig; import com.keuin.kbackupfabric.util.FilesystemUtil; import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.cow.FileCopier; @@ -29,14 +30,19 @@ public class IncrementalBackupStorageManager { private final Logger logger = Logger.getLogger(IncrementalBackupStorageManager.class.getName()); private final Path backupStorageBase; private final Logger LOGGER = Logger.getLogger(IncrementalBackupStorageManager.class.getName()); - private FileCopier copier = null; + private FileCopier copier; public IncrementalBackupStorageManager(Path backupStorageBase) { this.backupStorageBase = backupStorageBase; - try { - this.copier = FileCowCopier.getInstance(); - } catch (Exception | UnsatisfiedLinkError ex) { - PrintUtil.error("Failed to initialize kbackup-cow: " + ex + ex.getMessage()); + if (KBackupConfig.getInstance().getIncbakCow()) { + // try to use cow copier, if failed, fallback to normal copier + try { + this.copier = FileCowCopier.getInstance(); + } catch (Exception | UnsatisfiedLinkError ex) { + PrintUtil.error("Failed to initialize kbackup-cow: " + ex + ex.getMessage()); + this.copier = new FileEagerCopier(); + } + } else { this.copier = new FileEagerCopier(); } if (this.copier.isCow()) { diff --git a/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java b/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java new file mode 100644 index 0000000..8ece8d2 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/config/KBackupConfig.java @@ -0,0 +1,56 @@ +package com.keuin.kbackupfabric.config; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.keuin.kbackupfabric.util.PrintUtil; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class KBackupConfig { + + private static KBackupConfig instance = getDefault(); + private static final String CONFIG_FILE = "kbackup.json"; + + @JsonProperty("incbak_cow") + private Boolean incbakCow; + + public static KBackupConfig getInstance() { + return instance; + } + + private static KBackupConfig getDefault() { + return new KBackupConfig(false); + } + + public static void load() throws IOException { + File file = new File(CONFIG_FILE); + ObjectMapper om = new ObjectMapper(); + try { + instance = om.readValue(file, KBackupConfig.class); + } catch (FileNotFoundException ignored) { + // generate default config file + PrintUtil.info("Config file does not exist. Creating default config: " + CONFIG_FILE); + instance = getDefault(); + ObjectWriter w = om.writerWithDefaultPrettyPrinter(); + w.writeValue(file, instance); + } + } + + public KBackupConfig() { + } + + public KBackupConfig(Boolean incbakCow) { + this.incbakCow = incbakCow; + } + + public Boolean getIncbakCow() { + return this.incbakCow; + } + + public void setIncbakCow(Boolean incbakCow) { + this.incbakCow = incbakCow; + } +} \ No newline at end of file -- cgit v1.2.3