From 7e3cd92742383c43f2741449c551208e6487154e Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 20 Jan 2024 13:20:41 +0800 Subject: feature: configurable CoW incremental backup --- .../manager/IncrementalBackupStorageManager.java | 26 +++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/backup') 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 7870620..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,15 +4,18 @@ 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; +import com.keuin.kbackupfabric.util.cow.FileCowCopier; +import com.keuin.kbackupfabric.util.cow.FileEagerCopier; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.util.*; import java.util.logging.Logger; @@ -27,9 +30,26 @@ 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; public IncrementalBackupStorageManager(Path backupStorageBase) { this.backupStorageBase = backupStorageBase; + 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()) { + PrintUtil.info("Copy-on-write is enabled"); + } else { + PrintUtil.info("Copy-on-write is disabled"); + } } /** @@ -85,7 +105,7 @@ public class IncrementalBackupStorageManager { if (!contains(entry.getValue())) { // element does not exist. copy. logger.fine("Copy new file `" + copySourceFile.getName() + "`."); - Files.copy(copySourceFile.toPath(), copyDestination.toPath()); + copier.copy(copyDestination.getAbsolutePath(), copySourceFile.getAbsolutePath()); copyCount = copyCount.addWith(new IncCopyResult(1, 1, fileBytes, fileBytes)); } else { // element exists (file reused). Just update the stat info @@ -198,7 +218,7 @@ public class IncrementalBackupStorageManager { } } - Files.copy(copySource.toPath(), copyTarget.toPath()); + copier.copy(copyTarget.getAbsolutePath(), copySource.getAbsolutePath()); ++copyCount; } -- cgit v1.2.3