diff options
author | Keuin <[email protected]> | 2021-01-13 12:51:15 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-13 12:51:15 +0800 |
commit | 81497ebee2cdb0dac3bcdc26c5d77d06bdb98b5d (patch) | |
tree | 9e7aad680a438b845126b266374155c8fb306179 /src/main/java/com/keuin/kbackupfabric/operation | |
parent | f4d926c1eb91749a4b0f89c7a35538821ff7e21b (diff) |
Clean old incremental backup codes.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/operation')
9 files changed, 92 insertions, 234 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java index 465b293..f8736c8 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java @@ -2,6 +2,7 @@ package com.keuin.kbackupfabric.operation; import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation; import com.keuin.kbackupfabric.operation.backup.BackupMethod; +import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; import com.keuin.kbackupfabric.util.PrintUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; @@ -19,16 +20,16 @@ import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.*; public class BackupOperation extends InvokableAsyncBlockingOperation { private final CommandContext<ServerCommandSource> context; - private final String backupName; + private final String customBackupName; private final Map<World, Boolean> oldWorldsSavingDisabled = new HashMap<>(); private final BackupMethod backupMethod; private long startTime; - public BackupOperation(CommandContext<ServerCommandSource> context, String backupName, BackupMethod backupMethod) { + public BackupOperation(CommandContext<ServerCommandSource> context, String customBackupName, BackupMethod backupMethod) { super("BackupWorker"); this.context = context; - this.backupName = backupName; + this.customBackupName = customBackupName; this.backupMethod = backupMethod; } @@ -49,18 +50,16 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { // Make zip String levelPath = getLevelPath(server); - String backupFileName = getBackupFileName(backupName); + String backupFileName = getBackupFileName(customBackupName); - BackupMethod.BackupResult result = backupMethod.backup(backupName,levelPath,backupSaveDirectory); + PrimitiveBackupFeedback result = backupMethod.backup(customBackupName,levelPath,backupSaveDirectory); if(result.isSuccess()) { - // Restore old autosave switch stat + // Restore old auto-save switch stat server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true)); // Print finish message: time elapsed and file size long timeElapsedMillis = System.currentTimeMillis() - startTime; - String msgText = String.format("Backup finished. Time elapsed: %.2fs.", timeElapsedMillis / 1000.0); - File backupZipFile = new File(backupSaveDirectory, backupFileName); - msgText += String.format(" File size: %s.", getFriendlyFileSizeString(result.getBackupSizeBytes())); + String msgText = String.format("Backup finished. Time elapsed: %.2fs. ", timeElapsedMillis / 1000.0) + result.getFeedback(); PrintUtil.msgInfo(context, msgText, true); } else { // failed @@ -77,7 +76,7 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { protected boolean sync() { //// Save world, save old autosave configs - PrintUtil.broadcast(String.format("Making backup %s, please wait ...", backupName)); + PrintUtil.broadcast(String.format("Making backup %s, please wait ...", customBackupName)); // Get server MinecraftServer server = context.getSource().getMinecraftServer(); diff --git a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java index 444ca9a..3ae09f5 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java @@ -1,8 +1,8 @@ package com.keuin.kbackupfabric.operation; import com.keuin.kbackupfabric.operation.abstracts.InvokableAsyncBlockingOperation; -import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.keuin.kbackupfabric.util.PrintUtil; +import com.keuin.kbackupfabric.util.backup.suggestion.BackupNameSuggestionProvider; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; @@ -10,27 +10,26 @@ import net.minecraft.server.command.ServerCommandSource; import java.io.File; import java.io.IOException; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupFileName; -import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; import static com.keuin.kbackupfabric.util.PrintUtil.msgErr; import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; +import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; import static org.apache.commons.io.FileUtils.forceDelete; public class DeleteOperation extends InvokableAsyncBlockingOperation { //private static final Logger LOGGER = LogManager.getLogger(); - private final String backupName; + private final String backupFileName; private final CommandContext<ServerCommandSource> context; - public DeleteOperation(CommandContext<ServerCommandSource> context, String backupName) { + public DeleteOperation(CommandContext<ServerCommandSource> context, String backupFileName) { super("BackupDeletingWorker"); - this.backupName = backupName; + this.backupFileName = backupFileName; this.context = context; } @Override public String toString() { - return String.format("deletion of %s", backupName); + return String.format("deletion of %s", backupFileName); } @Override @@ -41,8 +40,7 @@ public class DeleteOperation extends InvokableAsyncBlockingOperation { private void delete() { MinecraftServer server = context.getSource().getMinecraftServer(); - String backupFileName = getBackupFileName(backupName); - PrintUtil.info("Deleting backup " + backupName); + PrintUtil.info("Deleting backup file " + this.backupFileName); File backupFile = new File(getBackupSaveDirectory(server), backupFileName); int tryCounter = 0; do { @@ -59,7 +57,7 @@ public class DeleteOperation extends InvokableAsyncBlockingOperation { } ++tryCounter; } while (backupFile.exists()); - PrintUtil.info("Deleted backup " + backupName); - msgInfo(context, "Deleted backup " + backupName); + PrintUtil.info("Successfully deleted backup file " + this.backupFileName); + msgInfo(context, "Successfully deleted backup file " + this.backupFileName); } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java index 22397a1..b95c767 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java @@ -1,9 +1,9 @@ package com.keuin.kbackupfabric.operation; -import com.keuin.kbackupfabric.exception.ZipUtilException; import com.keuin.kbackupfabric.operation.abstracts.InvokableBlockingOperation; +import com.keuin.kbackupfabric.operation.backup.BackupMethod; +import com.keuin.kbackupfabric.operation.backup.PrimitiveBackupMethod; import com.keuin.kbackupfabric.util.PrintUtil; -import com.keuin.kbackupfabric.util.ZipUtil; import com.mojang.brigadier.context.CommandContext; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; @@ -13,33 +13,33 @@ import java.io.IOException; import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupFileName; import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getBackupSaveDirectory; -import static org.apache.commons.io.FileUtils.forceDelete; public class RestoreOperation extends InvokableBlockingOperation { //private static final Logger LOGGER = LogManager.getLogger(); - private final String backupName; + private final String backupFileName; private final Thread serverThread; - private final String backupFilePath; - private final String levelDirectory; + private final String backupSavePath; + private final String levelPath; private final CommandContext<ServerCommandSource> context; private final MinecraftServer server; + private final BackupMethod backupMethod = PrimitiveBackupMethod.getInstance(); - public RestoreOperation(CommandContext<ServerCommandSource> context, String backupFilePath, String levelDirectory, String backupName) { + public RestoreOperation(CommandContext<ServerCommandSource> context, String backupSavePath, String levelPath, String backupFileName) { server = context.getSource().getMinecraftServer(); - this.backupName = backupName; + this.backupFileName = backupFileName; this.serverThread = server.getThread(); - this.backupFilePath = backupFilePath; - this.levelDirectory = levelDirectory; + this.backupSavePath = backupSavePath; + this.levelPath = levelPath; this.context = context; } @Override protected boolean blockingContext() { // do restore to backupName - PrintUtil.broadcast(String.format("Restoring to previous world %s ...", backupName)); + PrintUtil.broadcast(String.format("Restoring to backup %s ...", backupFileName)); - String backupFileName = getBackupFileName(backupName); + String backupFileName = getBackupFileName(this.backupFileName); PrintUtil.debug("Backup file name: " + backupFileName); File backupFile = new File(getBackupSaveDirectory(server), backupFileName); @@ -67,7 +67,7 @@ public class RestoreOperation extends InvokableBlockingOperation { @Override public String toString() { - return String.format("restoration from %s", backupName); + return String.format("restoration from %s", backupFileName); } private class WorkerThread implements Runnable { @@ -94,12 +94,16 @@ public class RestoreOperation extends InvokableBlockingOperation { }while(--cnt > 0); //////////////////// + backupMethod.restore(backupFileName, levelPath, backupSavePath); //ServerRestartUtil.forkAndRestart(); System.exit(111); } catch (SecurityException e) { PrintUtil.error("An exception occurred while restoring: " + e.getMessage()); + } catch (IOException e) { + PrintUtil.error(e.toString()); + PrintUtil.error("Failed to restore."); } } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java index 4e9eb6c..b65a076 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java @@ -1,7 +1,6 @@ package com.keuin.kbackupfabric.operation.backup; -import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; +import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; import java.io.IOException; @@ -16,29 +15,8 @@ public interface BackupMethod { * @param backupName the backup name. * @return if the backup operation succeed. */ - BackupResult backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException; + PrimitiveBackupFeedback backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException; boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException; - BackupFileNameBuilder getBackupFileNameBuilder(); - - BackupFileNameFormatter getBackupFileNameFormatter(); - - class BackupResult { - private final boolean success; - private final long backupSizeBytes; - - public BackupResult(boolean success, long backupSizeBytes) { - this.success = success; - this.backupSizeBytes = backupSizeBytes; - } - - public boolean isSuccess() { - return success; - } - - public long getBackupSizeBytes() { - return backupSizeBytes; - } - } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupMethod.java deleted file mode 100644 index 4a87bb3..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupMethod.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup; - -import com.google.gson.JsonObject; -import com.keuin.kbackupfabric.util.PrintUtil; -import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.builder.ObjectTreeBackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; -import com.keuin.kbackupfabric.util.backup.formatter.ObjectTreeBackupFileNameFormatter; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Paths; -import java.time.LocalDateTime; - -public class IncrementalBackupMethod implements BackupMethod { - - private static final IncrementalBackupMethod INSTANCE = new IncrementalBackupMethod(); - - public static IncrementalBackupMethod getInstance() { - return INSTANCE; - } - - @Override - public BackupResult backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { - /* - 1. Analyze the save directory, to get a json containing md5 values of all files. - 2. Copy new files which we do not have in our backup repository. - 3. Save the above json as a backup file. When restoring from this, - what we have to do is just copy all files back from the repository, - based on their md5 digests. - */ - - boolean success = true; - // Generate JSON - JsonObject hashJson = IncrementalBackupUtil.generateDirectoryJsonObject(levelPath); - // Copy files - long newFilesSizeBytes = IncrementalBackupUtil.saveNewFiles(backupSaveDirectory, levelPath, hashJson); - if(newFilesSizeBytes < 0) { - success = false; - PrintUtil.error("Failed to copy new files to object tree."); - } - // Save JSON tree - File jsonFile = new File(String.valueOf(Paths.get(backupSaveDirectory, BackupFileNameBuilder.objectTreeBackup().build(LocalDateTime.now(), backupName)))); - // TODO - return new BackupResult(success, newFilesSizeBytes); - } - - @Override - public boolean restore(String backupName, String levelPath, String backupSaveDirectory) throws IOException { - return false; - } - - @Override - public BackupFileNameBuilder getBackupFileNameBuilder() { - return null; - } - - @Override - public BackupFileNameFormatter getBackupFileNameFormatter() { - return null; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupUtil.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupUtil.java deleted file mode 100644 index f90aef1..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/IncrementalBackupUtil.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.keuin.kbackupfabric.util.FilesystemUtil; -import org.apache.commons.codec.digest.DigestUtils; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.*; -import java.util.Map; - -public class IncrementalBackupUtil { - /** - * Generate a json object representing a directory and its all sub files and directories. - * @param path path to the directory. - * @return a json object. - */ - public static JsonObject generateDirectoryJsonObject(String path) throws IOException { - JsonObject json = new JsonObject(); - File directory = new File(path); - if (!(directory.isDirectory() && directory.exists())) - throw new IOException(String.format("Path %s is not a valid directory.", path)); - - // Iterate all sub files using BFS. - try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(path))) { - for (Path sub : directoryStream) { - if (sub.toFile().isFile()) { - // A sub file - // Just hash and add it as a string - try (InputStream is = Files.newInputStream(sub)) { - String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(is); - json.addProperty(sub.getFileName().toString(), md5); - } - } else { - // A sub directory - // Search into - json.addProperty(String.valueOf(sub.getFileName()), sub.toString()); - } - } - } - - return json; - } - - /** - * Save new (or modified) files to target path, based on hash json. - * @param targetSavePath where we should save new files. - * @param sourcePath where new files come from. This path must be the base directory of given hash json. - * @param hashJson the json object obtained by calling generateDirectoryJsonObject method. - * @return total size of new files. If failed, will return -1. - */ - public static long saveNewFiles(String targetSavePath, String sourcePath, JsonObject hashJson) throws IOException { - long bytesCopied = 0; - for (Map.Entry<String, JsonElement> entry : hashJson.entrySet()) { - String key = entry.getKey(); - JsonElement value = entry.getValue(); - if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) { - // A sub file - // key is file name - // value is file md5 - String md5 = value.getAsJsonPrimitive().getAsString(); - File saveTarget = new File(targetSavePath, md5); - if (!saveTarget.exists()) { - // Target file does not exist. We have to copy this to the target. - File sourceFile = new File(sourcePath, key); - Files.copy(sourceFile.toPath(), saveTarget.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); - try { - bytesCopied += sourceFile.length(); - } catch (SecurityException ignored) { - // failed to get the file size. Just ignore this. - } - } - } else if (value.isJsonObject()) { - // A sub directory - // key is directory name - // value is directory json object - // Go into - if(!value.isJsonObject()) - throw new IllegalArgumentException(String.format("Hash json contains illegal argument of a directory item: %s -> %s.", key, value)); - Path pathSource = Paths.get(sourcePath, key); - bytesCopied += saveNewFiles(targetSavePath, pathSource.toString(), value.getAsJsonObject()); - } else { - throw new IllegalArgumentException(String.format("Hash json contains illegal element: %s -> %s.", key, value)); - } - } - return bytesCopied; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java index 854355d..9c065a6 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java @@ -2,11 +2,13 @@ package com.keuin.kbackupfabric.operation.backup; import com.keuin.kbackupfabric.exception.ZipUtilException; import com.keuin.kbackupfabric.metadata.BackupMetadata; +import com.keuin.kbackupfabric.operation.backup.feedback.PrimitiveBackupFeedback; import com.keuin.kbackupfabric.util.FilesystemUtil; import com.keuin.kbackupfabric.util.PrintUtil; import com.keuin.kbackupfabric.util.ZipUtil; -import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; +import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil; +import com.keuin.kbackupfabric.util.backup.BackupNameTimeFormatter; +import com.keuin.kbackupfabric.util.backup.name.PrimitiveBackupFileNameEncoder; import java.io.File; import java.io.IOException; @@ -23,27 +25,32 @@ public class PrimitiveBackupMethod implements BackupMethod { return INSTANCE; } + @Deprecated + private String getBackupFileName(LocalDateTime time, String backupName) { + String timeString = BackupNameTimeFormatter.localDateTimeToString(time); + return String.format("%s%s_%s%s", BackupFilesystemUtil.getBackupFileNamePrefix(), timeString, backupName, ".zip"); + } + @Override - public BackupResult backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { - String backupFileName = BackupFileNameBuilder.primitiveZipBackup().build(LocalDateTime.now(),backupName); + public PrimitiveBackupFeedback backup(String customBackupName, String levelPath, String backupSavePath) throws IOException { +// String backupFileName = getBackupFileName(LocalDateTime.now(),backupName); + String backupFileName = new PrimitiveBackupFileNameEncoder().encode(customBackupName, LocalDateTime.now()); try { - BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), backupName); - - PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectory)); + BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), customBackupName); + PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSavePath)); PrintUtil.info("Compressing level ..."); - ZipUtil.makeBackupZip(levelPath, backupSaveDirectory, backupFileName, backupMetadata); - + ZipUtil.makeBackupZip(levelPath, backupSavePath, backupFileName, backupMetadata); } catch (ZipUtilException exception) { PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); - return new BackupResult(false, 0); + return new PrimitiveBackupFeedback(false, 0); } // Get backup file size and return - return new BackupResult(true, FilesystemUtil.getFileSizeBytes(backupSaveDirectory, backupFileName)); + return new PrimitiveBackupFeedback(true, FilesystemUtil.getFileSizeBytes(backupSavePath, backupFileName)); } @Override - public boolean restore(String backupName, String levelDirectory, String backupSaveDirectory) throws IOException { + public boolean restore(String backupFileName, String levelDirectory, String backupSaveDirectory) throws IOException { // Delete old level PrintUtil.info("Server stopped. Deleting old level ..."); File levelDirFile = new File(levelDirectory); @@ -73,10 +80,10 @@ public class PrimitiveBackupMethod implements BackupMethod { // TODO: Refactor this to the concrete BackupMethod. // Decompress archive PrintUtil.info("Decompressing archived level ..."); - ZipUtil.unzip(Paths.get(backupSaveDirectory, backupName).toString(), levelDirectory, false); + ZipUtil.unzip(Paths.get(backupSaveDirectory, backupFileName).toString(), levelDirectory, false); long endTime = System.currentTimeMillis(); PrintUtil.info(String.format("Restore complete! (%.2fs) Please restart the server manually.", (endTime - startTime) / 1000.0)); - PrintUtil.info("If you want to restart automatically after restoring, please visit the project manual at: https://github.com/keuin/KBackup-Fabric/blob/master/README.md"); + PrintUtil.info("If you want to restart automatically after restoring, please check the manual at: https://github.com/keuin/KBackup-Fabric/blob/master/README.md"); // try { // Thread.sleep(1000); @@ -85,14 +92,4 @@ public class PrimitiveBackupMethod implements BackupMethod { return true; } - - @Override - public BackupFileNameBuilder getBackupFileNameBuilder() { - return BackupFileNameBuilder.primitiveZipBackup(); - } - - @Override - public BackupFileNameFormatter getBackupFileNameFormatter() { - return BackupFileNameFormatter.primitiveZipBackup(); - } } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java new file mode 100644 index 0000000..92a9f39 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/BackupFeedback.java @@ -0,0 +1,5 @@ +package com.keuin.kbackupfabric.operation.backup.feedback; + +public interface BackupFeedback { + String getFeedback(); +} 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 new file mode 100644 index 0000000..6d7a15b --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/feedback/PrimitiveBackupFeedback.java @@ -0,0 +1,29 @@ +package com.keuin.kbackupfabric.operation.backup.feedback; + +import static com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil.getFriendlyFileSizeString; + +public class PrimitiveBackupFeedback implements BackupFeedback { + private final boolean success; + private final long backupSizeBytes; + + public PrimitiveBackupFeedback(boolean success, long backupSizeBytes) { + this.success = success; + this.backupSizeBytes = backupSizeBytes; + } + + public boolean isSuccess() { + return success; + } + + public long getBackupSizeBytes() { + return backupSizeBytes; + } + + @Override + public String getFeedback() { + if (success && backupSizeBytes >= 0) + return String.format(" File size: %s.", getFriendlyFileSizeString(backupSizeBytes)); + else + return ""; + } +} |