diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/BackupGUI.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/BackupGUI.java index 4357647..e325836 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/BackupGUI.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/BackupGUI.java @@ -75,19 +75,19 @@ public class BackupGUI implements ActionListener { * Destroys the backup GUI */ public static void destroy() { - BackupUtil.abortBackup(); - progressTextArea = null; - progressBar = null; - cancelButton = null; if (frame != null) { frame.dispose(); + frame = null; + progressBar = null; + progressTextArea = null; + cancelButton = null; } - frame = null; } @Override public void actionPerformed(ActionEvent actionEvent) { if (actionEvent.getSource() == cancelButton) { + BackupUtil.abortBackup(); destroy(); } } diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/BackupUtil.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/BackupUtil.java index ed6df5d..cfd5e0c 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/BackupUtil.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/BackupUtil.java @@ -36,18 +36,21 @@ public class BackupUtil { /** * Recursively copies a folder to another location * - * @param source

The folder to copy

- * @param destination

Target destination

+ * @param source

The folder to copy

+ * @param destination

Target destination

+ * @param backupFileSize

The total file size of the backup in progress

+ * @param alreadyCopied

The amount of bytes already copied

* @throws IOException

If we can't start a file stream

*/ - private static synchronized long backupFolder(File source, File destination, long backupFileSize, long alreadyCopied) throws IOException { + private static synchronized long backupFolder(File source, File destination, long backupFileSize, + long alreadyCopied) throws IOException { if (backupAborted) { return 0L; } if (!source.isDirectory()) { long copiedFileSize = copyFile(source, destination); BackupGUI.updateProgress("Copying " + source + "\n to " + destination, - (int)((alreadyCopied + copiedFileSize) * 100 / backupFileSize)); + (int) ((alreadyCopied + copiedFileSize) * 100 / backupFileSize)); return copiedFileSize; } else { if (!destination.exists() && !destination.mkdir()) { @@ -62,7 +65,7 @@ public class BackupUtil { copiedFilesSize += backupFolder(srcFile, destinationFile, backupFileSize, alreadyCopied + copiedFilesSize); BackupGUI.updateProgress("Copying " + source + "\n to " + destination, - (int)((alreadyCopied + copiedFilesSize) * 100 / backupFileSize)); + (int) ((alreadyCopied + copiedFilesSize) * 100 / backupFileSize)); } } return copiedFilesSize; @@ -72,8 +75,8 @@ public class BackupUtil { /** * Copies a file from one location to another * - * @param source

The file to copy

- * @param destination

The location of the copied file

+ * @param source

The file to copy

+ * @param destination

The location of the copied file

* @throws IOException

If reading or writing fails

*/ private static long copyFile(File source, File destination) throws IOException { @@ -91,6 +94,8 @@ public class BackupUtil { /** * Copies all server directories to a folder specified by the user + * + * @param gui

The GUI to use for informing the user

*/ public synchronized static void backup(GUI gui) { backupAborted = false; @@ -116,46 +121,70 @@ public class BackupUtil { gui.setStatus("Backing up " + (backupFileSize / 1000000) + "MB"); + performBackup(gui, serverFolders, backupFileSize); + } + + /** + * Performs the actual backup after checks have passed and necessary info is available + * @param gui

The GUI to use for informing the user

+ * @param serverFolders

The folders of the servers to backup

+ * @param backupFileSize

The total size of the folders to backup

+ */ + private static void performBackup(GUI gui, List> serverFolders, long backupFileSize) { new BackupGUI(); BackupGUI.updateProgress("Backup starting...", 0); long alreadyCopied = 0; for (List serverFolder : serverFolders) { - if (backupAborted) { + if (backupAborted || !backupRunning) { gui.setStatus("Backup aborted"); backupRunning = false; return; } - File srcFolder = serverFolder.get(0); - File destinationFolder = serverFolder.get(1); - - //Create child folders - if (!destinationFolder.exists() && !destinationFolder.mkdirs()) { - backupRunning = false; - gui.logError("Unable to create necessary sub-folder in the backup folder"); - throw new IllegalArgumentException("Unable to create necessary sub-folder in the backup folder"); - } - //Backup - try { - alreadyCopied += backupFolder(srcFolder, destinationFolder, backupFileSize, alreadyCopied); - } catch (IOException e) { - e.printStackTrace(); - gui.setStatus("Backup caused an error"); - backupRunning = false; - } + alreadyCopied = backupServerFiles(gui, serverFolder, backupFileSize, alreadyCopied); } backupRunning = false; - BackupGUI.destroy(); if (backupAborted) { gui.setStatus("Backup aborted"); } else { + BackupGUI.destroy(); gui.setStatus("Backup finished"); } } + /** + * Backs up the files for a single server + * + * @param gui

The GUI to use for informing the user

+ * @param serverFolder

The server's input and output folders

+ * @param backupFileSize

The total size of the files to backup

+ * @param alreadyCopied

The amount of bytes already copied

+ * @return

The new amount of bytes copied

+ */ + private static long backupServerFiles(GUI gui, List serverFolder, long backupFileSize, long alreadyCopied) { + File srcFolder = serverFolder.get(0); + File destinationFolder = serverFolder.get(1); + + //Create child folders + if (!destinationFolder.exists() && !destinationFolder.mkdirs()) { + backupRunning = false; + gui.logError("Unable to create necessary sub-folder in the backup folder"); + throw new IllegalArgumentException("Unable to create necessary sub-folder in the backup folder"); + } + //Backup + try { + alreadyCopied += backupFolder(srcFolder, destinationFolder, backupFileSize, alreadyCopied); + } catch (IOException e) { + e.printStackTrace(); + gui.setStatus("Backup caused an error"); + backupRunning = false; + } + return alreadyCopied; + } + /** * Gets the size of a list of folders * - * @param gui

The GUI to write any errors to

+ * @param gui

The GUI to write any errors to

* @param serverFolders

The folder to find the size of

* @return

The size of the given folders

*/ @@ -208,6 +237,7 @@ public class BackupUtil { /** * Gets the size of a file given its path + * * @param path

The path to a file

* @return

The size of the file in bytes, or 0 if an exception is thrown

*/