diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation')
2 files changed, 27 insertions, 9 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java index 094438c..0d99cf5 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java @@ -3,12 +3,23 @@ package com.keuin.kbackupfabric.operation.backup.feedback; import static com.keuin.kbackupfabric.backup.BackupFilesystemUtil.getFriendlyFileSizeString; public class PrimitiveBackupFeedback implements BackupFeedback { + private final boolean success; - private final long backupSizeBytes; + private final long backupSizeBytes; // if success==false, this is invalid + private final String message; // if success==true, this is invalid - public PrimitiveBackupFeedback(boolean success, long backupSizeBytes) { + private PrimitiveBackupFeedback(boolean success, long backupSizeBytes, String message) { this.success = success; this.backupSizeBytes = backupSizeBytes; + this.message = message; + } + + public static PrimitiveBackupFeedback createSuccessFeedback(long backupSizeBytes) { + return new PrimitiveBackupFeedback(true, backupSizeBytes, ""); + } + + public static PrimitiveBackupFeedback createFailFeedback(String message) { + return new PrimitiveBackupFeedback(false, -1, message); } @Override @@ -25,6 +36,6 @@ public class PrimitiveBackupFeedback implements BackupFeedback { if (success && backupSizeBytes >= 0) return String.format("Backup file size: %s.", getFriendlyFileSizeString(backupSizeBytes)); else - return ""; + return message; } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java index 0047c44..ded514c 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java @@ -12,6 +12,7 @@ import com.keuin.kbackupfabric.util.ZipUtil; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Paths; import java.time.LocalDateTime; import java.util.logging.Logger; @@ -47,20 +48,26 @@ public class ConfiguredPrimitiveBackupMethod implements ConfiguredBackupMethod { PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSavePath)); PrintUtil.info("Compressing level ..."); ZipUtil.makeBackupZip(levelPath, backupSavePath, backupFileName, backupMetadata); - feedback = new PrimitiveBackupFeedback(true, FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName)); + feedback = PrimitiveBackupFeedback.createSuccessFeedback( + FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName)); } catch (ZipUtilException exception) { - PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); - feedback = new PrimitiveBackupFeedback(false, 0); + String msg = "Infinite recursive of directory tree detected, backup was aborted."; + PrintUtil.info(msg); + feedback = PrimitiveBackupFeedback.createFailFeedback(msg); } catch (IOException e) { - feedback = new PrimitiveBackupFeedback(false, 0); + feedback = PrimitiveBackupFeedback.createFailFeedback(e.getMessage()); } if (!feedback.isSuccess()) { // do clean-up if failed File backupFile = new File(backupSavePath, backupFileName); if (backupFile.exists()) { - if (!backupFile.delete()) { - LOGGER.warning("Failed to clean up: cannot delete file " + backupFile.getName()); + LOGGER.info(String.format("Deleting incomplete backup file \"%s\"...", backupFile.getPath())); + try { + Files.delete(backupFile.toPath()); + LOGGER.info("Failed to backup, all files are cleaned up."); + } catch (IOException e) { + LOGGER.warning("Cannot delete: " + e.getMessage()); } } } |