diff options
author | Keuin <[email protected]> | 2021-06-11 17:24:46 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2021-06-11 18:54:04 +0800 |
commit | 1ec5112f3487d7a2ca7aa9b1fe4825a8e3777bc3 (patch) | |
tree | 40b6cb086bb0f4f074f2c422dba491806e7f177e /src/main/java/com/keuin/kbackupfabric/operation/backup/method | |
parent | fe652450ac33adc4bb15a487eff24d7b5120e1a2 (diff) |
fix silent-fail IO exception in ZipUtil; Improve code quality in ZipUtil
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation/backup/method')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/operation/backup/method/ConfiguredPrimitiveBackupMethod.java | 19 |
1 files changed, 13 insertions, 6 deletions
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()); } } } |